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.forms.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.service.ServiceException;
029: import org.apache.avalon.framework.service.ServiceManager;
030: import org.apache.avalon.framework.service.ServiceSelector;
031: import org.apache.avalon.framework.service.Serviceable;
032: import org.apache.avalon.framework.context.Contextualizable;
033: import org.apache.avalon.framework.context.Context;
034: import org.apache.avalon.framework.context.ContextException;
035: import org.apache.cocoon.components.LifecycleHelper;
036:
037: /**
038: * A very simple ServiceSelector for ThreadSafe services.
039: *
040: * @version $Id: SimpleServiceSelector.java 449149 2006-09-23 03:58:05Z crossley $
041: */
042: public class SimpleServiceSelector extends AbstractLogEnabled implements
043: ServiceSelector, Configurable, Serviceable, Disposable,
044: Contextualizable {
045:
046: private Context context;
047: private ServiceManager serviceManager;
048:
049: private final String hintShortHand;
050: private final Class componentClass;
051: private Map components = new HashMap();
052:
053: //
054: // Lifecycle
055: //
056:
057: public SimpleServiceSelector(String hintShortHand,
058: Class componentClass) {
059: this .hintShortHand = hintShortHand;
060: this .componentClass = componentClass;
061: }
062:
063: public void contextualize(Context context) throws ContextException {
064: this .context = context;
065: }
066:
067: public void service(ServiceManager serviceManager)
068: throws ServiceException {
069: this .serviceManager = serviceManager;
070: }
071:
072: public void configure(Configuration configuration)
073: throws ConfigurationException {
074: Configuration[] componentConfs = configuration
075: .getChildren(hintShortHand);
076: for (int i = 0; i < componentConfs.length; i++) {
077: String name = componentConfs[i].getAttribute("name");
078: String src = componentConfs[i].getAttribute("src");
079:
080: Class clazz;
081: try {
082: clazz = Class.forName(src);
083: } catch (ClassNotFoundException e) {
084: throw new ConfigurationException("Class not found: "
085: + src + ", declared at "
086: + componentConfs[i].getLocation(), e);
087: }
088:
089: if (!componentClass.isAssignableFrom(clazz)) {
090: throw new ConfigurationException(
091: "The class \""
092: + src
093: + "\" is of an incorrect type, it should implement or extend "
094: + componentClass.getName());
095: }
096:
097: Object component;
098: try {
099: component = clazz.newInstance();
100: LifecycleHelper.setupComponent(component, getLogger(),
101: context, serviceManager, componentConfs[i]);
102: } catch (Exception e) {
103: throw new ConfigurationException("Error creating "
104: + hintShortHand + " declared at "
105: + componentConfs[i].getLocation(), e);
106: }
107:
108: components.put(name, component);
109: }
110: }
111:
112: public void dispose() {
113: Iterator i = components.values().iterator();
114: while (i.hasNext()) {
115: Object service = i.next();
116: if (service instanceof Disposable) {
117: try {
118: ((Disposable) service).dispose();
119: } catch (Exception e) {
120: getLogger().error(
121: "Error disposing service " + service, e);
122: }
123: }
124: }
125: components.clear();
126: }
127:
128: //
129: // ServiceSelector
130: //
131:
132: public Object select(Object hint) throws ServiceException {
133: if (!isSelectable(hint)) {
134: throw new ServiceException((String) hint,
135: "Non-existing component for this hint");
136: }
137: String stringHint = (String) hint;
138: return components.get(stringHint);
139: }
140:
141: public boolean isSelectable(Object hint) {
142: String stringHint = (String) hint;
143: return components.containsKey(stringHint);
144: }
145:
146: public void release(Object o) {
147: }
148:
149: }
|