package com.avaje.ebean.config; import java.util.ArrayList; import java.util.List; import javax.sql.DataSource; import com.avaje.ebean.EbeanServerFactory; import com.avaje.ebean.annotation.Encrypted; import com.avaje.ebean.cache.ServerCacheFactory; import com.avaje.ebean.cache.ServerCacheManager; import com.avaje.ebean.config.GlobalProperties.PropertySource; import com.avaje.ebean.config.dbplatform.DatabasePlatform; import com.avaje.ebean.config.dbplatform.DbEncrypt; import com.avaje.ebean.event.BeanPersistController; import com.avaje.ebean.event.BeanPersistListener; import com.avaje.ebean.event.BeanQueryAdapter; import com.avaje.ebean.event.BulkTableEventListener; import com.avaje.ebean.event.ServerConfigStartup; import com.avaje.ebean.event.TransactionEventListener; import com.avaje.ebean.meta.MetaInfoManager; import com.avaje.ebean.util.ClassUtil; /** * The configuration used for creating a EbeanServer. *
* Used to programmatically construct an EbeanServer and optionally register it * with the Ebean singleton. *
** If you just use Ebean without this programmatic configuration Ebean will read * the ebean.properties file and take the configuration from there. This usually * includes searching the class path and automatically registering any entity * classes and listeners etc. *
* *
* ServerConfig c = new ServerConfig();
* c.setName("ordh2");
*
* // read the ebean.properties and load
* // those settings into this serverConfig object
* c.loadFromProperties();
*
* // generate DDL and run it
* c.setDdlGenerate(true);
* c.setDdlRun(true);
*
* // add any classes found in the app.data package
* c.addPackage("app.data");
*
* // add the names of Jars that contain entities
* c.addJar("myJarContainingEntities.jar");
* c.addJar("someOtherJarContainingEntities.jar");
*
* // register as the 'Default' server
* c.setDefaultServer(true);
*
* EbeanServer server = EbeanServerFactory.create(c);
*
*
*
* @see EbeanServerFactory
*
* @author emcgreal
* @author rbygrave
*/
public class ServerConfig {
/**
* The Constant DEFAULT_QUERY_BATCH_SIZE. Default: 100
*/
private final static int DEFAULT_QUERY_BATCH_SIZE = 100;
/**
* The EbeanServer name.
*/
private String name;
/**
* The resource directory.
*/
private String resourceDirectory;
/**
* The enhance log level. Used with subclass generation.
*/
private int enhanceLogLevel;
/**
* Set to true to register this EbeanServer with the Ebean singleton.
*/
private boolean register = true;
/**
* Set to true if this is the default/primary server.
*/
private boolean defaultServer;
/**
* List of interesting classes such as entities, embedded, ScalarTypes,
* Listeners, Finders, Controllers etc.
*/
private List* By default this is set to true. *
*/ public boolean isRegister() { return register; } /** * Set to false if you do not want this server to be registered with the Ebean * singleton when it is created. ** By default this is set to true. *
*/ public void setRegister(boolean register) { this.register = register; } /** * Return true if this server should be registered as the "default" server * with the Ebean singleton. ** This is only used when {@link #setRegister(boolean)} is also true. *
*/ public boolean isDefaultServer() { return defaultServer; } /** * Set true if this EbeanServer should be registered as the "default" server * with the Ebean singleton. ** This is only used when {@link #setRegister(boolean)} is also true. *
*/ public void setDefaultServer(boolean defaultServer) { this.defaultServer = defaultServer; } /** * Returns true if by default JDBC batching is used for persisting or deleting * beans. ** With this Ebean will batch up persist requests and use the JDBC batch api. * This is a performance optimisation designed to reduce the network chatter. *
*/ public boolean isPersistBatching() { return persistBatching; } /** * Use isPersistBatching() instead. * * @deprecated */ public boolean isUsePersistBatching() { return persistBatching; } /** * Set to true if you what to use JDBC batching for persisting and deleting * beans. ** With this Ebean will batch up persist requests and use the JDBC batch api. * This is a performance optimisation designed to reduce the network chatter. *
*/ public void setPersistBatching(boolean persistBatching) { this.persistBatching = persistBatching; } /** * Use setPersistBatching() instead. * * @deprecated */ public void setUsePersistBatching(boolean persistBatching) { this.persistBatching = persistBatching; } /** * Return the batch size used for JDBC batching. This defaults to 20. */ public int getPersistBatchSize() { return persistBatchSize; } /** * Set the batch size used for JDBC batching. If unset this defaults to 20. */ public void setPersistBatchSize(int persistBatchSize) { this.persistBatchSize = persistBatchSize; } /** * Return the default batch size for lazy loading of beans and collections. */ public int getLazyLoadBatchSize() { return lazyLoadBatchSize; } /** * Gets the query batch size. * * @return the query batch size */ public int getQueryBatchSize() { return queryBatchSize; } /** * Sets the query batch size. * * @param queryBatchSize * the new query batch size */ public void setQueryBatchSize(int queryBatchSize) { this.queryBatchSize = queryBatchSize; } /** * Set the default batch size for lazy loading. ** This is the number of beans or collections loaded when lazy loading is * invoked by default. *
** The default value is for this is 1 (load 1 bean or collection). *
** You can explicitly control the lazy loading batch size for a given join on * a query using +lazy(batchSize) or JoinConfig. *
*/ public void setLazyLoadBatchSize(int lazyLoadBatchSize) { this.lazyLoadBatchSize = lazyLoadBatchSize; } /** * Set the number of sequences to fetch/preallocate when using DB sequences. ** This is a performance optimisation to reduce the number times Ebean * requests a sequence to be used as an Id for a bean (aka reduce network * chatter). *
*/ public void setDatabaseSequenceBatchSize(int databaseSequenceBatchSize) { this.databaseSequenceBatchSize = databaseSequenceBatchSize; } /** * Return true if we are running in a JTA Transaction manager. */ public boolean isUseJtaTransactionManager() { return useJtaTransactionManager; } /** * Set to true if we are running in a JTA Transaction manager. */ public void setUseJtaTransactionManager(boolean useJtaTransactionManager) { this.useJtaTransactionManager = useJtaTransactionManager; } /** * Return the external transaction manager. */ public ExternalTransactionManager getExternalTransactionManager() { return externalTransactionManager; } /** * Set the external transaction manager. */ public void setExternalTransactionManager(ExternalTransactionManager externalTransactionManager) { this.externalTransactionManager = externalTransactionManager; } /** * Return the ServerCacheFactory. */ public ServerCacheFactory getServerCacheFactory() { return serverCacheFactory; } /** * Set the ServerCacheFactory to use. */ public void setServerCacheFactory(ServerCacheFactory serverCacheFactory) { this.serverCacheFactory = serverCacheFactory; } /** * Return the ServerCacheManager. */ public ServerCacheManager getServerCacheManager() { return serverCacheManager; } /** * Set the ServerCacheManager to use. */ public void setServerCacheManager(ServerCacheManager serverCacheManager) { this.serverCacheManager = serverCacheManager; } /** * Return the log level used for "subclassing" enhancement. */ public int getEnhanceLogLevel() { return enhanceLogLevel; } /** * Set the log level used for "subclassing" enhancement. */ public void setEnhanceLogLevel(int enhanceLogLevel) { this.enhanceLogLevel = enhanceLogLevel; } /** * Return the NamingConvention. ** If none has been set the default UnderscoreNamingConvention is used. *
*/ public NamingConvention getNamingConvention() { return namingConvention; } /** * Set the NamingConvention. ** If none is set the default UnderscoreNamingConvention is used. *
*/ public void setNamingConvention(NamingConvention namingConvention) { this.namingConvention = namingConvention; } /** * Return the configuration for the Autofetch feature. */ public AutofetchConfig getAutofetchConfig() { return autofetchConfig; } /** * Set the configuration for the Autofetch feature. */ public void setAutofetchConfig(AutofetchConfig autofetchConfig) { this.autofetchConfig = autofetchConfig; } /** * Return the PreparedStatementDelegate. */ public PstmtDelegate getPstmtDelegate() { return pstmtDelegate; } /** * Set the PstmtDelegate which can be used to support JDBC driver specific * features. ** Typically this means Oracle JDBC driver specific workarounds. *
*/ public void setPstmtDelegate(PstmtDelegate pstmtDelegate) { this.pstmtDelegate = pstmtDelegate; } /** * Return the DataSource. */ public DataSource getDataSource() { return dataSource; } /** * Set a DataSource. */ public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } /** * Return the configuration to build a DataSource using Ebean's own DataSource * implementation. */ public DataSourceConfig getDataSourceConfig() { return dataSourceConfig; } /** * Set the configuration required to build a DataSource using Ebean's own * DataSource implementation. */ public void setDataSourceConfig(DataSourceConfig dataSourceConfig) { this.dataSourceConfig = dataSourceConfig; } /** * Return the JNDI name of the DataSource to use. */ public String getDataSourceJndiName() { return dataSourceJndiName; } /** * Set the JNDI name of the DataSource to use. ** By default a prefix of "java:comp/env/jdbc/" is used to lookup the * DataSource. This prefix is not used if dataSourceJndiName starts with * "java:". *
*/ public void setDataSourceJndiName(String dataSourceJndiName) { this.dataSourceJndiName = dataSourceJndiName; } /** * Return a value used to represent TRUE in the database. ** This is used for databases that do not support boolean natively. *
** The value returned is either a Integer or a String (e.g. "1", or "T"). *
*/ public String getDatabaseBooleanTrue() { return databaseBooleanTrue; } /** * Set the value to represent TRUE in the database. ** This is used for databases that do not support boolean natively. *
** The value set is either a Integer or a String (e.g. "1", or "T"). *
*/ public void setDatabaseBooleanTrue(String databaseTrue) { this.databaseBooleanTrue = databaseTrue; } /** * Return a value used to represent FALSE in the database. ** This is used for databases that do not support boolean natively. *
** The value returned is either a Integer or a String (e.g. "0", or "F"). *
*/ public String getDatabaseBooleanFalse() { return databaseBooleanFalse; } /** * Set the value to represent FALSE in the database. ** This is used for databases that do not support boolean natively. *
** The value set is either a Integer or a String (e.g. "0", or "F"). *
*/ public void setDatabaseBooleanFalse(String databaseFalse) { this.databaseBooleanFalse = databaseFalse; } /** * Return the number of DB sequence values that should be preallocated. */ public int getDatabaseSequenceBatchSize() { return databaseSequenceBatchSize; } /** * Set the number of DB sequence values that should be preallocated and cached * by Ebean. ** This is only used for DB's that use sequences and is a performance * optimisation. This reduces the number of times Ebean needs to get a * sequence value from the Database reducing network chatter. *
** By default this value is 10 so when we need another Id (and don't have one * in our cache) Ebean will fetch 10 id's from the database. Note that when * the cache drops to have full (which is 5 by default) Ebean will fetch * another batch of Id's in a background thread. *
*/ public void setDatabaseSequenceBatch(int databaseSequenceBatchSize) { this.databaseSequenceBatchSize = databaseSequenceBatchSize; } /** * Return the database platform name (can be null). ** If null then the platform is determined automatically via the JDBC driver * information. *
*/ public String getDatabasePlatformName() { return databasePlatformName; } /** * Explicitly set the database platform name ** If none is set then the platform is determined automatically via the JDBC * driver information. *
** This can be used when the Database Platform can not be automatically * detected from the JDBC driver (possibly 3rd party JDBC driver). It is also * useful when you want to do offline DDL generation for a database platform * that you don't have access to. *
** Values are oracle, h2, postgres, mysql, mssqlserver2005. *
* * @see DataSourceConfig#setOffline(boolean) */ public void setDatabasePlatformName(String databasePlatformName) { this.databasePlatformName = databasePlatformName; } /** * Return the database platform to use for this server. */ public DatabasePlatform getDatabasePlatform() { return databasePlatform; } /** * Explicitly set the database platform to use. ** If none is set then the platform is determined via the databasePlatformName * or automatically via the JDBC driver information. *
*/ public void setDatabasePlatform(DatabasePlatform databasePlatform) { this.databasePlatform = databasePlatform; } /** * Return the EncryptKeyManager. */ public EncryptKeyManager getEncryptKeyManager() { return encryptKeyManager; } /** * Set the EncryptKeyManager. ** This is required when you want to use encrypted properties. *
** You can also set this in ebean.proprerties: *
* ** # set via ebean.properties * * ebean.encryptKeyManager=com.avaje.tests.basic.encrypt.BasicEncyptKeyManager **/ public void setEncryptKeyManager(EncryptKeyManager encryptKeyManager) { this.encryptKeyManager = encryptKeyManager; } /** * Return the EncryptDeployManager. *
* This is optionally used to programmatically define which columns are * encrypted instead of using the {@link Encrypted} Annotation. *
*/ public EncryptDeployManager getEncryptDeployManager() { return encryptDeployManager; } /** * Set the EncryptDeployManager. ** This is optionally used to programmatically define which columns are * encrypted instead of using the {@link Encrypted} Annotation. *
*/ public void setEncryptDeployManager(EncryptDeployManager encryptDeployManager) { this.encryptDeployManager = encryptDeployManager; } /** * Return the Encryptor used to encrypt data on the java client side (as * opposed to DB encryption functions). */ public Encryptor getEncryptor() { return encryptor; } /** * Set the Encryptor used to encrypt data on the java client side (as opposed * to DB encryption functions). ** Ebean has a default implementation that it will use if you do not set your * own Encryptor implementation. *
*/ public void setEncryptor(Encryptor encryptor) { this.encryptor = encryptor; } /** * Return the DbEncrypt used to encrypt and decrypt properties. ** Note that if this is not set then the DbPlatform may already have a * DbEncrypt set and that will be used. *
*/ public DbEncrypt getDbEncrypt() { return dbEncrypt; } /** * Set the DbEncrypt used to encrypt and decrypt properties. ** Note that if this is not set then the DbPlatform may already have a * DbEncrypt set (H2, MySql, Postgres and Oracle platforms have a DbEncrypt) *
*/ public void setDbEncrypt(DbEncrypt dbEncrypt) { this.dbEncrypt = dbEncrypt; } /** * Return true if UUID should be stored as binary(16) (as opposed to varchar(40)). */ public boolean isUuidStoreAsBinary() { return uuidStoreAsBinary; } /** * Set to true if UUID should be stored as binary(16) (as opposed to varchar(40)). */ public void setUuidStoreAsBinary(boolean uuidStoreAsBinary) { this.uuidStoreAsBinary = uuidStoreAsBinary; } /** * Set to true to run the DDL generation on startup. */ public void setDdlGenerate(boolean ddlGenerate) { this.ddlGenerate = ddlGenerate; } /** * Set to true to run the generated DDL on startup. */ public void setDdlRun(boolean ddlRun) { this.ddlRun = ddlRun; } /** * Return true if the DDL should be generated. */ public boolean isDdlGenerate() { return ddlGenerate; } /** * Return true if the DDL should be run. */ public boolean isDdlRun() { return ddlRun; } /** * Programmatically add classes (typically entities) that this server should * use. ** The class can be an Entity, Embedded type, ScalarType, BeanPersistListener, * BeanFinder or BeanPersistController. *
** If no classes are specified then the classes are found automatically via * searching the class path. *
** Alternatively the classes can be added via {@link #setClasses(List)}. *
* * @param cls * the entity type (or other type) that should be registered by this * server. */ public void addClass(Class> cls) { if (classes == null) { classes = new ArrayList* This is only used if classes have not been explicitly specified. *
*/ public void addPackage(String packageName) { if (packages == null) { packages = new ArrayList* This is only used if classes have not been explicitly specified. *
*/ public List* This is only used if classes have not been explicitly specified. *
*/ public void setPackages(List* This is only used if classes have not been explicitly specified. *
** If you are using ebean.properties you can specify jars to search by setting * a ebean.search.jars property. *
* ** # EBean will search through classes for entities, but will not search jar files * # unless you tell it to do so, for performance reasons. Set this value to a * # comma-delimited list of jar files you want ebean to search. * ebean.search.jars=example.jar **/ public void addJar(String jarName) { if (searchJars == null) { searchJars = new ArrayList
* This is only used if classes have not been explicitly specified. *
*/ public List* This is only used if classes have not been explicitly specified. *
*/ public void setJars(List* If no classes are specified then the classes are found automatically via * searching the class path. *
** Alternatively the classes can contain added via {@link #addClass(Class)}. *
*/ public void setClasses(List* These statistics can be used to highlight code/query 'origin points' that result in lots of lazy loading. *
** It is considered safe/fine to have this set to true for production. *
** This information can be later retrieved via {@link MetaInfoManager}. *
* @see MetaInfoManager */ public void setCollectQueryStatsByNode(boolean collectQueryStatsByNode) { this.collectQueryStatsByNode = collectQueryStatsByNode; } /** * Return true if query plans should also collect their 'origins'. This means for a given query plan you * can identify the code/origin points where this query resulted from including lazy loading origins. */ public boolean isCollectQueryOrigins() { return collectQueryOrigins; } /** * Set to true if query plans should collect their 'origin' points. This means for a given query plan you * can identify the code/origin points where this query resulted from including lazy loading origins. ** This information can be later retrieved via {@link MetaInfoManager}. *
* @see MetaInfoManager */ public void setCollectQueryOrigins(boolean collectQueryOrigins) { this.collectQueryOrigins = collectQueryOrigins; } /** * Returns the resource directory. */ public String getResourceDirectory() { return resourceDirectory; } /** * Sets the resource directory. */ public void setResourceDirectory(String resourceDirectory) { this.resourceDirectory = resourceDirectory; } /** * Register a BeanQueryAdapter instance. ** Note alternatively you can use {@link #setQueryAdapters(List)} to set all * the BeanQueryAdapter instances. *
*/ public void add(BeanQueryAdapter beanQueryAdapter) { queryAdapters.add(beanQueryAdapter); } /** * Return the BeanQueryAdapter instances. */ public List* Note alternatively you can use {@link #add(BeanQueryAdapter)} to add * BeanQueryAdapter instances one at a time. *
*/ public void setQueryAdapters(List* Note alternatively you can use {@link #setPersistControllers(List)} to set * all the BeanPersistController instances. *
*/ public void add(BeanPersistController beanPersistController) { persistControllers.add(beanPersistController); } /** * Return the BeanPersistController instances. */ public List* Note alternatively you can use {@link #add(BeanPersistController)} to add * BeanPersistController instances one at a time. *
*/ public void setPersistControllers(List* Note alternatively you can use {@link #setTransactionEventListeners(List)} * to set all the TransactionEventListener instances. *
*/ public void add(TransactionEventListener listener) { transactionEventListeners.add(listener); } /** * Return the TransactionEventListener instances. */ public List* Note alternatively you can use {@link #add(TransactionEventListener)} to * add TransactionEventListener instances one at a time. *
*/ public void setTransactionEventListeners(List* Note alternatively you can use {@link #setPersistListeners(List)} to set * all the BeanPersistListener instances. *
*/ public void add(BeanPersistListener> beanPersistListener) { persistListeners.add(beanPersistListener); } /** * Return the BeanPersistListener instances. */ public List* Note alternatively you can use {@link #add(BeanPersistListener)} to add * BeanPersistListener instances one at a time. *
*/ public void setPersistListeners(List