1 16 package org.apache.cocoon.components.web3.impl; 17 18 import org.apache.avalon.framework.logger.AbstractLogEnabled; 19 import org.apache.avalon.framework.configuration.Configuration; 20 import org.apache.avalon.framework.configuration.ConfigurationException; 21 import org.apache.avalon.framework.service.ServiceException; 22 import org.apache.avalon.framework.service.ServiceManager; 23 import org.apache.avalon.framework.thread.ThreadSafe; 24 25 import EDU.oswego.cs.dl.util.concurrent.Mutex; 26 27 import com.sap.mw.jco.JCO; 28 29 import org.apache.cocoon.components.web3.Web3Client; 30 import org.apache.cocoon.components.web3.Web3DataSource; 31 32 40 public class Web3DataSourceImpl extends AbstractLogEnabled 41 implements Web3DataSource, ThreadSafe { 42 43 protected Web3Properties properties = null; 44 protected int poolsize = 0; 45 protected int current_clients = 0; 46 protected String mySID = null; 47 48 protected boolean trace = false; 49 protected int level = 0; 50 51 private static Mutex lock = new Mutex(); 52 protected ServiceManager manager; 53 54 public void service(ServiceManager manager) throws ServiceException { 55 this.manager = manager; 56 } 57 58 59 public void configure(final Configuration configuration) 60 throws ConfigurationException { 61 if (null != configuration) { 62 this.properties = new Web3Properties (); 63 Configuration child = configuration.getChild("pool"); 64 this.trace = child.getAttributeAsBoolean("trace", false); 65 this.level = child.getAttributeAsInteger("level", 0); 66 this.mySID = configuration.getAttribute("name"); 67 this.poolsize = child.getAttributeAsInteger("size"); 68 69 this.properties.put("jco.client.client", 70 child.getChild("client").getValue()); 71 this.properties.put("jco.client.user", 72 child.getChild("user").getValue()); 73 this.properties.put("jco.client.passwd", 74 child.getChild("password").getValue()); 75 this.properties.put("jco.client.ashost", 76 child.getChild("route").getValue()); 77 this.properties.put("jco.client.sysnr", 78 child.getChild("system").getValue()); 79 this.properties.put("sap.gateway", 80 child.getChild("gateway").getValue("")); 81 this.properties.put("sap.programid", 82 child.getChild("program-id").getValue("")); 83 84 if ( getLogger().isDebugEnabled() ) { 85 getLogger ().debug ("Configure R3DataSource [mySID=" 86 + this.mySID ); 87 } 88 } else { 89 getLogger ().error ("Couldn't configure Web3DataSource." + 90 " No configuration provided!"); 91 } 92 } 93 94 95 public void initialize() throws Exception { 96 try { 97 Web3DataSourceImpl.lock.acquire(); 98 JCO.addClientPool( this.mySID, this.poolsize, this.properties ); 99 JCO.getClientPoolManager().getPool( this.mySID ).setTrace( this.trace ); 100 JCO.setTraceLevel( this.level ); 101 } catch (Exception ex) { 102 getLogger ().error ("Couldn't initialize Web3DataSource " 103 + this.mySID, ex); 104 throw new Exception ( ex.getMessage() + this.mySID ); 105 } 106 finally { 107 Web3DataSourceImpl.lock.release(); 108 } 109 } 110 111 113 public Web3Client getWeb3Client() throws Exception { 114 Web3Client theClient = null; 115 if ( this.current_clients + 1 < this.poolsize ) { 116 this.current_clients++; 117 try { 118 Web3DataSourceImpl.lock.acquire(); 119 theClient = (Web3Client) this.manager.lookup( Web3Client.ROLE ); 120 theClient.initClient (JCO.getClient(this.mySID)); 121 122 if ( getLogger().isDebugEnabled() ) { 123 getLogger ().debug ("returning client " + theClient); 124 } 125 } catch (Exception ex){ 126 getLogger ().error ( this.mySID, ex); 127 throw new Exception ( ex.getMessage() ); 128 } finally { 129 Web3DataSourceImpl.lock.release(); 130 } 131 } 132 return theClient; 133 } 134 135 public void releaseWeb3Client(Web3Client client) { 136 try { 137 Web3DataSourceImpl.lock.acquire(); 138 client.releaseClient(); 139 this.current_clients--; 140 manager.release( client ); 141 } 142 catch (Exception x) { 143 getLogger().error( x.getMessage(), x); 144 } 145 finally { 146 Web3DataSourceImpl.lock.release(); 147 } 148 } 149 150 151 public void dispose() { 152 try { 153 JCO.removeClientPool(this.mySID); 154 } catch (Exception ex) { 155 getLogger ().error ("Web3DataSource: couldn't" + 156 " return Web3DataSource", ex); 157 } 158 this.properties = null; 159 this.mySID = null; 160 getLogger ().debug ("Web3DataSource disposed."); 161 } 162 163 } 164 | Popular Tags |