From 060a34e4f4bd6cc6dace77f94eacd57295e76c70 Mon Sep 17 00:00:00 2001 From: erik Date: Thu, 15 May 2008 10:17:53 +0000 Subject: [PATCH] ORMappingConfig is now provided by the datasource component. ExternalDatasourceComponent reads config from property file. Next step is that it requires config from a separate Properties object which is provided by either a property component, or a DatabaseComponent (e.g. Derby). --- .../encryption/Md5HexMessageDigester.java | 2 +- .../hibernate/AuthorizationComponent.java | 2 + .../UserAdministrationComponent.java | 2 + .../ExternalDatasourceComponent.java | 67 ++++++++++++++----- ...t.org.wamblee.security.database.properties | 3 + 5 files changed, 57 insertions(+), 19 deletions(-) diff --git a/trunk/security/src/main/java/org/wamblee/security/encryption/Md5HexMessageDigester.java b/trunk/security/src/main/java/org/wamblee/security/encryption/Md5HexMessageDigester.java index 180a9a3a..161b652e 100644 --- a/trunk/security/src/main/java/org/wamblee/security/encryption/Md5HexMessageDigester.java +++ b/trunk/security/src/main/java/org/wamblee/security/encryption/Md5HexMessageDigester.java @@ -45,7 +45,7 @@ public class Md5HexMessageDigester implements MessageDigester { try { MessageDigest digest = MessageDigest.getInstance("MD5"); byte[] result = digest.digest(aValue.getBytes()); - char[] charResult = new Hex().encodeHex(result); + char[] charResult = Hex.encodeHex(result); return new String(charResult); } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException("MD5 not supported????"); diff --git a/trunk/security/src/main/java/org/wamblee/usermgt/hibernate/AuthorizationComponent.java b/trunk/security/src/main/java/org/wamblee/usermgt/hibernate/AuthorizationComponent.java index 0093dc8d..339a0f1a 100644 --- a/trunk/security/src/main/java/org/wamblee/usermgt/hibernate/AuthorizationComponent.java +++ b/trunk/security/src/main/java/org/wamblee/usermgt/hibernate/AuthorizationComponent.java @@ -31,6 +31,7 @@ import org.wamblee.system.core.DefaultRequiredInterface; import org.wamblee.system.core.ProvidedInterface; import org.wamblee.system.core.Scope; import org.wamblee.system.spring.component.HibernateComponent; +import org.wamblee.system.spring.component.ORMappingConfig; import org.wamblee.usermgt.UserAccessor; import org.wamblee.usermgt.UserAdministration; import org.wamblee.usermgt.UserGroupRepositoryComponent; @@ -59,6 +60,7 @@ public class AuthorizationComponent extends DefaultContainer { addRequiredInterface(new DefaultRequiredInterface("datasource", DataSource.class)); addRequiredInterface(new DefaultRequiredInterface("userAccessor", UserAccessor.class)); + addRequiredInterface(new DefaultRequiredInterface("ormconfig", ORMappingConfig.class)); if (aExposeInternals) { addProvidedInterface(TRANSACTION_MGR); diff --git a/trunk/security/src/main/java/org/wamblee/usermgt/hibernate/UserAdministrationComponent.java b/trunk/security/src/main/java/org/wamblee/usermgt/hibernate/UserAdministrationComponent.java index 6ac0ee94..dd0fb98e 100644 --- a/trunk/security/src/main/java/org/wamblee/usermgt/hibernate/UserAdministrationComponent.java +++ b/trunk/security/src/main/java/org/wamblee/usermgt/hibernate/UserAdministrationComponent.java @@ -29,6 +29,7 @@ import org.wamblee.system.core.DefaultRequiredInterface; import org.wamblee.system.core.ProvidedInterface; import org.wamblee.system.core.Scope; import org.wamblee.system.spring.component.HibernateComponent; +import org.wamblee.system.spring.component.ORMappingConfig; import org.wamblee.usermgt.UserAdministration; import org.wamblee.usermgt.UserGroupRepositoryComponent; @@ -60,6 +61,7 @@ public class UserAdministrationComponent extends DefaultContainer { addRequiredInterface(new DefaultRequiredInterface("datasource", DataSource.class)); + addRequiredInterface(new DefaultRequiredInterface("ormconfig", ORMappingConfig.class)); if (aExposeInternals) { addProvidedInterface(TRANSACTION_MGR); diff --git a/trunk/security/src/test/java/org/wamblee/usermgt/hibernate/ExternalDatasourceComponent.java b/trunk/security/src/test/java/org/wamblee/usermgt/hibernate/ExternalDatasourceComponent.java index 673aaceb..55a2537c 100644 --- a/trunk/security/src/test/java/org/wamblee/usermgt/hibernate/ExternalDatasourceComponent.java +++ b/trunk/security/src/test/java/org/wamblee/usermgt/hibernate/ExternalDatasourceComponent.java @@ -23,35 +23,66 @@ import java.util.TreeMap; import javax.sql.DataSource; import org.springframework.core.io.ClassPathResource; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.wamblee.system.core.AbstractComponent; import org.wamblee.system.core.DefaultProvidedInterface; import org.wamblee.system.core.ProvidedInterface; import org.wamblee.system.core.RequiredInterface; +import org.wamblee.system.core.Scope; import org.wamblee.system.spring.SpringComponent; +import org.wamblee.system.spring.component.ORMappingConfig; +import org.wamblee.system.spring.component.ORMappingConfig.DatabaseType; -public class ExternalDatasourceComponent extends SpringComponent { +public class ExternalDatasourceComponent extends AbstractComponent { private static final String DATABASE_PROPERTIES = "properties/test.org.wamblee.security.database.properties"; - private static final String DATASOURCE_SPRING_CONFIG = "spring/test.org.wamblee.security.datasource.xml"; + + private static ProvidedInterface DATASOURCE = new DefaultProvidedInterface("datasource", + DataSource.class); + private static ProvidedInterface ORM_CONFIG = new DefaultProvidedInterface("ormconfig", + ORMappingConfig.class); + private Properties _dbProps; + public ExternalDatasourceComponent(String aName) throws IOException { - super( - aName, - new String[] { DATASOURCE_SPRING_CONFIG }, - createProvidedMap(), new TreeMap()); - Properties props = new Properties(); - props.load(new ClassPathResource(DATABASE_PROPERTIES).getInputStream()); - - for (Object key: props.keySet()) { - System.out.println("Key " + key + " value " + props.getProperty((String)key)); - } - addProperties(props); + this(aName, loadProperties()); } - private static Map createProvidedMap() { - Map provided = new TreeMap(); - provided.put("dataSource", new DefaultProvidedInterface("datasource", - DataSource.class)); - return provided; + private static Properties loadProperties() throws IOException { + Properties dbProps = new Properties(); + dbProps.load(new ClassPathResource(DATABASE_PROPERTIES) + .getInputStream()); + return dbProps; } + + public ExternalDatasourceComponent(String aName, Properties aProps) throws IOException { + super(aName); + _dbProps = aProps; + + addProvidedInterface(DATASOURCE); + addProvidedInterface(ORM_CONFIG); + } + + @Override + protected DataSource doStart(Scope aScope) { + DriverManagerDataSource ds = new DriverManagerDataSource( + _dbProps.getProperty("database.driver"), + _dbProps.getProperty("database.url"), + _dbProps.getProperty("database.username"), + _dbProps.getProperty("database.password")); + addInterface(DATASOURCE, ds, aScope); + DatabaseType type = DatabaseType.valueOf(_dbProps.getProperty("database.type")); + + ORMappingConfig config = new ORMappingConfig(true, type); + + addInterface(ORM_CONFIG, config, aScope); + + return ds; + } + + @Override + protected void doStop(DataSource aRuntime) { + // Empty. + } } diff --git a/trunk/security/src/test/resources/properties/test.org.wamblee.security.database.properties b/trunk/security/src/test/resources/properties/test.org.wamblee.security.database.properties index 4c2ddd98..bff2ee47 100644 --- a/trunk/security/src/test/resources/properties/test.org.wamblee.security.database.properties +++ b/trunk/security/src/test/resources/properties/test.org.wamblee.security.database.properties @@ -2,6 +2,9 @@ # Database properties for test runs in a J2SE environment. +# See ORMappingConfig.DatabaseType for allowed values +database.type=MYSQL_INNO_DB + database.driver=com.mysql.jdbc.Driver database.url=jdbc:mysql://localhost:3306/test database.username=erik -- 2.31.1