1 16 package org.apache.cocoon.forms.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.avalon.framework.context.Contextualizable; 33 import org.apache.avalon.framework.context.Context; 34 import org.apache.avalon.framework.context.ContextException; 35 import org.apache.cocoon.components.LifecycleHelper; 36 37 42 public class SimpleServiceSelector extends AbstractLogEnabled implements ServiceSelector, Configurable, LogEnabled, 43 Serviceable, Disposable, Contextualizable { 44 private final String hintShortHand; 45 private final Class componentClass; 46 private Map components = new HashMap (); 47 private ServiceManager serviceManager; 48 private Context context; 49 50 public SimpleServiceSelector(String hintShortHand, Class componentClass) { 51 this.hintShortHand = hintShortHand; 52 this.componentClass = componentClass; 53 } 54 55 public void service(ServiceManager serviceManager) throws ServiceException { 56 this.serviceManager = serviceManager; 57 } 58 59 public void contextualize(Context context) throws ContextException { 60 this.context = context; 61 } 62 63 public void configure(Configuration configuration) throws ConfigurationException { 64 Configuration[] componentConfs = configuration.getChildren(hintShortHand); 65 for (int i = 0; i < componentConfs.length; i++) { 66 String name = componentConfs[i].getAttribute("name"); 67 String src = componentConfs[i].getAttribute("src"); 68 69 Class clazz = null; 70 try { 71 clazz = Class.forName(src); 72 } catch (ClassNotFoundException e) { 73 throw new ConfigurationException("Class not found: " + src + ", declared at " + componentConfs[i].getLocation(), e); 74 } 75 76 if (!componentClass.isAssignableFrom(clazz)) 77 throw new ConfigurationException("The class \"" + src + "\" is of an incorrect type, it should implement or extend " + componentClass.getName()); 78 79 Object component = null; 80 try { 81 component = clazz.newInstance(); 82 LifecycleHelper.setupComponent( 83 component, 84 getLogger(), 85 context, 86 serviceManager, 87 componentConfs[i]); 88 } catch (Exception e) { 89 throw new ConfigurationException("Error creating " + hintShortHand + " declared at " + componentConfs[i].getLocation(), e); 90 } 91 92 components.put(name, component); 93 } 94 } 95 96 public Object select(Object hint) throws ServiceException { 97 if (!isSelectable(hint)) 98 throw new ServiceException((String )hint, "Non-existing component for this hint"); 99 String stringHint = (String )hint; 100 return components.get(stringHint); 101 } 102 103 public boolean isSelectable(Object hint) { 104 String stringHint = (String )hint; 105 return components.containsKey(stringHint); 106 } 107 108 public void release(Object o) { 109 } 110 111 public void dispose() { 112 Iterator serviceIt = components.values().iterator(); 113 while (serviceIt.hasNext()) { 114 Object service = serviceIt.next(); 115 if (service instanceof Disposable) { 116 try { 117 ((Disposable)service).dispose(); 118 } catch (Exception e) { 119 getLogger().error("Error disposing service " + service, e); 120 } 121 } 122 } 123 } 124 } 125 | Popular Tags |