GPPointFileInfo\LicensedProducts.vb
Creating a non-trival custom GP tool - inputs, outputs, and responding to environment settings
GPPointFileInfo\LicensedProducts.vb
' 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.
' 

Imports Microsoft.VisualBasic
Imports System
Imports ESRI.ArcGIS.esriSystem

Namespace GPPointFileInfo
    Public Class LicensedProducts
        Implements ILicensedProducts
        Private _aoInitialize As IAoInitialize
        Private _licensedProduct As esriLicenseProductCode

        Public Sub New(ByVal aoInitialize As IAoInitialize)
            ValidateAoInitialize(aoInitialize)

            _aoInitialize = aoInitialize
            _licensedProduct = GetLicensedProduct()
        End Sub

        Private Sub ValidateAoInitialize(ByVal aoInitialize As IAoInitialize)
            If aoInitialize Is Nothing Then
                Throw New GPException(GPExceptionSeverity.Error, "Unable To Initialize Licensed Products. AoInitialize Is Null.")
            End If
        End Sub

        Private Function GetLicensedProduct() As esriLicenseProductCode
            Dim licensedProduct As esriLicenseProductCode

            If HasLicense(esriLicenseProductCode.esriLicenseProductCodeArcInfo) Then
                licensedProduct = esriLicenseProductCode.esriLicenseProductCodeArcInfo
            ElseIf HasLicense(esriLicenseProductCode.esriLicenseProductCodeArcEditor) Then
                licensedProduct = esriLicenseProductCode.esriLicenseProductCodeArcEditor
            ElseIf HasLicense(esriLicenseProductCode.esriLicenseProductCodeArcView) Then
                licensedProduct = esriLicenseProductCode.esriLicenseProductCodeArcView
            ElseIf HasLicense(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB) Then
                licensedProduct = esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB
            ElseIf HasLicense(esriLicenseProductCode.esriLicenseProductCodeEngine) Then
                licensedProduct = esriLicenseProductCode.esriLicenseProductCodeEngine
            Else
                Throw New GPException(GPExceptionSeverity.Error, "Unable To Initialize Licensed Products. Product License Not Found.")
            End If

            Return licensedProduct
        End Function

        Public ReadOnly Property LicensedProduct() As esriLicenseProductCode Implements ILicensedProducts.LicensedProduct
            Get
                Return _licensedProduct
            End Get
        End Property

        Private Function HasLicense(ByVal licensedProduct As esriLicenseProductCode) As Boolean
            Return (_aoInitialize.IsProductCodeAvailable(licensedProduct) = esriLicenseStatus.esriLicenseAvailable)
        End Function

        Public Sub Initialize() Implements ILicensedProducts.Initialize
            InitializeLicense(_licensedProduct)
        End Sub

        Private Sub InitializeLicense(ByVal licensedProduct As esriLicenseProductCode)
            _aoInitialize.Initialize(licensedProduct)

        End Sub

        Public ReadOnly Property IsLicensed() As Boolean Implements ILicensedProducts.IsLicensed
            Get
                Return True
            End Get
        End Property

        Public Sub Shutdown() Implements ILicensedProducts.Shutdown
            _aoInitialize.Shutdown()
        End Sub
    End Class
End Namespace