KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > web3 > impl > Web3DataSourceSelectorImpl


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.components.web3.impl;
17
18 import org.apache.cocoon.components.web3.Web3DataSource;
19
20 import org.apache.avalon.framework.activity.Disposable;
21 import org.apache.avalon.framework.service.ServiceException;
22 import org.apache.avalon.framework.service.ServiceManager;
23 import org.apache.avalon.framework.service.ServiceSelector;
24 import org.apache.avalon.framework.service.Serviceable;
25 import org.apache.avalon.framework.thread.ThreadSafe;
26 import org.apache.avalon.framework.configuration.Configuration;
27 import org.apache.avalon.framework.configuration.Configurable;
28 import org.apache.avalon.framework.configuration.ConfigurationException;
29 import org.apache.avalon.framework.logger.AbstractLogEnabled;
30 import org.apache.avalon.framework.logger.LogEnabled;
31 import org.apache.cocoon.util.ClassUtils;
32
33 import java.util.Enumeration JavaDoc;
34 import java.util.Hashtable JavaDoc;
35
36 import EDU.oswego.cs.dl.util.concurrent.Mutex;
37
38 /**
39  * The Default implementation for R3DataSources in Web3. This uses the
40  * normal <code>com.sap.mw.jco.JCO</code> classes.
41  *
42  * @author <a HREF="mailto:michael.gerzabek@at.efp.cc">Michael Gerzabek</a>
43  * @since 2.1
44  * @version CVS $Id: Web3DataSourceSelectorImpl.java 53915 2004-10-06 21:54:58Z antonio $
45  */

46 public class Web3DataSourceSelectorImpl
47     extends AbstractLogEnabled
48     implements ServiceSelector, Disposable, Serviceable, Configurable, ThreadSafe {
49
50     /** The service manager instance */
51     protected ServiceManager manager = null;
52     protected Configuration configuration = null;
53     private static Hashtable JavaDoc pools = new Hashtable JavaDoc();
54     private static Mutex lock = new Mutex();
55
56     /**
57      * Set the current <code>ServiceManager</code> instance used by this
58      * <code>Serviceable</code>.
59      */

60     public void service(ServiceManager manager) throws ServiceException {
61         this.manager = manager;
62     }
63
64     public void configure(Configuration configuration)
65         throws ConfigurationException {
66         if (null != configuration) {
67             this.configuration = configuration;
68         } else {
69             getLogger().error(
70                 "Couldn't configure Web3DataSourceSelector."
71                     + " No configuration provided!");
72         }
73     }
74
75     public boolean isSelectable(Object JavaDoc obj) {
76         return Web3DataSourceSelectorImpl.pools.containsKey(obj);
77     }
78
79     public Object JavaDoc select(Object JavaDoc obj) throws ServiceException {
80         Web3DataSource pool = null;
81         try {
82             Web3DataSourceSelectorImpl.lock.acquire();
83             if (null != obj) {
84                 if (Web3DataSourceSelectorImpl.pools.containsKey(obj)) {
85                     pool = (Web3DataSource)Web3DataSourceSelectorImpl.pools.get(obj);
86                 } else {
87                     Configuration a[] = this.configuration.getChildren("backend");
88                     Configuration c = null;
89
90                     if (null != a)
91                         for (int i = 0; i < a.length; i++) {
92                             try {
93                                 String JavaDoc s = a[i].getAttribute("name");
94                                 if (null != s && s.equals(obj.toString())) {
95                                     // a backend with a name can be defined only once
96
c = a[i];
97                                     break;
98                                 }
99                             } catch (ConfigurationException x) {
100                                 // this configuration element has no mandatory
101
//attribute name
102
}
103                         }
104                     // No configuration for this backend-id found!
105
if (null == c) {
106                         return null;
107                     }
108                     Class JavaDoc theClass =
109                         Class.forName(
110                             c.getChild("class").getValue(
111                                 "org.apache.cocoon.components.web3.impl.Web3DataSourceImpl"),
112                             true,
113                             ClassUtils.getClassLoader());
114                     pool = (Web3DataSource) theClass.newInstance();
115                     if (pool instanceof LogEnabled) {
116                         ((LogEnabled) pool).enableLogging(getLogger());
117                     }
118                     pool.service(this.manager);
119                     pool.configure(c);
120                     pool.initialize();
121                     Web3DataSourceSelectorImpl.pools.put(obj, pool);
122                 }
123             }
124         } catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc sid = null;
143             Web3DataSource pool;
144             for (Enumeration JavaDoc enumeration = Web3DataSourceSelectorImpl.pools.keys();
145                 enumeration.hasMoreElements();
146                 ) {
147                 sid = (String JavaDoc) enumeration.nextElement();
148                 pool =
149                     (Web3DataSource) Web3DataSourceSelectorImpl.pools.get(sid);
150                 pool.dispose();
151             }
152             Web3DataSourceSelectorImpl.pools.clear();
153         } catch (Exception JavaDoc ex) {
154         } finally {
155             Web3DataSourceSelectorImpl.lock.release();
156         }
157         Web3DataSourceSelectorImpl.lock = null;
158     }
159 }
160
Popular Tags