diff options
Diffstat (limited to 'OpenSim/Grid/AssetInventoryServer/Plugins')
2 files changed, 366 insertions, 0 deletions
diff --git a/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetStoragePlugin.cs b/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetStoragePlugin.cs new file mode 100644 index 0000000..7f5f85a --- /dev/null +++ b/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetStoragePlugin.cs | |||
@@ -0,0 +1,352 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2008 Intel Corporation | ||
3 | * All rights reserved. | ||
4 | * Redistribution and use in source and binary forms, with or without | ||
5 | * modification, are permitted provided that the following conditions | ||
6 | * are met: | ||
7 | * | ||
8 | * -- Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * -- Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * -- Neither the name of the Intel Corporation nor the names of its | ||
14 | * contributors may be used to endorse or promote products derived from | ||
15 | * this software without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
18 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||
20 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR ITS | ||
21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | using System; | ||
31 | using System.Collections.Generic; | ||
32 | using System.Net; | ||
33 | using System.Data; | ||
34 | using MySql.Data.MySqlClient; | ||
35 | using ExtensionLoader; | ||
36 | using ExtensionLoader.Config; | ||
37 | using OpenMetaverse; | ||
38 | using OpenMetaverse.StructuredData; | ||
39 | using OpenSim.Framework; | ||
40 | using OpenSim.Grid.AssetInventoryServer.Extensions; | ||
41 | |||
42 | namespace OpenSim.Grid.AssetInventoryServer.Plugins | ||
43 | { | ||
44 | public class OpenSimAssetStoragePlugin : IAssetStorageProvider | ||
45 | { | ||
46 | const string EXTENSION_NAME = "OpenSimAssetStorage"; // Used in metrics reporting | ||
47 | |||
48 | private AssetInventoryServer server; | ||
49 | private IAssetProviderPlugin m_assetProvider; | ||
50 | |||
51 | public OpenSimAssetStoragePlugin() | ||
52 | { | ||
53 | } | ||
54 | |||
55 | #region IAssetStorageProvider implementation | ||
56 | |||
57 | public BackendResponse TryFetchMetadata(UUID assetID, out Metadata metadata) | ||
58 | { | ||
59 | metadata = null; | ||
60 | BackendResponse ret; | ||
61 | |||
62 | using (MySqlConnection dbConnection = new MySqlConnection(DBConnString.GetConnectionString(server.ConfigFile))) | ||
63 | { | ||
64 | IDataReader reader; | ||
65 | |||
66 | try | ||
67 | { | ||
68 | dbConnection.Open(); | ||
69 | |||
70 | IDbCommand command = dbConnection.CreateCommand(); | ||
71 | command.CommandText = String.Format("SELECT name,description,assetType,temporary FROM assets WHERE id='{0}'", assetID.ToString()); | ||
72 | reader = command.ExecuteReader(); | ||
73 | |||
74 | if (reader.Read()) | ||
75 | { | ||
76 | metadata = new Metadata(); | ||
77 | metadata.CreationDate = OpenMetaverse.Utils.Epoch; | ||
78 | metadata.SHA1 = null; | ||
79 | metadata.ID = assetID; | ||
80 | metadata.Name = reader.GetString(0); | ||
81 | metadata.Description = reader.GetString(1); | ||
82 | metadata.ContentType = Utils.SLAssetTypeToContentType(reader.GetInt32(2)); | ||
83 | metadata.Temporary = reader.GetBoolean(3); | ||
84 | |||
85 | ret = BackendResponse.Success; | ||
86 | } | ||
87 | else | ||
88 | { | ||
89 | ret = BackendResponse.NotFound; | ||
90 | } | ||
91 | } | ||
92 | catch (MySqlException ex) | ||
93 | { | ||
94 | Logger.Log.Error("Connection to MySQL backend failed: " + ex.Message); | ||
95 | ret = BackendResponse.Failure; | ||
96 | } | ||
97 | } | ||
98 | |||
99 | server.MetricsProvider.LogAssetMetadataFetch(EXTENSION_NAME, ret, assetID, DateTime.Now); | ||
100 | return ret; | ||
101 | } | ||
102 | |||
103 | public BackendResponse TryFetchData(UUID assetID, out byte[] assetData) | ||
104 | { | ||
105 | assetData = null; | ||
106 | BackendResponse ret; | ||
107 | |||
108 | using (MySqlConnection dbConnection = new MySqlConnection(DBConnString.GetConnectionString(server.ConfigFile))) | ||
109 | { | ||
110 | IDataReader reader; | ||
111 | |||
112 | try | ||
113 | { | ||
114 | dbConnection.Open(); | ||
115 | |||
116 | IDbCommand command = dbConnection.CreateCommand(); | ||
117 | command.CommandText = String.Format("SELECT data FROM assets WHERE id='{0}'", assetID.ToString()); | ||
118 | reader = command.ExecuteReader(); | ||
119 | |||
120 | if (reader.Read()) | ||
121 | { | ||
122 | assetData = (byte[])reader.GetValue(0); | ||
123 | ret = BackendResponse.Success; | ||
124 | } | ||
125 | else | ||
126 | { | ||
127 | ret = BackendResponse.NotFound; | ||
128 | } | ||
129 | } | ||
130 | catch (MySqlException ex) | ||
131 | { | ||
132 | Logger.Log.Error("Connection to MySQL backend failed: " + ex.Message); | ||
133 | ret = BackendResponse.Failure; | ||
134 | } | ||
135 | } | ||
136 | |||
137 | server.MetricsProvider.LogAssetDataFetch(EXTENSION_NAME, ret, assetID, (assetData != null ? assetData.Length : 0), DateTime.Now); | ||
138 | return ret; | ||
139 | } | ||
140 | |||
141 | public BackendResponse TryFetchDataMetadata(UUID assetID, out Metadata metadata, out byte[] assetData) | ||
142 | { | ||
143 | metadata = null; | ||
144 | assetData = null; | ||
145 | BackendResponse ret; | ||
146 | |||
147 | using (MySqlConnection dbConnection = new MySqlConnection(DBConnString.GetConnectionString(server.ConfigFile))) | ||
148 | { | ||
149 | IDataReader reader; | ||
150 | |||
151 | try | ||
152 | { | ||
153 | dbConnection.Open(); | ||
154 | |||
155 | IDbCommand command = dbConnection.CreateCommand(); | ||
156 | command.CommandText = String.Format("SELECT name,description,assetType,temporary,data FROM assets WHERE id='{0}'", assetID.ToString()); | ||
157 | reader = command.ExecuteReader(); | ||
158 | |||
159 | if (reader.Read()) | ||
160 | { | ||
161 | metadata = new Metadata(); | ||
162 | metadata.CreationDate = OpenMetaverse.Utils.Epoch; | ||
163 | metadata.SHA1 = null; | ||
164 | metadata.ID = assetID; | ||
165 | metadata.Name = reader.GetString(0); | ||
166 | metadata.Description = reader.GetString(1); | ||
167 | metadata.ContentType = Utils.SLAssetTypeToContentType(reader.GetInt32(2)); | ||
168 | metadata.Temporary = reader.GetBoolean(3); | ||
169 | |||
170 | assetData = (byte[])reader.GetValue(4); | ||
171 | |||
172 | ret = BackendResponse.Success; | ||
173 | } | ||
174 | else | ||
175 | { | ||
176 | ret = BackendResponse.NotFound; | ||
177 | } | ||
178 | } | ||
179 | catch (MySqlException ex) | ||
180 | { | ||
181 | Logger.Log.Error("Connection to MySQL backend failed: " + ex.Message); | ||
182 | ret = BackendResponse.Failure; | ||
183 | } | ||
184 | } | ||
185 | |||
186 | server.MetricsProvider.LogAssetMetadataFetch(EXTENSION_NAME, ret, assetID, DateTime.Now); | ||
187 | server.MetricsProvider.LogAssetDataFetch(EXTENSION_NAME, ret, assetID, (assetData != null ? assetData.Length : 0), DateTime.Now); | ||
188 | return ret; | ||
189 | } | ||
190 | |||
191 | public BackendResponse TryCreateAsset(Metadata metadata, byte[] assetData, out UUID assetID) | ||
192 | { | ||
193 | assetID = metadata.ID = UUID.Random(); | ||
194 | return TryCreateAsset(metadata, assetData); | ||
195 | } | ||
196 | |||
197 | public BackendResponse TryCreateAsset(Metadata metadata, byte[] assetData) | ||
198 | { | ||
199 | BackendResponse ret; | ||
200 | |||
201 | using (MySqlConnection dbConnection = new MySqlConnection(DBConnString.GetConnectionString(server.ConfigFile))) | ||
202 | { | ||
203 | try | ||
204 | { | ||
205 | dbConnection.Open(); | ||
206 | |||
207 | MySqlCommand command = new MySqlCommand( | ||
208 | "REPLACE INTO assets (name,description,assetType,local,temporary,data,id) VALUES " + | ||
209 | "(?name,?description,?assetType,?local,?temporary,?data,?id)", dbConnection); | ||
210 | |||
211 | command.Parameters.AddWithValue("?name", metadata.Name); | ||
212 | command.Parameters.AddWithValue("?description", metadata.Description); | ||
213 | command.Parameters.AddWithValue("?assetType", Utils.ContentTypeToSLAssetType(metadata.ContentType)); | ||
214 | command.Parameters.AddWithValue("?local", 0); | ||
215 | command.Parameters.AddWithValue("?temporary", metadata.Temporary); | ||
216 | command.Parameters.AddWithValue("?data", assetData); | ||
217 | command.Parameters.AddWithValue("?id", metadata.ID.ToString()); | ||
218 | |||
219 | int rowsAffected = command.ExecuteNonQuery(); | ||
220 | if (rowsAffected == 1) | ||
221 | { | ||
222 | ret = BackendResponse.Success; | ||
223 | } | ||
224 | else if (rowsAffected == 2) | ||
225 | { | ||
226 | Logger.Log.Info("Replaced asset " + metadata.ID.ToString()); | ||
227 | ret = BackendResponse.Success; | ||
228 | } | ||
229 | else | ||
230 | { | ||
231 | Logger.Log.ErrorFormat("MySQL REPLACE query affected {0} rows", rowsAffected); | ||
232 | ret = BackendResponse.Failure; | ||
233 | } | ||
234 | } | ||
235 | catch (MySqlException ex) | ||
236 | { | ||
237 | Logger.Log.Error("Connection to MySQL backend failed: " + ex.Message); | ||
238 | ret = BackendResponse.Failure; | ||
239 | } | ||
240 | } | ||
241 | |||
242 | server.MetricsProvider.LogAssetCreate(EXTENSION_NAME, ret, metadata.ID, assetData.Length, DateTime.Now); | ||
243 | return ret; | ||
244 | } | ||
245 | |||
246 | public int ForEach(Action<Metadata> action, int start, int count) | ||
247 | { | ||
248 | int rowCount = 0; | ||
249 | |||
250 | using (MySqlConnection dbConnection = new MySqlConnection(DBConnString.GetConnectionString(server.ConfigFile))) | ||
251 | { | ||
252 | MySqlDataReader reader; | ||
253 | |||
254 | try | ||
255 | { | ||
256 | dbConnection.Open(); | ||
257 | |||
258 | MySqlCommand command = dbConnection.CreateCommand(); | ||
259 | command.CommandText = String.Format("SELECT name,description,assetType,temporary,data,id FROM assets LIMIT {0}, {1}", | ||
260 | start, count); | ||
261 | reader = command.ExecuteReader(); | ||
262 | } | ||
263 | catch (MySqlException ex) | ||
264 | { | ||
265 | Logger.Log.Error("Connection to MySQL backend failed: " + ex.Message); | ||
266 | return 0; | ||
267 | } | ||
268 | |||
269 | while (reader.Read()) | ||
270 | { | ||
271 | Metadata metadata = new Metadata(); | ||
272 | metadata.CreationDate = OpenMetaverse.Utils.Epoch; | ||
273 | metadata.Description = reader.GetString(1); | ||
274 | metadata.ID = UUID.Parse(reader.GetString(5)); | ||
275 | metadata.Name = reader.GetString(0); | ||
276 | metadata.SHA1 = OpenMetaverse.Utils.SHA1((byte[])reader.GetValue(4)); | ||
277 | metadata.Temporary = reader.GetBoolean(3); | ||
278 | metadata.ContentType = Utils.SLAssetTypeToContentType(reader.GetInt32(2)); | ||
279 | |||
280 | action(metadata); | ||
281 | ++rowCount; | ||
282 | } | ||
283 | |||
284 | reader.Close(); | ||
285 | } | ||
286 | |||
287 | return rowCount; | ||
288 | } | ||
289 | |||
290 | #endregion IAssetStorageProvider implementation | ||
291 | |||
292 | #region IPlugin implementation | ||
293 | |||
294 | public void Initialise(AssetInventoryServer server) | ||
295 | { | ||
296 | this.server = server; | ||
297 | |||
298 | try | ||
299 | { | ||
300 | m_assetProvider = LoadDatabasePlugin("OpenSim.Data.MySQL.dll", server.ConfigFile.Configs["MySQL"].GetString("database_connect", null)); | ||
301 | if (m_assetProvider == null) | ||
302 | { | ||
303 | Logger.Log.Error("[ASSET]: Failed to load a database plugin, server halting."); | ||
304 | Environment.Exit(-1); | ||
305 | } | ||
306 | else | ||
307 | Logger.Log.InfoFormat("[ASSET]: Loaded storage backend: {0}", Version); | ||
308 | } | ||
309 | catch (Exception e) | ||
310 | { | ||
311 | Logger.Log.WarnFormat("[ASSET]: Failure loading data plugin: {0}", e.ToString()); | ||
312 | } | ||
313 | } | ||
314 | |||
315 | /// <summary> | ||
316 | /// <para>Initialises asset interface</para> | ||
317 | /// </summary> | ||
318 | public void Initialise() | ||
319 | { | ||
320 | Logger.Log.InfoFormat("[ASSET]: {0} cannot be default-initialized!", Name); | ||
321 | throw new PluginNotInitialisedException(Name); | ||
322 | } | ||
323 | |||
324 | public void Dispose() | ||
325 | { | ||
326 | } | ||
327 | |||
328 | public string Version | ||
329 | { | ||
330 | get { return m_assetProvider.Version; } | ||
331 | } | ||
332 | |||
333 | public string Name | ||
334 | { | ||
335 | get { return "AssetInventoryServer storage provider"; } | ||
336 | } | ||
337 | |||
338 | #endregion IPlugin implementation | ||
339 | |||
340 | private IAssetProviderPlugin LoadDatabasePlugin(string provider, string connect) | ||
341 | { | ||
342 | PluginLoader<IAssetProviderPlugin> loader = new PluginLoader<IAssetProviderPlugin>(new AssetDataInitialiser(connect)); | ||
343 | |||
344 | // Loader will try to load all providers (MySQL, MSSQL, etc) | ||
345 | // unless it is constrainted to the correct "Provider" entry in the addin.xml | ||
346 | loader.Add("/OpenSim/AssetData", new PluginProviderFilter (provider)); | ||
347 | loader.Load(); | ||
348 | |||
349 | return loader.Plugin; | ||
350 | } | ||
351 | } | ||
352 | } | ||
diff --git a/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/Resources/AssetInventoryServerOpenSimPlugins.addin.xml b/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/Resources/AssetInventoryServerOpenSimPlugins.addin.xml new file mode 100644 index 0000000..eeda54a --- /dev/null +++ b/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/Resources/AssetInventoryServerOpenSimPlugins.addin.xml | |||
@@ -0,0 +1,14 @@ | |||
1 | <Addin id="OpenSim.Grid.AssetInventoryServer.Plugins.OpenSim" version="0.1"> | ||
2 | <Runtime> | ||
3 | <Import assembly="OpenSim.Grid.AssetInventoryServer.Plugins.OpenSim.dll"/> | ||
4 | </Runtime> | ||
5 | <Dependencies> | ||
6 | <Addin id="OpenSim.Grid.AssetInventoryServer" version="0.1" /> | ||
7 | </Dependencies> | ||
8 | <ExtensionPoint path = "/OpenSim/AssetData"> | ||
9 | <ExtensionNode name="Plugin" type="OpenSim.Framework.PluginExtensionNode" objectType="OpenSim.Framework.IAssetProviderPlugin"/> | ||
10 | </ExtensionPoint> | ||
11 | <Extension path="/OpenSim/AssetInventoryServer/StorageProvider"> | ||
12 | <Plugin id="OpenSimAssetStorage" provider="OpenSim.Grid.AssetInventoryServer.Plugins.OpenSim.dll" type="OpenSim.Grid.AssetInventoryServer.Plugins.OpenSimAssetStoragePlugin" /> | ||
13 | </Extension> | ||
14 | </Addin> | ||