d05eef3dd6b507c8706a5d2dd865b3de4edc06c6
[utils] / support / general / src / main / java / org / wamblee / general / BeanKernel.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.general;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.util.Properties;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.wamblee.io.ClassPathResource;
25 import org.wamblee.io.InputResource;
26
27 /**
28  * The standard means to obtain the bean factory. This works by reading a
29  * property {@value #BEAN_FACTORY_CLASS} from a property file named
30  * {@value #BEAN_KERNEL_PROP_FILE} from the class path. This property identifies
31  * the bean factory implementation to use. The configured bean factory must have
32  * a no-arg constructor.
33  */
34 public final class BeanKernel {
35
36     private static final Log LOG = LogFactory.getLog(BeanKernel.class);
37
38     /**
39      * Bean factory kernel properties file.
40      */
41     private static final String BEAN_KERNEL_PROP_FILE = "org.wamblee.beanfactory.properties";
42
43     /**
44      * Name of the property to define the name of the bean factory class to use.
45      * THis class must have a public default constructor.
46      */
47     private static final String BEAN_FACTORY_CLASS = "org.wamblee.beanfactory.class";
48
49     /**
50      * Cached bean factory.
51      */
52     private static BeanFactory BEAN_FACTORY;
53
54     /**
55      * Disabled constructor.
56      * 
57      */
58     private BeanKernel() {
59         // Empty
60     }
61
62     /**
63      * Overrides the default mechanism for looking up the bean factory by
64      * specifying it yourself.
65      * 
66      * @param aOverride
67      *            Override bean factory.
68      */
69     public static void overrideBeanFactory(BeanFactory aOverride) {
70         BEAN_FACTORY = aOverride;
71     }
72
73     /**
74      * Gets the bean factory.
75      * 
76      * @return Bean factory.
77      */
78     public static BeanFactory getBeanFactory() {
79         synchronized (BeanFactory.class) {
80             if (BEAN_FACTORY == null) {
81                 BEAN_FACTORY = lookupBeanFactory(BEAN_KERNEL_PROP_FILE);
82             }
83         }
84         return BEAN_FACTORY;
85     }
86
87     /**
88      * Lookup the bean factory based on the properties file.
89      * 
90      * @return Bean factory.
91      */
92     static BeanFactory lookupBeanFactory(String aPropertyFilename) {
93         InputResource resource = new ClassPathResource(aPropertyFilename);
94         InputStream is;
95         try {
96             is = resource.getInputStream();
97         } catch (IOException e) {
98             throw new BeanFactoryException("Cannot open resource " + resource,
99                     e);
100         }
101         try {
102             Properties props = new Properties();
103             props.load(is);
104             String className = props.getProperty(BEAN_FACTORY_CLASS);
105             Class beanFactory = Class.forName(className);
106             return (BeanFactory) beanFactory.newInstance();
107         } catch (Exception e) {
108             throw new BeanFactoryException("Cannot read from resource "
109                     + resource, e);
110         } finally {
111             try {
112                 is.close();
113             } catch (IOException e) {
114                 // last resort cannot do much now.
115                 LOG.error("Error closing resource " + resource);
116             }
117         }
118     }
119 }