KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > resource > ConnectorConnectionPoolDeployer


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

23
24 package com.sun.enterprise.resource;
25
26 import com.sun.enterprise.ManagementObjectManager;
27
28 import com.sun.enterprise.server.ResourceDeployer;
29 import com.sun.enterprise.server.ResourcesUtil;
30 import com.sun.enterprise.config.serverbeans.Resources;
31 import com.sun.enterprise.config.serverbeans.ElementProperty;
32 import com.sun.enterprise.config.serverbeans.SecurityMap;
33 import com.sun.enterprise.connectors.ConnectorRuntime;
34 import com.sun.enterprise.connectors.ConnectorConnectionPool;
35 import com.sun.enterprise.deployment.ConnectionDefDescriptor;
36 import com.sun.logging.LogDomains;
37 import java.util.logging.*;
38
39 import java.util.Set JavaDoc;
40 import java.util.HashSet JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.regex.*;
43
44 import com.sun.enterprise.connectors.ConnectorRuntimeException;
45 import com.sun.enterprise.connectors.ConnectorRegistry;
46 import com.sun.enterprise.deployment.ConnectorDescriptor;
47 import com.sun.enterprise.connectors.ConnectorDescriptorInfo;
48 import com.sun.enterprise.deployment.EnvironmentProperty;
49 import com.sun.enterprise.repository.IASJ2EEResourceFactoryImpl;
50 import com.sun.enterprise.util.i18n.StringManager;
51 import com.sun.enterprise.PoolManager;
52 import com.sun.enterprise.connectors.ConnectorConstants;
53 import com.sun.enterprise.connectors.util.ConnectionPoolObjectsUtils;
54 import com.sun.enterprise.connectors.util.SecurityMapUtils;
55
56 /**
57  *
58  * @author Srikanth P, Sivakumar Thyagarajan
59  * @version
60  */

61
62 public class ConnectorConnectionPoolDeployer extends GlobalResourceDeployer
63         implements ResourceDeployer {
64
65     private static final String JavaDoc QUEUE_CF = "javax.jms.QueueConnectionFactory";
66     private static final String JavaDoc TOPIC_CF = "javax.jms.TopicConnectionFactory";
67     private static final String JavaDoc UNIVERSAL_CF = "javax.jms.ConnectionFactory";
68     
69     static Logger _logger = LogDomains.getLogger(LogDomains.CORE_LOGGER);
70
71     private static StringManager localStrings =
72         StringManager.getManager( ConnectorConnectionPoolDeployer.class);
73     /**
74      * ResourceManager callback to indicate resource-deployment
75      * Since 8.1 PE/SE/EE, this is a no-op
76      *
77      * @param resource The resource to be undeployed.
78      * @throws Exception if there is an error undeploying the resource.
79      */

80     public synchronized void deployResource(Object JavaDoc resource) throws Exception JavaDoc {
81         _logger.fine("ConnectorConnectionPoolDeployer : deployResource ");
82
83         final com.sun.enterprise.config.serverbeans.ConnectorConnectionPool
84         domainCcp =
85         (com.sun.enterprise.config.serverbeans.ConnectorConnectionPool)resource;
86
87         // If the user is trying to modify the default pool,
88
// redirect call to redeployResource
89
if (ConnectionPoolObjectsUtils.isPoolSystemPool(domainCcp)){
90             this.redeployResource(resource);
91             return;
92         }
93             
94        
95         final ConnectorConnectionPool ccp =
96             getConnectorConnectionPool(domainCcp);
97         final String JavaDoc defName = domainCcp.getConnectionDefinitionName();
98         final ConnectorRuntime crt = ConnectorRuntime.getRuntime();
99         
100         if (domainCcp.isEnabled()) {
101             if (UNIVERSAL_CF.equals(defName) || QUEUE_CF.equals(defName) || TOPIC_CF.equals(defName)) {
102             //registers the jsr77 object for the mail resource deployed
103
final ManagementObjectManager mgr =
104                 getAppServerSwitchObject().getManagementObjectManager();
105             mgr.registerJMSResource(domainCcp.getName(), defName, null, null,
106                     getPropNamesAsStrArr(domainCcp.getElementProperty()),
107                     getPropValuesAsStrArr(domainCcp.getElementProperty()));
108             }
109             
110         } else {
111                 _logger.log(Level.INFO, "core.resource_disabled",
112                         new Object JavaDoc[] {domainCcp.getName(),
113                         IASJ2EEResourceFactoryImpl.CONNECTOR_CONN_POOL_TYPE});
114         }
115
116         _logger.log(Level.FINE,
117                    "Calling backend to add connectorConnectionPool",
118                    domainCcp.getResourceAdapterName());
119         crt.createConnectorConnectionPool(ccp,
120             defName, domainCcp.getResourceAdapterName(),
121             domainCcp.getElementProperty(),
122             domainCcp.getSecurityMap());
123        _logger.log(Level.FINE,
124                    "Added connectorConnectionPool in backend",
125                    domainCcp.getResourceAdapterName());
126
127     }
128     
129     /**
130      * Undeploys the connector connection pool resource.
131      *
132      * @param resource The resource to be undeployed.
133      * @throws Exception if there is an error undeploying the resource.
134      */

135     public synchronized void undeployResource(Object JavaDoc resource)
136     throws Exception JavaDoc {
137         _logger.fine("ConnectorConnectionPoolDeployer : undeployResource : " );
138         final com.sun.enterprise.config.serverbeans.ConnectorConnectionPool
139         domainCcp =
140         (com.sun.enterprise.config.serverbeans.ConnectorConnectionPool)resource;
141         final String JavaDoc poolName = domainCcp.getName();
142         final ConnectorRuntime crt = ConnectorRuntime.getRuntime();
143         final String JavaDoc defName = domainCcp.getConnectionDefinitionName();
144         
145         _logger.log(Level.FINE,
146                  "Calling backend to delete ConnectorConnectionPool",poolName);
147         crt.deleteConnectorConnectionPool(poolName);
148         _logger.log(Level.FINE,
149                    "Deleted ConnectorConnectionPool in backend",poolName);
150         
151         //unregister the managed object
152
if (QUEUE_CF.equals(defName) || TOPIC_CF.equals(defName)) {
153             //registers the jsr77 object for the mail resource deployed
154
final ManagementObjectManager mgr =
155                 getAppServerSwitchObject().getManagementObjectManager();
156             mgr.unregisterJMSResource(domainCcp.getName());
157         }
158     }
159
160     public synchronized void redeployResource(Object JavaDoc resource)
161                throws Exception JavaDoc {
162         //Connector connection pool reconfiguration or
163
//change in security maps
164
com.sun.enterprise.config.serverbeans.ConnectorConnectionPool
165         domainCcp =
166         (com.sun.enterprise.config.serverbeans.ConnectorConnectionPool)resource;
167         SecurityMap[] securityMaps = domainCcp.getSecurityMap();
168         String JavaDoc poolName = domainCcp.getName();
169         ConnectorRuntime crt = ConnectorRuntime.getRuntime();
170         
171         //Since 8.1 PE/SE/EE, only if pool has already been deployed in this
172
//server-instance earlier, reconfig this pool
173
if (!crt.isConnectorConnectionPoolDeployed(poolName)) {
174             _logger.fine("The connector connection pool " + poolName
175                             + " is either not referred or not yet created in "
176                             + "this server instance and pool and hence "
177                             + "redeployment is ignored");
178             return;
179         }
180         
181
182         String JavaDoc rarName = domainCcp.getResourceAdapterName();
183         String JavaDoc connDefName = domainCcp.getConnectionDefinitionName();
184         ElementProperty[] props = domainCcp.getElementProperty();
185         ConnectorConnectionPool ccp = getConnectorConnectionPool(domainCcp);
186         populateConnectorConnectionPool( ccp, connDefName, rarName, props,
187             securityMaps);
188     
189         boolean poolRecreateRequired = false;
190         try {
191             _logger.fine("Calling reconfigure pool");
192                 poolRecreateRequired = crt.reconfigureConnectorConnectionPool( ccp,
193                 new HashSet JavaDoc());
194         } catch (ConnectorRuntimeException cre ) {
195             cre.printStackTrace();
196         }
197         
198         if (poolRecreateRequired){
199             _logger.fine("Pool recreation required");
200             crt.recreateConnectorConnectionPool( ccp );
201             _logger.fine("Pool recreation done");
202         }
203     }
204
205     public synchronized void disableResource(Object JavaDoc resource)
206                throws Exception JavaDoc {
207     }
208
209     public synchronized void enableResource(Object JavaDoc resource)
210                throws Exception JavaDoc {
211     }
212
213     public Object JavaDoc getResource(String JavaDoc name, Resources rbeans)
214                throws Exception JavaDoc {
215
216         Object JavaDoc res = rbeans.getConnectorConnectionPoolByName(name);
217         if (res == null) {
218             Exception JavaDoc ex = new Exception JavaDoc("No such resource");
219             _logger.log(Level.SEVERE,"no_resource",name);
220             _logger.log(Level.SEVERE,"",ex);
221             throw ex;
222         }
223         return res;
224     }
225
226     private ConnectorConnectionPool getConnectorConnectionPool(
227          com.sun.enterprise.config.serverbeans.ConnectorConnectionPool
228              domainCcp ) throws Exception JavaDoc {
229         ConnectorConnectionPool ccp =
230                        new ConnectorConnectionPool(domainCcp.getName());
231         ccp.setSteadyPoolSize(domainCcp.getSteadyPoolSize());
232         ccp.setMaxPoolSize(domainCcp.getMaxPoolSize());
233         ccp.setMaxWaitTimeInMillis(domainCcp.getMaxWaitTimeInMillis());
234         ccp.setPoolResizeQuantity(domainCcp.getPoolResizeQuantity());
235         ccp.setIdleTimeoutInSeconds(domainCcp.getIdleTimeoutInSeconds());
236         ccp.setFailAllConnections(domainCcp.isFailAllConnections());
237         ccp.setAuthCredentialsDefinedInPool(
238             isAuthCredentialsDefinedInPool(domainCcp));
239     //The line below will change for 9.0. We will get this from
240
//the domain.xml
241
ccp.setConnectionValidationRequired(
242         isIsConnectionValidationRequired() );
243     String JavaDoc txSupport = domainCcp.getTransactionSupport();
244     int txSupportIntVal = parseTransactionSupportString( txSupport );
245
246         if (txSupportIntVal == -1 ) {
247         //if transaction-support attribute is null load the value
248
//from the ra.xml
249
if (_logger.isLoggable( Level.FINE ) ) {
250             _logger.fine("Got transaction-support attr null from domain.xml");
251         }
252         txSupportIntVal = ConnectionPoolObjectsUtils.getTransactionSupportFromRaXml(
253             domainCcp.getResourceAdapterName() );
254         
255     } else {
256         //We got some valid transaction-support attribute value
257
//so go figure if it is valid.
258
//The tx support is valid if it is less-than/equal-to
259
//the value specified in the ra.xml
260
if (! isTxSupportConfigurationSane( txSupportIntVal,
261             domainCcp.getResourceAdapterName() ) ) {
262                 
263             String JavaDoc i18nMsg = localStrings.getString(
264                 "ccp_deployer.incorrect_tx_support");
265             ConnectorRuntimeException cre = new
266                 ConnectorRuntimeException(i18nMsg);
267                 
268                 _logger.log(Level.SEVERE, "rardeployment.incorrect_tx_support",
269                 ccp.getName());
270             throw cre;
271         }
272     }
273         if (_logger.isLoggable(Level.FINE)) {
274         _logger.fine("setting txSupportVal to " + txSupportIntVal +
275             " in pool " + domainCcp.getName() );
276     }
277     ccp.setTransactionSupport( txSupportIntVal );
278
279         //Always for ccp
280
ccp.setLazyConnectionAssoc( false );
281     ccp.setAssociateWithThread( false );
282     ccp.setNonComponent( false );
283     ccp.setNonTransactional( false );
284     ccp.setConnectionLeakTracing( false );
285
286     //IMPORTANT
287
//Here all properties that will be checked by the
288
//convertElementPropertyToPoolProperty method need to be set to
289
//their default values
290
ccp.setMatchConnections( true );
291
292     convertElementPropertyToPoolProperty( ccp, domainCcp );
293         return ccp;
294     }
295     
296     private void populateConnectorConnectionPool( ConnectorConnectionPool ccp,
297         String JavaDoc connectionDefinitionName, String JavaDoc rarName,
298     ElementProperty[] props, SecurityMap[] securityMaps)
299     throws ConnectorRuntimeException
300     {
301     
302     ConnectorRegistry _registry = ConnectorRegistry.getInstance();
303         ConnectorDescriptor connectorDescriptor = _registry.getDescriptor(rarName);
304         if (connectorDescriptor == null) {
305             ConnectorRuntimeException cre = new ConnectorRuntimeException(
306                             "Failed to get connection pool object");
307             _logger.log(Level.SEVERE,
308                  "rardeployment.connector_descriptor_notfound_registry",rarName);
309             _logger.log(Level.SEVERE,"",cre);
310             throw cre;
311         }
312         Set JavaDoc connectionDefs =
313              connectorDescriptor.getOutboundResourceAdapter().getConnectionDefs();
314         ConnectionDefDescriptor cdd = null;
315         Iterator JavaDoc it = connectionDefs.iterator();
316         while(it.hasNext()) {
317           cdd = (ConnectionDefDescriptor)it.next();
318           if(connectionDefinitionName.equals(cdd.getConnectionFactoryIntf()))
319               break;
320
321         }
322         ConnectorDescriptorInfo cdi = new ConnectorDescriptorInfo();
323
324         cdi.setRarName(rarName);
325         cdi.setResourceAdapterClassName(
326                     connectorDescriptor.getResourceAdapterClass());
327         cdi.setConnectionDefinitionName(cdd.getConnectionFactoryIntf());
328         cdi.setManagedConnectionFactoryClass(
329                     cdd.getManagedConnectionFactoryImpl());
330         cdi.setConnectionFactoryClass(cdd.getConnectionFactoryImpl());
331         cdi.setConnectionFactoryInterface(cdd.getConnectionFactoryIntf());
332         cdi.setConnectionClass(cdd.getConnectionImpl());
333         cdi.setConnectionInterface(cdd.getConnectionIntf());
334         Set JavaDoc mergedProps = mergeProps(props, cdd.getConfigProperties());
335         cdi.setMCFConfigProperties(mergedProps);
336         cdi.setResourceAdapterConfigProperties(
337                     connectorDescriptor.getConfigProperties());
338         ccp.setConnectorDescriptorInfo( cdi );
339         ccp.setSecurityMaps(SecurityMapUtils.getConnectorSecurityMaps(securityMaps));
340              
341     }
342
343     private Set JavaDoc mergeProps(ElementProperty[] props, Set JavaDoc defaultMCFProps) {
344         HashSet JavaDoc mergedSet = new HashSet JavaDoc();
345
346         Object JavaDoc[] defaultProps = ( defaultMCFProps == null ) ?
347         new Object JavaDoc[0] :
348         defaultMCFProps.toArray();
349
350         for (int i =0; i< defaultProps.length; i++) {
351          mergedSet.add(defaultProps[i]);
352         }
353
354         for (int i =0; i< props.length; i++) {
355          if ( props[i] != null ) {
356              EnvironmentProperty ep = new EnvironmentProperty(
357                         props[i].getName(),props[i].getValue(),null);
358              if (defaultMCFProps.contains(ep)) {
359                  mergedSet.remove(ep);
360              }
361              mergedSet.add(ep);
362          }
363         }
364
365         return mergedSet;
366     }
367
368     private boolean isTxSupportConfigurationSane(int txSupport, String JavaDoc raName) {
369         int raXmlTxSupport = ConnectorConstants.UNDEFINED_TRANSACTION_INT;
370     
371     try {
372             raXmlTxSupport = ConnectionPoolObjectsUtils.getTransactionSupportFromRaXml( raName ) ;
373         } catch (Exception JavaDoc e) {
374         _logger.log(Level.WARNING,
375             (e.getMessage() != null ? e.getMessage() : " " ));
376     }
377         if (_logger.isLoggable(Level.FINE) ) {
378             _logger.log(Level.FINE,"isTxSupportConfigSane:: txSupport => "
379             + txSupport + " raXmlTxSupport => " + raXmlTxSupport);
380     }
381
382         return (txSupport > raXmlTxSupport) ? false : true ;
383        
384     }
385
386     
387     private int parseTransactionSupportString( String JavaDoc txSupport ) {
388         int txSupportIntVal = ConnectorConstants.UNDEFINED_TRANSACTION_INT;
389         return ConnectionPoolObjectsUtils.parseTransactionSupportString( txSupport );
390     }
391    
392     public boolean isIsConnectionValidationRequired() {
393         String JavaDoc val = System.getProperty( "validation" );
394         return ( val != null ) ? (new Boolean JavaDoc(val)).booleanValue() : false;
395     }
396
397     public boolean isMatchConnections() {
398         String JavaDoc val= System.getProperty( "matchConnections" );
399         return ( val != null ) ? (new Boolean JavaDoc(val)).booleanValue() : true;
400     }
401
402     /**
403      * The idea is to convert the ElementProperty values coming from the admin
404      * connection pool to standard pool attributes thereby making it
405      * easy in case of a reconfig
406      */

407     public void convertElementPropertyToPoolProperty( ConnectorConnectionPool ccp,
408     com.sun.enterprise.config.serverbeans.ConnectorConnectionPool domainCcp)
409     {
410         ElementProperty[] elemProps = domainCcp.getElementProperty();
411     if ( elemProps == null ) {
412         return;
413     }
414         for( ElementProperty ep : elemProps ) {
415         
416         if (ep != null && "MATCHCONNECTIONS".equals( ep.getName().toUpperCase()) ) {
417         //the foreach loop seems to handle change in hte underlying datastructure.
418
domainCcp.removeElementProperty( ep );
419         if (_logger.isLoggable( Level.FINE) ) {
420                     _logger.fine("@@@@ ConnectorConnectionPoolDeployer:: Setting matchConnections");
421                 }
422         ccp.setMatchConnections( toBoolean( ep.getValue(), true ));
423             }else if ( "LAZYCONNECTIONASSOCIATION".equals(ep.getName().toUpperCase()) ){
424                 ConnectionPoolObjectsUtils.setLazyEnlistAndLazyAssocProperties(ep.getValue(),domainCcp, ccp);
425                 boolean assoc = toBoolean( ep.getValue(), false );
426                 _logger.log(Level.FINE,"ccp_deployer.lazy_con_assoc_value",new Object JavaDoc[]{ccp.getName(), String.valueOf(assoc)});
427             }else if ( "LAZYCONNECTIONENLISTMENT".equals(ep.getName().toUpperCase()) ){
428                 boolean enlist = toBoolean( ep.getValue(), false );
429                 ccp.setLazyConnectionEnlist( enlist ) ;
430                 _logger.log(Level.FINE,"ccp_deployer.lazy_con_enlist_value",new Object JavaDoc[]{ccp.getName(), String.valueOf(enlist)});
431         }
432     }
433     }
434
435     private boolean toBoolean( Object JavaDoc prop, boolean defaultVal ) {
436         if ( prop == null ) {
437         return defaultVal;
438     }
439
440     return (new Boolean JavaDoc( ((String JavaDoc)prop).toLowerCase())).booleanValue();
441     }
442
443     private boolean isAuthCredentialsDefinedInPool(
444             com.sun.enterprise.config.serverbeans.ConnectorConnectionPool domainCcp) {
445         ElementProperty[] elemProps = domainCcp.getElementProperty();
446         if ( elemProps == null ) {
447             return false;
448         }
449
450         for( int i =0; i < elemProps.length; i++ ) {
451             ElementProperty ep = elemProps[i];
452             if (ep.getName().equalsIgnoreCase("UserName") ||
453                 ep.getName().equalsIgnoreCase("User") ||
454                 ep.getName().equalsIgnoreCase("Password")) {
455                 return true;
456             }
457         }
458         return false;
459     }
460 }
461
Popular Tags