1 16 package org.apache.cocoon.components.language.generator; 17 18 import org.apache.avalon.excalibur.component.ComponentHandler; 19 import org.apache.avalon.excalibur.component.ExcaliburComponentSelector; 20 import org.apache.avalon.excalibur.component.LogkitLoggerManager; 21 import org.apache.avalon.excalibur.component.RoleManager; 22 import org.apache.avalon.excalibur.logger.LogKitManager; 23 import org.apache.avalon.excalibur.logger.LoggerManager; 24 import org.apache.avalon.framework.activity.Disposable; 25 import org.apache.avalon.framework.component.Component; 26 import org.apache.avalon.framework.component.ComponentException; 27 import org.apache.avalon.framework.component.ComponentManager; 28 import org.apache.avalon.framework.context.Context; 29 import org.apache.cocoon.Constants; 30 import org.apache.cocoon.components.classloader.ClassLoaderManager; 31 import org.apache.cocoon.components.language.programming.Program; 32 33 import java.io.File ; 34 import java.util.ArrayList ; 35 import java.util.HashMap ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 import java.util.Map ; 39 40 48 public class GeneratorSelector extends ExcaliburComponentSelector implements Disposable { 49 50 public final static String ROLE = "org.apache.cocoon.components.language.generator.ServerPages"; 51 52 private ClassLoaderManager classManager; 53 54 55 protected ComponentManager manager; 56 57 private LogkitLoggerManager logKitManager; 58 59 protected Context context; 60 61 protected RoleManager roles; 62 63 protected Map componentHandlers = new HashMap (); 64 65 66 private Map componentMapping = new HashMap (); 67 68 69 public void contextualize(Context context) { 70 super.contextualize(context); 71 this.context = context; 72 } 73 74 public void setRoleManager(RoleManager roleMgr) { 75 super.setRoleManager(roleMgr); 76 this.roles = roleMgr; 77 } 78 79 82 public void setLogKitManager( final LogKitManager logkit ) { 83 super.setLogKitManager(logkit); 84 if( null == this.logKitManager ) { 85 this.logKitManager = new LogkitLoggerManager( null, logkit ); 86 } 87 } 88 89 92 public void setLoggerManager( final LoggerManager logkit ) { 93 super.setLoggerManager(logkit); 94 if( null == this.logKitManager ) { 95 this.logKitManager = new LogkitLoggerManager( logkit, null ); 96 } 97 } 98 99 public void compose (ComponentManager manager) throws ComponentException { 100 super.compose(manager); 101 this.manager = manager; 102 103 try { 104 this.classManager = (ClassLoaderManager) manager.lookup(ClassLoaderManager.ROLE); 105 } catch (ComponentException cme) { 106 throw new ComponentException(ClassLoaderManager.ROLE, "GeneratorSelector", cme); 107 } 108 109 try { 110 this.classManager.addDirectory((File ) this.m_context.get(Constants.CONTEXT_WORK_DIR)); 111 } catch (Exception e) { 112 throw new ComponentException(ROLE, "Could not add repository to ClassLoaderManager", e); 113 } 114 } 115 116 public Component select(Object hint) throws ComponentException { 117 118 ComponentHandler handler = (ComponentHandler) this.componentHandlers.get(hint); 119 if (handler == null) { 120 throw new ComponentException(ROLE, "Could not find component for hint: " + hint); 121 } 122 123 try { 124 Component component = handler.get(); 125 componentMapping.put(component, handler); 126 return component; 127 } catch (Exception ce) { 128 if (getLogger().isDebugEnabled()) 129 getLogger().debug("Could not access component for hint: " + hint, ce); 130 throw new ComponentException(ROLE, "Could not access component for hint: " + hint, ce); 131 } 132 } 133 134 public void release(Component component) { 135 ComponentHandler handler = (ComponentHandler)componentMapping.remove(component); 136 if (handler != null) { 137 try { 138 handler.put(component); 139 } catch (Exception e) { 140 getLogger().error("Error trying to release component", e); 141 } 142 } 143 } 144 145 public void addGenerator(ComponentManager newManager, 146 Object hint, Program generator) 147 throws Exception { 148 try { 149 final ComponentHandler handler = 150 generator.getHandler(newManager, this.context, this.roles, this.logKitManager); 151 handler.enableLogging(getLogger()); 152 handler.initialize(); 153 this.componentHandlers.put(hint, handler); 154 if (getLogger().isDebugEnabled()) { 155 getLogger().debug("Adding " + generator.getName() + " for " + hint); 156 } 157 } catch(final Exception e) { 158 if (getLogger().isDebugEnabled()) { 160 getLogger().debug("Could not set up Component for hint: " + hint, e); 161 } 162 throw e; 163 } 164 } 165 166 public void removeGenerator(Object hint) { 167 ComponentHandler handler = (ComponentHandler) this.componentHandlers.remove(hint); 168 if (handler != null) { 169 handler.dispose(); 170 this.classManager.reinstantiate(); 171 if (getLogger().isDebugEnabled()) { 172 getLogger().debug("Removing " + handler.getClass().getName() + " for " + hint.toString()); 173 } 174 } 175 } 176 177 public void dispose() { 178 this.manager.release(this.classManager); 179 180 synchronized(this) { 181 Iterator keys = this.componentHandlers.keySet().iterator(); 182 List keyList = new ArrayList (); 183 184 while(keys.hasNext()) { 185 Object key = keys.next(); 186 ComponentHandler handler = 187 (ComponentHandler)this.componentHandlers.get(key); 188 189 handler.dispose(); 190 191 keyList.add(key); 192 } 193 194 keys = keyList.iterator(); 195 196 while(keys.hasNext()) { 197 this.componentHandlers.remove(keys.next()); 198 } 199 200 keyList.clear(); 201 } 202 203 super.dispose(); 204 } 205 } 206 | Popular Tags |