LicenseInitializer.cs
上传用户:songlsx
上传日期:2022-06-19
资源大小:2227k
文件大小:13k
源码类别:

GIS编程

开发平台:

Visual C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using ESRI.ArcGIS.esriSystem;
  5. //FILE AUTOMATICALLY GENERATED BY ESRI LICENSE INITIALIZATION ADDIN
  6. //YOU SHOULD NOT NORMALLY EDIT OR REMOVE THIS FILE FROM THE PROJECT
  7. namespace LinGIS
  8. {
  9.     internal sealed class LicenseInitializer
  10.     {
  11.         private IAoInitialize m_AoInit = new AoInitializeClass();
  12.         #region Private members
  13.         private const string MessageNoLicensesRequested = "Product: No licenses were requested";
  14.         private const string MessageProductAvailable = "Product: {0}: Available";
  15.         private const string MessageProductNotLicensed = "Product: {0}: Not Licensed";
  16.         private const string MessageExtensionAvailable = " Extension: {0}: Available";
  17.         private const string MessageExtensionNotLicensed = " Extension: {0}: Not Licensed";
  18.         private const string MessageExtensionFailed = " Extension: {0}: Failed";
  19.         private const string MessageExtensionUnavailable = " Extension: {0}: Unavailable";
  20.         private bool m_hasShutDown = false;
  21.         private bool m_hasInitializeProduct = false;
  22.         private List<int> m_requestedProducts;
  23.         private List<esriLicenseExtensionCode> m_requestedExtensions;
  24.         private Dictionary<esriLicenseProductCode, esriLicenseStatus> m_productStatus = new Dictionary<esriLicenseProductCode, esriLicenseStatus>();
  25.         private Dictionary<esriLicenseExtensionCode, esriLicenseStatus> m_extensionStatus = new Dictionary<esriLicenseExtensionCode, esriLicenseStatus>();
  26.         private bool m_productCheckOrdering = true; //default from low to high
  27.         #endregion
  28.         public bool InitializeApplication(esriLicenseProductCode[] productCodes, esriLicenseExtensionCode[] extensionLics)
  29.         {
  30.             //Cache product codes by enum int so can be sorted without custom sorter
  31.             m_requestedProducts = new List<int>();
  32.             foreach (esriLicenseProductCode code in productCodes)
  33.             {
  34.                 int requestCodeNum = Convert.ToInt32(code);
  35.                 if (!m_requestedProducts.Contains(requestCodeNum))
  36.                 {
  37.                     m_requestedProducts.Add(requestCodeNum);
  38.                 }
  39.             }
  40.             AddExtensions(extensionLics);
  41.             return Initialize();
  42.         }
  43.         /// <summary>
  44.         /// A summary of the status of product and extensions initialization.
  45.         /// </summary>
  46.         public string LicenseMessage()
  47.         {
  48.             string prodStatus = string.Empty;
  49.             if (m_productStatus == null || m_productStatus.Count == 0)
  50.             {
  51.                 prodStatus = MessageNoLicensesRequested + Environment.NewLine;
  52.             }
  53.             else if (m_productStatus.ContainsValue(esriLicenseStatus.esriLicenseAlreadyInitialized)
  54.                 || m_productStatus.ContainsValue(esriLicenseStatus.esriLicenseCheckedOut))
  55.             {
  56.                 prodStatus = ReportInformation(m_AoInit as ILicenseInformation,
  57.                     m_AoInit.InitializedProduct(),
  58.                     esriLicenseStatus.esriLicenseCheckedOut) + Environment.NewLine;
  59.             }
  60.             else
  61.             {
  62.                 //Failed...
  63.                 foreach (KeyValuePair<esriLicenseProductCode, esriLicenseStatus> item in m_productStatus)
  64.                 {
  65.                     prodStatus += ReportInformation(m_AoInit as ILicenseInformation,
  66.                         item.Key, item.Value) + Environment.NewLine;
  67.                 }
  68.             }
  69.             string extStatus = string.Empty;
  70.             foreach (KeyValuePair<esriLicenseExtensionCode, esriLicenseStatus> item in m_extensionStatus)
  71.             {
  72.                 string info = ReportInformation(m_AoInit as ILicenseInformation, item.Key, item.Value);
  73.                 if (!string.IsNullOrEmpty(info))
  74.                     extStatus += info + Environment.NewLine;
  75.             }
  76.             string status = prodStatus + extStatus;
  77.             return status.Trim();
  78.         }
  79.         /// <summary>
  80.         /// Shuts down AoInitialize object and check back in extensions to ensure
  81.         /// any ESRI libraries that have been used are unloaded in the correct order.
  82.         /// </summary>
  83.         /// <remarks>Once Shutdown has been called, you cannot re-initialize the product license
  84.         /// and should not make any ArcObjects call.</remarks>
  85.         public void ShutdownApplication()
  86.         {
  87.             if (m_hasShutDown)
  88.                 return;
  89.             //Check back in extensions
  90.             foreach (KeyValuePair<esriLicenseExtensionCode, esriLicenseStatus> item in m_extensionStatus)
  91.             {
  92.                 if (item.Value == esriLicenseStatus.esriLicenseCheckedOut)
  93.                     m_AoInit.CheckInExtension(item.Key);
  94.             }
  95.             m_requestedProducts.Clear();
  96.             m_requestedExtensions.Clear();
  97.             m_extensionStatus.Clear();
  98.             m_productStatus.Clear();
  99.             m_AoInit.Shutdown();
  100.             m_hasShutDown = true;
  101.             //m_hasInitializeProduct = false;
  102.         }
  103.         /// <summary>
  104.         /// Indicates if the extension is currently checked out.
  105.         /// </summary>
  106.         public bool IsExtensionCheckedOut(esriLicenseExtensionCode code)
  107.         {
  108.             return m_AoInit.IsExtensionCheckedOut(code);
  109.         }
  110.         /// <summary>
  111.         /// Set the extension(s) to be checked out for your ArcObjects code. 
  112.         /// </summary>
  113.         public bool AddExtensions(params esriLicenseExtensionCode[] requestCodes)
  114.         {
  115.             if (m_requestedExtensions == null)
  116.                 m_requestedExtensions = new List<esriLicenseExtensionCode>();
  117.             foreach (esriLicenseExtensionCode code in requestCodes)
  118.             {
  119.                 if (!m_requestedExtensions.Contains(code))
  120.                     m_requestedExtensions.Add(code);
  121.             }
  122.             if (m_hasInitializeProduct)
  123.                 return CheckOutLicenses(this.InitializedProduct);
  124.             return false;
  125.         }
  126.         /// <summary>
  127.         /// Check in extension(s) when it is no longer needed.
  128.         /// </summary>
  129.         public void RemoveExtensions(params esriLicenseExtensionCode[] requestCodes)
  130.         {
  131.             if (m_extensionStatus == null || m_extensionStatus.Count == 0)
  132.                 return;
  133.             foreach (esriLicenseExtensionCode code in requestCodes)
  134.             {
  135.                 if (m_extensionStatus.ContainsKey(code))
  136.                 {
  137.                     if (m_AoInit.CheckInExtension(code) == esriLicenseStatus.esriLicenseCheckedIn)
  138.                     {
  139.                         m_extensionStatus[code] = esriLicenseStatus.esriLicenseCheckedIn;
  140.                     }
  141.                 }
  142.             }
  143.         }
  144.         /// <summary>
  145.         /// Get/Set the ordering of product code checking. If true, check from lowest to 
  146.         /// highest license. True by default.
  147.         /// </summary>
  148.         public bool InitializeLowerProductFirst
  149.         {
  150.             get
  151.             {
  152.                 return m_productCheckOrdering;
  153.             }
  154.             set
  155.             {
  156.                 m_productCheckOrdering = value;
  157.             }
  158.         }
  159.         /// <summary>
  160.         /// Retrieves the product code initialized in the ArcObjects application
  161.         /// </summary>
  162.         public esriLicenseProductCode InitializedProduct
  163.         {
  164.             get
  165.             {
  166.                 try
  167.                 {
  168.                     return m_AoInit.InitializedProduct();
  169.                 }
  170.                 catch
  171.                 {
  172.                     return 0;
  173.                 }
  174.             }
  175.         }
  176.         #region Helper methods
  177.         private bool Initialize()
  178.         {
  179.             if (m_requestedProducts == null || m_requestedProducts.Count == 0)
  180.                 return false;
  181.             esriLicenseProductCode currentProduct = new esriLicenseProductCode();
  182.             bool productInitialized = false;
  183.             //Try to initialize a product
  184.             ILicenseInformation licInfo = (ILicenseInformation)m_AoInit;
  185.             m_requestedProducts.Sort();
  186.             if (!InitializeLowerProductFirst) //Request license from highest to lowest
  187.                 m_requestedProducts.Reverse();
  188.             foreach (int prodNumber in m_requestedProducts)
  189.             {
  190.                 esriLicenseProductCode prod = (esriLicenseProductCode)Enum.ToObject(typeof(esriLicenseProductCode), prodNumber);
  191.                 esriLicenseStatus status = m_AoInit.IsProductCodeAvailable(prod);
  192.                 if (status == esriLicenseStatus.esriLicenseAvailable)
  193.                 {
  194.                     status = m_AoInit.Initialize(prod);
  195.                     if (status == esriLicenseStatus.esriLicenseAlreadyInitialized ||
  196.                         status == esriLicenseStatus.esriLicenseCheckedOut)
  197.                     {
  198.                         productInitialized = true;
  199.                         currentProduct = m_AoInit.InitializedProduct();
  200.                     }
  201.                 }
  202.                 m_productStatus.Add(prod, status);
  203.                 if (productInitialized)
  204.                     break;
  205.             }
  206.             m_hasInitializeProduct = productInitialized;
  207.             m_requestedProducts.Clear();
  208.             //No product is initialized after trying all requested licenses, quit
  209.             if (!productInitialized)
  210.             {
  211.                 return false;
  212.             }
  213.             //Check out extension licenses
  214.             return CheckOutLicenses(currentProduct);
  215.         }
  216.         private bool CheckOutLicenses(esriLicenseProductCode currentProduct)
  217.         {
  218.             bool allSuccessful = true;
  219.             //Request extensions
  220.             if (m_requestedExtensions != null && currentProduct != 0)
  221.             {
  222.                 foreach (esriLicenseExtensionCode ext in m_requestedExtensions)
  223.                 {
  224.                     esriLicenseStatus licenseStatus = m_AoInit.IsExtensionCodeAvailable(currentProduct, ext);
  225.                     if (licenseStatus == esriLicenseStatus.esriLicenseAvailable)//skip unavailable extensions
  226.                     {
  227.                         licenseStatus = m_AoInit.CheckOutExtension(ext);
  228.                     }
  229.                     allSuccessful = (allSuccessful && licenseStatus == esriLicenseStatus.esriLicenseCheckedOut);
  230.                     if (m_extensionStatus.ContainsKey(ext))
  231.                         m_extensionStatus[ext] = licenseStatus;
  232.                     else
  233.                         m_extensionStatus.Add(ext, licenseStatus);
  234.                 }
  235.                 m_requestedExtensions.Clear();
  236.             }
  237.             return allSuccessful;
  238.         }
  239.         private string ReportInformation(ILicenseInformation licInfo,
  240.            esriLicenseProductCode code, esriLicenseStatus status)
  241.         {
  242.             string prodName = string.Empty;
  243.             try
  244.             {
  245.                 prodName = licInfo.GetLicenseProductName(code);
  246.             }
  247.             catch
  248.             {
  249.                 prodName = code.ToString();
  250.             }
  251.             string statusInfo = string.Empty;
  252.             switch (status)
  253.             {
  254.                 case esriLicenseStatus.esriLicenseAlreadyInitialized:
  255.                 case esriLicenseStatus.esriLicenseCheckedOut:
  256.                     statusInfo = string.Format(MessageProductAvailable, prodName);
  257.                     break;
  258.                 default:
  259.                     statusInfo = string.Format(MessageProductNotLicensed, prodName);
  260.                     break;
  261.             }
  262.             return statusInfo;
  263.         }
  264.         private string ReportInformation(ILicenseInformation licInfo,
  265.             esriLicenseExtensionCode code, esriLicenseStatus status)
  266.         {
  267.             string extensionName = string.Empty;
  268.             try
  269.             {
  270.                 extensionName = licInfo.GetLicenseExtensionName(code);
  271.             }
  272.             catch
  273.             {
  274.                 extensionName = code.ToString();
  275.             }
  276.             string statusInfo = string.Empty;
  277.             switch (status)
  278.             {
  279.                 case esriLicenseStatus.esriLicenseAlreadyInitialized:
  280.                 case esriLicenseStatus.esriLicenseCheckedOut:
  281.                     statusInfo = string.Format(MessageExtensionAvailable, extensionName);
  282.                     break;
  283.                 case esriLicenseStatus.esriLicenseCheckedIn:
  284.                     break;
  285.                 case esriLicenseStatus.esriLicenseUnavailable:
  286.                     statusInfo = string.Format(MessageExtensionUnavailable, extensionName);
  287.                     break;
  288.                 case esriLicenseStatus.esriLicenseFailure:
  289.                     statusInfo = string.Format(MessageExtensionFailed, extensionName);
  290.                     break;
  291.                 default:
  292.                     statusInfo = string.Format(MessageExtensionNotLicensed, extensionName);
  293.                     break;
  294.             }
  295.             return statusInfo;
  296.         }
  297.         #endregion
  298.     }
  299. }