SpringComponent now supports adding properties as beans instead of
authorErik Brakkee <erik@brakkee.org>
Wed, 14 May 2008 21:11:36 +0000 (21:11 +0000)
committerErik Brakkee <erik@brakkee.org>
Wed, 14 May 2008 21:11:36 +0000 (21:11 +0000)
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/src/main/java/org/wamblee/system/spring/ConfiguredProperties.java [new file with mode: 0644]
system/spring/src/main/java/org/wamblee/system/spring/PropertySetter.java
system/spring/src/main/java/org/wamblee/system/spring/SpringComponent.java
system/spring/src/main/java/org/wamblee/system/spring/component/HibernateComponent.java
system/spring/src/main/resources/properties/org.wamblee.system.spring.component.hibernate.properties
system/spring/src/main/resources/spring/org.wamblee.system.spring.component.hibernate.xml
system/spring/src/test/java/org/wamblee/system/spring/ConfiguredPropertiesTest.java [new file with mode: 0644]
system/spring/src/test/java/org/wamblee/system/spring/HelloService2.java [new file with mode: 0644]
system/spring/src/test/java/org/wamblee/system/spring/SpringComponentTest.java
system/spring/src/test/resources/test.org.wamblee.system.springWithProperties2.xml [new file with mode: 0644]

diff --git a/system/spring/src/main/java/org/wamblee/system/spring/ConfiguredProperties.java b/system/spring/src/main/java/org/wamblee/system/spring/ConfiguredProperties.java
new file mode 100644 (file)
index 0000000..7443b57
--- /dev/null
@@ -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();
+    }
+}
index a53dab516e6be01a8b89ae808bbd30f5db5dd4c7..f88c40541f45204cfede596f497742c136fca110 100644 (file)
@@ -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);
index 1da30a324ed53c4c9812306669e2adc287b5b6a8..d0abbcd173b8f20152744cac6b4b35df1cd94f53 100644 (file)
@@ -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<Scope> {
        private String[] _configFiles;
        private Map<String, ProvidedInterface> _provided;
        private Map<RequiredInterface, String> _required;
+       private Map<String, Properties> _propertyObjects;
 
        /**
         * Constructs a spring system.
@@ -75,6 +77,8 @@ public class SpringComponent extends AbstractComponent<Scope> {
                _configFiles = aConfigFiles;
                _provided = aProvided;
                _required = aRequired;
+               _propertyObjects = new HashMap<String, Properties>(); 
+               
        }
 
        /**
@@ -94,6 +98,14 @@ public class SpringComponent extends AbstractComponent<Scope> {
                        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<Scope> {
                        GenericApplicationContext parentContext = new GenericApplicationContext();
 
                        registerRequiredServices(parentContext);
+                       registerPropertyObjects(parentContext);
 
                        parentContext.refresh();
                        
@@ -164,6 +177,18 @@ public class SpringComponent extends AbstractComponent<Scope> {
                        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) {
index 7db715a8b156c39993ca1c048bbcd71e43c7850e..364b250f1b82b9b30fce55cadd3320fe2e320842 100644 (file)
@@ -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<RequiredInterface, String> createRequired() {
index 47a21e2a0468d0b76b28d87b08f564c5bd4ed540..5c38ff635c001e272ec13ed04cde811cf131e444 100644 (file)
@@ -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
index c92090539ee40b1f12d5cd880bf6c5f2aa076817..73c02b4433fc0f895f32be8edef9ebe535e92639 100644 (file)
             <ref bean="dataSource"/>
         </property>
         <property name="hibernateProperties">
-            <props>
-                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
-                <prop key="hibernate.cache.provider_class">${hibernate.cache.provider}</prop>
-                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
-                <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
-            </props>
+            <ref bean="hibernateProperties"/>
         </property>
         <property name="schemaUpdate">
             <value>${hibernate.schemaupdate}</value>
diff --git a/system/spring/src/test/java/org/wamblee/system/spring/ConfiguredPropertiesTest.java b/system/spring/src/test/java/org/wamblee/system/spring/ConfiguredPropertiesTest.java
new file mode 100644 (file)
index 0000000..00002de
--- /dev/null
@@ -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/system/spring/src/test/java/org/wamblee/system/spring/HelloService2.java b/system/spring/src/test/java/org/wamblee/system/spring/HelloService2.java
new file mode 100644 (file)
index 0000000..e9c7708
--- /dev/null
@@ -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; 
+    }
+}
index 170297fa2c3f5ec0b3ce297e9920c979f6e5d7b3..3e63c7eaba4ee1b18b5399d9c3a7dca40bda7df4 100644 (file)
@@ -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<String> 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<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
+        provided.put("helloService", new DefaultProvidedInterface("hello",
+                HelloService2.class));
+        SpringComponent system = new SpringComponent("system",
+                new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML2 },
+                provided, new HashMap<RequiredInterface, String>());
+        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/system/spring/src/test/resources/test.org.wamblee.system.springWithProperties2.xml b/system/spring/src/test/resources/test.org.wamblee.system.springWithProperties2.xml
new file mode 100644 (file)
index 0000000..1546261
--- /dev/null
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://www.springframework.org/schema/beans
+    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
+
+    <bean id="helloService" class="org.wamblee.system.spring.HelloService2">
+        <constructor-arg>
+            <ref bean="properties"/>
+        </constructor-arg>
+    </bean>
+
+</beans>