001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.woody.util;
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022:
023: import org.apache.avalon.framework.activity.Disposable;
024: import org.apache.avalon.framework.configuration.Configurable;
025: import org.apache.avalon.framework.configuration.Configuration;
026: import org.apache.avalon.framework.configuration.ConfigurationException;
027: import org.apache.avalon.framework.logger.AbstractLogEnabled;
028: import org.apache.avalon.framework.logger.LogEnabled;
029: import org.apache.avalon.framework.service.ServiceException;
030: import org.apache.avalon.framework.service.ServiceManager;
031: import org.apache.avalon.framework.service.ServiceSelector;
032: import org.apache.avalon.framework.service.Serviceable;
033: import org.apache.cocoon.components.LifecycleHelper;
034:
035: /**
036: * A very simple ServiceSelector for ThreadSafe services.
037: *
038: * @version $Id: SimpleServiceSelector.java 433543 2006-08-22 06:22:54Z crossley $
039: */
040: public class SimpleServiceSelector extends AbstractLogEnabled implements
041: ServiceSelector, Configurable, LogEnabled, Serviceable,
042: Disposable {
043: private final String hintShortHand;
044: private final Class componentClass;
045: private Map components = new HashMap();
046: private ServiceManager serviceManager;
047:
048: public SimpleServiceSelector(String hintShortHand,
049: Class componentClass) {
050: this .hintShortHand = hintShortHand;
051: this .componentClass = componentClass;
052: }
053:
054: public void service(ServiceManager serviceManager)
055: throws ServiceException {
056: this .serviceManager = serviceManager;
057: }
058:
059: public void configure(Configuration configuration)
060: throws ConfigurationException {
061: Configuration[] componentConfs = configuration
062: .getChildren(hintShortHand);
063: for (int i = 0; i < componentConfs.length; i++) {
064: String name = componentConfs[i].getAttribute("name");
065: String src = componentConfs[i].getAttribute("src");
066:
067: Class clazz = null;
068: try {
069: clazz = Class.forName(src);
070: } catch (ClassNotFoundException e) {
071: throw new ConfigurationException("Class not found: "
072: + src + ", declared at "
073: + componentConfs[i].getLocation(), e);
074: }
075:
076: if (!componentClass.isAssignableFrom(clazz))
077: throw new ConfigurationException(
078: "The class \""
079: + src
080: + "\" is of an incorrect type, it should implement or exted "
081: + componentClass.getName());
082:
083: Object component = null;
084: try {
085: component = clazz.newInstance();
086: LifecycleHelper lifecycleHelper = new LifecycleHelper(
087: getLogger(), null, serviceManager, null,
088: componentConfs[i]);
089: lifecycleHelper.setupComponent(component);
090: } catch (Exception e) {
091: throw new ConfigurationException("Error creating "
092: + hintShortHand + " declared at "
093: + componentConfs[i].getLocation(), e);
094: }
095:
096: components.put(name, component);
097: }
098: }
099:
100: public Object select(Object hint) throws ServiceException {
101: if (!isSelectable(hint))
102: throw new ServiceException((String) hint,
103: "Non-existing component for this hint");
104: String stringHint = (String) hint;
105: return components.get(stringHint);
106: }
107:
108: public boolean isSelectable(Object hint) {
109: String stringHint = (String) hint;
110: return components.containsKey(stringHint);
111: }
112:
113: public void release(Object o) {
114: }
115:
116: public void dispose() {
117: Iterator serviceIt = components.values().iterator();
118: while (serviceIt.hasNext()) {
119: Object service = serviceIt.next();
120: if (service instanceof Disposable) {
121: try {
122: ((Disposable) service).dispose();
123: } catch (Exception e) {
124: getLogger().error(
125: "Error disposing service " + service, e);
126: }
127: }
128: }
129: }
130: }
|