2 * Copyright 2005-2010 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.ioc;
18 import org.wamblee.io.ClassPathResource;
19 import org.wamblee.io.InputResource;
21 import java.io.IOException;
22 import java.io.InputStream;
24 import java.util.Properties;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
29 * The standard means to obtain the bean factory. This works by reading a
30 * property {@value #BEAN_FACTORY_CLASS} from a property file named
31 * {@value #BEAN_KERNEL_PROP_FILE} from the class path. This property identifies
32 * the bean factory implementation to use. The configured bean factory must have
33 * a no-arg constructor.
35 public final class BeanKernel {
36 private static final Logger LOG = Logger.getLogger(BeanKernel.class
40 * Bean factory kernel properties file.
42 private static final String BEAN_KERNEL_PROP_FILE = "org.wamblee.beanfactory.properties";
45 * Name of the property to define the name of the bean factory class to use.
46 * THis class must have a public default constructor.
48 private static final String BEAN_FACTORY_CLASS = "org.wamblee.beanfactory.class";
51 * Cached bean factory.
53 private static BeanFactory BEAN_FACTORY;
56 * Disabled constructor.
59 private BeanKernel() {
64 * Overrides the default mechanism for looking up the bean factory by
65 * specifying it yourself.
68 * Override bean factory.
70 public static void overrideBeanFactory(BeanFactory aOverride) {
71 BEAN_FACTORY = aOverride;
75 * Gets the bean factory.
77 * @return Bean factory.
79 public static BeanFactory getBeanFactory() {
80 synchronized (BeanFactory.class) {
81 if (BEAN_FACTORY == null) {
82 BEAN_FACTORY = lookupBeanFactory(BEAN_KERNEL_PROP_FILE);
90 * Lookup the bean factory based on the properties file.
93 * @return Bean factory.
96 static BeanFactory lookupBeanFactory(String aPropertyFilename) {
97 InputResource resource = new ClassPathResource(aPropertyFilename);
101 is = resource.getInputStream();
102 } catch (IOException e) {
103 throw new BeanFactoryException("Cannot open resource " + resource,
108 Properties props = new Properties();
111 String className = props.getProperty(BEAN_FACTORY_CLASS);
112 Class beanFactory = Class.forName(className);
114 return (BeanFactory) beanFactory.newInstance();
115 } catch (Exception e) {
116 throw new BeanFactoryException("Cannot read from resource " +
121 } catch (IOException e) {
122 // last resort cannot do much now.
123 LOG.log(Level.WARNING, "Error closing resource " + resource, e);