Repository: cayenne Updated Branches: refs/heads/master 00dc72119 -> 12e83f21b
http://git-wip-us.apache.org/repos/asf/cayenne/blob/12e83f21/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/pref/DBConnectionInfo.java ---------------------------------------------------------------------- diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/pref/DBConnectionInfo.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/pref/DBConnectionInfo.java index e91fabf..921d451 100644 --- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/pref/DBConnectionInfo.java +++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/pref/DBConnectionInfo.java @@ -31,6 +31,7 @@ import org.apache.cayenne.datasource.DriverDataSource; import org.apache.cayenne.dba.AutoAdapter; import org.apache.cayenne.dba.DbAdapter; import org.apache.cayenne.di.AdhocObjectFactory; +import org.apache.cayenne.log.NoopJdbcEventLogger; import org.apache.cayenne.modeler.Application; import org.apache.cayenne.modeler.ClassLoadingService; import org.apache.cayenne.pref.CayennePreference; @@ -38,293 +39,286 @@ import org.apache.cayenne.util.Util; public class DBConnectionInfo extends CayennePreference { - public static final String DB_ADAPTER_PROPERTY = "dbAdapter"; - public static final String JDBC_DRIVER_PROPERTY = "jdbcDriver"; - public static final String PASSWORD_PROPERTY = "password"; - public static final String URL_PROPERTY = "url"; - public static final String USER_NAME_PROPERTY = "userName"; - private static final String DB_CONNECTION_INFO = "dbConnectionInfo"; - - public static final String ID_PK_COLUMN = "id"; - - private String nodeName; - - private String dbAdapter; - private String jdbcDriver; - private String password; - private String url; - private String userName; - - private Preferences dbConnectionInfoPreferences; - - public DBConnectionInfo() { - dbConnectionInfoPreferences = getCayennePreference().node(DB_CONNECTION_INFO); - setCurrentPreference(dbConnectionInfoPreferences); - }; - - public DBConnectionInfo(String nameNode, boolean initFromPreferences) { - this(); - setNodeName(nameNode); - if (initFromPreferences) { - initObjectPreference(); - } - }; - - @Override - public Preferences getCurrentPreference() { - if (getNodeName() == null) { - return super.getCurrentPreference(); - } - return dbConnectionInfoPreferences.node(getNodeName()); - } - - @Override - public void setObject(CayennePreference object) { - if (object instanceof DBConnectionInfo) { - setUrl(((DBConnectionInfo) object).getUrl()); - setUserName(((DBConnectionInfo) object).getUserName()); - setPassword(((DBConnectionInfo) object).getPassword()); - setJdbcDriver(((DBConnectionInfo) object).getJdbcDriver()); - setDbAdapter(((DBConnectionInfo) object).getDbAdapter()); - } - } - - @Override - public void saveObjectPreference() { - if (getCurrentPreference() != null) { - if (getDbAdapter() != null) { - getCurrentPreference().put(DB_ADAPTER_PROPERTY, getDbAdapter()); - } - if (getUrl() != null) { - getCurrentPreference().put(URL_PROPERTY, getUrl()); - } - if (getUserName() != null) { - getCurrentPreference().put(USER_NAME_PROPERTY, getUserName()); - } - if (getPassword() != null) { - getCurrentPreference().put(PASSWORD_PROPERTY, getPassword()); - } - if (getJdbcDriver() != null) { - getCurrentPreference().put(JDBC_DRIVER_PROPERTY, getJdbcDriver()); - } - } - } - - public void initObjectPreference() { - if (getCurrentPreference() != null) { - setDbAdapter(getCurrentPreference().get(DB_ADAPTER_PROPERTY, null)); - setUrl(getCurrentPreference().get(URL_PROPERTY, null)); - setUserName(getCurrentPreference().get(USER_NAME_PROPERTY, null)); - setPassword(getCurrentPreference().get(PASSWORD_PROPERTY, null)); - setJdbcDriver(getCurrentPreference().get(JDBC_DRIVER_PROPERTY, null)); - setNodeName(getCurrentPreference().name()); - } - } - - public String getNodeName() { - return nodeName; - } - - public void setNodeName(String nodeName) { - this.nodeName = nodeName; - } - - public String getDbAdapter() { - return dbAdapter; - } - - public void setDbAdapter(String dbAdapter) { - this.dbAdapter = dbAdapter; - } - - public String getJdbcDriver() { - return jdbcDriver; - } - - public void setJdbcDriver(String jdbcDriver) { - this.jdbcDriver = jdbcDriver; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - public String getUrl() { - return url; - } - - public void setUrl(String url) { - this.url = url; - } - - public String getUserName() { - return userName; - } - - public void setUserName(String userName) { - this.userName = userName; - } - - public Preferences getDbConnectionInfoPreferences() { - return dbConnectionInfoPreferences; - } - - public void setDbConnectionInfoPreferences(Preferences dbConnectionInfoPreferences) { - this.dbConnectionInfoPreferences = dbConnectionInfoPreferences; - } - - /** - * Creates a DbAdapter based on configured values. - */ - public DbAdapter makeAdapter(ClassLoadingService classLoader) throws Exception { - String adapterClassName = getDbAdapter(); - Application appInstance = Application.getInstance(); - - if (adapterClassName == null - || AutoAdapter.class.getName().equals(adapterClassName)) { - return appInstance - .getInjector() - .getInstance(DbAdapterFactory.class) - .createAdapter(null, makeDataSource(classLoader)); - } - - try { - return appInstance - .getInjector() - .getInstance(AdhocObjectFactory.class) - .newInstance(DbAdapter.class, adapterClassName); - } - catch (Throwable th) { - th = Util.unwindException(th); - throw new Exception("DbAdapter load error: " + th.getLocalizedMessage()); - } - } - - /** - * Returns a DataSource that uses connection information from this object. Returned - * DataSource is not pooling its connections. It can be wrapped in PoolManager if - * pooling is needed. - */ - public DataSource makeDataSource(ClassLoadingService classLoader) throws SQLException { - - // validate... - if (getJdbcDriver() == null) { - throw new SQLException("No JDBC driver set."); - } - - if (getUrl() == null) { - throw new SQLException("No DB URL set."); - } - - // load driver... - Driver driver; - - try { - driver = classLoader.loadClass(Driver.class, getJdbcDriver()).newInstance(); - } - catch (Throwable th) { - th = Util.unwindException(th); - throw new SQLException("Driver load error: " + th.getLocalizedMessage()); - } - - return new DriverDataSource(driver, getUrl(), getUserName(), getPassword()); - } - - /** - * Updates another DBConnectionInfo with this object's values. - */ - public boolean copyTo(DBConnectionInfo dataSourceInfo) { - boolean updated = false; - - if (!Util.nullSafeEquals(dataSourceInfo.getUrl(), getUrl())) { - dataSourceInfo.setUrl(getUrl()); - updated = true; - } - - if (!Util.nullSafeEquals(dataSourceInfo.getUserName(), getUserName())) { - dataSourceInfo.setUserName(getUserName()); - updated = true; - } - - if (!Util.nullSafeEquals(dataSourceInfo.getPassword(), getPassword())) { - dataSourceInfo.setPassword(getPassword()); - updated = true; - } - - if (!Util.nullSafeEquals(dataSourceInfo.getJdbcDriver(), getJdbcDriver())) { - dataSourceInfo.setJdbcDriver(getJdbcDriver()); - updated = true; - } - - if (!Util.nullSafeEquals(dataSourceInfo.getDbAdapter(), getDbAdapter())) { - dataSourceInfo.setDbAdapter(getDbAdapter()); - updated = true; - } - - return updated; - } - - /** - * Updates DataSourceInfo with this object's values. - * <p> - * <i>Currently doesn't set the adapter property. Need to change the UI to handle - * adapter via DataSourceInfo first, and then it should be safe to do an adapter - * update here. </i> - * </p> - */ - public boolean copyTo(DataSourceInfo dataSourceInfo) { - boolean updated = false; - - if (!Util.nullSafeEquals(dataSourceInfo.getDataSourceUrl(), getUrl())) { - dataSourceInfo.setDataSourceUrl(getUrl()); - updated = true; - } - - if (!Util.nullSafeEquals(dataSourceInfo.getUserName(), getUserName())) { - dataSourceInfo.setUserName(getUserName()); - updated = true; - } - - if (!Util.nullSafeEquals(dataSourceInfo.getPassword(), getPassword())) { - dataSourceInfo.setPassword(getPassword()); - updated = true; - } - - if (!Util.nullSafeEquals(dataSourceInfo.getJdbcDriver(), getJdbcDriver())) { - dataSourceInfo.setJdbcDriver(getJdbcDriver()); - updated = true; - } - - return updated; - } - - public boolean copyFrom(DataSourceInfo dataSourceInfo) { - boolean updated = false; - - if (!Util.nullSafeEquals(dataSourceInfo.getDataSourceUrl(), getUrl())) { - setUrl(dataSourceInfo.getDataSourceUrl()); - updated = true; - } - - if (!Util.nullSafeEquals(dataSourceInfo.getUserName(), getUserName())) { - setUserName(dataSourceInfo.getUserName()); - updated = true; - } - - if (!Util.nullSafeEquals(dataSourceInfo.getPassword(), getPassword())) { - setPassword(dataSourceInfo.getPassword()); - updated = true; - } - - if (!Util.nullSafeEquals(dataSourceInfo.getJdbcDriver(), getJdbcDriver())) { - setJdbcDriver(dataSourceInfo.getJdbcDriver()); - updated = true; - } - - return updated; - } + public static final String DB_ADAPTER_PROPERTY = "dbAdapter"; + public static final String JDBC_DRIVER_PROPERTY = "jdbcDriver"; + public static final String PASSWORD_PROPERTY = "password"; + public static final String URL_PROPERTY = "url"; + public static final String USER_NAME_PROPERTY = "userName"; + private static final String DB_CONNECTION_INFO = "dbConnectionInfo"; + + public static final String ID_PK_COLUMN = "id"; + + private String nodeName; + + private String dbAdapter; + private String jdbcDriver; + private String password; + private String url; + private String userName; + + private Preferences dbConnectionInfoPreferences; + + public DBConnectionInfo() { + dbConnectionInfoPreferences = getCayennePreference().node(DB_CONNECTION_INFO); + setCurrentPreference(dbConnectionInfoPreferences); + }; + + public DBConnectionInfo(String nameNode, boolean initFromPreferences) { + this(); + setNodeName(nameNode); + if (initFromPreferences) { + initObjectPreference(); + } + }; + + @Override + public Preferences getCurrentPreference() { + if (getNodeName() == null) { + return super.getCurrentPreference(); + } + return dbConnectionInfoPreferences.node(getNodeName()); + } + + @Override + public void setObject(CayennePreference object) { + if (object instanceof DBConnectionInfo) { + setUrl(((DBConnectionInfo) object).getUrl()); + setUserName(((DBConnectionInfo) object).getUserName()); + setPassword(((DBConnectionInfo) object).getPassword()); + setJdbcDriver(((DBConnectionInfo) object).getJdbcDriver()); + setDbAdapter(((DBConnectionInfo) object).getDbAdapter()); + } + } + + @Override + public void saveObjectPreference() { + if (getCurrentPreference() != null) { + if (getDbAdapter() != null) { + getCurrentPreference().put(DB_ADAPTER_PROPERTY, getDbAdapter()); + } + if (getUrl() != null) { + getCurrentPreference().put(URL_PROPERTY, getUrl()); + } + if (getUserName() != null) { + getCurrentPreference().put(USER_NAME_PROPERTY, getUserName()); + } + if (getPassword() != null) { + getCurrentPreference().put(PASSWORD_PROPERTY, getPassword()); + } + if (getJdbcDriver() != null) { + getCurrentPreference().put(JDBC_DRIVER_PROPERTY, getJdbcDriver()); + } + } + } + + public void initObjectPreference() { + if (getCurrentPreference() != null) { + setDbAdapter(getCurrentPreference().get(DB_ADAPTER_PROPERTY, null)); + setUrl(getCurrentPreference().get(URL_PROPERTY, null)); + setUserName(getCurrentPreference().get(USER_NAME_PROPERTY, null)); + setPassword(getCurrentPreference().get(PASSWORD_PROPERTY, null)); + setJdbcDriver(getCurrentPreference().get(JDBC_DRIVER_PROPERTY, null)); + setNodeName(getCurrentPreference().name()); + } + } + + public String getNodeName() { + return nodeName; + } + + public void setNodeName(String nodeName) { + this.nodeName = nodeName; + } + + public String getDbAdapter() { + return dbAdapter; + } + + public void setDbAdapter(String dbAdapter) { + this.dbAdapter = dbAdapter; + } + + public String getJdbcDriver() { + return jdbcDriver; + } + + public void setJdbcDriver(String jdbcDriver) { + this.jdbcDriver = jdbcDriver; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public Preferences getDbConnectionInfoPreferences() { + return dbConnectionInfoPreferences; + } + + public void setDbConnectionInfoPreferences(Preferences dbConnectionInfoPreferences) { + this.dbConnectionInfoPreferences = dbConnectionInfoPreferences; + } + + /** + * Creates a DbAdapter based on configured values. + */ + public DbAdapter makeAdapter(ClassLoadingService classLoader) throws Exception { + String adapterClassName = getDbAdapter(); + Application appInstance = Application.getInstance(); + + if (adapterClassName == null || AutoAdapter.class.getName().equals(adapterClassName)) { + return appInstance.getInjector().getInstance(DbAdapterFactory.class) + .createAdapter(null, makeDataSource(classLoader)); + } + + try { + return appInstance.getInjector().getInstance(AdhocObjectFactory.class) + .newInstance(DbAdapter.class, adapterClassName); + } catch (Throwable th) { + th = Util.unwindException(th); + throw new Exception("DbAdapter load error: " + th.getLocalizedMessage()); + } + } + + /** + * Returns a DataSource that uses connection information from this object. + * Returned DataSource is not pooling its connections. It can be wrapped in + * PoolManager if pooling is needed. + */ + public DataSource makeDataSource(ClassLoadingService classLoader) throws SQLException { + + // validate... + if (getJdbcDriver() == null) { + throw new SQLException("No JDBC driver set."); + } + + if (getUrl() == null) { + throw new SQLException("No DB URL set."); + } + + // load driver... + Driver driver; + + try { + driver = classLoader.loadClass(Driver.class, getJdbcDriver()).newInstance(); + } catch (Throwable th) { + th = Util.unwindException(th); + throw new SQLException("Driver load error: " + th.getLocalizedMessage()); + } + + return new DriverDataSource(driver, getUrl(), getUserName(), getPassword(), NoopJdbcEventLogger.getInstance()); + } + + /** + * Updates another DBConnectionInfo with this object's values. + */ + public boolean copyTo(DBConnectionInfo dataSourceInfo) { + boolean updated = false; + + if (!Util.nullSafeEquals(dataSourceInfo.getUrl(), getUrl())) { + dataSourceInfo.setUrl(getUrl()); + updated = true; + } + + if (!Util.nullSafeEquals(dataSourceInfo.getUserName(), getUserName())) { + dataSourceInfo.setUserName(getUserName()); + updated = true; + } + + if (!Util.nullSafeEquals(dataSourceInfo.getPassword(), getPassword())) { + dataSourceInfo.setPassword(getPassword()); + updated = true; + } + + if (!Util.nullSafeEquals(dataSourceInfo.getJdbcDriver(), getJdbcDriver())) { + dataSourceInfo.setJdbcDriver(getJdbcDriver()); + updated = true; + } + + if (!Util.nullSafeEquals(dataSourceInfo.getDbAdapter(), getDbAdapter())) { + dataSourceInfo.setDbAdapter(getDbAdapter()); + updated = true; + } + + return updated; + } + + /** + * Updates DataSourceInfo with this object's values. + * <p> + * <i>Currently doesn't set the adapter property. Need to change the UI to + * handle adapter via DataSourceInfo first, and then it should be safe to do + * an adapter update here. </i> + * </p> + */ + public boolean copyTo(DataSourceInfo dataSourceInfo) { + boolean updated = false; + + if (!Util.nullSafeEquals(dataSourceInfo.getDataSourceUrl(), getUrl())) { + dataSourceInfo.setDataSourceUrl(getUrl()); + updated = true; + } + + if (!Util.nullSafeEquals(dataSourceInfo.getUserName(), getUserName())) { + dataSourceInfo.setUserName(getUserName()); + updated = true; + } + + if (!Util.nullSafeEquals(dataSourceInfo.getPassword(), getPassword())) { + dataSourceInfo.setPassword(getPassword()); + updated = true; + } + + if (!Util.nullSafeEquals(dataSourceInfo.getJdbcDriver(), getJdbcDriver())) { + dataSourceInfo.setJdbcDriver(getJdbcDriver()); + updated = true; + } + + return updated; + } + + public boolean copyFrom(DataSourceInfo dataSourceInfo) { + boolean updated = false; + + if (!Util.nullSafeEquals(dataSourceInfo.getDataSourceUrl(), getUrl())) { + setUrl(dataSourceInfo.getDataSourceUrl()); + updated = true; + } + + if (!Util.nullSafeEquals(dataSourceInfo.getUserName(), getUserName())) { + setUserName(dataSourceInfo.getUserName()); + updated = true; + } + + if (!Util.nullSafeEquals(dataSourceInfo.getPassword(), getPassword())) { + setPassword(dataSourceInfo.getPassword()); + updated = true; + } + + if (!Util.nullSafeEquals(dataSourceInfo.getJdbcDriver(), getJdbcDriver())) { + setJdbcDriver(dataSourceInfo.getJdbcDriver()); + updated = true; + } + + return updated; + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cayenne/blob/12e83f21/plugins/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbGeneratorMojo.java ---------------------------------------------------------------------- diff --git a/plugins/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbGeneratorMojo.java b/plugins/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbGeneratorMojo.java index e15c65e..e61aa95 100644 --- a/plugins/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbGeneratorMojo.java +++ b/plugins/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbGeneratorMojo.java @@ -165,7 +165,7 @@ public class DbGeneratorMojo extends AbstractMojo { // load driver taking custom CLASSPATH into account... DriverDataSource dataSource = new DriverDataSource((Driver) Class.forName(driver).newInstance(), url, - username, password); + username, password, NoopJdbcEventLogger.getInstance()); generator.runGenerator(dataSource); } catch (Exception ex) {
