1 8 package org.apache.avalon.excalibur.i18n; 9 10 import java.util.Locale ; 11 import java.io.FileNotFoundException ; 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 30 31 public class DefaultBundleFactory extends AbstractLogEnabled implements BundleFactory, Configurable, Composable, Initializable { 32 33 public static final class ConfigurationKeys { 34 public static final String BUNDLE = "bundle"; 35 public static final String MAPPER = "mapper"; 36 public static final String BUNDLE_CONF = "bundle-conf"; 37 } 38 39 40 private String bundleClassName = null; 41 42 43 private Configuration bundleConf = null; 44 45 46 private String mapperHint = null; 47 48 49 protected ComponentManager manager = null; 50 51 public void compose(ComponentManager manager) { 52 this.manager = manager; 53 } 54 55 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 { 74 } 76 77 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 e) { 107 getLogger().warn("File not found while loading bundle: " + bi); 108 bundle = null; 109 } 110 catch (Exception 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 e) { 128 getLogger().error("Could not create bundle instance!", e); 129 } catch (IllegalAccessException e) { 130 getLogger().error("Could not create bundle instance!", e); 131 } catch (ClassNotFoundException e) { 132 getLogger().error("Could not create bundle instance!", e); 133 } 134 return null; 135 } 136 } 137 | Popular Tags |