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.components.web3.impl;
018:
019: import java.util.Enumeration;
020: import java.util.Hashtable;
021:
022: import org.apache.avalon.framework.activity.Disposable;
023: import org.apache.avalon.framework.configuration.Configurable;
024: import org.apache.avalon.framework.configuration.Configuration;
025: import org.apache.avalon.framework.configuration.ConfigurationException;
026: import org.apache.avalon.framework.logger.AbstractLogEnabled;
027: import org.apache.avalon.framework.service.ServiceException;
028: import org.apache.avalon.framework.service.ServiceManager;
029: import org.apache.avalon.framework.service.ServiceSelector;
030: import org.apache.avalon.framework.service.Serviceable;
031: import org.apache.avalon.framework.thread.ThreadSafe;
032:
033: import org.apache.cocoon.components.LifecycleHelper;
034: import org.apache.cocoon.components.web3.Web3DataSource;
035: import org.apache.cocoon.util.ClassUtils;
036:
037: import EDU.oswego.cs.dl.util.concurrent.Mutex;
038:
039: /**
040: * TBD
041: *
042: * @author <a href="mailto:michael.gerzabek@at.efp.cc">Michael Gerzabek</a>
043: * @since 2.1
044: * @version CVS $Id: Web3DataSourceSelectorImpl.java 524407 2007-03-31 10:59:28Z joerg $
045: */
046: public class Web3DataSourceSelectorImpl extends AbstractLogEnabled
047: implements ServiceSelector, Disposable, Serviceable,
048: Configurable, ThreadSafe {
049:
050: /** The service manager instance */
051: protected ServiceManager manager;
052: protected Configuration configuration;
053: private static final Hashtable pools = new Hashtable();
054: private static final Mutex lock = new Mutex();
055:
056: /**
057: * Set the current <code>ServiceManager</code> instance used by this
058: * <code>Serviceable</code>.
059: */
060: public void service(ServiceManager manager) throws ServiceException {
061: this .manager = manager;
062: }
063:
064: public void configure(Configuration configuration)
065: throws ConfigurationException {
066: if (null != configuration) {
067: this .configuration = configuration;
068: } else {
069: getLogger().error(
070: "Couldn't configure Web3DataSourceSelector."
071: + " No configuration provided!");
072: }
073: }
074:
075: public boolean isSelectable(Object obj) {
076: return Web3DataSourceSelectorImpl.pools.containsKey(obj);
077: }
078:
079: public Object select(Object obj) throws ServiceException {
080: Web3DataSource pool = null;
081: try {
082: Web3DataSourceSelectorImpl.lock.acquire();
083: if (null != obj) {
084: if (Web3DataSourceSelectorImpl.pools.containsKey(obj)) {
085: pool = (Web3DataSource) Web3DataSourceSelectorImpl.pools
086: .get(obj);
087: } else {
088: Configuration a[] = this .configuration
089: .getChildren("backend");
090: Configuration c = null;
091:
092: if (null != a)
093: for (int i = 0; i < a.length; i++) {
094: try {
095: String s = a[i].getAttribute("name");
096: if (null != s
097: && s.equals(obj.toString())) {
098: // a backend with a name can be defined only once
099: c = a[i];
100: break;
101: }
102: } catch (ConfigurationException x) {
103: // this configuration element has no mandatory
104: //attribute name
105: }
106: }
107: // No configuration for this backend-id found!
108: if (null == c) {
109: return null;
110: }
111: Class theClass = Class
112: .forName(
113: c
114: .getChild("class")
115: .getValue(
116: "org.apache.cocoon.components.web3.impl.Web3DataSourceImpl"),
117: true, ClassUtils.getClassLoader());
118: pool = (Web3DataSource) theClass.newInstance();
119: LifecycleHelper.setupComponent(pool, getLogger(),
120: null, this .manager, c);
121: Web3DataSourceSelectorImpl.pools.put(obj, pool);
122: }
123: }
124: } catch (Exception ex) {
125: getLogger().error(ex.getMessage(), ex);
126: throw new ServiceException(null, ex.getMessage());
127: } finally {
128: Web3DataSourceSelectorImpl.lock.release();
129: }
130: getLogger().debug("Returning Web3DataSource[" + pool + "]");
131: return pool;
132: }
133:
134: public void release(Object object) {
135: }
136:
137: /** Dispose properly of the pool */
138: public void dispose() {
139: this .manager = null;
140: try {
141: Web3DataSourceSelectorImpl.lock.acquire();
142: String sid = null;
143: Web3DataSource pool;
144: for (Enumeration enumeration = Web3DataSourceSelectorImpl.pools
145: .keys(); enumeration.hasMoreElements();) {
146: sid = (String) enumeration.nextElement();
147: pool = (Web3DataSource) Web3DataSourceSelectorImpl.pools
148: .get(sid);
149: pool.dispose();
150: }
151: Web3DataSourceSelectorImpl.pools.clear();
152: } catch (Exception ex) {
153: } finally {
154: Web3DataSourceSelectorImpl.lock.release();
155: }
156: }
157:
158: }
|