DataPluginFactory.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 OpenSim 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 OpenSim.Framework;
  30. namespace OpenSim.Data
  31. {
  32. /// <summary>
  33. /// A static class containing methods for obtaining handles to database
  34. /// storage objects.
  35. /// </summary>
  36. public static class DataPluginFactory
  37. {
  38. /// <summary>
  39. /// Based on <typeparam name="T" />, returns the appropriate
  40. /// PluginInitialiserBase instance in <paramref name="init" /> and
  41. /// extension point path in <paramref name="path" />.
  42. /// </summary>
  43. /// <param name="connect">
  44. /// The DB connection string used when creating a new
  45. /// PluginInitialiserBase, returned in <paramref name="init" />.
  46. /// </param>
  47. /// <param name="init">
  48. /// A reference to a PluginInitialiserBase object in which the proper
  49. /// initialiser will be returned.
  50. /// </param>
  51. /// <param name="path">
  52. /// A string in which the proper extension point path will be returned.
  53. /// </param>
  54. /// <typeparam name="T">
  55. /// The type of data plugin requested.
  56. /// </typeparam>
  57. /// <exception cref="NotImplementedException">
  58. /// Thrown if <typeparamref name="T" /> is not one of the expected data
  59. /// interfaces.
  60. /// </exception>
  61. private static void PluginLoaderParamFactory<T>(string connect, out PluginInitialiserBase init, out string path) where T : IPlugin
  62. {
  63. Type type = typeof(T);
  64. if (type == typeof(IInventoryDataPlugin))
  65. {
  66. init = new InventoryDataInitialiser(connect);
  67. path = "/OpenSim/InventoryData";
  68. }
  69. else if (type == typeof(IUserDataPlugin))
  70. {
  71. init = new UserDataInitialiser(connect);
  72. path = "/OpenSim/UserData";
  73. }
  74. else if (type == typeof(IGridDataPlugin))
  75. {
  76. init = new GridDataInitialiser(connect);
  77. path = "/OpenSim/GridData";
  78. }
  79. else if (type == typeof(ILogDataPlugin))
  80. {
  81. init = new LogDataInitialiser(connect);
  82. path = "/OpenSim/LogData";
  83. }
  84. else if (type == typeof(IAssetDataPlugin))
  85. {
  86. init = new AssetDataInitialiser(connect);
  87. path = "/OpenSim/AssetData";
  88. }
  89. else
  90. {
  91. // We don't support this data plugin.
  92. throw new NotImplementedException(String.Format("The type '{0}' is not a valid data plugin.", type));
  93. }
  94. }
  95. /// <summary>
  96. /// Returns a list of new <typeparamref name="T" /> data plugins.
  97. /// Plugins will be requested in the order they were added.
  98. /// </summary>
  99. /// <param name="provider">
  100. /// The filename of the inventory server plugin DLL.
  101. /// </param>
  102. /// <param name="connect">
  103. /// The connection string for the storage backend.
  104. /// </param>
  105. /// <typeparam name="T">
  106. /// The type of data plugin requested.
  107. /// </typeparam>
  108. /// <returns>
  109. /// A list of all loaded plugins matching <typeparamref name="T" />.
  110. /// </returns>
  111. public static List<T> LoadDataPlugins<T>(string provider, string connect) where T : IPlugin
  112. {
  113. PluginInitialiserBase pluginInitialiser;
  114. string extensionPointPath;
  115. PluginLoaderParamFactory<T>(connect, out pluginInitialiser, out extensionPointPath);
  116. PluginLoader<T> loader = new PluginLoader<T>(pluginInitialiser);
  117. // loader will try to load all providers (MySQL, MSSQL, etc)
  118. // unless it is constrainted to the correct "Provider" entry in the addin.xml
  119. loader.Add(extensionPointPath, new PluginProviderFilter(provider));
  120. loader.Load();
  121. return loader.Plugins;
  122. }
  123. /// <summary>
  124. /// Returns a new <typeparamref name="T" /> data plugin instance if
  125. /// only one was loaded, otherwise returns null (<c>default(T)</c>).
  126. /// </summary>
  127. /// <param name="provider">
  128. /// The filename of the inventory server plugin DLL.
  129. /// </param>
  130. /// <param name="connect">
  131. /// The connection string for the storage backend.
  132. /// </param>
  133. /// <typeparam name="T">
  134. /// The type of data plugin requested.
  135. /// </typeparam>
  136. /// <returns>
  137. /// A list of all loaded plugins matching <typeparamref name="T" />.
  138. /// </returns>
  139. public static T LoadDataPlugin<T>(string provider, string connect) where T : IPlugin
  140. {
  141. List<T> plugins = LoadDataPlugins<T>(provider, connect);
  142. return (plugins.Count == 1) ? plugins[0] : default(T);
  143. }
  144. }
  145. }