1 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 ; 34 import java.util.Hashtable ; 35 36 import EDU.oswego.cs.dl.util.concurrent.Mutex; 37 38 46 public class Web3DataSourceSelectorImpl 47 extends AbstractLogEnabled 48 implements ServiceSelector, Disposable, Serviceable, Configurable, ThreadSafe { 49 50 51 protected ServiceManager manager = null; 52 protected Configuration configuration = null; 53 private static Hashtable pools = new Hashtable (); 54 private static Mutex lock = new Mutex(); 55 56 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 obj) { 76 return Web3DataSourceSelectorImpl.pools.containsKey(obj); 77 } 78 79 public Object select(Object 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 s = a[i].getAttribute("name"); 94 if (null != s && s.equals(obj.toString())) { 95 c = a[i]; 97 break; 98 } 99 } catch (ConfigurationException x) { 100 } 103 } 104 if (null == c) { 106 return null; 107 } 108 Class 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 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 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.keys(); 145 enumeration.hasMoreElements(); 146 ) { 147 sid = (String ) enumeration.nextElement(); 148 pool = 149 (Web3DataSource) Web3DataSourceSelectorImpl.pools.get(sid); 150 pool.dispose(); 151 } 152 Web3DataSourceSelectorImpl.pools.clear(); 153 } catch (Exception ex) { 154 } finally { 155 Web3DataSourceSelectorImpl.lock.release(); 156 } 157 Web3DataSourceSelectorImpl.lock = null; 158 } 159 } 160 | Popular Tags |