private boolean isHSQL(String dbName) { Configuration dbConfig = new Configuration(dbName); String db = dbConfig.getProperty("db"); return ("mem".equals(db) || "fs".equals(db) || "org.hsqldb.jdbcDriver".equals(dbConfig.getProperty("db.driver"))); }
public String getProperty(String key, String defaultString) { if (key != null) { String newKey = generateKey(key); Object value = Play.configuration.get(newKey); if (value == null && this.isDefault()) { value = Play.configuration.get(key); } if (value != null) { return value.toString(); } } return defaultString; }
public static synchronized boolean applyScript(boolean runScript, String moduleKey, VirtualFile evolutionsDirectory) { // Look over all the DB Set<String> dBNames = Configuration.getDbNames(); for (String dbName : dBNames) { return applyScript(dbName, runScript, moduleKey, evolutionsDirectory); } return true; }
protected Properties properties(String dbName, Configuration dbConfig) { Properties properties = new Properties(); properties.putAll(dbConfig.getProperties()); properties.put("javax.persistence.transaction", "RESOURCE_LOCAL"); properties.put("javax.persistence.provider", "org.hibernate.ejb.HibernatePersistence"); properties.put("hibernate.dialect", getDefaultDialect(dbConfig, dbConfig.getProperty("db.driver"))); if (!dbConfig.getProperty("jpa.ddl", Play.mode.isDev() ? "update" : "none").equals("none")) { properties.setProperty("hibernate.hbm2ddl.auto", dbConfig.getProperty("jpa.ddl", "update")); } properties.put("hibernate.connection.datasource", DB.getDataSource(dbName)); return properties; }
/** * Reads the configuration file and initialises required JPA EntityManagerFactories. */ @Override public void onApplicationStart() { org.apache.log4j.Logger.getLogger("org.hibernate.SQL").setLevel(Level.OFF); Set<String> dBNames = Configuration.getDbNames(); for (String dbName : dBNames) { Configuration dbConfig = new Configuration(dbName); if (dbConfig.getProperty("jpa.debugSQL", "false").equals("true")) { org.apache.log4j.Logger.getLogger("org.hibernate.SQL").setLevel(Level.ALL); } Thread thread = Thread.currentThread(); ClassLoader contextClassLoader = thread.getContextClassLoader(); thread.setContextClassLoader(Play.classloader); try { if (Logger.isTraceEnabled()) { Logger.trace("Initializing JPA for %s...", dbName); } JPA.emfs.put(dbName, newEntityManagerFactory(dbName, dbConfig)); } finally { thread.setContextClassLoader(contextClassLoader); } } JPQL.instance = new JPQL(); }
private boolean changed() { Set<String> dbNames = Configuration.getDbNames(); Configuration dbConfig = new Configuration(dbName); if ("mem".equals(dbConfig.getProperty("db")) && dbConfig.getProperty("db.url") == null) { dbConfig.put("db.driver", "org.h2.Driver"); dbConfig.put("db.url", "jdbc:h2:mem:play;MODE=MYSQL"); dbConfig.put("db.user", "sa"); dbConfig.put("db.pass", ""); if ("fs".equals(dbConfig.getProperty("db")) && dbConfig.getProperty("db.url") == null) { dbConfig.put("db.driver", "org.h2.Driver"); dbConfig.put("db.url", "jdbc:h2:" + (new File(Play.applicationPath, "db/h2/play").getAbsolutePath()) + ";MODE=MYSQL"); dbConfig.put("db.user", "sa"); dbConfig.put("db.pass", ""); String datasourceName = dbConfig.getProperty("db", ""); DataSource ds = DB.getDataSource(dbName); if ((datasourceName.startsWith("java:") || datasourceName.startsWith("jndi:")) && dbConfig.getProperty("db.url") == null) { if (ds == null) { return true; dbConfig.put("db.destroyMethod", "close"); Matcher m = new jregex.Pattern("^mysql:(//)?(({user}[a-zA-Z0-9_]+)(:({pwd}[^@]+))?@)?(({host}[^/]+)/)?({name}[a-zA-Z0-9_]+)(\\?)?({parameters}[^\\s]+)?$").matcher(dbConfig.getProperty("db", "")); if (m.matches()) { String user = m.group("user");
public String getProperty(String key) { return this.getProperty(key, null); }
} else if (this.isDefault()) { boolean isDefaultProperty = true; Set<String> dBNames = Configuration.getDbNames(); for (String dbName : dBNames) { if (keyName.startsWith("db." + dbName) ||
Set<String> dBNames = Configuration.getDbNames(); boolean defaultExitCode = true; Configuration dbConfig = new Configuration(dbName);
/** * Add a parameter in the configuration * * @param key * the key of the parameter * @param value * the value of the parameter * @return the previous value of the specified key in this hashtable, or null if it did not have one */ public Object put(String key, String value) { if (key != null) { return Play.configuration.put(generateKey(key), value); } return null; }
public static String getDefaultDialect(String driver) { return getDefaultDialect(new Configuration("default"), driver); }
@Override public void beforeInvocation() { if (isDisabled() || Play.mode.isProd()) { return; } try { checkEvolutionsState(); } catch (InvalidDatabaseRevision e) { Set<String> dbNames = Configuration.getDbNames(); for (String dbName : dbNames) { Configuration dbConfig = new Configuration(dbName); for (Entry<String, VirtualFile> moduleRoot : modulesWithEvolutions.entrySet()) { if ("mem".equals(dbConfig.getProperty("db")) && listDatabaseEvolutions(e.getDbName(), moduleRoot.getKey()).peek().revision == 0) { Logger.info("Automatically applying evolutions in in-memory database"); Logger.info("Applying evolutions for '" + moduleRoot.getKey() + "'"); applyScript(true, moduleRoot.getKey(), moduleRoot.getValue()); } else { throw e; } } } } }
protected DataSourceFactory factory(Configuration dbConfig) { String dbFactory = dbConfig.getProperty("db.factory", "play.db.hikaricp.HikariDataSourceFactory"); try { return (DataSourceFactory) Class.forName(dbFactory).newInstance(); } catch (Exception e) { throw new IllegalArgumentException("Expected implementation of " + DataSourceFactory.class.getName() + ", but received: " + dbFactory); } }
Set<String> dbNames = Configuration.getDbNames(); Iterator<String> it = dbNames.iterator(); while (it.hasNext()) { dbName = it.next(); Configuration dbConfig = new Configuration(dbName); String datasourceName = dbConfig.getProperty("db", ""); String driver = dbConfig.getProperty("db.driver"); try { Driver d = (Driver) Class.forName(driver, true, Play.classloader).newInstance(); if (dbConfig.getProperty("db.user") == null) { fake = DriverManager.getConnection(dbConfig.getProperty("db.url")); } else { fake = DriverManager.getConnection(dbConfig.getProperty("db.url"), dbConfig.getProperty("db.user"), dbConfig.getProperty("db.pass")); String destroyMethod = dbConfig.getProperty("db.destroyMethod", ""); DB.datasource = ds; DB.destroyMethod = destroyMethod;
private static synchronized boolean isOracleDialectInUse(String dbName) { boolean isOracle = false; Configuration dbConfig = new Configuration(dbName); String jpaDialect = JPAPlugin.getDefaultDialect(dbConfig.getProperty("db.driver")); if (jpaDialect != null) { try { Class<?> dialectClass = Play.classloader.loadClass(jpaDialect); // Oracle 8i dialect is the base class for oracle dialects (at least for now) isOracle = org.hibernate.dialect.Oracle8iDialect.class.isAssignableFrom(dialectClass); } catch (ClassNotFoundException e) { // swallow Logger.warn("jpa.dialect class %s not found", jpaDialect); } } return isOracle; }
private List<String> mappingFiles(Configuration dbConfig) { String mappingFile = dbConfig.getProperty("jpa.mapping-file", ""); return mappingFile != null && mappingFile.length() > 0 ? singletonList(mappingFile) : emptyList(); }
public static synchronized void checkEvolutionsState() { // Look over all the DB Set<String> dBNames = Configuration.getDbNames(); for (String dbName : dBNames) { checkEvolutionsState(dbName); } }
private static boolean isMySqlDialectInUse(String dbName) { boolean isMySQl = false; Configuration dbConfig = new Configuration(dbName); String jpaDialect = JPAPlugin.getDefaultDialect(dbConfig.getProperty("db.driver")); if (jpaDialect != null) { try { Class<?> dialectClass = Play.classloader.loadClass(jpaDialect); // MySQLDialect is the base class for MySQL dialects isMySQl = org.hibernate.dialect.MySQLDialect.class.isAssignableFrom(dialectClass); } catch (ClassNotFoundException e) { // swallow Logger.warn("jpa.dialect class %s not found", jpaDialect); } } return isMySQl; }
private static void check(Configuration config, String mode, String property) { if (!StringUtils.isEmpty(config.getProperty(property))) { Logger.warn("Ignoring " + property + " because running the in " + mode + " db."); } }
StringWriter sw = new StringWriter(); PrintWriter out = new PrintWriter(sw); Set<String> dbNames = Configuration.getDbNames();