d140de3d48a8f911fee5c9599d3bbf3e8047ecd0
[utils] / support / src / 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.
29  */
30 public final class BeanKernel {
31
32     private static final Log LOG = LogFactory.getLog(BeanKernel.class);
33
34     /**
35      * Bean factory kernel properties file.
36      */
37     private static final String BEAN_KERNEL_PROP_FILE = "org.wamblee.beanfactory.properties";
38
39     /**
40      * Name of the property to define the name of the bean factory class to use.
41      * THis class must have a public default constructor.
42      */
43     private static final String BEAN_FACTORY_CLASS = "org.wamblee.beanfactory.class";
44
45     /**
46      * Cached bean factory.
47      */
48     private static BeanFactory BEAN_FACTORY;
49     
50     /**
51      * Disabled constructor.
52      *
53      */
54     private BeanKernel() { 
55         // Empty
56     }
57
58     /**
59      * Overrides the default mechanism for looking up the bean factory by
60      * specifying it yourself.
61      * 
62      * @param aOverride
63      *            Override bean factory.
64      */
65     public static void overrideBeanFactory(BeanFactory aOverride) {
66         BEAN_FACTORY = aOverride;
67     }
68
69     /**
70      * Gets the bean factory. 
71      * @return Bean factory. 
72      */
73     public static BeanFactory getBeanFactory() {
74         synchronized (BeanFactory.class) {
75             if (BEAN_FACTORY == null) {
76                 BEAN_FACTORY = lookupBeanFactory();
77             }
78         }
79         return BEAN_FACTORY;
80     }
81
82     /**
83      * Lookup the bean factory based on the properties file.
84      * 
85      * @return Bean factory.
86      */
87     private static BeanFactory lookupBeanFactory() {
88         InputResource resource = new ClassPathResource(BEAN_KERNEL_PROP_FILE);
89         InputStream is;
90         try {
91             is = resource.getInputStream();
92         } catch (IOException e) {
93             throw new BeanFactoryException("Cannot open resource " + resource,
94                     e);
95         }
96         try {
97             Properties props = new Properties();
98             props.load(resource.getInputStream());
99             String className = props.getProperty(BEAN_FACTORY_CLASS);
100             Class beanFactory = Class.forName(className);
101             return (BeanFactory) beanFactory.newInstance();
102         } catch (Exception e) {
103             throw new BeanFactoryException("Cannot read from resource "
104                     + resource, e);
105         } finally {
106             try {
107                 is.close();
108             } catch (IOException e) {
109                 // last resort cannot do much now.
110                 LOG.error("Error closing resource " + resource);
111             }
112         }
113     }
114 }