diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Services/AssetService/XAssetService.cs | 213 |
1 files changed, 213 insertions, 0 deletions
diff --git a/OpenSim/Services/AssetService/XAssetService.cs b/OpenSim/Services/AssetService/XAssetService.cs new file mode 100644 index 0000000..d161c58 --- /dev/null +++ b/OpenSim/Services/AssetService/XAssetService.cs | |||
@@ -0,0 +1,213 @@ | |||
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.Collections.Generic; | ||
30 | using System.IO; | ||
31 | using System.Reflection; | ||
32 | using Nini.Config; | ||
33 | using log4net; | ||
34 | using OpenSim.Framework; | ||
35 | using OpenSim.Data; | ||
36 | using OpenSim.Services.Interfaces; | ||
37 | using OpenMetaverse; | ||
38 | |||
39 | namespace OpenSim.Services.AssetService | ||
40 | { | ||
41 | /// <summary> | ||
42 | /// This will be developed into a de-duplicating asset service. | ||
43 | /// XXX: Currently it's a just a copy of the existing AssetService. so please don't attempt to use it. | ||
44 | /// </summary> | ||
45 | public class XAssetService : AssetServiceBase, IAssetService | ||
46 | { | ||
47 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
48 | |||
49 | protected static XAssetService m_RootInstance; | ||
50 | |||
51 | public XAssetService(IConfigSource config) : base(config) | ||
52 | { | ||
53 | if (m_RootInstance == null) | ||
54 | { | ||
55 | m_RootInstance = this; | ||
56 | |||
57 | if (m_AssetLoader != null) | ||
58 | { | ||
59 | IConfig assetConfig = config.Configs["AssetService"]; | ||
60 | if (assetConfig == null) | ||
61 | throw new Exception("No AssetService configuration"); | ||
62 | |||
63 | string loaderArgs = assetConfig.GetString("AssetLoaderArgs", | ||
64 | String.Empty); | ||
65 | |||
66 | bool assetLoaderEnabled = assetConfig.GetBoolean("AssetLoaderEnabled", true); | ||
67 | |||
68 | if (assetLoaderEnabled) | ||
69 | { | ||
70 | m_log.DebugFormat("[XASSET SERVICE]: Loading default asset set from {0}", loaderArgs); | ||
71 | |||
72 | m_AssetLoader.ForEachDefaultXmlAsset( | ||
73 | loaderArgs, | ||
74 | delegate(AssetBase a) | ||
75 | { | ||
76 | AssetBase existingAsset = Get(a.ID); | ||
77 | // AssetMetadata existingMetadata = GetMetadata(a.ID); | ||
78 | |||
79 | if (existingAsset == null || Util.SHA1Hash(existingAsset.Data) != Util.SHA1Hash(a.Data)) | ||
80 | { | ||
81 | // m_log.DebugFormat("[ASSET]: Storing {0} {1}", a.Name, a.ID); | ||
82 | Store(a); | ||
83 | } | ||
84 | }); | ||
85 | } | ||
86 | |||
87 | m_log.Debug("[XASSET SERVICE]: Local asset service enabled"); | ||
88 | } | ||
89 | } | ||
90 | } | ||
91 | |||
92 | public virtual AssetBase Get(string id) | ||
93 | { | ||
94 | // m_log.DebugFormat("[ASSET SERVICE]: Get asset for {0}", id); | ||
95 | |||
96 | UUID assetID; | ||
97 | |||
98 | if (!UUID.TryParse(id, out assetID)) | ||
99 | { | ||
100 | m_log.WarnFormat("[XASSET SERVICE]: Could not parse requested asset id {0}", id); | ||
101 | return null; | ||
102 | } | ||
103 | |||
104 | try | ||
105 | { | ||
106 | return m_Database.GetAsset(assetID); | ||
107 | } | ||
108 | catch (Exception e) | ||
109 | { | ||
110 | m_log.ErrorFormat("[XASSET SERVICE]: Exception getting asset {0} {1}", assetID, e); | ||
111 | return null; | ||
112 | } | ||
113 | } | ||
114 | |||
115 | public virtual AssetBase GetCached(string id) | ||
116 | { | ||
117 | return Get(id); | ||
118 | } | ||
119 | |||
120 | public virtual AssetMetadata GetMetadata(string id) | ||
121 | { | ||
122 | // m_log.DebugFormat("[XASSET SERVICE]: Get asset metadata for {0}", id); | ||
123 | |||
124 | UUID assetID; | ||
125 | |||
126 | if (!UUID.TryParse(id, out assetID)) | ||
127 | return null; | ||
128 | |||
129 | AssetBase asset = m_Database.GetAsset(assetID); | ||
130 | if (asset != null) | ||
131 | return asset.Metadata; | ||
132 | |||
133 | return null; | ||
134 | } | ||
135 | |||
136 | public virtual byte[] GetData(string id) | ||
137 | { | ||
138 | // m_log.DebugFormat("[XASSET SERVICE]: Get asset data for {0}", id); | ||
139 | |||
140 | UUID assetID; | ||
141 | |||
142 | if (!UUID.TryParse(id, out assetID)) | ||
143 | return null; | ||
144 | |||
145 | AssetBase asset = m_Database.GetAsset(assetID); | ||
146 | return asset.Data; | ||
147 | } | ||
148 | |||
149 | public virtual bool Get(string id, Object sender, AssetRetrieved handler) | ||
150 | { | ||
151 | //m_log.DebugFormat("[XASSET SERVICE]: Get asset async {0}", id); | ||
152 | |||
153 | UUID assetID; | ||
154 | |||
155 | if (!UUID.TryParse(id, out assetID)) | ||
156 | return false; | ||
157 | |||
158 | AssetBase asset = m_Database.GetAsset(assetID); | ||
159 | |||
160 | //m_log.DebugFormat("[XASSET SERVICE]: Got asset {0}", asset); | ||
161 | |||
162 | handler(id, sender, asset); | ||
163 | |||
164 | return true; | ||
165 | } | ||
166 | |||
167 | public virtual string Store(AssetBase asset) | ||
168 | { | ||
169 | if (!m_Database.ExistsAsset(asset.FullID)) | ||
170 | { | ||
171 | // m_log.DebugFormat( | ||
172 | // "[XASSET SERVICE]: Storing asset {0} {1}, bytes {2}", asset.Name, asset.FullID, asset.Data.Length); | ||
173 | m_Database.StoreAsset(asset); | ||
174 | } | ||
175 | // else | ||
176 | // { | ||
177 | // m_log.DebugFormat( | ||
178 | // "[XASSET SERVICE]: Not storing asset {0} {1}, bytes {2} as it already exists", asset.Name, asset.FullID, asset.Data.Length); | ||
179 | // } | ||
180 | |||
181 | return asset.ID; | ||
182 | } | ||
183 | |||
184 | public bool UpdateContent(string id, byte[] data) | ||
185 | { | ||
186 | return false; | ||
187 | } | ||
188 | |||
189 | public virtual bool Delete(string id) | ||
190 | { | ||
191 | m_log.DebugFormat("[XASSET SERVICE]: Deleting asset {0}", id); | ||
192 | UUID assetID; | ||
193 | if (!UUID.TryParse(id, out assetID)) | ||
194 | return false; | ||
195 | |||
196 | AssetBase asset = m_Database.GetAsset(assetID); | ||
197 | if (asset == null) | ||
198 | return false; | ||
199 | |||
200 | if ((int)(asset.Flags & AssetFlags.Maptile) != 0) | ||
201 | { | ||
202 | return m_Database.Delete(id); | ||
203 | } | ||
204 | else | ||
205 | { | ||
206 | m_log.DebugFormat("[XASSET SERVICE]: Request to delete asset {0}, but flags are not Maptile", id); | ||
207 | } | ||
208 | |||
209 | return false; | ||
210 | } | ||
211 | } | ||
212 | } | ||
213 | |||