diff options
Diffstat (limited to 'OpenSim/Grid/AssetInventoryServer/Plugins/Simple/SimpleAssetStoragePlugin.cs')
-rw-r--r-- | OpenSim/Grid/AssetInventoryServer/Plugins/Simple/SimpleAssetStoragePlugin.cs | 290 |
1 files changed, 0 insertions, 290 deletions
diff --git a/OpenSim/Grid/AssetInventoryServer/Plugins/Simple/SimpleAssetStoragePlugin.cs b/OpenSim/Grid/AssetInventoryServer/Plugins/Simple/SimpleAssetStoragePlugin.cs deleted file mode 100644 index 4ec2d96..0000000 --- a/OpenSim/Grid/AssetInventoryServer/Plugins/Simple/SimpleAssetStoragePlugin.cs +++ /dev/null | |||
@@ -1,290 +0,0 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Reflection; | ||
30 | using System.Collections.Generic; | ||
31 | using System.IO; | ||
32 | using OpenMetaverse; | ||
33 | using OpenSim.Framework; | ||
34 | using log4net; | ||
35 | |||
36 | namespace OpenSim.Grid.AssetInventoryServer.Plugins.Simple | ||
37 | { | ||
38 | public class SimpleAssetStoragePlugin : IAssetStorageProvider | ||
39 | { | ||
40 | const string EXTENSION_NAME = "SimpleAssetStorage"; // Used in metrics reporting | ||
41 | const string DEFAULT_DATA_DIR = "SimpleAssets"; | ||
42 | const string TEMP_DATA_DIR = "SimpleAssetsTemp"; | ||
43 | |||
44 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
45 | AssetInventoryServer server; | ||
46 | Dictionary<UUID, AssetMetadata> metadataStorage; | ||
47 | Dictionary<UUID, string> filenames; | ||
48 | |||
49 | public SimpleAssetStoragePlugin() | ||
50 | { | ||
51 | } | ||
52 | |||
53 | #region Required Interfaces | ||
54 | |||
55 | public BackendResponse TryFetchMetadata(UUID assetID, out AssetMetadata metadata) | ||
56 | { | ||
57 | metadata = null; | ||
58 | BackendResponse ret; | ||
59 | |||
60 | if (metadataStorage.TryGetValue(assetID, out metadata)) | ||
61 | ret = BackendResponse.Success; | ||
62 | else | ||
63 | ret = BackendResponse.NotFound; | ||
64 | |||
65 | server.MetricsProvider.LogAssetMetadataFetch(EXTENSION_NAME, ret, assetID, DateTime.Now); | ||
66 | return ret; | ||
67 | } | ||
68 | |||
69 | public BackendResponse TryFetchData(UUID assetID, out byte[] assetData) | ||
70 | { | ||
71 | assetData = null; | ||
72 | string filename; | ||
73 | BackendResponse ret; | ||
74 | |||
75 | if (filenames.TryGetValue(assetID, out filename)) | ||
76 | { | ||
77 | try | ||
78 | { | ||
79 | assetData = File.ReadAllBytes(filename); | ||
80 | ret = BackendResponse.Success; | ||
81 | } | ||
82 | catch (Exception ex) | ||
83 | { | ||
84 | m_log.ErrorFormat("[SIMPLEASSETSTORAGE]: Failed reading data for asset {0} from {1}: {2}", assetID, filename, ex.Message); | ||
85 | ret = BackendResponse.Failure; | ||
86 | } | ||
87 | } | ||
88 | else | ||
89 | { | ||
90 | ret = BackendResponse.NotFound; | ||
91 | } | ||
92 | |||
93 | server.MetricsProvider.LogAssetDataFetch(EXTENSION_NAME, ret, assetID, (assetData != null ? assetData.Length : 0), DateTime.Now); | ||
94 | return ret; | ||
95 | } | ||
96 | |||
97 | public BackendResponse TryFetchDataMetadata(UUID assetID, out AssetBase asset) | ||
98 | { | ||
99 | asset = new AssetBase(); | ||
100 | AssetMetadata metadata = asset.Metadata; | ||
101 | |||
102 | string filename; | ||
103 | BackendResponse ret; | ||
104 | |||
105 | if (metadataStorage.TryGetValue(assetID, out metadata) && | ||
106 | filenames.TryGetValue(assetID, out filename)) | ||
107 | { | ||
108 | try | ||
109 | { | ||
110 | asset.Data = File.ReadAllBytes(filename); | ||
111 | ret = BackendResponse.Success; | ||
112 | } | ||
113 | catch (Exception ex) | ||
114 | { | ||
115 | m_log.ErrorFormat("[SIMPLEASSETSTORAGE]: Failed reading data for asset {0} from {1}: {2}", assetID, filename, ex.Message); | ||
116 | ret = BackendResponse.Failure; | ||
117 | } | ||
118 | |||
119 | asset.Type = (sbyte) Utils.ContentTypeToSLAssetType(metadata.ContentType); | ||
120 | asset.Local = false; | ||
121 | } | ||
122 | else | ||
123 | { | ||
124 | asset = null; | ||
125 | ret = BackendResponse.NotFound; | ||
126 | } | ||
127 | |||
128 | server.MetricsProvider.LogAssetMetadataFetch(EXTENSION_NAME, ret, assetID, DateTime.Now); | ||
129 | server.MetricsProvider.LogAssetDataFetch(EXTENSION_NAME, ret, assetID, (asset != null && asset.Data != null ? asset.Data.Length : 0), DateTime.Now); | ||
130 | return ret; | ||
131 | } | ||
132 | |||
133 | public BackendResponse TryCreateAsset(AssetBase asset, out UUID assetID) | ||
134 | { | ||
135 | assetID = asset.FullID = UUID.Random(); | ||
136 | return TryCreateAsset(asset); | ||
137 | } | ||
138 | |||
139 | public BackendResponse TryCreateAsset(AssetBase asset) | ||
140 | { | ||
141 | BackendResponse ret; | ||
142 | AssetMetadata metadata = asset.Metadata; | ||
143 | |||
144 | string path; | ||
145 | string filename = String.Format("{0}.{1}", asset.FullID, Utils.ContentTypeToExtension(metadata.ContentType)); | ||
146 | |||
147 | if (asset.Temporary) | ||
148 | path = Path.Combine(TEMP_DATA_DIR, filename); | ||
149 | else | ||
150 | path = Path.Combine(DEFAULT_DATA_DIR, filename); | ||
151 | |||
152 | try | ||
153 | { | ||
154 | File.WriteAllBytes(path, asset.Data); | ||
155 | lock (filenames) filenames[asset.FullID] = path; | ||
156 | |||
157 | // Set the creation date to right now | ||
158 | metadata.CreationDate = DateTime.Now; | ||
159 | |||
160 | lock (metadataStorage) | ||
161 | metadataStorage[asset.FullID] = metadata; | ||
162 | |||
163 | ret = BackendResponse.Success; | ||
164 | } | ||
165 | catch (Exception ex) | ||
166 | { | ||
167 | m_log.ErrorFormat("[SIMPLEASSETSTORAGE]: Failed writing data for asset {0} to {1}: {2}", asset.FullID, filename, ex.Message); | ||
168 | ret = BackendResponse.Failure; | ||
169 | } | ||
170 | |||
171 | server.MetricsProvider.LogAssetCreate(EXTENSION_NAME, ret, asset.FullID, asset.Data.Length, DateTime.Now); | ||
172 | return ret; | ||
173 | } | ||
174 | |||
175 | public int ForEach(Action<AssetMetadata> action, int start, int count) | ||
176 | { | ||
177 | int rowCount = 0; | ||
178 | |||
179 | //lock (metadataStorage) | ||
180 | //{ | ||
181 | // foreach (Metadata metadata in metadataStorage.Values) | ||
182 | // { | ||
183 | // action(metadata); | ||
184 | // ++rowCount; | ||
185 | // } | ||
186 | //} | ||
187 | |||
188 | return rowCount; | ||
189 | } | ||
190 | |||
191 | #endregion Required Interfaces | ||
192 | |||
193 | #region IPlugin implementation | ||
194 | |||
195 | public void Initialise(AssetInventoryServer server) | ||
196 | { | ||
197 | this.server = server; | ||
198 | |||
199 | metadataStorage = new Dictionary<UUID, AssetMetadata>(); | ||
200 | filenames = new Dictionary<UUID, string>(); | ||
201 | |||
202 | LoadFiles(DEFAULT_DATA_DIR, false); | ||
203 | LoadFiles(TEMP_DATA_DIR, true); | ||
204 | |||
205 | m_log.InfoFormat("[SIMPLEASSETSTORAGE]: Initialized the store index with metadata for {0} assets", | ||
206 | metadataStorage.Count); | ||
207 | } | ||
208 | |||
209 | /// <summary> | ||
210 | /// <para>Initialises asset interface</para> | ||
211 | /// </summary> | ||
212 | public void Initialise() | ||
213 | { | ||
214 | m_log.InfoFormat("[SIMPLEASSETSTORAGE]: {0} cannot be default-initialized!", Name); | ||
215 | throw new PluginNotInitialisedException(Name); | ||
216 | } | ||
217 | |||
218 | public void Dispose() | ||
219 | { | ||
220 | WipeTemporary(); | ||
221 | } | ||
222 | |||
223 | public string Version | ||
224 | { | ||
225 | // TODO: this should be something meaningful and not hardcoded? | ||
226 | get { return "0.1"; } | ||
227 | } | ||
228 | |||
229 | public string Name | ||
230 | { | ||
231 | get { return "SimpleAssetStorage"; } | ||
232 | } | ||
233 | |||
234 | #endregion IPlugin implementation | ||
235 | |||
236 | public void WipeTemporary() | ||
237 | { | ||
238 | if (Directory.Exists(TEMP_DATA_DIR)) | ||
239 | { | ||
240 | try { Directory.Delete(TEMP_DATA_DIR); } | ||
241 | catch (Exception ex) { m_log.Error("[SIMPLEASSETSTORAGE]: " + ex.Message); } | ||
242 | } | ||
243 | } | ||
244 | |||
245 | void LoadFiles(string folder, bool temporary) | ||
246 | { | ||
247 | // Try to create the directory if it doesn't already exist | ||
248 | if (!Directory.Exists(folder)) | ||
249 | { | ||
250 | try { Directory.CreateDirectory(folder); } | ||
251 | catch (Exception ex) | ||
252 | { | ||
253 | m_log.Warn("[SIMPLEASSETSTORAGE]: " + ex.Message); | ||
254 | return; | ||
255 | } | ||
256 | } | ||
257 | |||
258 | lock (metadataStorage) | ||
259 | { | ||
260 | try | ||
261 | { | ||
262 | string[] assets = Directory.GetFiles(folder); | ||
263 | |||
264 | for (int i = 0; i < assets.Length; i++) | ||
265 | { | ||
266 | string filename = assets[i]; | ||
267 | byte[] data = File.ReadAllBytes(filename); | ||
268 | |||
269 | AssetMetadata metadata = new AssetMetadata(); | ||
270 | metadata.CreationDate = File.GetCreationTime(filename); | ||
271 | metadata.Description = String.Empty; | ||
272 | metadata.FullID = SimpleUtils.ParseUUIDFromFilename(filename); | ||
273 | metadata.Name = SimpleUtils.ParseNameFromFilename(filename); | ||
274 | metadata.SHA1 = OpenMetaverse.Utils.SHA1(data); | ||
275 | metadata.Temporary = false; | ||
276 | metadata.ContentType = Utils.ExtensionToContentType(Path.GetExtension(filename).TrimStart('.')); | ||
277 | |||
278 | // Store the loaded data | ||
279 | metadataStorage[metadata.FullID] = metadata; | ||
280 | filenames[metadata.FullID] = filename; | ||
281 | } | ||
282 | } | ||
283 | catch (Exception ex) | ||
284 | { | ||
285 | m_log.Warn("[SIMPLEASSETSTORAGE]: " + ex.Message); | ||
286 | } | ||
287 | } | ||
288 | } | ||
289 | } | ||
290 | } | ||