opensim-development – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 eva 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 log4net;
29 using Nini.Config;
30 using System;
31 using System.Collections.Generic;
32 using System.Collections.Specialized;
33 using System.Reflection;
34 using System.Web;
35 using OpenSim.Framework;
36 using OpenSim.Services.Interfaces;
37 using OpenSim.Services.Connectors.Hypergrid;
38 using OpenSim.Services.Connectors.SimianGrid;
39  
40 namespace OpenSim.Services.Connectors
41 {
42 public class HGAssetServiceConnector : IAssetService
43 {
44 private static readonly ILog m_log =
45 LogManager.GetLogger(
46 MethodBase.GetCurrentMethod().DeclaringType);
47  
48 private Dictionary<IAssetService, object> m_endpointSerializer = new Dictionary<IAssetService, object>();
49 private object EndPointLock(IAssetService connector)
50 {
51 lock (m_endpointSerializer)
52 {
53 object eplock = null;
54  
55 if (! m_endpointSerializer.TryGetValue(connector, out eplock))
56 {
57 eplock = new object();
58 m_endpointSerializer.Add(connector, eplock);
59 // m_log.WarnFormat("[WEB UTIL] add a new host to end point serializer {0}",endpoint);
60 }
61  
62 return eplock;
63 }
64 }
65  
66 private Dictionary<string, IAssetService> m_connectors = new Dictionary<string, IAssetService>();
67  
68 public HGAssetServiceConnector(IConfigSource source)
69 {
70 IConfig moduleConfig = source.Configs["Modules"];
71 if (moduleConfig != null)
72 {
73 // string name = moduleConfig.GetString("AssetServices", "");
74  
75 IConfig assetConfig = source.Configs["AssetService"];
76 if (assetConfig == null)
77 {
78 m_log.Error("[HG ASSET SERVICE]: AssetService missing from OpenSim.ini");
79 return;
80 }
81  
82 m_log.Info("[HG ASSET SERVICE]: HG asset service enabled");
83 }
84 }
85  
86 private bool StringToUrlAndAssetID(string id, out string url, out string assetID)
87 {
88 url = String.Empty;
89 assetID = String.Empty;
90  
91 Uri assetUri;
92  
93 if (Uri.TryCreate(id, UriKind.Absolute, out assetUri) &&
94 assetUri.Scheme == Uri.UriSchemeHttp)
95 {
96 // Simian
97 if (assetUri.Query != string.Empty)
98 {
99 NameValueCollection qscoll = HttpUtility.ParseQueryString(assetUri.Query);
100 assetID = qscoll["id"];
101 if (assetID != null)
102 url = id.Replace(assetID, ""); // Malformed again, as simian expects
103 else
104 url = id; // !!! best effort
105 }
106 else // robust
107 {
108 url = "http://" + assetUri.Authority;
109 assetID = assetUri.LocalPath.Trim(new char[] { '/' });
110 }
111  
112 return true;
113 }
114  
115 m_log.DebugFormat("[HG ASSET SERVICE]: Malformed URL {0}", id);
116 return false;
117 }
118  
119 private IAssetService GetConnector(string url)
120 {
121 IAssetService connector = null;
122 lock (m_connectors)
123 {
124 if (m_connectors.ContainsKey(url))
125 {
126 connector = m_connectors[url];
127 }
128 else
129 {
130 // Still not as flexible as I would like this to be,
131 // but good enough for now
132 string connectorType = new HeloServicesConnector(url).Helo();
133 m_log.DebugFormat("[HG ASSET SERVICE]: HELO returned {0}", connectorType);
134 if (connectorType == "opensim-simian")
135 {
136 connector = new SimianAssetServiceConnector(url);
137 }
138 else
139 connector = new AssetServicesConnector(url);
140  
141 m_connectors.Add(url, connector);
142 }
143 }
144 return connector;
145 }
146  
147 public AssetBase Get(string id)
148 {
149 string url = string.Empty;
150 string assetID = string.Empty;
151  
152 if (StringToUrlAndAssetID(id, out url, out assetID))
153 {
154 IAssetService connector = GetConnector(url);
155 return connector.Get(assetID);
156 }
157  
158 return null;
159 }
160  
161 public AssetBase GetCached(string id)
162 {
163 string url = string.Empty;
164 string assetID = string.Empty;
165  
166 if (StringToUrlAndAssetID(id, out url, out assetID))
167 {
168 IAssetService connector = GetConnector(url);
169 return connector.GetCached(assetID);
170 }
171  
172 return null;
173 }
174  
175 public AssetMetadata GetMetadata(string id)
176 {
177 string url = string.Empty;
178 string assetID = string.Empty;
179  
180 if (StringToUrlAndAssetID(id, out url, out assetID))
181 {
182 IAssetService connector = GetConnector(url);
183 return connector.GetMetadata(assetID);
184 }
185  
186 return null;
187 }
188  
189 public byte[] GetData(string id)
190 {
191 return null;
192 }
193  
194 public bool Get(string id, Object sender, AssetRetrieved handler)
195 {
196 string url = string.Empty;
197 string assetID = string.Empty;
198  
199 if (StringToUrlAndAssetID(id, out url, out assetID))
200 {
201 IAssetService connector = GetConnector(url);
202 return connector.Get(assetID, sender, handler);
203 }
204  
205 return false;
206 }
207  
208 public string Store(AssetBase asset)
209 {
210 string url = string.Empty;
211 string assetID = string.Empty;
212  
213 if (StringToUrlAndAssetID(asset.ID, out url, out assetID))
214 {
215 IAssetService connector = GetConnector(url);
216 // Restore the assetID to a simple UUID
217 asset.ID = assetID;
218 lock (EndPointLock(connector))
219 return connector.Store(asset);
220 }
221  
222 return String.Empty;
223 }
224  
225 public bool UpdateContent(string id, byte[] data)
226 {
227 return false;
228 }
229  
230 public bool Delete(string id)
231 {
232 return false;
233 }
234 }
235 }