1 21 package oracle.toplink.essentials.ejb.cmp3; 23 24 import java.io.File ; 25 import java.util.Map ; 26 import java.util.HashMap ; 27 import javax.persistence.*; 28 import javax.persistence.spi.*; 29 30 import oracle.toplink.essentials.config.TargetDatabase; 31 import oracle.toplink.essentials.config.TopLinkProperties; 32 import oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerFactoryImpl; 33 import oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializer; 34 import oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerSetupImpl; 35 import oracle.toplink.essentials.internal.sessions.AbstractSession; 36 import oracle.toplink.essentials.logging.SessionLog; 37 import oracle.toplink.essentials.threetier.ServerSession; 38 39 import oracle.toplink.essentials.tools.schemaframework.SchemaManager; 40 import oracle.toplink.essentials.ejb.cmp3.persistence.SEPersistenceUnitInfo; 41 42 47 48 public class EntityManagerFactoryProvider implements javax.persistence.spi.PersistenceProvider { 49 50 53 public static final String TOPLINK_ORM_THROW_EXCEPTIONS = "toplink.orm.throw.exceptions"; 54 public static final String TOPLINK_VALIDATION_ONLY_PROPERTY = "toplink.validation-only"; 55 56 public static final String DDL_GENERATION = "toplink.ddl-generation"; 57 58 public static final String CREATE_ONLY = "create-tables"; 59 public static final String DROP_AND_CREATE = "drop-and-create-tables"; 60 public static final String NONE = "none"; 61 62 public static final String APP_LOCATION = "toplink.application-location"; 63 64 public static final String CREATE_JDBC_DDL_FILE = "toplink.create-ddl-jdbc-file-name"; 65 public static final String DROP_JDBC_DDL_FILE = "toplink.drop-ddl-jdbc-file-name"; 66 67 public static final String DEFAULT_APP_LOCATION = "." + File.separator; 68 public static final String DEFAULT_CREATE_JDBC_FILE_NAME = "createDDL.jdbc"; 69 public static final String DEFAULT_DROP_JDBC_FILE_NAME = "dropDDL.jdbc"; 70 public static final String JAVASE_DB_INTERACTION = "INTERACT_WITH_DB"; 71 72 public static final String DDL_GENERATION_MODE = "toplink.ddl-generation.output-mode"; 73 public static final String DDL_SQL_SCRIPT_GENERATION = "sql-script"; 74 public static final String DDL_DATABASE_GENERATION = "database"; 75 public static final String DDL_BOTH_GENERATION = "both"; 76 public static final String DEFAULT_DDL_GENERATION_MODE = DDL_SQL_SCRIPT_GENERATION; 78 79 protected static final String oldPropertyNames[][] = { 83 {TopLinkProperties.JDBC_WRITE_CONNECTIONS_MAX, "toplink.max-write-connections"}, 84 {TopLinkProperties.JDBC_WRITE_CONNECTIONS_MIN, "toplink.min-write-connections"}, 85 {TopLinkProperties.JDBC_READ_CONNECTIONS_MAX, "toplink.max-read-connections"}, 86 {TopLinkProperties.JDBC_READ_CONNECTIONS_MIN, "toplink.min-read-connections"}, 87 {TopLinkProperties.JDBC_BIND_PARAMETERS, "toplink.bind-all-parameters"}, 88 {TopLinkProperties.TARGET_DATABASE, "toplink.platform.class.name"}, 89 {TopLinkProperties.TARGET_SERVER, "toplink.server.platform.class.name"}, 90 {TopLinkProperties.CACHE_SIZE_DEFAULT, "toplink.cache.default-size"} 91 }; 92 93 96 public EntityManagerFactoryProvider() { 97 } 98 99 112 public EntityManagerFactory createEntityManagerFactory(String emName, Map properties){ 113 Map nonNullProperties = (properties == null) ? new HashMap () : properties; 114 String name = emName; 115 if (name == null){ 116 name = ""; 117 } 118 JavaSECMPInitializer initializer = JavaSECMPInitializer.getJavaSECMPInitializer(nonNullProperties); 119 EntityManagerSetupImpl emSetupImpl = initializer.getEntityManagerSetupImpl(name); 120 if(emSetupImpl.isUndeployed()) { 121 ((SEPersistenceUnitInfo)emSetupImpl.getPersistenceUnitInfo()).setClassLoader(JavaSECMPInitializer.getMainLoader()); 122 ((SEPersistenceUnitInfo)emSetupImpl.getPersistenceUnitInfo()).setNewTempClassLoader(JavaSECMPInitializer.getMainLoader()); 123 emSetupImpl.predeploy(null, nonNullProperties); 125 } 126 127 EntityManagerFactoryImpl factory = new EntityManagerFactoryImpl(emSetupImpl, nonNullProperties); 128 129 if (emSetupImpl.shouldGetSessionOnCreateFactory(nonNullProperties)) { 131 factory.getServerSession(); 132 } 133 return factory; 134 } 135 136 146 public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties){ 147 Map nonNullProperties = (properties == null) ? new HashMap () : properties; 148 EntityManagerSetupImpl emSetupImpl = new EntityManagerSetupImpl(); 149 emSetupImpl.setIsInContainerMode(true); 150 ClassTransformer transformer = null; 151 if(!emSetupImpl.isDeployed()) { 152 transformer = emSetupImpl.predeploy(info, nonNullProperties); 153 } 154 if (transformer != null){ 155 info.addTransformer(transformer); 156 } 157 EntityManagerFactoryImpl factory = new EntityManagerFactoryImpl(emSetupImpl, nonNullProperties); 160 161 if (emSetupImpl.shouldGetSessionOnCreateFactory(nonNullProperties)) { 163 factory.getServerSession(); 164 } 165 return factory; 166 } 167 168 169 175 public static void login(ServerSession session, Map properties) { 176 String toplinkPlatform = (String )properties.get(TopLinkProperties.TARGET_DATABASE); 177 if (!session.isConnected()) { 178 if (toplinkPlatform == null || toplinkPlatform.equals(TargetDatabase.Auto)) { 179 session.loginAndDetectDatasource(); 181 } else { 182 session.login(); 183 } 184 } 185 } 186 187 public static void generateDDLFiles(ServerSession session, Map props, 188 boolean inSEmode) { 189 boolean createTables = false, shouldDropFirst = false; 190 String appLocation; 191 String createDDLJdbc; 192 String dropDDLJdbc; 193 String ddlGeneration = NONE; 194 195 if(null == props){ 196 return; 197 } 198 199 ddlGeneration = (String )getConfigPropertyAsString(DDL_GENERATION, props, NONE); 200 ddlGeneration = ddlGeneration.toLowerCase(); 201 if(ddlGeneration.equals(NONE)) { 202 return; 203 } 204 205 if(ddlGeneration.equals(CREATE_ONLY) || 206 ddlGeneration.equals(DROP_AND_CREATE)) { 207 createTables = true; 208 if(ddlGeneration.equals(DROP_AND_CREATE)) { 209 shouldDropFirst = true; 210 } 211 } 212 213 if (createTables) { 214 String ddlGenerationMode = (String ) getConfigPropertyAsString(DDL_GENERATION_MODE, props, DEFAULT_DDL_GENERATION_MODE); 215 if (ddlGenerationMode.equals(NONE)) { 217 return; 218 } 219 220 appLocation = (String )getConfigPropertyAsString( APP_LOCATION, props, DEFAULT_APP_LOCATION); 221 createDDLJdbc = (String )getConfigPropertyAsString( CREATE_JDBC_DDL_FILE, props, DEFAULT_CREATE_JDBC_FILE_NAME); 222 dropDDLJdbc = (String )getConfigPropertyAsString( DROP_JDBC_DDL_FILE, props, DEFAULT_DROP_JDBC_FILE_NAME); 223 224 SchemaManager mgr = new SchemaManager(session); 225 226 if (ddlGenerationMode.equals(DDL_DATABASE_GENERATION) || inSEmode) { 229 runInSEMode(mgr, shouldDropFirst); 230 231 if (inSEmode) { 232 writeDDLsToFiles(mgr, appLocation, createDDLJdbc, dropDDLJdbc); 233 } 234 } else if (ddlGenerationMode.equals(DDL_SQL_SCRIPT_GENERATION)) { 235 writeDDLsToFiles(mgr, appLocation, createDDLJdbc, dropDDLJdbc); 236 } else if (ddlGenerationMode.equals(DDL_BOTH_GENERATION)) { 237 runInSEMode(mgr, shouldDropFirst); 238 writeDDLsToFiles(mgr, appLocation, createDDLJdbc, dropDDLJdbc); 239 } 240 } 241 } 242 243 public static void runInSEMode(SchemaManager mgr, boolean shouldDropFirst) { 244 String str = getConfigPropertyAsString(JAVASE_DB_INTERACTION, null ,"true"); 245 boolean interactWithDB = Boolean.valueOf(str.toLowerCase()).booleanValue(); 246 if (!interactWithDB){ 247 return; 248 } 249 createOrReplaceDefaultTables(mgr, shouldDropFirst); 250 } 251 252 260 public static String getConfigPropertyAsString(String propertyKey, Map overrides, String defaultValue){ 261 String value = getConfigPropertyAsString(propertyKey, overrides); 262 if (value == null){ 263 value = defaultValue; 264 } 265 return value; 266 } 267 268 public static String getConfigPropertyAsString(String propertyKey, Map overrides){ 269 String value = null; 270 if (overrides != null){ 271 value = (String )overrides.get(propertyKey); 272 } 273 if (value == null){ 274 value = System.getProperty(propertyKey); 275 } 276 277 return value; 278 } 279 280 public static String getConfigPropertyAsStringLogDebug(String propertyKey, Map overrides, String defaultValue, AbstractSession session){ 281 String value = getConfigPropertyAsStringLogDebug(propertyKey, overrides, session); 282 if (value == null){ 283 value = defaultValue; 284 session.log(SessionLog.FINEST, SessionLog.PROPERTIES, "property_value_default", new Object []{propertyKey, value}); 285 } 286 return value; 287 } 288 289 public static String getConfigPropertyAsStringLogDebug(String propertyKey, Map overrides, AbstractSession session){ 290 String value = null; 291 if (overrides != null){ 292 value = (String )overrides.get(propertyKey); 293 } 294 if (value == null){ 295 value = System.getProperty(propertyKey); 296 } 297 if(value != null && session != null) { 298 session.log(SessionLog.FINEST, SessionLog.PROPERTIES, "property_value_specified", new Object []{propertyKey, value}); 299 } 300 301 return value; 302 } 303 304 public static void createOrReplaceDefaultTables( 305 SchemaManager mgr, boolean shouldDropFirst) { 306 if (shouldDropFirst){ 307 mgr.replaceDefaultTables(true); 308 } else { 309 mgr.createDefaultTables(); 310 } 311 } 312 313 public static void writeDDLsToFiles(SchemaManager mgr, String appLocation, 314 String createDDLJdbc, String dropDDLJdbc) { 315 appLocation = addFileSeperator(appLocation); 317 if (null != createDDLJdbc) { 318 String createJdbcFileName = appLocation + createDDLJdbc; 319 mgr.outputCreateDDLToFile(createJdbcFileName); 320 } 321 322 if (null != dropDDLJdbc) { 323 String dropJdbcFileName = appLocation + dropDDLJdbc; 324 mgr.outputDropDDLToFile(dropJdbcFileName); 325 } 326 327 mgr.setCreateSQLFiles(false); 328 createOrReplaceDefaultTables(mgr, true); 331 mgr.closeDDLWriter(); 332 } 333 334 public static String addFileSeperator(String appLocation) { 335 int strLength = appLocation.length(); 336 if (appLocation.substring(strLength -1, strLength).equals(File.separator)) { 337 return appLocation; 338 } else { 339 return appLocation + File.separator; 340 } 341 } 342 343 350 public static Map mergeMaps(Map target, Map source){ 351 Map map = new HashMap (); 352 if (source != null){ 353 map.putAll(source); 354 } 355 356 if (target != null){ 357 map.putAll(target); 358 } 359 return map; 360 } 361 362 368 public static void translateOldProperties(Map m, AbstractSession session) { 369 for(int i=0; i < oldPropertyNames.length; i++) { 370 Object value = getConfigPropertyAsString(oldPropertyNames[i][1], m); 371 if(value != null) { 372 session.log(SessionLog.CONFIG, SessionLog.TRANSACTION, "deprecated_property", oldPropertyNames[i]); 373 m.put(oldPropertyNames[i][0], value); 374 } 375 } 376 } 377 } 378 | Popular Tags |