1 16 package org.apache.cocoon.woody.util; 17 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 import org.apache.avalon.framework.activity.Disposable; 23 import org.apache.avalon.framework.configuration.Configurable; 24 import org.apache.avalon.framework.configuration.Configuration; 25 import org.apache.avalon.framework.configuration.ConfigurationException; 26 import org.apache.avalon.framework.logger.AbstractLogEnabled; 27 import org.apache.avalon.framework.logger.LogEnabled; 28 import org.apache.avalon.framework.service.ServiceException; 29 import org.apache.avalon.framework.service.ServiceManager; 30 import org.apache.avalon.framework.service.ServiceSelector; 31 import org.apache.avalon.framework.service.Serviceable; 32 import org.apache.cocoon.components.LifecycleHelper; 33 34 39 public class SimpleServiceSelector extends AbstractLogEnabled implements ServiceSelector, Configurable, LogEnabled, 40 Serviceable, Disposable { 41 private final String hintShortHand; 42 private final Class componentClass; 43 private Map components = new HashMap (); 44 private ServiceManager serviceManager; 45 46 public SimpleServiceSelector(String hintShortHand, Class componentClass) { 47 this.hintShortHand = hintShortHand; 48 this.componentClass = componentClass; 49 } 50 51 public void service(ServiceManager serviceManager) throws ServiceException { 52 this.serviceManager = serviceManager; 53 } 54 55 public void configure(Configuration configuration) throws ConfigurationException { 56 Configuration[] componentConfs = configuration.getChildren(hintShortHand); 57 for (int i = 0; i < componentConfs.length; i++) { 58 String name = componentConfs[i].getAttribute("name"); 59 String src = componentConfs[i].getAttribute("src"); 60 61 Class clazz = null; 62 try { 63 clazz = Class.forName(src); 64 } catch (ClassNotFoundException e) { 65 throw new ConfigurationException("Class not found: " + src + ", declared at " + componentConfs[i].getLocation(), e); 66 } 67 68 if (!componentClass.isAssignableFrom(clazz)) 69 throw new ConfigurationException("The class \"" + src + "\" is of an incorrect type, it should implement or exted " + componentClass.getName()); 70 71 Object component = null; 72 try { 73 component = clazz.newInstance(); 74 LifecycleHelper lifecycleHelper = new LifecycleHelper(getLogger(), null, serviceManager, null, componentConfs[i]); 75 lifecycleHelper.setupComponent(component); 76 } catch (Exception e) { 77 throw new ConfigurationException("Error creating " + hintShortHand + " declared at " + componentConfs[i].getLocation(), e); 78 } 79 80 components.put(name, component); 81 } 82 } 83 84 public Object select(Object hint) throws ServiceException { 85 if (!isSelectable(hint)) 86 throw new ServiceException((String )hint, "Non-existing component for this hint"); 87 String stringHint = (String )hint; 88 return components.get(stringHint); 89 } 90 91 public boolean isSelectable(Object hint) { 92 String stringHint = (String )hint; 93 return components.containsKey(stringHint); 94 } 95 96 public void release(Object o) { 97 } 98 99 public void dispose() { 100 Iterator serviceIt = components.values().iterator(); 101 while (serviceIt.hasNext()) { 102 Object service = serviceIt.next(); 103 if (service instanceof Disposable) { 104 try { 105 ((Disposable)service).dispose(); 106 } catch (Exception e) { 107 getLogger().error("Error disposing service " + service, e); 108 } 109 } 110 } 111 } 112 } 113 | Popular Tags |