forked from Coded/SIPRP
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
220 lines
5.2 KiB
220 lines
5.2 KiB
package pt.evolute.data;
|
|
|
|
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import com.evolute.entity.ProviderInterface;
|
|
import com.evolute.entity.evo.EvoDataException;
|
|
import com.evolute.entity.evo.EvoDataObject;
|
|
import com.evolute.entity.evo.EvoDataProvider;
|
|
import com.evolute.entity.utils.ConnectionIdentity;
|
|
import com.evolute.properties.EvoDefaultPropertiesLoader;
|
|
import com.evolute.properties.EvoPropertyKey;
|
|
import com.evolute.properties.PropertiesException;
|
|
import com.evolute.utils.Singleton;
|
|
import com.evolute.utils.db.DBManager;
|
|
import com.evolute.utils.db.DriverProvider;
|
|
import com.evolute.utils.db.JDBCManager;
|
|
import com.evolute.utils.error.ErrorLogger;
|
|
import com.evolute.utils.sql.SQLQuery;
|
|
import com.evolute.utils.timer.Timer;
|
|
|
|
public class ProviderSpringBean {
|
|
|
|
private ProviderInterface<EvoDataObject<?>, EvoDataException> ENTITY_PROVIDER;
|
|
private DBManager DBMANAGER;
|
|
|
|
private String server;
|
|
private String port;
|
|
private String username;
|
|
private String password;
|
|
private String database;
|
|
private String parameters;
|
|
private String databasePrefix;
|
|
private String url;
|
|
|
|
private boolean readDefaults = true ;
|
|
|
|
|
|
|
|
public ProviderSpringBean()
|
|
{
|
|
;
|
|
}
|
|
|
|
public void clean() throws Exception {
|
|
|
|
if(ENTITY_PROVIDER != null)
|
|
{
|
|
ENTITY_PROVIDER.close();
|
|
}
|
|
|
|
if(DBMANAGER != null)
|
|
{
|
|
DBMANAGER.close();
|
|
}
|
|
|
|
Timer.stop();
|
|
DriverProvider.close();
|
|
|
|
}
|
|
|
|
|
|
public synchronized ProviderInterface<EvoDataObject<?>, EvoDataException> getENTITY_PROVIDER() {
|
|
if(ENTITY_PROVIDER == null)
|
|
{
|
|
try
|
|
{
|
|
init();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
ErrorLogger.logException(e, Level.SEVERE);
|
|
}
|
|
}
|
|
return ENTITY_PROVIDER;
|
|
}
|
|
|
|
public synchronized DBManager getDBMANAGER() {
|
|
if(DBMANAGER == null)
|
|
{
|
|
try
|
|
{
|
|
init();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
ErrorLogger.logException(e, Level.SEVERE);
|
|
}
|
|
}
|
|
return DBMANAGER;
|
|
}
|
|
|
|
|
|
|
|
|
|
private void init() throws Exception
|
|
{
|
|
if(readDefaults)
|
|
{
|
|
loadProperties();
|
|
}
|
|
setupProperties();
|
|
|
|
DBMANAGER = new JDBCManager( url, username, password, 50,10, new SQLQuery[]{} );
|
|
|
|
ConnectionIdentity conn = new ConnectionIdentity( url, username );
|
|
conn.setPassword( password );
|
|
|
|
ENTITY_PROVIDER = new EvoDataProvider(conn);
|
|
}
|
|
|
|
|
|
private void loadProperties()
|
|
{
|
|
if(!EvoDefaultPropertiesLoader.getInstance().isLoaded())
|
|
{
|
|
try
|
|
{
|
|
EvoDefaultPropertiesLoader.getInstance().load();
|
|
}
|
|
catch (PropertiesException e)
|
|
{
|
|
e.printStackTrace(System.out);
|
|
}
|
|
}
|
|
|
|
server = EvoDefaultPropertiesLoader.getInstance().findProperty( EvoPropertyKey.DB.CONNECTION.HOSTNAME );
|
|
port = EvoDefaultPropertiesLoader.getInstance().findProperty( EvoPropertyKey.DB.CONNECTION.PORT );
|
|
username = EvoDefaultPropertiesLoader.getInstance().findProperty( EvoPropertyKey.DB.USER );
|
|
password = EvoDefaultPropertiesLoader.getInstance().findProperty( EvoPropertyKey.DB.PASSWORD );
|
|
database = EvoDefaultPropertiesLoader.getInstance().findProperty( EvoPropertyKey.DB.NAME );
|
|
parameters = EvoDefaultPropertiesLoader.getInstance().findProperty( EvoPropertyKey.DB.PARAMETERS, "" );
|
|
databasePrefix = EvoDefaultPropertiesLoader.getInstance().findProperty( EvoPropertyKey.DB.CONNECTION.PREFIX, EvoPropertyKey.DEFAULT_DATABASE_PREFIX );
|
|
url = EvoDefaultPropertiesLoader.getInstance().findProperty( EvoPropertyKey.DB.CONNECTION.DB_URL_FULL );
|
|
if( EvoDefaultPropertiesLoader.getInstance().findProperty( EvoPropertyKey.DB.ALLOW_JNLP_OVERRIDE, false ) )
|
|
{
|
|
url = EvoDefaultPropertiesLoader.getInstance().findProperty( EvoPropertyKey.DB.CONNECTION.DB_URL_JNLP_FULL, url );
|
|
}
|
|
}
|
|
|
|
private void setupProperties()
|
|
{
|
|
if( url == null || url.isEmpty() )
|
|
{
|
|
char unionChar = '?';
|
|
if( database.contains( "?" ) )
|
|
{
|
|
unionChar = '&';
|
|
ErrorLogger.log( "invalid db name - " + database );
|
|
}
|
|
url = databasePrefix + server + ":" + port + "/" + database + unionChar
|
|
+ "logUnclosedConnections=true&loginTimeout=10&tcpKeepAlive=true"
|
|
+ ( parameters == null || parameters.isEmpty() ? "" : "&" + parameters );
|
|
}
|
|
}
|
|
|
|
public synchronized void setAsSingletonDefaults()
|
|
{
|
|
if(ENTITY_PROVIDER == null || DBMANAGER == null)
|
|
{
|
|
try
|
|
{
|
|
init();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
ErrorLogger.logException(e, Level.SEVERE);
|
|
}
|
|
}
|
|
Singleton.setInstance(Singleton.DEFAULT_EVO_DATA_PROVIDER, ENTITY_PROVIDER);
|
|
Singleton.setInstance(Singleton.DEFAULT_DBMANAGER, DBMANAGER);
|
|
Singleton.setInstance(Singleton.DEFAULT_OBJECT_PROVIDER, ENTITY_PROVIDER );
|
|
}
|
|
|
|
|
|
|
|
public void setServer(String server) {
|
|
this.server = server;
|
|
readDefaults = false ;
|
|
}
|
|
|
|
public void setPort(String port) {
|
|
this.port = port;
|
|
readDefaults = false ;
|
|
}
|
|
|
|
public void setUsername(String username) {
|
|
this.username = username;
|
|
readDefaults = false ;
|
|
}
|
|
|
|
public void setPassword(String password) {
|
|
this.password = password;
|
|
readDefaults = false ;
|
|
}
|
|
|
|
public void setDatabase(String database) {
|
|
this.database = database;
|
|
readDefaults = false ;
|
|
}
|
|
|
|
public void setParameters(String parameters) {
|
|
this.parameters = parameters;
|
|
readDefaults = false ;
|
|
}
|
|
|
|
public void setDatabasePrefix(String databasePrefix) {
|
|
this.databasePrefix = databasePrefix;
|
|
readDefaults = false ;
|
|
}
|
|
|
|
public void setUrl(String url) {
|
|
this.url = url;
|
|
readDefaults = false ;
|
|
}
|
|
|
|
|
|
}
|