source code formatting.
[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 org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20
21 import org.wamblee.io.ClassPathResource;
22 import org.wamblee.io.InputResource;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26
27 import java.util.Properties;
28
29
30 /**
31  * The standard means to obtain the bean factory. This works by reading a
32  * property {@value #BEAN_FACTORY_CLASS} from a property file named {@value
33  * #BEAN_KERNEL_PROP_FILE} from the class path. This property identifies the
34  * bean factory implementation to use. The configured bean factory must have a
35  * no-arg constructor.
36  */
37 public final class BeanKernel {
38     /**
39      * DOCUMENT ME!
40      */
41     private static final Log LOG = LogFactory.getLog(BeanKernel.class);
42
43     /**
44      * Bean factory kernel properties file.
45      */
46     private static final String BEAN_KERNEL_PROP_FILE = "org.wamblee.beanfactory.properties";
47
48     /**
49      * Name of the property to define the name of the bean factory
50      * class to use. THis class must have a public default constructor.
51      */
52     private static final String BEAN_FACTORY_CLASS = "org.wamblee.beanfactory.class";
53
54     /**
55      * Cached bean factory.
56      */
57     private static BeanFactory BEAN_FACTORY;
58
59 /**
60      * Disabled constructor.
61      *
62      */
63     private BeanKernel() {
64         // Empty
65     }
66
67     /**
68      * Overrides the default mechanism for looking up the bean factory
69      * by specifying it yourself.
70      *
71      * @param aOverride Override bean factory.
72      */
73     public static void overrideBeanFactory(BeanFactory aOverride) {
74         BEAN_FACTORY = aOverride;
75     }
76
77     /**
78      * Gets the bean factory.
79      *
80      * @return Bean factory.
81      */
82     public static BeanFactory getBeanFactory() {
83         synchronized (BeanFactory.class) {
84             if (BEAN_FACTORY == null) {
85                 BEAN_FACTORY = lookupBeanFactory(BEAN_KERNEL_PROP_FILE);
86             }
87         }
88
89         return BEAN_FACTORY;
90     }
91
92     /**
93      * Lookup the bean factory based on the properties file.
94      *
95      * @param aPropertyFilename DOCUMENT ME!
96      *
97      * @return Bean factory.
98      *
99      * @throws BeanFactoryException DOCUMENT ME!
100      */
101     static BeanFactory lookupBeanFactory(String aPropertyFilename) {
102         InputResource resource = new ClassPathResource(aPropertyFilename);
103         InputStream   is;
104
105         try {
106             is                 = resource.getInputStream();
107         } catch (IOException e) {
108             throw new BeanFactoryException("Cannot open resource " + resource, e);
109         }
110
111         try {
112             Properties props = new Properties();
113             props.load(is);
114
115             String className   = props.getProperty(BEAN_FACTORY_CLASS);
116             Class  beanFactory = Class.forName(className);
117
118             return (BeanFactory) beanFactory.newInstance();
119         } catch (Exception e) {
120             throw new BeanFactoryException("Cannot read from resource "
121                 + resource, e);
122         } finally {
123             try {
124                 is.close();
125             } catch (IOException e) {
126                 // last resort cannot do much now.
127                 LOG.error("Error closing resource " + resource);
128             }
129         }
130     }
131 }