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 java.io.IOException;
19 import java.io.InputStream;
20 import java.util.Properties;
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;
28 * The standard means to obtain the bean factory.
30 public class BeanKernel {
32 private static final Log LOG = LogFactory.getLog(BeanKernel.class);
35 * Bean factory kernel properties file.
37 private static final String BEAN_KERNEL_PROP_FILE = "org.wamblee.beanfactory.properties";
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.
43 private static final String BEAN_FACTORY_CLASS = "org.wamblee.beanfactory.class";
46 * Cached bean factory.
48 private static BeanFactory BEAN_FACTORY;
51 * Overrides the default mechanism for looking up the bean factory by
52 * specifying it yourself.
55 * Override bean factory.
57 public static void overrideBeanFactory(BeanFactory aOverride) {
58 BEAN_FACTORY = aOverride;
62 * Gets the bean factory.
63 * @return Bean factory.
65 public static BeanFactory getBeanFactory() {
66 synchronized (BeanFactory.class) {
67 if (BEAN_FACTORY == null) {
68 BEAN_FACTORY = lookupBeanFactory();
75 * Lookup the bean factory based on the properties file.
77 * @return Bean factory.
79 private static BeanFactory lookupBeanFactory() {
80 InputResource resource = new ClassPathResource(BEAN_KERNEL_PROP_FILE);
83 is = resource.getInputStream();
84 } catch (IOException e) {
85 throw new BeanFactoryException("Cannot open resource " + resource,
89 Properties props = new Properties();
90 props.load(resource.getInputStream());
91 String className = props.getProperty(BEAN_FACTORY_CLASS);
92 Class beanFactory = Class.forName(className);
93 return (BeanFactory) beanFactory.newInstance();
94 } catch (Exception e) {
95 throw new BeanFactoryException("Cannot read from resource "
100 } catch (IOException e) {
101 // last resort cannot do much now.
102 LOG.error("Error closing resource " + resource);