NHibernateGridData.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. 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 StoreProfile(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. manager.Update(profile);
  112. return DataResponse.RESPONSE_OK;
  113. }
  114. }
  115. public override DataResponse DeleteProfile(string uuid)
  116. {
  117. RegionProfileData regionProfileData = (RegionProfileData)manager.Get(typeof(RegionProfileData), new UUID(uuid));
  118. if (regionProfileData != null)
  119. {
  120. manager.Delete(regionProfileData);
  121. return DataResponse.RESPONSE_OK;
  122. }
  123. return DataResponse.RESPONSE_ERROR;
  124. }
  125. public override RegionProfileData GetProfileByUUID(UUID UUID)
  126. {
  127. return (RegionProfileData)manager.Get(typeof(RegionProfileData), UUID);
  128. }
  129. public override RegionProfileData GetProfileByHandle(ulong regionHandle)
  130. {
  131. using (ISession session = manager.GetSession())
  132. {
  133. ICriteria criteria = session.CreateCriteria(typeof(RegionProfileData));
  134. criteria.Add(Expression.Eq("RegionHandle", regionHandle));
  135. IList regions = criteria.List();
  136. if (regions.Count == 1)
  137. {
  138. return (RegionProfileData)regions[0];
  139. }
  140. else
  141. {
  142. return null;
  143. }
  144. }
  145. }
  146. public override RegionProfileData GetProfileByString(string regionName)
  147. {
  148. using (ISession session = manager.GetSession())
  149. {
  150. ICriteria criteria = session.CreateCriteria(typeof(RegionProfileData));
  151. criteria.Add(Expression.Eq("RegionName", regionName));
  152. IList regions = criteria.List();
  153. if (regions.Count == 1)
  154. {
  155. return (RegionProfileData)regions[0];
  156. }
  157. else
  158. {
  159. return null;
  160. }
  161. }
  162. }
  163. public override RegionProfileData[] GetProfilesInRange(uint Xmin, uint Ymin, uint Xmax, uint Ymax)
  164. {
  165. using (ISession session = manager.GetSession())
  166. {
  167. ICriteria criteria = session.CreateCriteria(typeof(RegionProfileData));
  168. criteria.Add(Expression.Ge("RegionLocX", Xmin));
  169. criteria.Add(Expression.Ge("RegionLocY", Ymin));
  170. criteria.Add(Expression.Le("RegionLocX", Xmax));
  171. criteria.Add(Expression.Le("RegionLocY", Ymax));
  172. IList regions = criteria.List();
  173. RegionProfileData[] regionArray = new RegionProfileData[regions.Count];
  174. for (int i=0;i<regionArray.Length;i++)
  175. {
  176. regionArray[i] = (RegionProfileData)regions[i];
  177. }
  178. return regionArray;
  179. }
  180. }
  181. public override List<RegionProfileData> GetRegionsByName(string namePrefix, uint maxNum)
  182. {
  183. using (ISession session = manager.GetSession())
  184. {
  185. ICriteria criteria = session.CreateCriteria(typeof(RegionProfileData));
  186. criteria.SetMaxResults((int)maxNum);
  187. criteria.Add(Expression.Like("RegionName", namePrefix, MatchMode.Start));
  188. IList regions = criteria.List();
  189. List<RegionProfileData> regionList = new List<RegionProfileData>();
  190. foreach (RegionProfileData regionProfileData in regions)
  191. {
  192. regionList.Add(regionProfileData);
  193. }
  194. return regionList;
  195. }
  196. }
  197. }
  198. }