KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > connectors > ActiveOutboundResourceAdapter


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.connectors;
25
26 import com.sun.enterprise.*;
27 import com.sun.enterprise.naming.*;
28 import com.sun.enterprise.deployment.*;
29 import com.sun.enterprise.deployment.runtime.connector.ResourceAdapter;
30 import com.sun.enterprise.deployment.runtime.connector.*;
31 import com.sun.enterprise.util.*;
32 import com.sun.enterprise.connectors.util.*;
33 import com.sun.enterprise.resource.*;
34 import com.sun.enterprise.server.*;
35 import com.sun.enterprise.config.*;
36 import com.sun.enterprise.util.i18n.StringManager;
37 import com.sun.logging.LogDomains;
38 import javax.resource.spi.*;
39 import javax.naming.*;
40 import javax.sql.*;
41 import java.security.*;
42 import java.sql.*;
43 import java.util.*;
44 import java.net.*;
45 import java.io.*;
46 import java.util.logging.*;
47
48
49 /**
50  * This class represents the abstraction of a 1.0 complient rar.
51  * It holds the ra.xml (connector decriptor) values, class loader used to
52  * to load the Resource adapter class and managed connection factory and
53  * module name (rar) to which it belongs.
54  * It is also the base class for ActiveInboundResourceAdapter(a 1.5 complient
55  * rar).
56  * @author : Srikanth P and Binod PG
57  */

58
59 public class ActiveOutboundResourceAdapter implements ActiveResourceAdapter {
60
61     protected ConnectorDescriptor desc_;
62     protected String JavaDoc moduleName_;
63     protected ClassLoader JavaDoc jcl_;
64     protected boolean writeToDomainXML_;
65     protected ConnectionDefDescriptor[] connectionDefs_;
66     protected ConnectorRuntime connectorRuntime_ = null;
67
68     static Logger _logger = LogDomains.getLogger(LogDomains.RSR_LOGGER);
69     private StringManager localStrings =
70         StringManager.getManager( ActiveOutboundResourceAdapter.class);
71
72     /**
73      * Constructor.
74      * @param desc Connector Descriptor. Holds the all ra.xml values
75      * @param moduleName Name of the module i.e rar Name. Incase of
76      * embedded resource adapters its name will be appName#rarName
77      * @param jcl Classloader used to load the ResourceAdapter and managed
78      * connection factory class.
79      * @param writeToDomainXML Flag indicating whether to write sun-ra.xml
80               values to domain.xml.
81      */

82
83     public ActiveOutboundResourceAdapter( ConnectorDescriptor desc,
84                  String JavaDoc moduleName, ClassLoader JavaDoc jcl,
85                  boolean writeToDomainXML)
86     {
87
88         this.desc_ = desc;
89         moduleName_ = moduleName;
90         jcl_ = jcl;
91         writeToDomainXML_ = writeToDomainXML;
92         connectorRuntime_ = ConnectorRuntime.getRuntime();
93         connectionDefs_ = ConnectorDDTransformUtils. getConnectionDefs(desc_);
94     }
95
96
97     public String JavaDoc getModuleName() {
98         return moduleName_;
99     }
100
101     /**
102      * It initializes the resource adapter. It also creates the default pools
103      * and resources of all the connection definitions.
104      * @throws ConnectorRuntimeException. This exception is thrown if the
105      * ra.xml is invalid or the default pools and resources couldn't
106      * be created
107      */

108
109     public void setup() throws ConnectorRuntimeException {
110
111         if(connectionDefs_ == null ||
112                     connectionDefs_.length != 1) {
113             _logger.log(Level.SEVERE,"rardeployment.invalid_connector_desc",
114                     moduleName_);
115         String JavaDoc i18nMsg = localStrings.getString(
116             "ccp_adm.invalid_connector_desc");
117             throw new ConnectorRuntimeException( i18nMsg );
118         }
119         ResourcesUtil resUtil = ResourcesUtil.getInstance();
120
121         if(isServer() && !resUtil.belongToSystemRar(moduleName_)) {
122             createAllConnectorResources();
123         }
124         _logger.log(Level.FINE,
125         "Completed Active Resource adapter setup", moduleName_);
126     }
127
128     /** Check if the execution environment is appserver runtime or application
129      * client container.
130      */

131
132     protected boolean isServer() {
133         if(connectorRuntime_.getEnviron() == ConnectorConstants.SERVER) {
134             return true;
135         } else {
136             return false;
137         }
138     }
139
140     /** Creates both the default connector connection pools and resources
141      */

142
143     protected void createAllConnectorResources()
144                          throws ConnectorRuntimeException
145     {
146         try {
147
148             if (desc_.getSunDescriptor() != null &&
149                 desc_.getSunDescriptor().getResourceAdapter() != null) {
150         
151                 // sun-ra.xml exists
152
String JavaDoc jndiName = (String JavaDoc)desc_.getSunDescriptor().
153                     getResourceAdapter().getValue(ResourceAdapter.JNDI_NAME);
154             
155                 if (jndiName == null || jndiName.equals("")) {
156                     // jndiName is empty, do not create duplicate pools, use setting in sun-ra.xml
157
createDefaultConnectorConnectionPools(true);
158                 } else {
159                     // jndiName is not empty, so create duplicate pools, both default and sun-ra.xml
160
createSunRAConnectionPool();
161                     createDefaultConnectorConnectionPools(false);
162                  }
163         
164              } else {
165             
166                  // sun-ra.xml doesn't exist, so create default pools
167
createDefaultConnectorConnectionPools(false);
168         
169              }
170         
171              // always create default connector resources
172
createDefaultConnectorResources();
173         } catch (ConnectorRuntimeException cre) {
174              //Connector deployment should _not_ fail if default connector
175
//connector pool and resource creation fails.
176
_logger.log(Level.SEVERE,"rardeployment.defaultpoolresourcecreation.failed",
177                     cre);
178             _logger.log(Level.FINE, "Error while trying to create the default connector" +
179                     "connection pool and resource", cre);
180           }
181
182     }
183
184     /** Deletes both the default connector connection pools and resources
185      */

186
187     protected void destroyAllConnectorResources() {
188        if(!(ResourcesUtil.getInstance().belongToSystemRar(moduleName_))){
189            deleteDefaultConnectorResources();
190            deleteDefaultConnectorConnectionPools();
191         
192            // Added to ensure clean-up of the Sun RA connection pool
193
if (desc_.getSunDescriptor() != null &&
194                desc_.getSunDescriptor().getResourceAdapter() != null) {
195                     
196                // sun-ra.xml exists
197
String JavaDoc jndiName = (String JavaDoc)desc_.getSunDescriptor().
198                    getResourceAdapter().getValue(ResourceAdapter.JNDI_NAME);
199             
200                if (jndiName == null || jndiName.equals("")) {
201                    // jndiName is empty, sunRA pool not created, so don't need to delete
202

203                 } else {
204                     // jndiName is not empty, need to delete pool
205
deleteSunRAConnectionPool();
206                 }
207            }
208        }
209     }
210
211     /** Deletes the default connector connection pools.
212      */

213
214     protected void deleteDefaultConnectorConnectionPools() {
215         for(int i=0;i<connectionDefs_.length;++i) {
216             String JavaDoc connectionDefName =
217                     connectionDefs_[i].getConnectionFactoryIntf();
218             String JavaDoc resourceJndiName = connectorRuntime_.getDefaultPoolName(
219                     moduleName_,
220                     connectionDefName);
221             try {
222                 connectorRuntime_.deleteConnectorConnectionPool(
223                                resourceJndiName);
224             } catch(ConnectorRuntimeException cre) {
225                 _logger.log(Level.WARNING,
226                     "rar.undeployment.default_resource_delete_fail",
227                     resourceJndiName);
228             }
229         }
230     }
231
232     /** Deletes the default connector resources.
233      */

234
235     protected void deleteDefaultConnectorResources() {
236         for(int i=0;i<connectionDefs_.length;++i) {
237             String JavaDoc connectionDefName =
238                     connectionDefs_[i].getConnectionFactoryIntf();
239             String JavaDoc resourceJndiName = connectorRuntime_.getDefaultResourceName(
240                     moduleName_,
241                     connectionDefName);
242             try {
243                 connectorRuntime_.deleteConnectorResource(resourceJndiName);
244             } catch(ConnectorRuntimeException cre) {
245                 _logger.log(Level.WARNING,
246                     "rar.undeployment.default_resource_delete_fail",
247                     resourceJndiName);
248                 _logger.log(Level.FINE, "Error while trying to delete the default " +
249                         "connector resource", cre);
250             }
251         }
252     }
253
254     /**
255      * uninitializes the resource adapter. It also destroys the default pools
256      * and resources
257      */

258
259     public void destroy() {
260         if(isServer()) {
261             destroyAllConnectorResources();
262         }
263     }
264
265     /** Returns the Connector descriptor which represents/holds ra.xml
266      * @return ConnectorDescriptor Representation of ra.xml.
267      */

268
269     public ConnectorDescriptor getDescriptor() {
270         return desc_;
271     }
272
273     /** Creates managed Connection factory instance.
274      * @param ccp Connector connection pool which contains the pool properties
275      * and ra.xml values pertaining to managed connection factory
276      * class. These values are used in MCF creation.
277      * @param jcl Classloader used to managed connection factory class.
278      * @return ManagedConnectionFactory created managed connection factory
279      * instance
280      */

281     public ManagedConnectionFactory createManagedConnectionFactory (
282                 ConnectorConnectionPool ccp, ClassLoader JavaDoc jcl)
283     {
284             final String JavaDoc mcfClass = ccp.getConnectorDescriptorInfo().
285                           getManagedConnectionFactoryClass();
286         try {
287
288              ManagedConnectionFactory mcf = null;
289              if (moduleName_.equals(ConnectorRuntime.DEFAULT_JMS_ADAPTER)) {
290                   Object JavaDoc tmp = AccessController.doPrivileged(
291                      new PrivilegedExceptionAction() {
292                   public Object JavaDoc run() throws Exception JavaDoc{
293                      return instantiateMCF(mcfClass);
294                     }
295                   });
296                   mcf = (ManagedConnectionFactory) tmp;
297          } else {
298                     mcf = instantiateMCF(mcfClass);
299              }
300
301             SetMethodAction setMethodAction = new SetMethodAction(mcf,
302                 ccp.getConnectorDescriptorInfo().getMCFConfigProperties());
303             setMethodAction.run();
304             _logger.log(Level.FINE,"Created MCF object : ",mcfClass);
305             return mcf;
306         } catch (PrivilegedActionException ex) {
307             _logger.log(Level.SEVERE,
308                       "rardeployment.privilegedaction_error",
309                        new Object JavaDoc[]{mcfClass, ex.getException().getMessage()});
310             _logger.log(Level.FINE,
311                     "rardeployment.privilegedaction_error",
312                        ex.getException());
313             return null;
314         } catch (ClassNotFoundException JavaDoc Ex) {
315             _logger.log(Level.SEVERE,"rardeployment.class_not_found",
316                         new Object JavaDoc[]{mcfClass, Ex.getMessage()});
317             _logger.log(Level.FINE,"rardeployment.class_not_found",
318                         Ex);
319             return null;
320         } catch (InstantiationException JavaDoc Ex) {
321             _logger.log(Level.SEVERE,
322                       "rardeployment.class_instantiation_error",
323                                 new Object JavaDoc[]{mcfClass, Ex.getMessage()});
324             _logger.log(Level.FINE,
325                     "rardeployment.class_instantiation_error",
326                                 Ex);
327             return null;
328         } catch (IllegalAccessException JavaDoc Ex) {
329             _logger.log(Level.SEVERE,
330                       "rardeployment.illegalaccess_error",
331                                 new Object JavaDoc[]{mcfClass, Ex.getMessage()});
332             _logger.log(Level.FINE,
333                     "rardeployment.illegalaccess_error",
334                                 Ex);
335
336             return null;
337         } catch (Exception JavaDoc Ex) {
338             _logger.log(Level.SEVERE,"rardeployment.mcfcreation_error",
339                         new Object JavaDoc[]{mcfClass, Ex.getMessage()});
340             _logger.log(Level.FINE,"rardeployment.mcfcreation_error",
341                         Ex);
342             return null;
343         }
344
345     }
346
347     private ManagedConnectionFactory instantiateMCF(String JavaDoc mcfClass)
348                                                    throws Exception JavaDoc {
349         if(jcl_ != null) {
350             return (ManagedConnectionFactory)jcl_.loadClass(
351                      mcfClass).newInstance();
352         } else {
353             return (ManagedConnectionFactory) Class.forName(
354                      mcfClass).newInstance();
355         }
356     }
357
358
359     /** Creates default connector resource
360      */

361
362     protected void createDefaultConnectorResources()
363                     throws ConnectorRuntimeException
364     {
365         for(int i=0; i<connectionDefs_.length;++i) {
366             String JavaDoc connectionDefName =
367                     connectionDefs_[i].getConnectionFactoryIntf();
368             String JavaDoc resourceName = connectorRuntime_.getDefaultResourceName(
369                     moduleName_,
370                     connectionDefName);
371             String JavaDoc poolName = connectorRuntime_.getDefaultPoolName(
372                     moduleName_,
373                     connectionDefName);
374             connectorRuntime_.createConnectorResource(
375                               resourceName,poolName,null);
376             _logger.log(Level.FINE,"Created default connector resource : ",
377                  resourceName);
378
379         }
380     }
381
382     /** Creates default connector connection pool
383      * @param useSunRA whether to use default pool settings or settings in sun-ra.xml
384      */

385
386     protected void createDefaultConnectorConnectionPools(boolean useSunRA)
387                     throws ConnectorRuntimeException
388     {
389
390         for(int i = 0;i < connectionDefs_.length;++i) {
391             String JavaDoc poolName = connectorRuntime_.getDefaultPoolName(
392                     moduleName_,
393                     connectionDefs_[i].getConnectionFactoryIntf());
394
395             ConnectorDescriptorInfo connectorDescriptorInfo =
396                        ConnectorDDTransformUtils.getConnectorDescriptorInfo(
397                        connectionDefs_[i]);
398             connectorDescriptorInfo.setRarName(moduleName_);
399             connectorDescriptorInfo.setResourceAdapterClassName(
400                        desc_.getResourceAdapterClass());
401             ConnectorConnectionPool connectorPoolObj;
402             
403             // if useSunRA is true, then create connectorPoolObject using settings
404
// from sunRAXML
405
if (useSunRA) {
406                 connectorPoolObj =
407                     ConnectionPoolObjectsUtils.createSunRaConnectorPoolObject(poolName, desc_, moduleName_);
408             } else {
409                 connectorPoolObj =
410                     ConnectionPoolObjectsUtils.createDefaultConnectorPoolObject(poolName, moduleName_);
411             }
412
413             connectorPoolObj.setConnectorDescriptorInfo(
414                 connectorDescriptorInfo );
415             connectorRuntime_.createConnectorConnectionPool(connectorPoolObj);
416             _logger.log(Level.FINE,"Created default connection pool : ",
417                  poolName);
418         }
419     }
420
421     /**
422      * Creates connector connection pool pertaining to sun-ra.xml. This is
423      * only for 1.0 complient rars.
424      * @throws ConnectorRuntimeException Thrown when pool creation fails.
425      */

426
427     public void createSunRAConnectionPool()
428                 throws ConnectorRuntimeException
429     {
430
431         String JavaDoc defaultPoolName = connectorRuntime_.getDefaultPoolName(
432                  moduleName_,connectionDefs_[0].getConnectionFactoryIntf());
433
434         String JavaDoc sunRAPoolName = defaultPoolName+ConnectorConstants.SUN_RA_POOL;
435
436         ConnectorDescriptorInfo connectorDescriptorInfo =
437             ConnectorDDTransformUtils.getConnectorDescriptorInfo(
438                  connectionDefs_[0]);
439         connectorDescriptorInfo.setRarName(moduleName_);
440         connectorDescriptorInfo.setResourceAdapterClassName(
441                  desc_.getResourceAdapterClass());
442         ConnectorConnectionPool connectorPoolObj =
443                  ConnectionPoolObjectsUtils.createSunRaConnectorPoolObject(
444                  sunRAPoolName,desc_,moduleName_);
445
446         connectorPoolObj.setConnectorDescriptorInfo(
447             connectorDescriptorInfo );
448         connectorRuntime_.createConnectorConnectionPool(connectorPoolObj);
449         _logger.log(Level.FINE,"Created SUNRA connection pool:",sunRAPoolName);
450         String JavaDoc jndiName = (String JavaDoc)desc_.getSunDescriptor().
451                  getResourceAdapter().getValue(ResourceAdapter.JNDI_NAME);
452         connectorRuntime_.createConnectorResource(jndiName,sunRAPoolName,null);
453         _logger.log(Level.FINE,"Created SUNRA connector resource : ",
454                      jndiName);
455
456         String JavaDoc key = System.getProperty("connector.sunra");
457         if(key != null && key.equals("true")) {
458             if(writeToDomainXML_) {
459                 writePoolResourceToServerXML(sunRAPoolName);
460             }
461         }
462     }
463
464     /**
465      * Added to clean up the connector connection pool pertaining to sun-ra.xml. This is
466      * only for 1.0 complient rars.
467      */

468
469     public void deleteSunRAConnectionPool()
470     {
471
472         String JavaDoc defaultPoolName = connectorRuntime_.getDefaultPoolName(
473                  moduleName_,connectionDefs_[0].getConnectionFactoryIntf());
474
475         String JavaDoc sunRAPoolName = defaultPoolName+ConnectorConstants.SUN_RA_POOL;
476         try {
477             connectorRuntime_.deleteConnectorConnectionPool(
478                            sunRAPoolName);
479         } catch(ConnectorRuntimeException cre) {
480             _logger.log(Level.WARNING,
481                 "rar.undeployment.default_resource_delete_fail",
482                 sunRAPoolName);
483         }
484     }
485
486     /**
487      * Returns the class loader that is used to load the RAR.
488      * @return <code>ClassLoader</code> object.
489      */

490     public ClassLoader JavaDoc getClassLoader(){
491         return jcl_;
492     }
493
494     /**
495      * Writes the sun-ra.xml properties to domain.xml
496      */

497
498     protected void writePoolResourceToServerXML(String JavaDoc poolName)
499                  throws ConnectorRuntimeException
500     {
501
502         ResourcesUtil resutil = ResourcesUtil.getInstance();
503         try {
504             resutil.writeSunConnector(desc_,moduleName_,poolName);
505         } catch(ConfigException ce) {
506              String JavaDoc i18nMsg = localStrings.getString("ccr_adm.err_writing_connector_desc");
507              ConnectorRuntimeException crex = new ConnectorRuntimeException(i18nMsg);
508              crex.initCause(ce);
509              throw crex;
510         }
511     }
512 }
513
Popular Tags