KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > connectors > util > ConnectionPoolObjectsUtils


1
2
3 /*
4  * The contents of this file are subject to the terms
5  * of the Common Development and Distribution License
6  * (the License). You may not use this file except in
7  * compliance with the License.
8  *
9  * You can obtain a copy of the license at
10  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
11  * glassfish/bootstrap/legal/CDDLv1.0.txt.
12  * See the License for the specific language governing
13  * permissions and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL
16  * Header Notice in each file and include the License file
17  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
18  * If applicable, add the following below the CDDL Header,
19  * with the fields enclosed by brackets [] replaced by
20  * you own identifying information:
21  * "Portions Copyrighted [year] [name of copyright owner]"
22  *
23  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24  */

25
26 package com.sun.enterprise.connectors.util;
27
28 import com.sun.enterprise.connectors.*;
29 import com.sun.enterprise.deployment.runtime.connector.*;
30 import com.sun.enterprise.deployment.*;
31 import com.sun.enterprise.config.serverbeans.ElementProperty;
32 import com.sun.enterprise.config.serverbeans.ServerTags;
33 import com.sun.enterprise.config.ConfigBean;
34 import com.sun.enterprise.server.ResourcesUtil;
35 import com.sun.enterprise.util.i18n.StringManager;
36 import com.sun.logging.*;
37 import java.util.logging.Logger JavaDoc;
38 import java.util.logging.Level JavaDoc;
39 import java.util.regex.Matcher JavaDoc;
40 import java.util.regex.Pattern JavaDoc;
41
42 import javax.security.auth.Subject JavaDoc;
43 import javax.resource.spi.security.PasswordCredential JavaDoc;
44 import java.security.AccessController JavaDoc;
45 import java.security.PrivilegedAction JavaDoc;
46
47 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
48 import java.lang.reflect.Method JavaDoc;
49
50 import org.netbeans.modules.schema2beans.Common;
51
52 /**
53  * This is an util class for creating poolObjects of the type
54  * ConnectorConnectionPool from ConnectorDescriptor and also using the
55  * default values.
56  * @author Srikanth P
57  */

58
59 public class ConnectionPoolObjectsUtils {
60
61     public static final String JavaDoc ELEMENT_PROPERTY = "ElementProperty";
62     private static Logger JavaDoc _logger = LogDomains.getLogger( LogDomains.RSR_LOGGER );
63
64     private static final String JavaDoc VALIDATE_ATMOST_EVERY_IDLE_SECS =
65     "com.sun.enterprise.connectors.ValidateAtmostEveryIdleSecs";
66
67     private static String JavaDoc validateAtmostEveryIdleSecsProperty = System.getProperty(VALIDATE_ATMOST_EVERY_IDLE_SECS);
68
69     private static StringManager localStrings =
70         StringManager.getManager( ConnectionPoolObjectsUtils.class);
71     /**
72      * Creates default ConnectorConnectionPool consisting of default
73      * pool values.
74      * @param poolName Name of the pool
75      * @return ConnectorConnectionPool created ConnectorConnectionPool instance
76      */

77         
78     public static ConnectorConnectionPool createDefaultConnectorPoolObject(
79                   String JavaDoc poolName, String JavaDoc rarName) {
80
81         ConnectorConnectionPool connectorPoolObj =
82                   new ConnectorConnectionPool(poolName);
83         connectorPoolObj.setMaxPoolSize("20");
84         connectorPoolObj.setSteadyPoolSize("10");
85         connectorPoolObj.setMaxWaitTimeInMillis("7889");
86         connectorPoolObj.setIdleTimeoutInSeconds("789");
87         connectorPoolObj.setPoolResizeQuantity("2");
88         connectorPoolObj.setFailAllConnections(false);
89     connectorPoolObj.setMatchConnections(true ); //always
90
try {
91             connectorPoolObj.setTransactionSupport(getTransactionSupportFromRaXml(rarName));
92             } catch (Exception JavaDoc ex) {
93                 _logger.fine("error in setting txSupport");
94             }
95         return connectorPoolObj;
96     }
97
98     /**
99      * Creates ConnectorConnectionPool object pertaining to the pool props
100      * mentioned in the sun-ra/xml i.e it represents the pool mentioned in the
101      * sun-ra.xm.
102      * @param poolName Name of the pool
103      * @param desc ConnectorDescriptor which represent ra.xml and sun-ra.xml.
104      * @return ConnectorConnectionPool created ConnectorConnectionPool instance
105      */

106
107     public static ConnectorConnectionPool createSunRaConnectorPoolObject(
108                 String JavaDoc poolName,ConnectorDescriptor desc, String JavaDoc rarName) {
109
110         ConnectorConnectionPool connectorPoolObj =
111                        new ConnectorConnectionPool(poolName);
112         SunConnector sundesc = desc.getSunDescriptor();
113         ResourceAdapter sunRAXML = sundesc.getResourceAdapter();
114
115         connectorPoolObj.setMaxPoolSize(
116                (String JavaDoc)sunRAXML.getValue(ResourceAdapter.MAX_POOL_SIZE));
117         connectorPoolObj.setSteadyPoolSize(
118                (String JavaDoc)sunRAXML.getValue(ResourceAdapter.STEADY_POOL_SIZE));
119         connectorPoolObj.setMaxWaitTimeInMillis((String JavaDoc)sunRAXML.getValue(
120                ResourceAdapter.MAX_WAIT_TIME_IN_MILLIS));
121         connectorPoolObj.setIdleTimeoutInSeconds((String JavaDoc)sunRAXML.getValue(
122                ResourceAdapter.IDLE_TIMEOUT_IN_SECONDS));
123         connectorPoolObj.setPoolResizeQuantity((String JavaDoc)"2");
124         connectorPoolObj.setFailAllConnections(false);
125     connectorPoolObj.setMatchConnections(true ); //always
126
try {
127             connectorPoolObj.setTransactionSupport(getTransactionSupportFromRaXml(rarName));
128             } catch (Exception JavaDoc ex) {
129                 _logger.fine("error in setting txSupport");
130             }
131
132          boolean validateAtmostEveryIdleSecs = false;
133
134          //For SunRAPool, get the value of system property VALIDATE_ATMOST_EVERY_IDLE_SECS.
135
if(validateAtmostEveryIdleSecsProperty != null && validateAtmostEveryIdleSecsProperty.equalsIgnoreCase("TRUE")){
136             validateAtmostEveryIdleSecs = true;
137             _logger.log(Level.FINE,"CCP.ValidateAtmostEveryIdleSecs.Set", poolName );
138         }
139         connectorPoolObj.setValidateAtmostEveryIdleSecs(validateAtmostEveryIdleSecs);
140
141         return connectorPoolObj;
142     }
143
144     /**
145      * Return the integer representation of the transaction-support attribure
146      *
147      * @param txSupport one of <br>
148      * <ul>
149      * <li>NoTransaction</li>
150      * <li>LocalTransaction</li>
151      * <li>XATransaction</li>
152      * </ul>
153      * @return one of
154      * <ul>
155      * <li>ConnectorConstants.UNDEFINED_TRANSACTION_INT</li>
156      * <li>ConnectorConstants.NO_TRANSACTION_INT</li>
157      * <li>ConnectorConstants.LOCAL_TRANSACTION_INT</li>
158      * <li>ConnectorConstants.XA_TRANSACTION_INT</li>
159      * </ul>
160      *
161      */

162     public static int parseTransactionSupportString( String JavaDoc txSupport ) {
163         int txSupportIntVal = ConnectorConstants.UNDEFINED_TRANSACTION_INT;
164
165     
166     if ( txSupport == null ) {
167         if (_logger.isLoggable(Level.FINE)) {
168             _logger.fine("txSupport is null");
169         }
170         return txSupportIntVal;
171     }
172     
173         if (_logger.isLoggable(Level.FINE) ) {
174         _logger.fine("parseTransactionSupportString: passed in " +
175             "txSupport =>" + txSupport);
176     }
177     
178         if (ConnectorConstants.NO_TRANSACTION_TX_SUPPORT_STRING.equals(txSupport) ) {
179         txSupportIntVal = ConnectorConstants.NO_TRANSACTION_INT;
180     } else if (ConnectorConstants.LOCAL_TRANSACTION_TX_SUPPORT_STRING.equals(txSupport)) {
181         txSupportIntVal = ConnectorConstants.LOCAL_TRANSACTION_INT;
182     } else if (ConnectorConstants.XA_TRANSACTION_TX_SUPPORT_STRING.equals(txSupport)) {
183         txSupportIntVal = ConnectorConstants.XA_TRANSACTION_INT;
184     }
185
186     return txSupportIntVal;
187     }
188
189     public static String JavaDoc getValueFromMCF(String JavaDoc prop, String JavaDoc poolName,
190             ManagedConnectionFactory JavaDoc mcf) {
191         String JavaDoc result = null;
192         try {
193             Method JavaDoc m = mcf.getClass().getMethod("get"+prop, (java.lang.Class JavaDoc[])null);
194             result = (String JavaDoc) m.invoke(mcf,(java.lang.Object JavaDoc[])null);
195         } catch (Throwable JavaDoc t) {
196             _logger.log(Level.FINE, t.getMessage(), t);
197         }
198         
199     return result == null ? "" : result;
200     }
201     
202     public static Subject JavaDoc createSubject(ManagedConnectionFactory JavaDoc mcf,
203             final ResourcePrincipal prin) {
204                                                                      
205        final Subject JavaDoc tempSubject = new Subject JavaDoc();
206         if( prin != null) {
207             String JavaDoc password = prin.getPassword();
208             if (password != null) {
209                 final PasswordCredential JavaDoc pc =
210                   new PasswordCredential JavaDoc(prin.getName(),
211                   password.toCharArray());
212                 pc.setManagedConnectionFactory(mcf);
213                 AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
214                 public Object JavaDoc run() {
215                     tempSubject.getPrincipals().add(prin);
216                     tempSubject.getPrivateCredentials().add(pc);
217                     return null;
218                 }
219                 });
220            }
221         }
222         return tempSubject;
223     }
224
225
226  
227       /**
228       * Validates and sets the values for LazyConnectionEnlistment and LazyConnectionAssociation.
229       * @param LazyConnectionAssociation Property value
230       * @param ConfigBean
231       * @param ConnectorConnectionPool
232       */

233      public static void setLazyEnlistAndLazyAssocProperties(String JavaDoc lazyAssocString, ConfigBean adminPool, ConnectorConnectionPool conConnPool){
234  
235          //Get LazyEnlistment value.
236
//To set LazyAssoc to true, LazyEnlist also need to be true.
237
//If LazyAssoc is true and LazyEnlist is not set, set it to true.
238
//If LazyAssoc is true and LazyEnlist is false, throw exception.
239

240          ElementProperty[] o = (ElementProperty[])adminPool.getValues(ELEMENT_PROPERTY);
241          if (o == null) return ;
242  
243          ElementProperty lazyEnlistElement = null;
244  
245           for (int i=0; i < o.length; i++) {
246               if(o[i].getAttributeValue(Common.convertName(ServerTags.NAME)).equalsIgnoreCase("LAZYCONNECTIONENLISTMENT")) {
247                   lazyEnlistElement = o[i];
248                   break;
249               }
250           }
251  
252              boolean lazyAssoc = toBoolean( lazyAssocString, false );
253  
254              if(lazyEnlistElement != null){
255  
256                  boolean lazyEnlist = toBoolean(lazyEnlistElement.getValue(),false);
257                  if(lazyAssoc){
258                      if(lazyEnlist){
259                          conConnPool.setLazyConnectionAssoc( true) ;
260                          conConnPool.setLazyConnectionEnlist( true);
261                      }else{
262                           _logger.log(Level.WARNING,"conn_pool_obj_utils.lazy_enlist-lazy_assoc-invalid-combination",conConnPool.getName());
263                          String JavaDoc i18nMsg = localStrings.getString(
264             "cpou.lazy_enlist-lazy_assoc-invalid-combination");
265                          throw new RuntimeException JavaDoc(i18nMsg + conConnPool.getName());
266                      }
267                  }
268              }else{
269                  if(lazyAssoc){
270                      conConnPool.setLazyConnectionAssoc( true) ;
271                      conConnPool.setLazyConnectionEnlist( true);
272                  }else{
273                      conConnPool.setLazyConnectionAssoc( false) ;
274                  }
275              }
276  
277  }
278  
279      private static boolean toBoolean( Object JavaDoc prop, boolean defaultVal ) {
280          if ( prop == null ) {
281              return defaultVal;
282          }
283          return (new Boolean JavaDoc( ((String JavaDoc)prop).toLowerCase())).booleanValue();
284       }
285     
286     public static boolean isPoolSystemPool(com.sun.enterprise.config.serverbeans.ConnectorConnectionPool
287             domainCcp){
288         String JavaDoc poolName = domainCcp.getName();
289         return isPoolSystemPool(poolName);
290     }
291         
292     public static boolean isPoolSystemPool(String JavaDoc poolName){
293         Pattern JavaDoc pattern = Pattern.compile("#");
294         Matcher JavaDoc matcher = pattern.matcher(poolName);
295         
296         // If the pool name does not contain #, return false
297
if (!matcher.find()){
298             return false;
299         }
300         
301         matcher.reset();
302         
303         String JavaDoc moduleNameFromPoolName = null;
304         int matchCount=0;
305         
306         while (matcher.find()){
307             matchCount++;
308             int patternStart = matcher.start();
309             moduleNameFromPoolName = poolName.substring(0,patternStart);
310         }
311         
312         // If pool name contains more than 2 #, return false as the
313
// default system pool will have exacly one # for a standalone rar
314
// and exactly two #s for an embedded rar
315
ResourcesUtil resUtil = ResourcesUtil.getInstance();
316         switch (matchCount){
317         
318         case 1:
319             if (resUtil.belongToStandAloneRar(moduleNameFromPoolName))
320                 return true;
321         case 2:
322             if (resUtil.belongToEmbeddedRar(moduleNameFromPoolName))
323                 return true;
324         default:
325             return false;
326         }
327     }
328
329     public static int getTransactionSupportFromRaXml( String JavaDoc rarName ) throws
330             ConnectorRuntimeException
331     {
332         String JavaDoc txSupport =
333             ConnectorRuntime.getRuntime().getConnectorDescriptor( rarName ).
334             getOutboundResourceAdapter().getTransSupport() ;
335
336         return parseTransactionSupportString( txSupport );
337     }
338
339 }
340
Popular Tags