KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > language > generator > GeneratorSelector


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39
40 /**
41  * This interface is the common base of all Compiled Components. This
42  * includes Sitemaps and XSP Pages
43  *
44  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
45  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
46  * @version CVS $Id: GeneratorSelector.java 123818 2004-12-31 19:34:30Z antonio $
47  */

48 public class GeneratorSelector extends ExcaliburComponentSelector implements Disposable {
49
50     public final static String JavaDoc ROLE = "org.apache.cocoon.components.language.generator.ServerPages";
51
52     private ClassLoaderManager classManager;
53
54     /** The component manager */
55     protected ComponentManager manager;
56
57     private LogkitLoggerManager logKitManager;
58
59     protected Context context;
60
61     protected RoleManager roles;
62
63     protected Map JavaDoc componentHandlers = new HashMap JavaDoc();
64
65     /** Dynamic component handlers mapping. */
66     private Map JavaDoc componentMapping = new HashMap JavaDoc();
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     /**
80      * Configure the LogKitManager
81      */

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     /**
90      * Configure the LoggerManager.
91      */

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 JavaDoc) this.m_context.get(Constants.CONTEXT_WORK_DIR));
111         } catch (Exception JavaDoc e) {
112             throw new ComponentException(ROLE, "Could not add repository to ClassLoaderManager", e);
113         }
114     }
115
116     public Component select(Object JavaDoc 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 JavaDoc 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 JavaDoc e) {
140                 getLogger().error("Error trying to release component", e);
141             }
142         }
143     }
144
145     public void addGenerator(ComponentManager newManager,
146                                 Object JavaDoc hint, Program generator)
147             throws Exception JavaDoc {
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 JavaDoc e) {
158             // Error will be logged by caller. This is for debug only
159
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 JavaDoc 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 JavaDoc keys = this.componentHandlers.keySet().iterator();
182             List JavaDoc keyList = new ArrayList JavaDoc();
183
184             while(keys.hasNext()) {
185                 Object JavaDoc 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