aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/CoreModules/ServiceConnectorsOut/Asset/LocalAssetServiceConnector.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/CoreModules/ServiceConnectorsOut/Asset/LocalAssetServiceConnector.cs')
-rw-r--r--OpenSim/Region/CoreModules/ServiceConnectorsOut/Asset/LocalAssetServiceConnector.cs256
1 files changed, 256 insertions, 0 deletions
diff --git a/OpenSim/Region/CoreModules/ServiceConnectorsOut/Asset/LocalAssetServiceConnector.cs b/OpenSim/Region/CoreModules/ServiceConnectorsOut/Asset/LocalAssetServiceConnector.cs
new file mode 100644
index 0000000..13c4b52
--- /dev/null
+++ b/OpenSim/Region/CoreModules/ServiceConnectorsOut/Asset/LocalAssetServiceConnector.cs
@@ -0,0 +1,256 @@
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
28using log4net;
29using Nini.Config;
30using System;
31using System.Collections.Generic;
32using System.Reflection;
33using OpenSim.Framework;
34using OpenSim.Server.Base;
35using OpenSim.Region.Framework.Interfaces;
36using OpenSim.Region.Framework.Scenes;
37using OpenSim.Services.Interfaces;
38
39namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Asset
40{
41 public class LocalAssetServicesConnector :
42 ISharedRegionModule, IAssetService
43 {
44 private static readonly ILog m_log =
45 LogManager.GetLogger(
46 MethodBase.GetCurrentMethod().DeclaringType);
47
48 private IImprovedAssetCache m_Cache = null;
49
50 private IAssetService m_AssetService;
51
52 private bool m_Enabled = false;
53
54 public string Name
55 {
56 get { return "LocalAssetServicesConnector"; }
57 }
58
59 public void Initialise(IConfigSource source)
60 {
61 IConfig moduleConfig = source.Configs["Modules"];
62 if (moduleConfig != null)
63 {
64 string name = moduleConfig.GetString("AssetServices", "");
65 if (name == Name)
66 {
67 IConfig assetConfig = source.Configs["AssetService"];
68 if (assetConfig == null)
69 {
70 m_log.Error("[ASSET CONNECTOR]: AssetService missing from OpenSim.ini");
71 return;
72 }
73
74 string serviceDll = assetConfig.GetString("LocalServiceModule",
75 String.Empty);
76
77 if (serviceDll == String.Empty)
78 {
79 m_log.Error("[ASSET CONNECTOR]: No LocalServiceModule named in section AssetService");
80 return;
81 }
82
83 Object[] args = new Object[] { source };
84 m_AssetService =
85 ServerUtils.LoadPlugin<IAssetService>(serviceDll,
86 args);
87
88 if (m_AssetService == null)
89 {
90 m_log.Error("[ASSET CONNECTOR]: Can't load asset service");
91 return;
92 }
93 m_Enabled = true;
94 m_log.Info("[ASSET CONNECTOR]: Local asset connector enabled");
95 }
96 }
97 }
98
99 public void PostInitialise()
100 {
101 }
102
103 public void Close()
104 {
105 }
106
107 public void AddRegion(Scene scene)
108 {
109 if (!m_Enabled)
110 return;
111
112 scene.RegisterModuleInterface<IAssetService>(this);
113 }
114
115 public void RemoveRegion(Scene scene)
116 {
117 }
118
119 public void RegionLoaded(Scene scene)
120 {
121 if (!m_Enabled)
122 return;
123
124 if (m_Cache == null)
125 {
126 m_Cache = scene.RequestModuleInterface<IImprovedAssetCache>();
127
128 if (!(m_Cache is ISharedRegionModule))
129 m_Cache = null;
130 }
131
132 m_log.InfoFormat("[ASSET CONNECTOR]: Enabled local assets for region {0}", scene.RegionInfo.RegionName);
133
134 if (m_Cache != null)
135 {
136 m_log.InfoFormat("[ASSET CONNECTOR]: Enabled asset caching for region {0}", scene.RegionInfo.RegionName);
137 }
138 else
139 {
140 // Short-circuit directly to storage layer
141 //
142 scene.UnregisterModuleInterface<IAssetService>(this);
143 scene.RegisterModuleInterface<IAssetService>(m_AssetService);
144 }
145 }
146
147 public AssetBase Get(string id)
148 {
149 AssetBase asset = null;
150 if (m_Cache != null)
151 asset = m_Cache.Get(id);
152
153 if (asset == null)
154 {
155 asset = m_AssetService.Get(id);
156 if ((m_Cache != null) && (asset != null))
157 m_Cache.Cache(asset);
158 }
159 return asset;
160 }
161
162 public AssetMetadata GetMetadata(string id)
163 {
164 AssetBase asset = null;
165 if (m_Cache != null)
166 asset = m_Cache.Get(id);
167
168 if (asset != null)
169 return asset.Metadata;
170
171 asset = m_AssetService.Get(id);
172 if (asset != null)
173 {
174 if (m_Cache != null)
175 m_Cache.Cache(asset);
176 return asset.Metadata;
177 }
178
179 return null;
180 }
181
182 public byte[] GetData(string id)
183 {
184 AssetBase asset = m_Cache.Get(id);
185
186 if (asset != null)
187 return asset.Data;
188
189 asset = m_AssetService.Get(id);
190 if (asset != null)
191 {
192 if (m_Cache != null)
193 m_Cache.Cache(asset);
194 return asset.Data;
195 }
196
197 return null;
198 }
199
200 public bool Get(string id, Object sender, AssetRetrieved handler)
201 {
202 AssetBase asset = null;
203
204 if (m_Cache != null)
205 m_Cache.Get(id);
206
207 if (asset != null)
208 {
209 handler.BeginInvoke(id, sender, asset, null, null);
210 return true;
211 }
212
213 return m_AssetService.Get(id, sender, delegate (string assetID, Object s, AssetBase a)
214 {
215 if ((a != null) && (m_Cache != null))
216 m_Cache.Cache(a);
217
218 handler.BeginInvoke(assetID, s, a, null, null);
219 });
220 }
221
222 public string Store(AssetBase asset)
223 {
224 if (m_Cache != null)
225 m_Cache.Cache(asset);
226
227 if (asset.Temporary || asset.Local)
228 return asset.ID;
229
230 return m_AssetService.Store(asset);
231 }
232
233 public bool UpdateContent(string id, byte[] data)
234 {
235 AssetBase asset = null;
236 if (m_Cache != null)
237 m_Cache.Get(id);
238 if (asset != null)
239 {
240 asset.Data = data;
241 if (m_Cache != null)
242 m_Cache.Cache(asset);
243 }
244
245 return m_AssetService.UpdateContent(id, data);
246 }
247
248 public bool Delete(string id)
249 {
250 if (m_Cache != null)
251 m_Cache.Expire(id);
252
253 return m_AssetService.Delete(id);
254 }
255 }
256}