1 4 package org.roller.business.hibernate; 5 6 import net.sf.hibernate.HibernateException; 7 import net.sf.hibernate.SessionFactory; 8 import net.sf.hibernate.cfg.Configuration; 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.roller.RollerException; 12 import org.roller.business.PersistenceStrategy; 13 import org.roller.business.utils.UpgradeDatabase; 14 import org.roller.model.BookmarkManager; 15 import org.roller.model.ConfigManager; 16 import org.roller.model.AutoPingManager; 17 import org.roller.model.PingQueueManager; 18 import org.roller.model.PingTargetManager; 19 import org.roller.model.PlanetManager; 20 import org.roller.model.PropertiesManager; 21 import org.roller.model.RefererManager; 22 import org.roller.model.Roller; 23 import org.roller.model.UserManager; 24 import org.roller.model.WeblogManager; 25 import org.roller.pojos.UserData; 26 27 import java.sql.Connection ; 28 29 35 public class HibernateRollerImpl extends org.roller.business.RollerImpl 36 { 37 static final long serialVersionUID = 5256135928578074652L; 38 39 private static Log mLogger = 40 LogFactory.getFactory().getInstance(HibernateRollerImpl.class); 41 42 protected BookmarkManager mBookmarkManager; 43 protected ConfigManager mConfigManager = null; 44 protected PropertiesManager mPropsManager = null; 45 protected PlanetManager planetManager = null; 46 protected RefererManager mRefererManager; 47 protected UserManager mUserManager; 48 protected WeblogManager mWeblogManager; 49 protected PingQueueManager mPingQueueManager; 50 protected AutoPingManager mAutoPingManager; 51 protected PingTargetManager mPingTargetManager; 52 protected static HibernateRollerImpl me; 53 protected PersistenceStrategy mStrategy = null; 54 protected static SessionFactory mSessionFactory; 55 56 57 protected HibernateRollerImpl() throws RollerException 58 { 59 mLogger.debug("Initializing sessionFactory for Hibernate"); 60 61 try 62 { 63 Configuration config = new Configuration(); 64 config.configure("/hibernate.cfg.xml"); 65 mSessionFactory = config.buildSessionFactory(); 66 } 67 catch (HibernateException e) 68 { 69 mLogger.error("Error Setting up SessionFactory",e); 70 throw new RollerException(e); 71 } 72 73 mStrategy = new HibernateStrategy(mSessionFactory); 74 } 75 76 77 81 public static Roller instantiate() throws RollerException 82 { 83 if (me == null) 84 { 85 mLogger.debug("Instantiating HibernateRollerImpl"); 86 me = new HibernateRollerImpl(); 87 } 88 89 return me; 90 } 91 92 93 public void begin() throws RollerException 94 { 95 mStrategy.begin(UserData.ANONYMOUS_USER); 96 } 97 98 public void begin(UserData user) throws RollerException 99 { 100 mStrategy.begin(user); 101 } 102 103 public UserData getUser() throws RollerException 104 { 105 return mStrategy.getUser(); 106 } 107 108 public void setUser(UserData user) throws RollerException 109 { 110 mStrategy.setUser(user); 111 } 112 113 public void commit() throws RollerException 114 { 115 mStrategy.commit(); 116 } 117 118 public void rollback() 119 { 120 try 121 { 122 mStrategy.rollback(); 123 } 124 catch (Exception e) 125 { 126 mLogger.error(e); 127 } 128 } 129 130 133 public UserManager getUserManager() throws RollerException 134 { 135 if ( mUserManager == null ) 136 { 137 mUserManager = new HibernateUserManagerImpl(mStrategy); 138 } 139 return mUserManager; 140 } 141 142 145 public BookmarkManager getBookmarkManager() throws RollerException 146 { 147 if ( mBookmarkManager == null ) 148 { 149 mBookmarkManager = new HibernateBookmarkManagerImpl(mStrategy); 150 } 151 return mBookmarkManager; 152 } 153 154 157 public WeblogManager getWeblogManager() throws RollerException 158 { 159 if ( mWeblogManager == null ) 160 { 161 mWeblogManager = new HibernateWeblogManagerImpl(mStrategy); 162 } 163 return mWeblogManager; 164 } 165 166 169 public RefererManager getRefererManager() throws RollerException 170 { 171 if ( mRefererManager == null ) 172 { 173 mRefererManager = new HibernateRefererManagerImpl(mStrategy); 174 } 175 return mRefererManager; 176 } 177 178 181 public ConfigManager getConfigManager() throws RollerException 182 { 183 if (mConfigManager == null) 184 { 185 mConfigManager = new HibernateConfigManagerImpl(mStrategy); 186 } 187 return mConfigManager; 188 } 189 190 193 public PropertiesManager getPropertiesManager() throws RollerException 194 { 195 if (mPropsManager == null) 196 { 197 mPropsManager = new HibernatePropertiesManagerImpl(mStrategy); 198 } 199 return mPropsManager; 200 } 201 202 205 public PingQueueManager getPingQueueManager() throws RollerException 206 { 207 if (mPingQueueManager == null) 208 { 209 mPingQueueManager = new HibernatePingQueueManagerImpl(mStrategy); 210 } 211 return mPingQueueManager; 212 } 213 214 217 public PlanetManager getPlanetManager() throws RollerException 218 { 219 if ( planetManager == null ) 220 { 221 planetManager = new HibernatePlanetManagerImpl(mStrategy,this); 222 } 223 return planetManager; 224 } 225 226 227 230 public AutoPingManager getAutopingManager() throws RollerException 231 { 232 if (mAutoPingManager == null) 233 { 234 mAutoPingManager = new HibernateAutoPingManagerImpl(mStrategy); 235 } 236 return mAutoPingManager; 237 } 238 239 240 243 public PingTargetManager getPingTargetManager() throws RollerException 244 { 245 if (mPingTargetManager == null) 246 { 247 mPingTargetManager = new HibernatePingTargetManagerImpl(mStrategy); 248 } 249 return mPingTargetManager; 250 } 251 252 253 256 public PersistenceStrategy getPersistenceStrategy() 257 { 258 return mStrategy; 259 } 260 261 264 public void upgradeDatabase(Connection con) throws RollerException 265 { 266 UpgradeDatabase.upgradeDatabase(con); 267 } 268 269 public void release() 270 { 271 super.release(); 272 273 if (mBookmarkManager != null) mBookmarkManager.release(); 274 if (mConfigManager != null) mConfigManager.release(); 275 if (mRefererManager != null) mRefererManager.release(); 276 if (mUserManager != null) mUserManager.release(); 277 if (mWeblogManager != null) mWeblogManager.release(); 278 if (mPingTargetManager != null) mPingTargetManager.release(); 279 if (mPingQueueManager != null) mPingQueueManager.release(); 280 if (mAutoPingManager != null) mAutoPingManager.release(); 281 282 try 283 { 284 if (mStrategy != null) mStrategy.release(); 285 } 286 catch (Exception e) 287 { 288 mLogger.error( 289 "Exception with mSupport.release() [" + e + "]", e); 290 } 291 } 292 293 public void shutdown() 294 { 295 super.shutdown(); 296 297 try 298 { 299 release(); 300 mSessionFactory.close(); 301 } 302 catch (HibernateException e) 303 { 304 mLogger.error("Unable to close SessionFactory", e); 305 } 306 } 307 } 308 | Popular Tags |