KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > modules > input > InputModuleHelper


1 /*
2  * Copyright 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.modules.input;
17
18 import org.apache.avalon.framework.CascadingRuntimeException;
19 import org.apache.avalon.framework.component.ComponentManager;
20 import org.apache.avalon.framework.component.ComponentSelector;
21 import org.apache.avalon.framework.configuration.Configuration;
22 import org.apache.avalon.framework.service.ServiceManager;
23 import org.apache.avalon.framework.service.ServiceSelector;
24
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28
29 /**
30  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
31  * @version CVS $Id: InputModuleHelper.java 30932 2004-07-29 17:35:38Z vgritsenko $
32  */

33 public class InputModuleHelper {
34     
35     // TODO consolidate code with AbstractMetaModule to use this class as delegate
36

37     protected final static String JavaDoc INPUT_MODULE_SELECTOR = InputModule.ROLE+"Selector";
38
39     /* Operation codes */
40     private final static int OP_GET = 0;
41     private final static int OP_VALUES = 1;
42     private final static int OP_NAMES = 2;
43
44     
45     private Map JavaDoc inputModules;
46     private ComponentManager componentManager;
47     private ComponentSelector componentInputSelector;
48     private ServiceManager serviceManager;
49     private ServiceSelector serviceInputSelector;
50     
51     /**
52      * Get the input module
53      */

54     private InputModule getInputModule(String JavaDoc name)
55     throws CascadingRuntimeException {
56         if ( this.inputModules == null ) {
57             throw new RuntimeException JavaDoc("ModuleHelper is not setup correctly.");
58         }
59         InputModule module = (InputModule) this.inputModules.get(name);
60         if ( module == null ) {
61             try {
62                 if ( this.componentManager != null ) {
63                     if (this.componentInputSelector.hasComponent(name)) {
64                         module = (InputModule) this.componentInputSelector.select(name);
65                     }
66                 } else {
67                     if (this.serviceInputSelector.isSelectable(name)) {
68                         module = (InputModule) this.serviceInputSelector.select(name);
69                     }
70                 }
71             } catch (Exception JavaDoc e) {
72                 throw new CascadingRuntimeException("Unable to lookup input module " + name, e);
73             }
74             if ( module == null ) {
75                 throw new RuntimeException JavaDoc("No such InputModule: "+name);
76             }
77             this.inputModules.put(name, module);
78         }
79         return module;
80     }
81     
82     /**
83      * Capsules use of an InputModule. Does all the lookups and so
84      * on. Returns either an Object, an Object[], or an Iterator,
85      * depending on the method called i.e. the op specified. The
86      * second module is preferred and has an non null name. If an
87      * exception is encountered, a warn message is printed and null is
88      * returned.
89      * @param op an <code>int</code> value encoding the desired operation
90      * @param name a <code>String</code> value holding the name of the
91      * InputModule
92      * @param attr a <code>String</code> value holding the name of the
93      * attribute to return. Is disregarded when attribute names is
94      * requested.
95      * @param objectModel a <code>Map</code> value holding the current
96      * ObjectModel
97      * @return an <code>Object</code> value
98      * @exception CascadingRuntimeException if an error occurs. The real
99      * exception can be obtained with <code>getCause</code>.
100      */

101     private Object JavaDoc get(int op, String JavaDoc name, String JavaDoc attr, Map JavaDoc objectModel, Configuration conf) throws CascadingRuntimeException {
102
103         Object JavaDoc value = null;
104         final InputModule input = this.getInputModule(name);
105
106         try {
107
108             switch (op) {
109             case OP_GET:
110                 value = input.getAttribute(attr, conf, objectModel);
111                 break;
112             case OP_VALUES:
113                 value = input.getAttributeValues(attr, conf, objectModel);
114                 break;
115             case OP_NAMES:
116                 value = input.getAttributeNames(conf, objectModel);
117                 break;
118             }
119
120         } catch (Exception JavaDoc e) {
121             throw new CascadingRuntimeException("Error accessing attribute '"+attr+"' from input module '"+name+"'. "+e.getMessage(), e);
122         }
123
124         return value;
125     }
126
127     private Object JavaDoc get(int op, String JavaDoc name, String JavaDoc attr, Map JavaDoc objectModel) throws RuntimeException JavaDoc {
128         return get(op, name, attr, objectModel, null);
129     }
130
131
132
133     /**
134      * Initializes the instance for first use. Stores references to
135      * component manager and component selector in instance
136      *
137      * @param manager a <code>ComponentManager</code> value
138      * @exception RuntimeException if an error occurs
139      * @deprecated Use the {@link #setup(ServiceManager)} method instead
140      */

141     public void setup(ComponentManager manager) throws RuntimeException JavaDoc {
142
143         this.inputModules = new HashMap JavaDoc();
144         this.componentManager = manager;
145         try {
146             this.componentInputSelector = (ComponentSelector) this.componentManager.lookup(INPUT_MODULE_SELECTOR);
147         } catch (Exception JavaDoc e) {
148             throw new CascadingRuntimeException("Could not obtain selector for InputModule.",e);
149         }
150     }
151
152     /**
153      * Initializes the instance for first use. Stores references to
154      * service manager and service selector in instance
155      *
156      * @param manager a <code>ServiceManager</code> value
157      * @exception RuntimeException if an error occurs
158      */

159     public void setup(ServiceManager manager) throws RuntimeException JavaDoc {
160
161         this.inputModules = new HashMap JavaDoc();
162         this.serviceManager = manager;
163         try {
164             this.serviceInputSelector = (ServiceSelector) this.serviceManager.lookup(INPUT_MODULE_SELECTOR);
165         } catch (Exception JavaDoc e) {
166             throw new CascadingRuntimeException("Could not obtain selector for InputModule.",e);
167         }
168     }
169
170
171     /**
172      * Get a single attribute value from a module. Uses cached
173      * reference if existing.
174      *
175      * @param objectModel a <code>Map</code> value
176      * @param conf a <code>Configuration</code> containing the module dynamic configuration (aka modeConf)
177      * @param module a <code>String</code> value holding the module name
178      * @param name a <code>String</code> value holding the attribute name
179      * @param deflt an <code>Object</code> value holding a default value
180      * @return an <code>Object</code> value
181      * @exception RuntimeException if an error occurs
182      */

183     public Object JavaDoc getAttribute(Map JavaDoc objectModel, Configuration conf, String JavaDoc module, String JavaDoc name, Object JavaDoc deflt) throws RuntimeException JavaDoc {
184
185         Object JavaDoc result = this.get(OP_GET, module, name, objectModel, conf);
186         if (result == null) result = deflt;
187         return result;
188     }
189
190     /**
191      * Get a single attribute value from a module. Same as {@link
192      * #getAttribute(Map, Configuration, String, String, Object)} with a
193      * <code>null</code> configuration.
194      */

195     public Object JavaDoc getAttribute(Map JavaDoc objectModel, String JavaDoc module, String JavaDoc name, Object JavaDoc deflt) throws RuntimeException JavaDoc {
196         return getAttribute(objectModel, null, module, name, deflt);
197     }
198
199
200     /**
201      * Get an array of values from a module. Uses cached reference if
202      * existing.
203      *
204      * @param objectModel a <code>Map</code> value
205      * @param conf a <code>Configuration</code> containing the module dynamic configuration (aka modeConf)
206      * @param module a <code>String</code> value holding the module name
207      * @param name a <code>String</code> value holding the attribute name
208      * @param deflt an <code>Object[]</code> value holding a default value
209      * @return an <code>Object[]</code> value
210      * @exception RuntimeException if an error occurs
211      */

212     public Object JavaDoc[] getAttributeValues(Map JavaDoc objectModel, Configuration conf, String JavaDoc module, String JavaDoc name, Object JavaDoc[] deflt) throws RuntimeException JavaDoc {
213
214         Object JavaDoc[] result = (Object JavaDoc[]) this.get(OP_VALUES, module, name, objectModel, conf);
215         if (result == null) result = deflt;
216         return result;
217     }
218
219     /**
220      * Get an array of values from a module. Same as
221      * {@link #getAttributeValues(Map, Configuration, String, String, Object[])}
222      * with a <code>null</code> configuration.
223      */

224     public Object JavaDoc[] getAttributeValues(Map JavaDoc objectModel, String JavaDoc module, String JavaDoc name, Object JavaDoc[] deflt) throws RuntimeException JavaDoc {
225         return getAttributeValues(objectModel, null, module, name, deflt);
226     }
227
228
229     /**
230      * Get an iterator to a collection of attribute names from a
231      * module.
232      *
233      * @param objectModel a <code>Map</code> value
234      * @param module the module's name
235      * @return an <code>Iterator</code> value
236      * @exception RuntimeException if an error occurs
237      */

238     public Iterator JavaDoc getAttributeNames(Map JavaDoc objectModel, Configuration conf, String JavaDoc module) throws RuntimeException JavaDoc {
239
240         return (Iterator JavaDoc) this.get(OP_NAMES, module, null, objectModel);
241     }
242
243     /** Get an iterator to a collection of attribute names from a module. Same
244      * as {@link #getAttributeNames(Map, Configuration, String)} with a
245      * <code>null</code> configuration.
246      */

247     public Iterator JavaDoc getAttributeNames(Map JavaDoc objectModel, String JavaDoc module) throws RuntimeException JavaDoc {
248         return getAttributeNames(objectModel, (Configuration)null, module);
249     }
250
251
252
253     /**
254      * Releases all obtained module references.
255      *
256      * @exception RuntimeException if an error occurs
257      */

258     public void releaseAll() throws RuntimeException JavaDoc {
259
260         if ( this.inputModules != null ) {
261             // test for component manager
262
if ( this.componentManager != null ) {
263                 try {
264                     Iterator JavaDoc iter = this.inputModules.keySet().iterator();
265                     while (iter.hasNext()) {
266                         this.componentInputSelector.release((InputModule) this.inputModules.get(iter.next()));
267                     }
268                     this.inputModules = null;
269                     this.componentManager.release(this.componentInputSelector);
270                     this.componentManager = null;
271                     this.inputModules = null;
272                 } catch (Exception JavaDoc e) {
273                     throw new CascadingRuntimeException("Could not release InputModules.",e);
274                 }
275                 
276             }
277             if ( this.serviceManager != null ) {
278                 try {
279                     Iterator JavaDoc iter = this.inputModules.keySet().iterator();
280                     while (iter.hasNext()) {
281                         this.serviceInputSelector.release(this.inputModules.get(iter.next()));
282                     }
283                     this.inputModules = null;
284                     this.serviceManager.release(this.serviceInputSelector);
285                     this.serviceManager = null;
286                     this.inputModules = null;
287                 } catch (Exception JavaDoc e) {
288                     throw new CascadingRuntimeException("Could not release InputModules.",e);
289                 }
290                 
291             }
292         }
293     }
294     
295 }
296
Popular Tags