2 * Copyright 2005 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.general;
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
21 import org.wamblee.io.ClassPathResource;
22 import org.wamblee.io.InputResource;
24 import java.io.IOException;
25 import java.io.InputStream;
27 import java.util.Properties;
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
37 public final class BeanKernel {
41 private static final Log LOG = LogFactory.getLog(BeanKernel.class);
44 * Bean factory kernel properties file.
46 private static final String BEAN_KERNEL_PROP_FILE = "org.wamblee.beanfactory.properties";
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.
52 private static final String BEAN_FACTORY_CLASS = "org.wamblee.beanfactory.class";
55 * Cached bean factory.
57 private static BeanFactory BEAN_FACTORY;
60 * Disabled constructor.
63 private BeanKernel() {
68 * Overrides the default mechanism for looking up the bean factory
69 * by specifying it yourself.
71 * @param aOverride Override bean factory.
73 public static void overrideBeanFactory(BeanFactory aOverride) {
74 BEAN_FACTORY = aOverride;
78 * Gets the bean factory.
80 * @return Bean factory.
82 public static BeanFactory getBeanFactory() {
83 synchronized (BeanFactory.class) {
84 if (BEAN_FACTORY == null) {
85 BEAN_FACTORY = lookupBeanFactory(BEAN_KERNEL_PROP_FILE);
93 * Lookup the bean factory based on the properties file.
95 * @param aPropertyFilename DOCUMENT ME!
97 * @return Bean factory.
99 * @throws BeanFactoryException DOCUMENT ME!
101 static BeanFactory lookupBeanFactory(String aPropertyFilename) {
102 InputResource resource = new ClassPathResource(aPropertyFilename);
106 is = resource.getInputStream();
107 } catch (IOException e) {
108 throw new BeanFactoryException("Cannot open resource " + resource, e);
112 Properties props = new Properties();
115 String className = props.getProperty(BEAN_FACTORY_CLASS);
116 Class beanFactory = Class.forName(className);
118 return (BeanFactory) beanFactory.newInstance();
119 } catch (Exception e) {
120 throw new BeanFactoryException("Cannot read from resource "
125 } catch (IOException e) {
126 // last resort cannot do much now.
127 LOG.error("Error closing resource " + resource);