KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > i18n > DefaultBundleFactory


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.i18n;
9
10 import java.util.Locale JavaDoc;
11 import java.io.FileNotFoundException JavaDoc;
12
13 import org.apache.avalon.framework.logger.LogEnabled;
14 import org.apache.avalon.framework.logger.AbstractLogEnabled;
15 import org.apache.avalon.framework.configuration.Configurable;
16 import org.apache.avalon.framework.configuration.Configuration;
17 import org.apache.avalon.framework.configuration.ConfigurationException;
18 import org.apache.avalon.framework.component.Composable;
19 import org.apache.avalon.framework.component.ComponentManager;
20 import org.apache.avalon.framework.component.ComponentSelector;
21 import org.apache.avalon.framework.component.Component;
22 import org.apache.avalon.framework.activity.Initializable;
23
24 /**
25  * Used to map locale information to URI space, to find the relevant bundle.
26  *
27  * @author <a HREF="mailto:neeme@apache.org">Neeme Praks</a>
28  * @version CVS $Revision: 1.1 $ $Date: 2002/01/02 19:04:56 $ $Author: neeme $
29  */

30
31 public class DefaultBundleFactory extends AbstractLogEnabled implements BundleFactory, Configurable, Composable, Initializable {
32
33     public static final class ConfigurationKeys {
34         public static final String JavaDoc BUNDLE = "bundle";
35         public static final String JavaDoc MAPPER = "mapper";
36         public static final String JavaDoc BUNDLE_CONF = "bundle-conf";
37     }
38
39     /** FQCN of the returned bundles */
40     private String JavaDoc bundleClassName = null;
41
42     /** Bundle configuration */
43     private Configuration bundleConf = null;
44
45     /** Mapper lookup hint **/
46     private String JavaDoc mapperHint = null;
47
48     /** Component Manager */
49     protected ComponentManager manager = null;
50
51     public void compose(ComponentManager manager) {
52         this.manager = manager;
53     }
54
55     /**
56      * Configure the component.
57      *
58      * @param configuration the configuration
59      */

60     public void configure(Configuration configuration) throws ConfigurationException {
61         if (bundleClassName == null) bundleClassName = configuration.getAttribute(ConfigurationKeys.BUNDLE);
62         if (mapperHint == null) mapperHint = configuration.getAttribute(ConfigurationKeys.MAPPER);
63         if (bundleConf == null) bundleConf = configuration.getChild(ConfigurationKeys.BUNDLE_CONF);
64
65         if (getLogger().isDebugEnabled()) {
66             getLogger().debug(
67                 "BundleLoader configured with bundle=" + bundleClassName +
68                 ", mapper=" + mapperHint +
69             "");
70         }
71     }
72
73     public void initialize() throws Exception JavaDoc {
74         // TODO: implement the preloading of bundles
75
}
76
77     /**
78      * Load a bundle, based on bundleInfo.
79      *
80      * @return the bundle
81      */

82     public Bundle createInstance(BundleInfo bi) {
83         Bundle bundle = null;
84         Bundle parentBundle = null;
85         BundleInfo parentBundleInfo = bi.getParent();
86         ComponentSelector mapperSelector = null;
87         BundleInfoMapper mapper = null;
88         ComponentSelector bundleSelector = null;
89         try {
90             mapperSelector = (ComponentSelector) manager.lookup(BundleInfoMapper.ROLE + "Selector");
91             mapper = (BundleInfoMapper) mapperSelector.select(mapperHint);
92             bundleSelector = (ComponentSelector) manager.lookup(Bundle.ROLE + "Selector");
93             if (getLogger().isDebugEnabled()) getLogger().debug("Loading bundle: " + bi);
94             if (parentBundleInfo != null)
95                 parentBundle = (Bundle) bundleSelector.select(parentBundleInfo);
96             bundle = getBundleInstance();
97             if (bundle instanceof LogEnabled) ((LogEnabled)bundle).enableLogging(getLogger());
98             if (bundle instanceof Configurable) ((Configurable)bundle).configure(bundleConf);
99             bundle.setBundleInfo(bi);
100             bundle.setMapper(mapper);
101             bundle.setParent(parentBundle);
102             if (bundle instanceof Composable) ((Composable)bundle).compose(this.manager);
103             bundle.setLastModified(System.currentTimeMillis());
104             if (bundle instanceof Initializable) ((Initializable)bundle).initialize();
105         }
106         catch (FileNotFoundException JavaDoc e) {
107             getLogger().warn("File not found while loading bundle: " + bi);
108             bundle = null;
109         }
110         catch (Exception JavaDoc e) {
111             getLogger().error("Error while loading bundle: " + bi, e);
112             bundle = null;
113         }
114         finally {
115             if (mapperSelector != null) {
116                 if (mapper != null) mapperSelector.release((Component) mapper);
117                 manager.release(mapperSelector);
118             }
119             if (bundleSelector != null) manager.release(bundleSelector);
120         }
121         return bundle;
122     }
123
124     private Bundle getBundleInstance() {
125         try {
126             return (Bundle) Thread.currentThread().getContextClassLoader().loadClass(bundleClassName).newInstance();
127         } catch (InstantiationException JavaDoc e) {
128             getLogger().error("Could not create bundle instance!", e);
129         } catch (IllegalAccessException JavaDoc e) {
130             getLogger().error("Could not create bundle instance!", e);
131         } catch (ClassNotFoundException JavaDoc e) {
132             getLogger().error("Could not create bundle instance!", e);
133         }
134         return null;
135     }
136 }
137
Popular Tags