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.
--- /dev/null
+/*
+ * 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();
+ }
+}
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);
*/
package org.wamblee.system.spring;
+import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
private String[] _configFiles;
private Map<String, ProvidedInterface> _provided;
private Map<RequiredInterface, String> _required;
+ private Map<String, Properties> _propertyObjects;
/**
* Constructs a spring system.
_configFiles = aConfigFiles;
_provided = aProvided;
_required = aRequired;
+ _propertyObjects = new HashMap<String, Properties>();
+
}
/**
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) {
GenericApplicationContext parentContext = new GenericApplicationContext();
registerRequiredServices(parentContext);
+ registerPropertyObjects(parentContext);
parentContext.refresh();
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) {
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;
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 {
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() {
###################################################################################
# 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
<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>
--- /dev/null
+/*
+ * 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"));
+ }
+}
+
--- /dev/null
+/*
+ * 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;
+ }
+}
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;
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 {
--- /dev/null
+<?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>