KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > 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.woody.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.cocoon.components.LifecycleHelper;
33
34 /**
35  * A very simple ServiceSelector for ThreadSafe services.
36  *
37  * @version $Id: SimpleServiceSelector.java 30932 2004-07-29 17:35:38Z vgritsenko $
38  */

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