OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
XAssetService.cs
Go to the documentation of this file.
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 {
44  [Obsolete]
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) : this(config, "AssetService") {}
52 
53  public XAssetService(IConfigSource config, string configName) : base(config, configName)
54  {
55  if (m_RootInstance == null)
56  {
57  m_RootInstance = this;
58 
59  if (m_AssetLoader != null)
60  {
61  IConfig assetConfig = config.Configs[configName];
62  if (assetConfig == null)
63  throw new Exception("No AssetService configuration");
64 
65  string loaderArgs = assetConfig.GetString("AssetLoaderArgs", String.Empty);
66 
67  bool assetLoaderEnabled = assetConfig.GetBoolean("AssetLoaderEnabled", true);
68 
69  if (assetLoaderEnabled && !HasChainedAssetService)
70  {
71  m_log.DebugFormat("[XASSET SERVICE]: Loading default asset set from {0}", loaderArgs);
72 
73  m_AssetLoader.ForEachDefaultXmlAsset(
74  loaderArgs,
75  a =>
76  {
77  AssetBase existingAsset = Get(a.ID);
78 // AssetMetadata existingMetadata = GetMetadata(a.ID);
79 
80  if (existingAsset == null || Util.SHA1Hash(existingAsset.Data) != Util.SHA1Hash(a.Data))
81  {
82 // m_log.DebugFormat("[ASSET]: Storing {0} {1}", a.Name, a.ID);
83  Store(a);
84  }
85  });
86  }
87 
88  m_log.Debug("[XASSET SERVICE]: Local asset service enabled");
89  m_log.Error("[XASSET SERVICE]: THIS ASSET SERVICE HAS BEEN MARKED OBSOLETE. PLEASE USE FSAssetService");
90  }
91  }
92  }
93 
94  public virtual AssetBase Get(string id)
95  {
96 // m_log.DebugFormat("[ASSET SERVICE]: Get asset for {0}", id);
97 
98  UUID assetID;
99 
100  if (!UUID.TryParse(id, out assetID))
101  {
102  m_log.WarnFormat("[XASSET SERVICE]: Could not parse requested asset id {0}", id);
103  return null;
104  }
105 
106  try
107  {
108  AssetBase asset = m_Database.GetAsset(assetID);
109 
110  if (asset != null)
111  {
112  return asset;
113  }
114  else if (HasChainedAssetService)
115  {
116  asset = m_ChainedAssetService.Get(id);
117 
118  if (asset != null)
119  MigrateFromChainedService(asset);
120 
121  return asset;
122  }
123 
124  return null;
125  }
126  catch (Exception e)
127  {
128  m_log.ErrorFormat("[XASSET SERVICE]: Exception getting asset {0} {1}", assetID, e);
129  return null;
130  }
131  }
132 
133  public virtual AssetBase GetCached(string id)
134  {
135  return Get(id);
136  }
137 
138  public virtual AssetMetadata GetMetadata(string id)
139  {
140 // m_log.DebugFormat("[XASSET SERVICE]: Get asset metadata for {0}", id);
141 
142  AssetBase asset = Get(id);
143 
144  if (asset != null)
145  return asset.Metadata;
146  else
147  return null;
148  }
149 
150  public virtual byte[] GetData(string id)
151  {
152 // m_log.DebugFormat("[XASSET SERVICE]: Get asset data for {0}", id);
153 
154  AssetBase asset = Get(id);
155 
156  if (asset != null)
157  return asset.Data;
158  else
159  return null;
160  }
161 
162  public virtual bool Get(string id, Object sender, AssetRetrieved handler)
163  {
164  //m_log.DebugFormat("[XASSET SERVICE]: Get asset async {0}", id);
165 
166  UUID assetID;
167 
168  if (!UUID.TryParse(id, out assetID))
169  return false;
170 
171  AssetBase asset = Get(id);
172 
173  //m_log.DebugFormat("[XASSET SERVICE]: Got asset {0}", asset);
174 
175  handler(id, sender, asset);
176 
177  return true;
178  }
179 
180  public virtual bool[] AssetsExist(string[] ids)
181  {
182  UUID[] uuid = Array.ConvertAll(ids, id => UUID.Parse(id));
183  return m_Database.AssetsExist(uuid);
184  }
185 
186  public virtual string Store(AssetBase asset)
187  {
188  bool exists = m_Database.AssetsExist(new[] { asset.FullID })[0];
189  if (!exists)
190  {
191 // m_log.DebugFormat(
192 // "[XASSET SERVICE]: Storing asset {0} {1}, bytes {2}", asset.Name, asset.FullID, asset.Data.Length);
193  m_Database.StoreAsset(asset);
194  }
195 // else
196 // {
197 // m_log.DebugFormat(
198 // "[XASSET SERVICE]: Not storing asset {0} {1}, bytes {2} as it already exists", asset.Name, asset.FullID, asset.Data.Length);
199 // }
200 
201  return asset.ID;
202  }
203 
204  public bool UpdateContent(string id, byte[] data)
205  {
206  return false;
207  }
208 
209  public virtual bool Delete(string id)
210  {
211 // m_log.DebugFormat("[XASSET SERVICE]: Deleting asset {0}", id);
212 
213  UUID assetID;
214  if (!UUID.TryParse(id, out assetID))
215  return false;
216 
217  if (HasChainedAssetService)
218  m_ChainedAssetService.Delete(id);
219 
220  return m_Database.Delete(id);
221  }
222 
223  private void MigrateFromChainedService(AssetBase asset)
224  {
225  Store(asset);
226  m_ChainedAssetService.Delete(asset.ID);
227  }
228  }
229 }
virtual bool Get(string id, Object sender, AssetRetrieved handler)
Get an asset synchronously or asynchronously (depending on whether it is locally cached) and fire a c...
virtual AssetBase Get(string id)
Get an asset synchronously.
bool UpdateContent(string id, byte[] data)
Update an asset's content
virtual string Store(AssetBase asset)
Creates a new asset
XAssetService(IConfigSource config, string configName)
virtual AssetMetadata GetMetadata(string id)
Get an asset's metadata
virtual AssetBase GetCached(string id)
Synchronously fetches an asset from the local cache only.
Asset class. All Assets are reference by this class or a class derived from this class ...
Definition: AssetBase.cs:49
virtual bool Delete(string id)
Delete an asset
A de-duplicating asset service.
delegate void AssetRetrieved(string id, Object sender, AssetBase asset)
virtual bool[] AssetsExist(string[] ids)
Check if assets exist in the database.
virtual byte[] GetData(string id)
Get an asset's data, ignoring the metadata.