From 53f6ff39d6e9f16d2a2d2fe7958013bad4c89172 Mon Sep 17 00:00:00 2001 From: erik Date: Tue, 28 Mar 2006 12:56:38 +0000 Subject: [PATCH] --- .../org/wamblee/general/beanRefContext.xml | 15 +++++ .../test/org/wamblee/general/spring1.xml | 11 ++++ .../src/org/wamblee/general/BeanKernel.java | 21 ++++--- support/src/org/wamblee/general/Pair.java | 3 + .../wamblee/general/SpringBeanFactory.java | 55 ++++++++++++------- .../general/SpringBeanFactoryTest.java | 47 ++++++++++++++++ 6 files changed, 123 insertions(+), 29 deletions(-) create mode 100644 support/resources/test/org/wamblee/general/beanRefContext.xml create mode 100644 support/resources/test/org/wamblee/general/spring1.xml create mode 100644 support/test/org/wamblee/general/SpringBeanFactoryTest.java diff --git a/support/resources/test/org/wamblee/general/beanRefContext.xml b/support/resources/test/org/wamblee/general/beanRefContext.xml new file mode 100644 index 00000000..a453f3de --- /dev/null +++ b/support/resources/test/org/wamblee/general/beanRefContext.xml @@ -0,0 +1,15 @@ + + + + + + + + + org/wamblee/general/spring1.xml + + + + + \ No newline at end of file diff --git a/support/resources/test/org/wamblee/general/spring1.xml b/support/resources/test/org/wamblee/general/spring1.xml new file mode 100644 index 00000000..52ec0bec --- /dev/null +++ b/support/resources/test/org/wamblee/general/spring1.xml @@ -0,0 +1,11 @@ + + + + + + + hello + + + \ No newline at end of file diff --git a/support/src/org/wamblee/general/BeanKernel.java b/support/src/org/wamblee/general/BeanKernel.java index d140de3d..5f32f687 100644 --- a/support/src/org/wamblee/general/BeanKernel.java +++ b/support/src/org/wamblee/general/BeanKernel.java @@ -25,7 +25,11 @@ import org.wamblee.io.ClassPathResource; import org.wamblee.io.InputResource; /** - * The standard means to obtain the bean factory. + * The standard means to obtain the bean factory. This works by reading a + * property {@value #BEAN_FACTORY_CLASS} from a property file named + * {@value #BEAN_KERNEL_PROP_FILE} from the class path. This property identifies + * the bean factory implementation to use. The configured bean factory must have + * a no-arg constructor. */ public final class BeanKernel { @@ -46,12 +50,12 @@ public final class BeanKernel { * Cached bean factory. */ private static BeanFactory BEAN_FACTORY; - + /** * Disabled constructor. - * + * */ - private BeanKernel() { + private BeanKernel() { // Empty } @@ -67,8 +71,9 @@ public final class BeanKernel { } /** - * Gets the bean factory. - * @return Bean factory. + * Gets the bean factory. + * + * @return Bean factory. */ public static BeanFactory getBeanFactory() { synchronized (BeanFactory.class) { @@ -84,7 +89,7 @@ public final class BeanKernel { * * @return Bean factory. */ - private static BeanFactory lookupBeanFactory() { + static BeanFactory lookupBeanFactory() { InputResource resource = new ClassPathResource(BEAN_KERNEL_PROP_FILE); InputStream is; try { @@ -95,7 +100,7 @@ public final class BeanKernel { } try { Properties props = new Properties(); - props.load(resource.getInputStream()); + props.load(is); String className = props.getProperty(BEAN_FACTORY_CLASS); Class beanFactory = Class.forName(className); return (BeanFactory) beanFactory.newInstance(); diff --git a/support/src/org/wamblee/general/Pair.java b/support/src/org/wamblee/general/Pair.java index fd8d494d..a6bc544d 100644 --- a/support/src/org/wamblee/general/Pair.java +++ b/support/src/org/wamblee/general/Pair.java @@ -1,3 +1,6 @@ + + + /* * Copyright 2005 the original author or authors. * diff --git a/support/src/org/wamblee/general/SpringBeanFactory.java b/support/src/org/wamblee/general/SpringBeanFactory.java index eda62110..91ac1c3f 100644 --- a/support/src/org/wamblee/general/SpringBeanFactory.java +++ b/support/src/org/wamblee/general/SpringBeanFactory.java @@ -12,7 +12,7 @@ * 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.general; import org.springframework.beans.BeansException; @@ -21,46 +21,59 @@ import org.springframework.beans.factory.access.BeanFactoryReference; import org.springframework.context.access.ContextSingletonBeanFactoryLocator; /** - * Bean factory which uses Spring. + * Bean factory which uses Spring. This bean factory cannot be configured + * directly in the {@link org.wamblee.general.BeanKernel} because it does not + * provide a default no-arg constructor. Therefore, it must be delegated to or + * it must tbe subclassed to provide a default constructor. */ public class SpringBeanFactory implements BeanFactory { - - private String _factoryName; - + + private BeanFactoryReference _factoryReference; + /** - * Constructs the bean factory. - * @param aFactoryName Spring bean factory to use. + * Constructs the bean factory. + * + * @param aSelector + * Selector to find the appropriate bean ref context. + * @param aFactoryName + * Spring bean factory to use. */ - public SpringBeanFactory(String aFactoryName) { - _factoryName = aFactoryName; + public SpringBeanFactory(String aSelector, String aFactoryName) { + BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator + .getInstance(aSelector); + _factoryReference = locator.useBeanFactory(aFactoryName); } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see org.wamblee.general.BeanFactory#find(java.lang.String) */ public Object find(String aId) { - return find(aId, Object.class); + return find(aId, Object.class); } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see org.wamblee.general.BeanFactory#find(java.lang.Class) */ public T find(Class aClass) { return find(aClass.getName(), aClass); } - /* (non-Javadoc) - * @see org.wamblee.general.BeanFactory#find(java.lang.String, java.lang.Class) + /* + * (non-Javadoc) + * + * @see org.wamblee.general.BeanFactory#find(java.lang.String, + * java.lang.Class) */ public T find(String aId, Class aClass) { - BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator.getInstance(); - BeanFactoryReference beanFactory = locator.useBeanFactory(_factoryName); - try { - Object obj = beanFactory.getFactory().getBean(aId, aClass); - assert obj != null; - return aClass.cast(obj); - } catch (BeansException e) { + Object obj = _factoryReference.getFactory().getBean(aId, aClass); + assert obj != null; + return aClass.cast(obj); + } catch (BeansException e) { throw new BeanFactoryException(e.getMessage(), e); } } diff --git a/support/test/org/wamblee/general/SpringBeanFactoryTest.java b/support/test/org/wamblee/general/SpringBeanFactoryTest.java new file mode 100644 index 00000000..c5f2c4b1 --- /dev/null +++ b/support/test/org/wamblee/general/SpringBeanFactoryTest.java @@ -0,0 +1,47 @@ +/* + * Copyright 2005 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.general; + +import org.springframework.beans.factory.access.SingletonBeanFactoryLocator; + +import junit.framework.TestCase; + +/** + * Tests the spring bean factory. + */ +public class SpringBeanFactoryTest extends TestCase { + + /* (non-Javadoc) + * @see junit.framework.TestCase#setUp() + */ + @Override + protected void setUp() throws Exception { + super.setUp(); + + } + + public void testExistingBeanRefContext() { + SpringBeanFactory factory = new SpringBeanFactory("org/wamblee/general/beanRefContext.xml", "test"); + + String value1 = factory.find(String.class); + assertEquals("hello", value1); + String value2 = (String)factory.find("java.lang.String"); + assertEquals("hello", value2); + String value3 = factory.find("java.lang.String", String.class); + assertEquals("hello", value3); + } +} -- 2.31.1