GPPointFileInfo\LicensedProducts.cs
Creating a non-trival custom GP tool - inputs, outputs, and responding to environment settings
GPPointFileInfo\LicensedProducts.cs
// Copyright 2008 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See use restrictions at <your ArcGIS install location>/developerkit/userestrictions.txt.
// 

using ESRI.ArcGIS.esriSystem;

namespace GPPointFileInfo
{
    public class LicensedProducts : ILicensedProducts
    {
        private IAoInitialize _aoInitialize;
        private esriLicenseProductCode _licensedProduct;

        public LicensedProducts(IAoInitialize aoInitialize)
        {
            ValidateAoInitialize(aoInitialize);

            _aoInitialize = aoInitialize;
            _licensedProduct = GetLicensedProduct();
        }

        private void ValidateAoInitialize(IAoInitialize aoInitialize)
        {
            if (aoInitialize == null)
            {
                throw new GPException(GPExceptionSeverity.Error, "Unable To Initialize Licensed Products. AoInitialize Is Null.");
            }
        }

        private esriLicenseProductCode GetLicensedProduct()
        {
            esriLicenseProductCode licensedProduct;

            if (HasLicense(esriLicenseProductCode.esriLicenseProductCodeArcInfo))
            {
                licensedProduct = esriLicenseProductCode.esriLicenseProductCodeArcInfo;
            }
            else if (HasLicense(esriLicenseProductCode.esriLicenseProductCodeArcEditor))
            {
                licensedProduct = esriLicenseProductCode.esriLicenseProductCodeArcEditor;
            }
            else if (HasLicense(esriLicenseProductCode.esriLicenseProductCodeArcView))
            {
                licensedProduct = esriLicenseProductCode.esriLicenseProductCodeArcView;
            }
            else if (HasLicense(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB))
            {
                licensedProduct = esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB;
            }
            else if (HasLicense(esriLicenseProductCode.esriLicenseProductCodeEngine))
            {
                licensedProduct = esriLicenseProductCode.esriLicenseProductCodeEngine;
            }
            else
            {
                throw new GPException(GPExceptionSeverity.Error, "Unable To Initialize Licensed Products. Product License Not Found.");
            }

            return licensedProduct;
        }

        public esriLicenseProductCode LicensedProduct
        {
            get
            {
                return _licensedProduct;
            }
        }

        private bool HasLicense(esriLicenseProductCode licensedProduct)
        {
            return (_aoInitialize.IsProductCodeAvailable(licensedProduct) == esriLicenseStatus.esriLicenseAvailable);
        }

        public void Initialize()
        {
            InitializeLicense(_licensedProduct);
        }

        private void InitializeLicense(esriLicenseProductCode licensedProduct)
        {
            _aoInitialize.Initialize(licensedProduct);

        }

        public bool IsLicensed
        {
            get
            {
                return true;
            }
        }

        public void Shutdown()
        {
            _aoInitialize.Shutdown();
        }
    }
}