1 9 package org.jboss.portal.core.hibernate; 10 11 import org.jboss.portal.server.util.Service; 12 import org.jboss.portal.common.util.Tools; 13 import org.hibernate.cfg.Configuration; 14 import org.hibernate.cfg.Environment; 15 import org.hibernate.SessionFactory; 16 import org.hibernate.Session; 17 import org.hibernate.Query; 18 import org.hibernate.exception.SQLGrammarException; 19 import org.hibernate.metadata.ClassMetadata; 20 import org.hibernate.tool.hbm2ddl.SchemaExport; 21 22 import java.net.URL ; 23 import java.util.Iterator ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.InputStream ; 26 import java.io.IOException ; 27 28 import bsh.Interpreter; 29 import bsh.EvalError; 30 31 40 public class SessionFactoryBinder extends Service 41 { 42 43 44 private String configLocation; 45 46 47 private String setupLocation; 48 49 50 private boolean doChecking; 51 52 53 private SessionFactory sessionFactory; 54 55 56 private URL configURL; 57 58 59 private URL setupURL; 60 61 62 private Configuration config; 63 64 67 public boolean getDoChecking() 68 { 69 return doChecking; 70 } 71 72 75 public void setDoChecking(boolean doChecking) 76 { 77 this.doChecking = doChecking; 78 } 79 80 83 public String getConfigLocation() 84 { 85 return configLocation; 86 } 87 88 91 public void setConfigLocation(String configLocation) 92 { 93 this.configLocation = configLocation; 94 } 95 96 99 public String getSetupLocation() 100 { 101 return setupLocation; 102 } 103 104 107 public void setSetupLocation(String setupLocation) 108 { 109 this.setupLocation = setupLocation; 110 } 111 112 protected void startService() throws Exception 113 { 114 configURL = Thread.currentThread().getContextClassLoader().getResource(configLocation); 116 setupURL = Thread.currentThread().getContextClassLoader().getResource(setupLocation); 117 118 config = new Configuration(); 120 config.configure(configURL); 121 122 config.setProperty(Environment.TRANSACTION_MANAGER_STRATEGY, "org.hibernate.transaction.JBossTransactionManagerLookup"); 124 config.setProperty(Environment.CACHE_PROVIDER, "org.hibernate.cache.HashtableCacheProvider"); 125 126 createSessionFactory(); 128 129 if (doChecking && !doCheck()) 131 { 132 createSchema(); 134 135 createContent(); 137 } 138 } 139 140 protected void stopService() throws Exception 141 { 142 if (sessionFactory != null) 143 { 144 sessionFactory.close(); 145 sessionFactory = null; 146 } 147 else 148 { 149 log.debug("No session factory to close"); 150 } 151 } 152 153 158 public boolean doCheck() 159 { 160 Session session = null; 161 try 162 { 163 session = sessionFactory.openSession(); 164 for (Iterator i = sessionFactory.getAllClassMetadata().values().iterator();i.hasNext();) 165 { 166 ClassMetadata cmd = (ClassMetadata)i.next(); 167 Query query = session.createQuery("from " + cmd.getEntityName()); 168 query.setFirstResult(0); 169 query.setMaxResults(0); 170 try 171 { 172 query.list(); 173 } 174 catch (SQLGrammarException e) 175 { 176 log.debug("Got sql grammar exception from hibernate, we consider the schema does not exists", e); 178 return false; 179 } 180 } 181 } 182 finally 183 { 184 Tools.safeClose(session); 185 } 186 187 log.debug("The schema was checked as valid"); 189 return true; 190 } 191 192 197 public void createSchema() 198 { 199 log.debug("Creating database schema"); 200 SchemaExport export = new SchemaExport(config); 201 export.create(false, true); 202 } 203 204 209 public void destroySchema() 210 { 211 log.debug("Destroying database schema"); 212 SchemaExport export = new SchemaExport(config); 213 export.drop(false, true); 214 } 215 216 221 public void createContent() 222 { 223 InputStream in = null; 224 try 225 { 226 log.info("Creating database content"); 227 228 ByteArrayOutputStream out = new ByteArrayOutputStream (); 230 in = setupURL.openStream(); 231 Tools.copy(in, out); 232 String script = out.toString("UTF-8"); 233 234 Interpreter interpreter = new Interpreter(); 236 interpreter.setClassLoader(Thread.currentThread().getContextClassLoader()); 237 interpreter.setOut(System.out); 238 interpreter.set("SessionFactory", sessionFactory); 239 interpreter.eval(script); 240 } 241 catch (EvalError e) 242 { 243 log.error("Error in the bsh script", e); 244 } 245 catch (IOException e) 246 { 247 log.error("Cannot load setup script", e); 248 } 249 finally 250 { 251 Tools.safeClose(in); 252 } 253 } 254 255 258 protected void createSessionFactory() 259 { 260 sessionFactory = config.buildSessionFactory(); 262 263 String dialect = config.getProperty(Environment.DIALECT); 265 log.info("Hibernate dialect used " + dialect); 266 } 267 } 268 | Popular Tags |