From 9f044681bb504c6e50850af0dba9e7707f61857e Mon Sep 17 00:00:00 2001 From: erik Date: Wed, 14 May 2008 21:11:36 +0000 Subject: [PATCH] SpringComponent now supports adding properties as beans instead of through property configurer. HibernateComponent no longer reads the hibernate properties. Next step is to put in generic hibernate properties through the constructor and to put in application-independent properties (schemaupdate and dialect) through a hibernate config object as a required interface. --- .../system/spring/ConfiguredProperties.java | 30 +++++++++++++++ .../wamblee/system/spring/PropertySetter.java | 2 +- .../system/spring/SpringComponent.java | 25 ++++++++++++ .../spring/component/HibernateComponent.java | 15 +++++--- ...stem.spring.component.hibernate.properties | 16 ++------ ...blee.system.spring.component.hibernate.xml | 7 +--- .../spring/ConfiguredPropertiesTest.java | 38 +++++++++++++++++++ .../wamblee/system/spring/HelloService2.java | 31 +++++++++++++++ .../system/spring/SpringComponentTest.java | 21 ++++++++++ ...g.wamblee.system.springWithProperties2.xml | 13 +++++++ 10 files changed, 173 insertions(+), 25 deletions(-) create mode 100644 trunk/system/spring/src/main/java/org/wamblee/system/spring/ConfiguredProperties.java create mode 100644 trunk/system/spring/src/test/java/org/wamblee/system/spring/ConfiguredPropertiesTest.java create mode 100644 trunk/system/spring/src/test/java/org/wamblee/system/spring/HelloService2.java create mode 100644 trunk/system/spring/src/test/resources/test.org.wamblee.system.springWithProperties2.xml diff --git a/trunk/system/spring/src/main/java/org/wamblee/system/spring/ConfiguredProperties.java b/trunk/system/spring/src/main/java/org/wamblee/system/spring/ConfiguredProperties.java new file mode 100644 index 00000000..7443b57c --- /dev/null +++ b/trunk/system/spring/src/main/java/org/wamblee/system/spring/ConfiguredProperties.java @@ -0,0 +1,30 @@ +/* + * Copyright 2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.system.spring; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +public class ConfiguredProperties extends Properties { + + public ConfiguredProperties(String aProps) throws IOException { + InputStream is = new ByteArrayInputStream(aProps.getBytes()); + load(is); + is.close(); + } +} diff --git a/trunk/system/spring/src/main/java/org/wamblee/system/spring/PropertySetter.java b/trunk/system/spring/src/main/java/org/wamblee/system/spring/PropertySetter.java index a53dab51..f88c4054 100644 --- a/trunk/system/spring/src/main/java/org/wamblee/system/spring/PropertySetter.java +++ b/trunk/system/spring/src/main/java/org/wamblee/system/spring/PropertySetter.java @@ -38,7 +38,7 @@ class PropertySetter extends PropertyPlaceholderConfigurer { setLocation(new StringResource(propFile)); } - private static String createPropertyFile(Properties aProps) { + public static String createPropertyFile(Properties aProps) { StringBuffer buf = new StringBuffer(); for (Object key: aProps.keySet()) { buf.append(key); diff --git a/trunk/system/spring/src/main/java/org/wamblee/system/spring/SpringComponent.java b/trunk/system/spring/src/main/java/org/wamblee/system/spring/SpringComponent.java index 1da30a32..d0abbcd1 100644 --- a/trunk/system/spring/src/main/java/org/wamblee/system/spring/SpringComponent.java +++ b/trunk/system/spring/src/main/java/org/wamblee/system/spring/SpringComponent.java @@ -15,6 +15,7 @@ */ package org.wamblee.system.spring; +import java.util.HashMap; import java.util.Map; import java.util.Properties; @@ -49,6 +50,7 @@ public class SpringComponent extends AbstractComponent { private String[] _configFiles; private Map _provided; private Map _required; + private Map _propertyObjects; /** * Constructs a spring system. @@ -75,6 +77,8 @@ public class SpringComponent extends AbstractComponent { _configFiles = aConfigFiles; _provided = aProvided; _required = aRequired; + _propertyObjects = new HashMap(); + } /** @@ -94,6 +98,14 @@ public class SpringComponent extends AbstractComponent { setProperty((String) key, aProperties.getProperty((String) key)); } } + + public void addProperties(String aBeanname, Properties aProperties) { + _propertyObjects.put(aBeanname, aProperties); + } + + public Properties getProperties(String aBeanname) { + return _propertyObjects.get(aBeanname); + } @Override protected Scope doStart(Scope aExternalScope) { @@ -107,6 +119,7 @@ public class SpringComponent extends AbstractComponent { GenericApplicationContext parentContext = new GenericApplicationContext(); registerRequiredServices(parentContext); + registerPropertyObjects(parentContext); parentContext.refresh(); @@ -164,6 +177,18 @@ public class SpringComponent extends AbstractComponent { aParentContext.registerBeanDefinition(beanName, definition); } } + + private void registerPropertyObjects(GenericApplicationContext aParentContext) { + for (String beanName: _propertyObjects.keySet()) { + ConstructorArgumentValues cargs = new ConstructorArgumentValues(); + cargs.addGenericArgumentValue(PropertySetter.createPropertyFile(_propertyObjects.get(beanName))); + BeanDefinition definition = new RootBeanDefinition( + ConfiguredProperties.class, cargs, + new MutablePropertyValues()); + aParentContext.registerBeanDefinition(beanName, definition); + } + } + @Override protected void doStop(Scope aRuntime) { diff --git a/trunk/system/spring/src/main/java/org/wamblee/system/spring/component/HibernateComponent.java b/trunk/system/spring/src/main/java/org/wamblee/system/spring/component/HibernateComponent.java index 7db715a8..364b250f 100644 --- a/trunk/system/spring/src/main/java/org/wamblee/system/spring/component/HibernateComponent.java +++ b/trunk/system/spring/src/main/java/org/wamblee/system/spring/component/HibernateComponent.java @@ -19,12 +19,11 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Properties; -import java.util.TreeMap; import javax.sql.DataSource; import org.hibernate.SessionFactory; -import org.springframework.core.io.ClassPathResource; +import org.hibernate.dialect.MySQLInnoDBDialect; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.transaction.PlatformTransactionManager; import org.wamblee.persistence.hibernate.HibernateMappingFiles; @@ -36,7 +35,7 @@ import org.wamblee.system.spring.SpringComponent; public class HibernateComponent extends SpringComponent { - private static final String HIBERNATE_PROPERTIES = "properties/org.wamblee.system.spring.component.hibernate.properties"; + private static final String HIBERNATE_PROPS_KEY = "hibernateProperties"; private static final String HIBERNATE_SPRING_CONFIG = "spring/org.wamblee.system.spring.component.hibernate.xml"; public HibernateComponent(String aName) throws IOException { @@ -44,8 +43,14 @@ public class HibernateComponent extends SpringComponent { createProvided(), createRequired()); Properties props = new Properties(); - props.load(new ClassPathResource(HIBERNATE_PROPERTIES).getInputStream()); - addProperties(props); + addProperties(HIBERNATE_PROPS_KEY, props); + + props.put("hibernate.dialect", MySQLInnoDBDialect.class.getName()); + setProperty("hibernate.schemaupdate", "true"); + } + + private Properties getHibernateProperties() { + return getProperties(HIBERNATE_PROPS_KEY); } private static Map createRequired() { diff --git a/trunk/system/spring/src/main/resources/properties/org.wamblee.system.spring.component.hibernate.properties b/trunk/system/spring/src/main/resources/properties/org.wamblee.system.spring.component.hibernate.properties index 47a21e2a..5c38ff63 100644 --- a/trunk/system/spring/src/main/resources/properties/org.wamblee.system.spring.component.hibernate.properties +++ b/trunk/system/spring/src/main/resources/properties/org.wamblee.system.spring.component.hibernate.properties @@ -2,24 +2,14 @@ ################################################################################### # dialect ################################################################################### -hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect - -################################################################################### -# debugging settings: Log4j configuration can provide more detail. -################################################################################### -hibernate.show_sql=false - -############################################################################## -# schema update for test -############################################################################## -hibernate.schemaupdate=true +#hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect ################################################################################### # hibernate cache provider ################################################################################### -hibernate.cache.provider=org.hibernate.cache.EhCacheProvider +#hibernate.cache.provider=org.hibernate.cache.EhCacheProvider ################################################################################### # query cache ################################################################################### -hibernate.cache.use_query_cache=true \ No newline at end of file +#hibernate.cache.use_query_cache=true \ No newline at end of file diff --git a/trunk/system/spring/src/main/resources/spring/org.wamblee.system.spring.component.hibernate.xml b/trunk/system/spring/src/main/resources/spring/org.wamblee.system.spring.component.hibernate.xml index c9209053..73c02b44 100644 --- a/trunk/system/spring/src/main/resources/spring/org.wamblee.system.spring.component.hibernate.xml +++ b/trunk/system/spring/src/main/resources/spring/org.wamblee.system.spring.component.hibernate.xml @@ -10,12 +10,7 @@ - - ${hibernate.dialect} - ${hibernate.cache.provider} - ${hibernate.show_sql} - ${hibernate.cache.use_query_cache} - + ${hibernate.schemaupdate} diff --git a/trunk/system/spring/src/test/java/org/wamblee/system/spring/ConfiguredPropertiesTest.java b/trunk/system/spring/src/test/java/org/wamblee/system/spring/ConfiguredPropertiesTest.java new file mode 100644 index 00000000..00002de1 --- /dev/null +++ b/trunk/system/spring/src/test/java/org/wamblee/system/spring/ConfiguredPropertiesTest.java @@ -0,0 +1,38 @@ +/* + * Copyright 2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.system.spring; + +import java.io.IOException; +import java.util.Properties; + +import junit.framework.TestCase; + +public class ConfiguredPropertiesTest extends TestCase { + + public void testProps() throws IOException { + Properties props = new Properties(); + props.put("x", "y"); + props.put("a", "b"); + + ConfiguredProperties props2 = new ConfiguredProperties( + PropertySetter.createPropertyFile(props)); + + assertEquals(props.size(), props2.size()); + assertEquals(props.get("x"), props2.get("x")); + assertEquals(props.get("a"), props2.get("a")); + } +} + diff --git a/trunk/system/spring/src/test/java/org/wamblee/system/spring/HelloService2.java b/trunk/system/spring/src/test/java/org/wamblee/system/spring/HelloService2.java new file mode 100644 index 00000000..e9c77080 --- /dev/null +++ b/trunk/system/spring/src/test/java/org/wamblee/system/spring/HelloService2.java @@ -0,0 +1,31 @@ +/* + * Copyright 2007 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.system.spring; + +import java.util.Properties; + +public class HelloService2 { + + private Properties _props; + + public HelloService2(Properties aProps) { + _props = aProps; + } + + public Properties getProperties() { + return _props; + } +} diff --git a/trunk/system/spring/src/test/java/org/wamblee/system/spring/SpringComponentTest.java b/trunk/system/spring/src/test/java/org/wamblee/system/spring/SpringComponentTest.java index 170297fa..3e63c7ea 100644 --- a/trunk/system/spring/src/test/java/org/wamblee/system/spring/SpringComponentTest.java +++ b/trunk/system/spring/src/test/java/org/wamblee/system/spring/SpringComponentTest.java @@ -37,6 +37,8 @@ public class SpringComponentTest extends TestCase { private static final String HELLO_SERVICE_SPRING_XML = "test.org.wamblee.system.spring.xml"; private static final String HELLO_SERVICE_SPRING_WITH_REQS_XML = "test.org.wamblee.system.springWithRequirements.xml"; private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML = "test.org.wamblee.system.springWithProperties.xml"; + private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML2 = "test.org.wamblee.system.springWithProperties2.xml"; + private static final String PROPERTY_FILE = "test.org.wamblee.system.spring.properties"; public static EventTracker EVENT_TRACKER; @@ -103,6 +105,25 @@ public class SpringComponentTest extends TestCase { ProvidedInterface[] services = scope.getProvidedInterfaces(); assertEquals("Property Value", scope.getInterfaceImplementation(services[0], HelloService.class).say()); } + + public void testWithPropertiesAsBean() throws IOException { + Map provided = new HashMap(); + provided.put("helloService", new DefaultProvidedInterface("hello", + HelloService2.class)); + SpringComponent system = new SpringComponent("system", + new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML2 }, + provided, new HashMap()); + Properties props = new Properties(); + props.load(new ClassPathResource(PROPERTY_FILE).getInputStream()); + system.addProperties("properties", props); + + Scope scope = system.start(_externalScope); + + ProvidedInterface[] services = scope.getProvidedInterfaces(); + + Properties props2 = scope.getInterfaceImplementation(services[0], HelloService2.class).getProperties(); + assertEquals(props, props2); + } public void testWithMissingRequirement() { try { diff --git a/trunk/system/spring/src/test/resources/test.org.wamblee.system.springWithProperties2.xml b/trunk/system/spring/src/test/resources/test.org.wamblee.system.springWithProperties2.xml new file mode 100644 index 00000000..1546261f --- /dev/null +++ b/trunk/system/spring/src/test/resources/test.org.wamblee.system.springWithProperties2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + -- 2.31.1