Registry() { statisticsQueue = configuration.collectStatistics() ? new StatisticsQueue(configuration.collectStatisticsOnHold()) : null; }
QueryCache() { String cacheManagerClass = Registry.instance().getConfiguration().getCacheManager(); if(cacheManagerClass != null){ try{ Class cmc = Class.forName(cacheManagerClass); cacheManager = (CacheManager)cmc.newInstance(); enabled = true; }catch(InitException e){ throw e; }catch(Exception e){ throw new InitException("Failed to initialize a CacheManager. Please, ensure that the property " + "'cache.manager' points to correct class which extends '" + CacheManager.class.getName() + "' and provides a default constructor.", e); } }else{ cacheManager = new NopeCacheManager(); } }
public static void log(Logger logger, LogLevel logLevel, String log, Object param1, Object param2) { if(Configuration.hasActiveLogger()) { Configuration.getActiveLogger().log(logger, logLevel, log, param1, param2); }else { if (matches(logger, logLevel, log)) { switch (logLevel){ case DEBUG: logger.debug(log, param1, param2); break; case INFO: logger.info(log, param1, param2); break; case WARNING: logger.warn(log, param1, param2); break; case ERROR: logger.error(log, param1, param2); break; default: } } } }
/** * Finds a connection {@link ConnectionSpec} that corresponds to system properties, * environment variables of <code>database.properties</code> configuration, whichever is found first. * Configuration of system properties overrides environment variables, which overrides * <code>database.properties</code>. * * @return {@link ConnectionSpec} used by {@link DB#open()} to open a "default" connection, as well as {@link Base#open()} methods. */ public ConnectionSpec getCurrentConnectionSpec(){ return getConnectionSpec(getEnvironment()); }
public Dialect getDialect(MetaModel mm){ return getDialect(mm.getDbType()); }
private static void collectStatistics(String query, long time, boolean cacheHit) { if (Registry.instance().getConfiguration().collectStatistics() && !cacheHit) { Registry.instance().getStatisticsQueue().enqueue(new QueryExecutionEvent(query, time)); } }
/** * This method will open a connection defined in the file 'database.properties' located at * root of classpath. The connection picked up from the file is defined by <code>ACTIVE_ENV</code> * environment variable or <code>active_env</code> system property. * If this variable is not defined, it defaults to 'development' environment. * * <p></p> * If there is JUnit on classpath, this method assumes it is running under test, and defaults to 'test'. * * @see Configuration#getEnvironment() */ public DB open(){ Configuration config = Registry.instance().getConfiguration(); ConnectionSpec spec = config.getCurrentConnectionSpec(); if(spec == null){ throw new DBException("Could not find configuration in a property file for environment: " + config.getEnvironment() + ". Are you sure you have a database.properties file configured?"); } return open(spec); }
protected static void findModels(String dbName) throws IOException, ClassNotFoundException { //this is for static instrumentation. In case of dynamic, the modelClassNames will already be filled. List<String> models = Registry.instance().getConfiguration().getModelNames(dbName); if (models != null && !models.isEmpty()) { for (String model : models) { modelFound(model); } } else { throw new InitException("you are trying to work with models, but no models are found. Maybe you have " + "no models in project, or you did not instrument the models. It is expected that you have " + "a file activejdbc_models.properties on classpath"); } }
/** * Provides a list of all connection wrappers corresponding to a current environment. * * @return a list of all connection wrappers corresponding to a current environment. */ public static List<ConnectionSpecWrapper> getConnectionSpecWrappers() { return getConnectionSpecWrappers(Configuration.getEnv()); }
public Dialect getDialect() { return Registry.instance().getConfiguration().getDialect(this); }
static void logQuery(Logger logger, String query, Object[] params, long queryStartTime){ long time = System.currentTimeMillis() - queryStartTime; if (Registry.instance().getConfiguration().collectStatistics()) { Registry.instance().getStatisticsQueue().enqueue(new QueryExecutionEvent(query, time)); } if (logger.isInfoEnabled()) { StringBuilder log = new StringBuilder().append("Query: \"").append(query).append('"'); if (!empty(params)) { log.append(", with parameters: ").append('<'); join(log, params, ">, <"); log.append('>'); } log(logger, log.append(", took: ").append(time).append(" milliseconds").toString()); } }
/** * Finds a connection {@link ConnectionSpec} that corresponds to system properties, * environment variables of <code>database.properties</code> configuration, whichever is found first. * Configuration of system properties overrides environment variables, which overrides * <code>database.properties</code>. * * @return {@link ConnectionSpec} used by {@link DB#open()} to open a "default" connection, as well as {@link Base#open()} methods. */ public ConnectionSpec getCurrentConnectionSpec(){ return getConnectionSpec(getEnvironment()); }
/** * This method will open a connection defined in the file 'database.properties' located at * root of classpath. The connection picked up from the file is defined by <code>ACTIVE_ENV</code> * environment variable or <code>active_env</code> system property. * If this variable is not defined, it defaults to 'development' environment. * * <p></p> * If there is JUnit on classpath, this method assumes it is running under test, and defaults to 'test'. * * @see Configuration#getEnvironment() */ public DB open(){ Configuration config = Registry.instance().getConfiguration(); ConnectionSpec spec = config.getCurrentConnectionSpec(); if(spec == null){ throw new DBException("Could not find configuration in a property file for environment: " + config.getEnvironment() + ". Are you sure you have a database.properties file configured?"); } return open(spec); }
protected static void findModels(String dbName) throws IOException, ClassNotFoundException { //this is for static instrumentation. In case of dynamic, the modelClassNames will already be filled. List<String> models = Registry.instance().getConfiguration().getModelNames(dbName); if (models != null && !models.isEmpty()) { for (String model : models) { modelFound(model); } } else { throw new InitException("you are trying to work with models, but no models are found. Maybe you have " + "no models in project, or you did not instrument the models. It is expected that you have " + "a file activejdbc_models.properties on classpath"); } }
public static void clearConnectionWrappers() { clearConnectionWrappers(Configuration.getEnv()); }
private Registry() { statisticsQueue = configuration.collectStatistics() ? new StatisticsQueue(configuration.collectStatisticsOnHold()) : null; }
public Dialect getDialect() { return Registry.instance().getConfiguration().getDialect(this); }
public static void log(Logger logger, LogLevel logLevel, String log, Object param) { if(Configuration.hasActiveLogger()) { Configuration.getActiveLogger().log(logger, logLevel, log, param); }else { if (matches(logger, logLevel, log)) { switch (logLevel){ case DEBUG: logger.debug(log, param); break; case INFO: logger.info(log, param); break; case WARNING: logger.warn(log, param); break; case ERROR: logger.error(log, param); break; default: } } } }
private static String getJson(String query, Object[] params, long time) { if (Registry.instance().getConfiguration().collectStatistics()) { Registry.instance().getStatisticsQueue().enqueue(new QueryExecutionEvent(query, time)); } return "{\"sql\":\"" + query.replace("\"", "'") + "\",\"params\":[" + getParamsJson(params) + "]" + ",\"duration_millis\":" + time + "}"; }
private QueryCache() { cacheManager = Registry.instance().getConfiguration().getCacheManager(); System.out.println(Registry.instance().getConfiguration().getCacheManager()); }