KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > util > SimpleServiceSelector


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.forms.util;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
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 /**
38  * A very simple ServiceSelector for ThreadSafe services.
39  *
40  * @version $Id: SimpleServiceSelector.java 76175 2004-11-17 21:45:08Z upayavira $
41  */

42 public class SimpleServiceSelector extends AbstractLogEnabled implements ServiceSelector, Configurable, LogEnabled,
43         Serviceable, Disposable, Contextualizable {
44     private final String JavaDoc hintShortHand;
45     private final Class JavaDoc componentClass;
46     private Map JavaDoc components = new HashMap JavaDoc();
47     private ServiceManager serviceManager;
48     private Context context;
49
50     public SimpleServiceSelector(String JavaDoc hintShortHand, Class JavaDoc 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 JavaDoc name = componentConfs[i].getAttribute("name");
67             String JavaDoc src = componentConfs[i].getAttribute("src");
68
69             Class JavaDoc clazz = null;
70             try {
71                 clazz = Class.forName(src);
72             } catch (ClassNotFoundException JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc select(Object JavaDoc hint) throws ServiceException {
97         if (!isSelectable(hint))
98             throw new ServiceException((String JavaDoc)hint, "Non-existing component for this hint");
99         String JavaDoc stringHint = (String JavaDoc)hint;
100         return components.get(stringHint);
101     }
102
103     public boolean isSelectable(Object JavaDoc hint) {
104         String JavaDoc stringHint = (String JavaDoc)hint;
105         return components.containsKey(stringHint);
106     }
107
108     public void release(Object JavaDoc o) {
109     }
110
111     public void dispose() {
112         Iterator JavaDoc serviceIt = components.values().iterator();
113         while (serviceIt.hasNext()) {
114             Object JavaDoc service = serviceIt.next();
115             if (service instanceof Disposable) {
116                 try {
117                     ((Disposable)service).dispose();
118                 } catch (Exception JavaDoc e) {
119                     getLogger().error("Error disposing service " + service, e);
120                 }
121             }
122         }
123     }
124 }
125
Popular Tags