NHibernateGridData.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 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 System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using log4net;
  32. using NHibernate;
  33. using NHibernate.Criterion;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. namespace OpenSim.Data.NHibernate
  37. {
  38. /// <summary>
  39. /// A GridData Interface to the NHibernate database
  40. /// </summary>
  41. public class NHibernateGridData : GridDataBase
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. private NHibernateManager manager;
  45. public NHibernateManager Manager
  46. {
  47. get
  48. {
  49. return manager;
  50. }
  51. }
  52. public override void Initialise()
  53. {
  54. m_log.Info("[NHibernateGridData]: " + Name + " cannot be default-initialized!");
  55. throw new PluginNotInitialisedException(Name);
  56. }
  57. public override void Initialise(string connect)
  58. {
  59. m_log.InfoFormat("[NHIBERNATE] Initializing NHibernateGridData");
  60. manager = new NHibernateManager(connect, "GridStore");
  61. }
  62. /***********************************************************************
  63. *
  64. * Public Interface Functions
  65. *
  66. **********************************************************************/
  67. public override void Dispose() { }
  68. /// <summary>
  69. /// The plugin being loaded
  70. /// </summary>
  71. /// <returns>A string containing the plugin name</returns>
  72. public override string Name
  73. {
  74. get { return "NHibernate Grid Data Interface"; }
  75. }
  76. /// <summary>
  77. /// The plugins version
  78. /// </summary>
  79. /// <returns>A string containing the plugin version</returns>
  80. public override string Version
  81. {
  82. get
  83. {
  84. Module module = GetType().Module;
  85. Version dllVersion = module.Assembly.GetName().Version;
  86. return string.Format("{0}.{1}.{2}.{3}",
  87. dllVersion.Major, dllVersion.Minor, dllVersion.Build, dllVersion.Revision);
  88. }
  89. }
  90. public override bool AuthenticateSim(UUID UUID, ulong regionHandle, string simrecvkey)
  91. {
  92. bool throwHissyFit = false; // Should be true by 1.0
  93. if (throwHissyFit)
  94. throw new Exception("CRYPTOWEAK AUTHENTICATE: Refusing to authenticate due to replay potential.");
  95. RegionProfileData data = GetProfileByUUID(UUID);
  96. return (regionHandle == data.regionHandle && simrecvkey == data.regionSecret);
  97. }
  98. public override ReservationData GetReservationAtPoint(uint x, uint y)
  99. {
  100. throw new NotImplementedException();
  101. }
  102. public override DataResponse AddProfile(RegionProfileData profile)
  103. {
  104. if (manager.Get(typeof(RegionProfileData), profile.Uuid) == null)
  105. {
  106. manager.Insert(profile);
  107. return DataResponse.RESPONSE_OK;
  108. }
  109. else
  110. {
  111. return DataResponse.RESPONSE_ERROR;
  112. }
  113. }
  114. public override DataResponse UpdateProfile(RegionProfileData profile)
  115. {
  116. if (manager.Get(typeof(RegionProfileData), profile.Uuid) != null)
  117. {
  118. manager.Update(profile);
  119. return DataResponse.RESPONSE_OK;
  120. }
  121. else
  122. {
  123. return DataResponse.RESPONSE_ERROR;
  124. }
  125. }
  126. public override DataResponse DeleteProfile(string uuid)
  127. {
  128. RegionProfileData regionProfileData = (RegionProfileData)manager.Get(typeof(RegionProfileData), new UUID(uuid));
  129. if (regionProfileData != null)
  130. {
  131. manager.Delete(regionProfileData);
  132. return DataResponse.RESPONSE_OK;
  133. }
  134. return DataResponse.RESPONSE_ERROR;
  135. }
  136. public override RegionProfileData GetProfileByUUID(UUID UUID)
  137. {
  138. return (RegionProfileData)manager.Get(typeof(RegionProfileData), UUID);
  139. }
  140. public override RegionProfileData GetProfileByHandle(ulong regionHandle)
  141. {
  142. using (ISession session = manager.GetSession())
  143. {
  144. ICriteria criteria = session.CreateCriteria(typeof(RegionProfileData));
  145. criteria.Add(Expression.Eq("RegionHandle", regionHandle));
  146. IList regions = criteria.List();
  147. if (regions.Count == 1)
  148. {
  149. return (RegionProfileData)regions[0];
  150. }
  151. else
  152. {
  153. return null;
  154. }
  155. }
  156. }
  157. public override RegionProfileData GetProfileByString(string regionName)
  158. {
  159. using (ISession session = manager.GetSession())
  160. {
  161. ICriteria criteria = session.CreateCriteria(typeof(RegionProfileData));
  162. criteria.Add(Expression.Eq("RegionName", regionName));
  163. IList regions = criteria.List();
  164. if (regions.Count == 1)
  165. {
  166. return (RegionProfileData)regions[0];
  167. }
  168. else
  169. {
  170. return null;
  171. }
  172. }
  173. }
  174. public override RegionProfileData[] GetProfilesInRange(uint Xmin, uint Ymin, uint Xmax, uint Ymax)
  175. {
  176. using (ISession session = manager.GetSession())
  177. {
  178. ICriteria criteria = session.CreateCriteria(typeof(RegionProfileData));
  179. criteria.Add(Expression.Ge("RegionLocX", Xmin));
  180. criteria.Add(Expression.Ge("RegionLocY", Ymin));
  181. criteria.Add(Expression.Le("RegionLocX", Xmax));
  182. criteria.Add(Expression.Le("RegionLocY", Ymax));
  183. IList regions = criteria.List();
  184. RegionProfileData[] regionArray = new RegionProfileData[regions.Count];
  185. for (int i=0;i<regionArray.Length;i++)
  186. {
  187. regionArray[i] = (RegionProfileData)regions[i];
  188. }
  189. return regionArray;
  190. }
  191. }
  192. public override List<RegionProfileData> GetRegionsByName(string namePrefix, uint maxNum)
  193. {
  194. using (ISession session = manager.GetSession())
  195. {
  196. ICriteria criteria = session.CreateCriteria(typeof(RegionProfileData));
  197. criteria.SetMaxResults((int)maxNum);
  198. criteria.Add(Expression.Like("RegionName", namePrefix, MatchMode.Start));
  199. IList regions = criteria.List();
  200. List<RegionProfileData> regionList = new List<RegionProfileData>();
  201. foreach (RegionProfileData regionProfileData in regions)
  202. {
  203. regionList.Add(regionProfileData);
  204. }
  205. return regionList;
  206. }
  207. }
  208. }
  209. }