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