JsonStoreModule.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * Copyright (c) Contributors
  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 OpenSim 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. using Mono.Addins;
  28. using System;
  29. using System.Reflection;
  30. using System.Threading;
  31. using System.Text;
  32. using System.Net;
  33. using System.Net.Sockets;
  34. using log4net;
  35. using Nini.Config;
  36. using OpenMetaverse;
  37. using OpenMetaverse.StructuredData;
  38. using OpenSim.Framework;
  39. using OpenSim.Region.Framework.Interfaces;
  40. using OpenSim.Region.Framework.Scenes;
  41. using System.Collections.Generic;
  42. using System.Text.RegularExpressions;
  43. namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
  44. {
  45. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "JsonStoreModule")]
  46. public class JsonStoreModule : INonSharedRegionModule, IJsonStoreModule
  47. {
  48. private static readonly ILog m_log =
  49. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. private IConfig m_config = null;
  51. private bool m_enabled = false;
  52. private Scene m_scene = null;
  53. private Dictionary<UUID,JsonStore> m_JsonValueStore;
  54. private UUID m_sharedStore;
  55. #region IRegionModule Members
  56. // -----------------------------------------------------------------
  57. /// <summary>
  58. /// Name of this shared module is it's class name
  59. /// </summary>
  60. // -----------------------------------------------------------------
  61. public string Name
  62. {
  63. get { return this.GetType().Name; }
  64. }
  65. // -----------------------------------------------------------------
  66. /// <summary>
  67. /// Initialise this shared module
  68. /// </summary>
  69. /// <param name="scene">this region is getting initialised</param>
  70. /// <param name="source">nini config, we are not using this</param>
  71. // -----------------------------------------------------------------
  72. public void Initialise(IConfigSource config)
  73. {
  74. try
  75. {
  76. if ((m_config = config.Configs["JsonStore"]) == null)
  77. {
  78. // There is no configuration, the module is disabled
  79. // m_log.InfoFormat("[JsonStore] no configuration info");
  80. return;
  81. }
  82. m_enabled = m_config.GetBoolean("Enabled", m_enabled);
  83. }
  84. catch (Exception e)
  85. {
  86. m_log.ErrorFormat("[JsonStore] initialization error: {0}",e.Message);
  87. return;
  88. }
  89. if (m_enabled)
  90. m_log.DebugFormat("[JsonStore] module is enabled");
  91. }
  92. // -----------------------------------------------------------------
  93. /// <summary>
  94. /// everything is loaded, perform post load configuration
  95. /// </summary>
  96. // -----------------------------------------------------------------
  97. public void PostInitialise()
  98. {
  99. }
  100. // -----------------------------------------------------------------
  101. /// <summary>
  102. /// Nothing to do on close
  103. /// </summary>
  104. // -----------------------------------------------------------------
  105. public void Close()
  106. {
  107. }
  108. // -----------------------------------------------------------------
  109. /// <summary>
  110. /// </summary>
  111. // -----------------------------------------------------------------
  112. public void AddRegion(Scene scene)
  113. {
  114. if (m_enabled)
  115. {
  116. m_scene = scene;
  117. m_scene.RegisterModuleInterface<IJsonStoreModule>(this);
  118. m_sharedStore = UUID.Zero;
  119. m_JsonValueStore = new Dictionary<UUID,JsonStore>();
  120. m_JsonValueStore.Add(m_sharedStore,new JsonStore(""));
  121. }
  122. }
  123. // -----------------------------------------------------------------
  124. /// <summary>
  125. /// </summary>
  126. // -----------------------------------------------------------------
  127. public void RemoveRegion(Scene scene)
  128. {
  129. // need to remove all references to the scene in the subscription
  130. // list to enable full garbage collection of the scene object
  131. }
  132. // -----------------------------------------------------------------
  133. /// <summary>
  134. /// Called when all modules have been added for a region. This is
  135. /// where we hook up events
  136. /// </summary>
  137. // -----------------------------------------------------------------
  138. public void RegionLoaded(Scene scene)
  139. {
  140. if (m_enabled) {}
  141. }
  142. /// -----------------------------------------------------------------
  143. /// <summary>
  144. /// </summary>
  145. // -----------------------------------------------------------------
  146. public Type ReplaceableInterface
  147. {
  148. get { return null; }
  149. }
  150. #endregion
  151. #region ScriptInvocationInteface
  152. // -----------------------------------------------------------------
  153. /// <summary>
  154. ///
  155. /// </summary>
  156. // -----------------------------------------------------------------
  157. public bool CreateStore(string value, out UUID result)
  158. {
  159. result = UUID.Zero;
  160. if (! m_enabled) return false;
  161. UUID uuid = UUID.Random();
  162. JsonStore map = null;
  163. try
  164. {
  165. map = new JsonStore(value);
  166. }
  167. catch (Exception e)
  168. {
  169. m_log.InfoFormat("[JsonStore] Unable to initialize store from {0}; {1}",value,e.Message);
  170. return false;
  171. }
  172. lock (m_JsonValueStore)
  173. m_JsonValueStore.Add(uuid,map);
  174. result = uuid;
  175. return true;
  176. }
  177. // -----------------------------------------------------------------
  178. /// <summary>
  179. ///
  180. /// </summary>
  181. // -----------------------------------------------------------------
  182. public bool DestroyStore(UUID storeID)
  183. {
  184. if (! m_enabled) return false;
  185. lock (m_JsonValueStore)
  186. m_JsonValueStore.Remove(storeID);
  187. return true;
  188. }
  189. // -----------------------------------------------------------------
  190. /// <summary>
  191. ///
  192. /// </summary>
  193. // -----------------------------------------------------------------
  194. public bool TestPath(UUID storeID, string path, bool useJson)
  195. {
  196. if (! m_enabled) return false;
  197. JsonStore map = null;
  198. lock (m_JsonValueStore)
  199. {
  200. if (! m_JsonValueStore.TryGetValue(storeID,out map))
  201. {
  202. m_log.InfoFormat("[JsonStore] Missing store {0}",storeID);
  203. return true;
  204. }
  205. }
  206. try
  207. {
  208. lock (map)
  209. return map.TestPath(path,useJson);
  210. }
  211. catch (Exception e)
  212. {
  213. m_log.InfoFormat("[JsonStore] Path test failed for {0} in {1}; {2}",path,storeID,e.Message);
  214. }
  215. return false;
  216. }
  217. // -----------------------------------------------------------------
  218. /// <summary>
  219. ///
  220. /// </summary>
  221. // -----------------------------------------------------------------
  222. public bool SetValue(UUID storeID, string path, string value, bool useJson)
  223. {
  224. if (! m_enabled) return false;
  225. JsonStore map = null;
  226. lock (m_JsonValueStore)
  227. {
  228. if (! m_JsonValueStore.TryGetValue(storeID,out map))
  229. {
  230. m_log.InfoFormat("[JsonStore] Missing store {0}",storeID);
  231. return false;
  232. }
  233. }
  234. try
  235. {
  236. lock (map)
  237. if (map.SetValue(path,value,useJson))
  238. return true;
  239. }
  240. catch (Exception e)
  241. {
  242. m_log.InfoFormat("[JsonStore] Unable to assign {0} to {1} in {2}; {3}",value,path,storeID,e.Message);
  243. }
  244. return false;
  245. }
  246. // -----------------------------------------------------------------
  247. /// <summary>
  248. ///
  249. /// </summary>
  250. // -----------------------------------------------------------------
  251. public bool RemoveValue(UUID storeID, string path)
  252. {
  253. if (! m_enabled) return false;
  254. JsonStore map = null;
  255. lock (m_JsonValueStore)
  256. {
  257. if (! m_JsonValueStore.TryGetValue(storeID,out map))
  258. {
  259. m_log.InfoFormat("[JsonStore] Missing store {0}",storeID);
  260. return false;
  261. }
  262. }
  263. try
  264. {
  265. lock (map)
  266. if (map.RemoveValue(path))
  267. return true;
  268. }
  269. catch (Exception e)
  270. {
  271. m_log.InfoFormat("[JsonStore] Unable to remove {0} in {1}; {2}",path,storeID,e.Message);
  272. }
  273. return false;
  274. }
  275. // -----------------------------------------------------------------
  276. /// <summary>
  277. ///
  278. /// </summary>
  279. // -----------------------------------------------------------------
  280. public bool GetValue(UUID storeID, string path, bool useJson, out string value)
  281. {
  282. value = String.Empty;
  283. if (! m_enabled) return false;
  284. JsonStore map = null;
  285. lock (m_JsonValueStore)
  286. {
  287. if (! m_JsonValueStore.TryGetValue(storeID,out map))
  288. return false;
  289. }
  290. try
  291. {
  292. lock (map)
  293. {
  294. return map.GetValue(path, out value, useJson);
  295. }
  296. }
  297. catch (Exception e)
  298. {
  299. m_log.InfoFormat("[JsonStore] unable to retrieve value; {0}",e.Message);
  300. }
  301. return false;
  302. }
  303. // -----------------------------------------------------------------
  304. /// <summary>
  305. ///
  306. /// </summary>
  307. // -----------------------------------------------------------------
  308. public void TakeValue(UUID storeID, string path, bool useJson, TakeValueCallback cback)
  309. {
  310. if (! m_enabled)
  311. {
  312. cback(String.Empty);
  313. return;
  314. }
  315. JsonStore map = null;
  316. lock (m_JsonValueStore)
  317. {
  318. if (! m_JsonValueStore.TryGetValue(storeID,out map))
  319. {
  320. cback(String.Empty);
  321. return;
  322. }
  323. }
  324. try
  325. {
  326. lock (map)
  327. {
  328. map.TakeValue(path, useJson, cback);
  329. return;
  330. }
  331. }
  332. catch (Exception e)
  333. {
  334. m_log.InfoFormat("[JsonStore] unable to retrieve value; {0}",e.ToString());
  335. }
  336. cback(String.Empty);
  337. }
  338. // -----------------------------------------------------------------
  339. /// <summary>
  340. ///
  341. /// </summary>
  342. // -----------------------------------------------------------------
  343. public void ReadValue(UUID storeID, string path, bool useJson, TakeValueCallback cback)
  344. {
  345. if (! m_enabled)
  346. {
  347. cback(String.Empty);
  348. return;
  349. }
  350. JsonStore map = null;
  351. lock (m_JsonValueStore)
  352. {
  353. if (! m_JsonValueStore.TryGetValue(storeID,out map))
  354. {
  355. cback(String.Empty);
  356. return;
  357. }
  358. }
  359. try
  360. {
  361. lock (map)
  362. {
  363. map.ReadValue(path, useJson, cback);
  364. return;
  365. }
  366. }
  367. catch (Exception e)
  368. {
  369. m_log.InfoFormat("[JsonStore] unable to retrieve value; {0}",e.ToString());
  370. }
  371. cback(String.Empty);
  372. }
  373. #endregion
  374. }
  375. }