NHibernateGridData.cs 8.5 KB

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