(no commit message)
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Tue, 28 Mar 2006 12:56:38 +0000 (12:56 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Tue, 28 Mar 2006 12:56:38 +0000 (12:56 +0000)
support/resources/test/org/wamblee/general/beanRefContext.xml [new file with mode: 0644]
support/resources/test/org/wamblee/general/spring1.xml [new file with mode: 0644]
support/src/org/wamblee/general/BeanKernel.java
support/src/org/wamblee/general/Pair.java
support/src/org/wamblee/general/SpringBeanFactory.java
support/test/org/wamblee/general/SpringBeanFactoryTest.java [new file with mode: 0644]

diff --git a/support/resources/test/org/wamblee/general/beanRefContext.xml b/support/resources/test/org/wamblee/general/beanRefContext.xml
new file mode 100644 (file)
index 0000000..a453f3d
--- /dev/null
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
+ <beans> 
+     
+     <bean id="test"
+         class="org.springframework.context.support.ClassPathXmlApplicationContext">
+         <constructor-arg>
+             <list>
+               <value>org/wamblee/general/spring1.xml</value>
+             </list>
+         </constructor-arg>
+     </bean>
+     
+ </beans>
\ 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 (file)
index 0000000..52ec0be
--- /dev/null
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
+
+<beans>
+  
+    <bean id="java.lang.String"
+          class="java.lang.String">
+        <constructor-arg><value>hello</value></constructor-arg>
+    </bean>  
+  
+</beans> 
\ No newline at end of file
index d140de3d48a8f911fee5c9599d3bbf3e8047ecd0..5f32f68756fd5a0ceaa4f8d83eeb34a7329bcbd5 100644 (file)
@@ -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();
index fd8d494d0392fb5e42cc765fa86fdc8b19cf9cf5..a6bc544d6e55f3b5c9cbcc4b3912592f67aaead8 100644 (file)
@@ -1,3 +1,6 @@
+
+
+
 /*
  * Copyright 2005 the original author or authors.
  * 
index eda621102c2dbfccbc950b5bcc523b4358d11059..91ac1c3f28fbb85e49bc6c9979572092266e0508 100644 (file)
@@ -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> T find(Class<T> 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> T find(String aId, Class<T> 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 (file)
index 0000000..c5f2c4b
--- /dev/null
@@ -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);
+    }
+}