AssetInventoryServer.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using OpenSim.Framework;
  31. using OpenSim.Framework.Servers;
  32. using OpenSim.Framework.Servers.HttpServer;
  33. using OpenSim.Framework.Console;
  34. using OpenSim.Framework.AssetLoader.Filesystem;
  35. using Nini.Config;
  36. using log4net;
  37. namespace OpenSim.Grid.AssetInventoryServer
  38. {
  39. public class AssetInventoryServer : BaseOpenSimServer
  40. {
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. public IConfigSource ConfigFile;
  43. public IAssetStorageProvider StorageProvider;
  44. public IInventoryStorageProvider InventoryProvider;
  45. public IAuthenticationProvider AuthenticationProvider;
  46. public IAuthorizationProvider AuthorizationProvider;
  47. public IMetricsProvider MetricsProvider;
  48. private List<IAssetInventoryServerPlugin> m_frontends = new List<IAssetInventoryServerPlugin>();
  49. private List<IAssetInventoryServerPlugin> m_backends = new List<IAssetInventoryServerPlugin>();
  50. public AssetInventoryServer(IConfigSource config)
  51. {
  52. ConfigFile = config;
  53. m_console = new LocalConsole("AssetInventory");
  54. MainConsole.Instance = m_console;
  55. }
  56. public bool Start()
  57. {
  58. Startup();
  59. m_log.Info("[ASSETINVENTORY]: Starting AssetInventory Server");
  60. try
  61. {
  62. ConfigFile = AssetInventoryConfig.LoadConfig(ConfigFile);
  63. }
  64. catch (Exception)
  65. {
  66. m_log.Error("[ASSETINVENTORY]: Failed to load the config.");
  67. return false;
  68. }
  69. StorageProvider = LoadAssetInventoryServerPlugin("/OpenSim/AssetInventoryServer/AssetStorageProvider",
  70. "asset_storage_provider", false) as IAssetStorageProvider;
  71. m_backends.Add(StorageProvider);
  72. InventoryProvider = LoadAssetInventoryServerPlugin("/OpenSim/AssetInventoryServer/InventoryStorageProvider",
  73. "inventory_storage_provider", false) as IInventoryStorageProvider;
  74. m_backends.Add(InventoryProvider);
  75. MetricsProvider = LoadAssetInventoryServerPlugin("/OpenSim/AssetInventoryServer/MetricsProvider",
  76. "metrics_provider", false) as IMetricsProvider;
  77. m_backends.Add(MetricsProvider);
  78. try
  79. {
  80. InitHttpServer((uint) ConfigFile.Configs["Config"].GetInt("listen_port"));
  81. }
  82. catch (Exception ex)
  83. {
  84. m_log.Error("[ASSETINVENTORY]: Initializing the HTTP server failed, shutting down: " + ex.Message);
  85. Shutdown();
  86. return false;
  87. }
  88. LoadDefaultAssets();
  89. AuthenticationProvider = LoadAssetInventoryServerPlugin("/OpenSim/AssetInventoryServer/AuthenticationProvider",
  90. "authentication_provider", false) as IAuthenticationProvider;
  91. m_backends.Add(AuthenticationProvider);
  92. AuthorizationProvider = LoadAssetInventoryServerPlugin("/OpenSim/AssetInventoryServer/AuthorizationProvider",
  93. "authorization_provider", false) as IAuthorizationProvider;
  94. m_backends.Add(AuthorizationProvider);
  95. m_frontends.AddRange(LoadAssetInventoryServerPlugins("/OpenSim/AssetInventoryServer/Frontend", "frontends"));
  96. // Inform the user if we don't have any frontends at this point.
  97. if (m_frontends.Count == 0)
  98. m_log.Info("[ASSETINVENTORY]: Starting with no frontends loaded, which isn't extremely useful. Did you set the 'frontends' configuration parameter?");
  99. return true;
  100. }
  101. public void Work()
  102. {
  103. m_console.Output("Enter help for a list of commands");
  104. while (true)
  105. {
  106. m_console.Prompt();
  107. }
  108. }
  109. public override void ShutdownSpecific()
  110. {
  111. foreach (IAssetInventoryServerPlugin plugin in m_frontends)
  112. {
  113. m_log.Debug("[ASSETINVENTORY]: Disposing plugin " + plugin.Name);
  114. try { plugin.Dispose(); }
  115. catch (Exception ex)
  116. { m_log.ErrorFormat("[ASSETINVENTORY]: Failure shutting down plugin {0}: {1}", plugin.Name, ex.Message); }
  117. }
  118. foreach (IAssetInventoryServerPlugin plugin in m_backends)
  119. {
  120. m_log.Debug("[ASSETINVENTORY]: Disposing plugin " + plugin.Name);
  121. try { plugin.Dispose(); }
  122. catch (Exception ex)
  123. { m_log.ErrorFormat("[ASSETINVENTORY]: Failure shutting down plugin {0}: {1}", plugin.Name, ex.Message); }
  124. }
  125. if (HttpServer != null)
  126. HttpServer.Stop();
  127. }
  128. void InitHttpServer(uint port)
  129. {
  130. m_httpServer = new BaseHttpServer(port);
  131. m_httpServer.Start();
  132. m_log.Info("[ASSETINVENTORY]: AssetInventory server is listening on port " + port);
  133. }
  134. private IAssetInventoryServerPlugin LoadAssetInventoryServerPlugin(string addinPath, string configParam, bool optional)
  135. {
  136. IAssetInventoryServerPlugin result = null;
  137. List<IAssetInventoryServerPlugin> plugins = LoadAssetInventoryServerPlugins(addinPath, configParam);
  138. if (plugins.Count == 1)
  139. {
  140. result = plugins[0];
  141. }
  142. else if (plugins.Count > 1)
  143. {
  144. m_log.ErrorFormat("[ASSETINVENTORY]: Only 1 plugin expected for extension point '{0}', {1} plugins loaded. Check the '{2}' parameter in the config file.",
  145. addinPath, plugins.Count, configParam);
  146. Shutdown();
  147. Environment.Exit(0);
  148. }
  149. else if (!optional)
  150. {
  151. m_log.ErrorFormat("[ASSETINVENTORY]: The extension point '{0}' is not optional. Check the '{1}' parameter in the config file.", addinPath, configParam);
  152. Shutdown();
  153. Environment.Exit(0);
  154. }
  155. return result;
  156. }
  157. private List<IAssetInventoryServerPlugin> LoadAssetInventoryServerPlugins(string addinPath, string configParam)
  158. {
  159. PluginLoader<IAssetInventoryServerPlugin> loader = new PluginLoader<IAssetInventoryServerPlugin>(new AssetInventoryServerPluginInitialiser(this));
  160. loader.Add(addinPath, new PluginIdFilter(ConfigFile.Configs["Plugins"].GetString(configParam)));
  161. try
  162. {
  163. loader.Load();
  164. }
  165. catch (PluginNotInitialisedException e)
  166. {
  167. m_log.ErrorFormat("[ASSETINVENTORY]: Error initialising plugin '{0}' for extension point '{1}'.", e.Message, addinPath);
  168. Shutdown();
  169. Environment.Exit(0);
  170. }
  171. return loader.Plugins;
  172. }
  173. private void LoadDefaultAssets()
  174. {
  175. AssetLoaderFileSystem assetLoader = new AssetLoaderFileSystem();
  176. assetLoader.ForEachDefaultXmlAsset(ConfigFile.Configs["Config"].GetString("assetset_location"), StoreAsset);
  177. }
  178. private void StoreAsset(AssetBase asset)
  179. {
  180. StorageProvider.TryCreateAsset(asset);
  181. }
  182. }
  183. }