KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 /**
33  * The Default implementation for R3DataSources in Web3. This uses the
34  * normal <code>com.sap.mw.jco.JCO</code> classes.
35  *
36  * @author <a HREF="mailto:michael.gerzabek@at.efp.cc">Michael Gerzabek</a>
37  * @since 2.1
38  * @version CVS $Id: Web3DataSourceImpl.java 30932 2004-07-29 17:35:38Z vgritsenko $
39  */

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 JavaDoc 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     /** Configure backend component */
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     /** initialize the component */
95     public void initialize() throws Exception JavaDoc {
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 JavaDoc ex) {
102             getLogger ().error ("Couldn't initialize Web3DataSource "
103                 + this.mySID, ex);
104             throw new Exception JavaDoc ( ex.getMessage() + this.mySID );
105         }
106         finally {
107             Web3DataSourceImpl.lock.release();
108         }
109     }
110
111     /** Get the backend client, returns <code>null</code> if there is no more
112         client in the pool. */

113     public Web3Client getWeb3Client() throws Exception JavaDoc {
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 JavaDoc ex){
126                 getLogger ().error ( this.mySID, ex);
127                 throw new Exception JavaDoc ( 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 JavaDoc x) {
143             getLogger().error( x.getMessage(), x);
144         }
145         finally {
146             Web3DataSourceImpl.lock.release();
147         }
148     }
149
150     /** Dispose properly of the pool */
151     public void dispose() {
152         try {
153             JCO.removeClientPool(this.mySID);
154         } catch (Exception JavaDoc 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