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 org.apache.avalon.framework.logger.AbstractLogEnabled;
020: import org.apache.avalon.framework.configuration.Configuration;
021: import org.apache.avalon.framework.configuration.ConfigurationException;
022: import org.apache.avalon.framework.service.ServiceException;
023: import org.apache.avalon.framework.service.ServiceManager;
024: import org.apache.avalon.framework.thread.ThreadSafe;
025:
026: import EDU.oswego.cs.dl.util.concurrent.Mutex;
027:
028: import com.sap.mw.jco.JCO;
029:
030: import org.apache.cocoon.components.web3.Web3Client;
031: import org.apache.cocoon.components.web3.Web3DataSource;
032:
033: /**
034: * The Default implementation for R3DataSources in Web3. This uses the
035: * normal <code>com.sap.mw.jco.JCO</code> classes.
036: *
037: * @author <a href="mailto:michael.gerzabek@at.efp.cc">Michael Gerzabek</a>
038: * @since 2.1
039: * @version CVS $Id: Web3DataSourceImpl.java 524407 2007-03-31 10:59:28Z joerg $
040: */
041: public class Web3DataSourceImpl extends AbstractLogEnabled implements
042: Web3DataSource, ThreadSafe {
043:
044: protected Web3Properties properties;
045: protected int poolsize = 0;
046: protected int current_clients = 0;
047: protected String mySID;
048:
049: protected boolean trace = false;
050: protected int level = 0;
051:
052: private static Mutex lock = new Mutex();
053: protected ServiceManager manager;
054:
055: public void service(ServiceManager manager) throws ServiceException {
056: this .manager = manager;
057: }
058:
059: /** Configure backend component */
060: public void configure(final Configuration configuration)
061: throws ConfigurationException {
062: if (null != configuration) {
063: this .properties = new Web3Properties();
064: Configuration child = configuration.getChild("pool");
065: this .trace = child.getAttributeAsBoolean("trace", false);
066: this .level = child.getAttributeAsInteger("level", 0);
067: this .mySID = configuration.getAttribute("name");
068: this .poolsize = child.getAttributeAsInteger("size");
069:
070: this .properties.put("jco.client.client", child.getChild(
071: "client").getValue());
072: this .properties.put("jco.client.user", child.getChild(
073: "user").getValue());
074: this .properties.put("jco.client.passwd", child.getChild(
075: "password").getValue());
076: this .properties.put("jco.client.ashost", child.getChild(
077: "route").getValue());
078: this .properties.put("jco.client.sysnr", child.getChild(
079: "system").getValue());
080: this .properties.put("sap.gateway", child
081: .getChild("gateway").getValue(""));
082: this .properties.put("sap.programid", child.getChild(
083: "program-id").getValue(""));
084:
085: if (getLogger().isDebugEnabled()) {
086: getLogger().debug(
087: "Configure R3DataSource [mySID=" + this .mySID);
088: }
089: } else {
090: getLogger().error(
091: "Couldn't configure Web3DataSource."
092: + " No configuration provided!");
093: }
094: }
095:
096: /** initialize the component */
097: public void initialize() throws Exception {
098: try {
099: Web3DataSourceImpl.lock.acquire();
100: JCO.addClientPool(this .mySID, this .poolsize,
101: this .properties);
102: JCO.getClientPoolManager().getPool(this .mySID).setTrace(
103: this .trace);
104: JCO.setTraceLevel(this .level);
105: } catch (Exception ex) {
106: getLogger().error(
107: "Couldn't initialize Web3DataSource " + this .mySID,
108: ex);
109: throw new Exception(ex.getMessage() + this .mySID);
110: } finally {
111: Web3DataSourceImpl.lock.release();
112: }
113: }
114:
115: /** Get the backend client, returns <code>null</code> if there is no more
116: client in the pool. */
117: public Web3Client getWeb3Client() throws Exception {
118: Web3Client theClient = null;
119: if (this .current_clients + 1 < this .poolsize) {
120: this .current_clients++;
121: try {
122: Web3DataSourceImpl.lock.acquire();
123: theClient = (Web3Client) this .manager
124: .lookup(Web3Client.ROLE);
125: theClient.initClient(JCO.getClient(this .mySID));
126:
127: if (getLogger().isDebugEnabled()) {
128: getLogger().debug("returning client " + theClient);
129: }
130: } catch (Exception ex) {
131: getLogger().error(this .mySID, ex);
132: throw new Exception(ex.getMessage());
133: } finally {
134: Web3DataSourceImpl.lock.release();
135: }
136: }
137: return theClient;
138: }
139:
140: public void releaseWeb3Client(Web3Client client) {
141: try {
142: Web3DataSourceImpl.lock.acquire();
143: client.releaseClient();
144: this .current_clients--;
145: manager.release(client);
146: } catch (Exception x) {
147: getLogger().error(x.getMessage(), x);
148: } finally {
149: Web3DataSourceImpl.lock.release();
150: }
151: }
152:
153: /** Dispose properly of the pool */
154: public void dispose() {
155: try {
156: JCO.removeClientPool(this .mySID);
157: } catch (Exception ex) {
158: getLogger().error(
159: "Web3DataSource: couldn't"
160: + " return Web3DataSource", ex);
161: }
162: this .properties = null;
163: this .mySID = null;
164: getLogger().debug("Web3DataSource disposed.");
165: }
166:
167: }
|