KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > repository > ConnectorResource


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 package com.sun.enterprise.repository;
24
25 import java.security.AccessController JavaDoc;
26 import java.security.PrivilegedAction JavaDoc;
27 import java.security.PrivilegedExceptionAction JavaDoc;
28 import java.security.PrivilegedActionException JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30 import java.io.Serializable JavaDoc;
31 import java.util.*;
32 import javax.naming.*;
33 import javax.resource.spi.*;
34 import javax.resource.ResourceException JavaDoc;
35 import com.sun.enterprise.util.Utility;
36 import com.sun.enterprise.util.JarClassLoader;
37 import com.sun.enterprise.resource.*;
38 import com.sun.enterprise.deployment.ConnectorDescriptor;
39 import com.sun.enterprise.deployment.EnvironmentProperty;
40 import com.sun.enterprise.Switch;
41 import com.sun.enterprise.log.Log;
42 // IASRI 4660742 START
43
import java.util.logging.*;
44 import com.sun.logging.*;
45 // IASRI 4660742 END
46

47 import com.sun.enterprise.connectors.ConnectionManagerImpl;
48 /**
49  * Resource info for Connector connection factories
50  *
51  * @author Tony Ng
52  */

53 public class ConnectorResource extends J2EEResourceBase implements Serializable JavaDoc {
54
55 // IASRI 4660742 START
56
private static Logger _logger=null;
57     static{
58        _logger=LogDomains.getLogger(LogDomains.ROOT_LOGGER);
59         }
60 // IASRI 4660742 END
61
static public final String JavaDoc CONNECTOR_OBJECT_FACTORY =
62         "com.sun.enterprise.naming.factory.ConnectorObjectFactory";
63
64     private String JavaDoc dbuser_;
65     private String JavaDoc dbpassword_;
66     private String JavaDoc connectorname_;
67     private String JavaDoc appname_;
68     private String JavaDoc connectionFactoryClass_;
69     
70     public ConnectorResource(String JavaDoc name) {
71         super(name);
72         dbuser_ = "";
73         dbpassword_ = "";
74         appname_ = "";
75     }
76
77     protected J2EEResource doClone(String JavaDoc name) {
78         ConnectorResource clone = new ConnectorResource(name);
79         clone.setDbuser(getDbuser());
80         clone.setDbpassword(getDbpassword());
81         clone.setConnectorName(getConnectorName());
82         clone.setApplicationName(getApplicationName());
83         clone.setConnectionFactoryClass(getConnectionFactoryClass());
84         return clone;
85     }
86
87     public int getType() {
88         return J2EEResource.CONNECTOR_RESOURCE;
89     }
90
91     public String JavaDoc getApplicationName() {
92         return appname_;
93     }
94
95     public void setApplicationName(String JavaDoc appname) {
96         if (appname == null) {
97             appname_ = "";
98         } else {
99             appname_ = appname;
100         }
101     }
102
103     public String JavaDoc getConnectorName() {
104         return connectorname_;
105     }
106
107     public void setConnectorName(String JavaDoc connectorname) {
108         connectorname_ = connectorname;
109     }
110
111     public String JavaDoc getConnectionFactoryClass() {
112         return connectionFactoryClass_;
113     }
114
115     public void setConnectionFactoryClass(String JavaDoc cfclass) {
116         connectionFactoryClass_ = cfclass;
117     }
118
119     public String JavaDoc getDbuser() {
120         return dbuser_;
121     }
122
123     public void setDbuser(String JavaDoc dbuser) {
124         if (dbuser == null) {
125             dbuser_ = "";
126         } else {
127             dbuser_ = dbuser;
128         }
129     }
130
131     public String JavaDoc getDbpassword() {
132         return dbpassword_;
133     }
134
135     public void setDbpassword(String JavaDoc dbpassword) {
136         if (dbpassword == null) {
137             dbpassword_ = "";
138         } else {
139             dbpassword_ = dbpassword;
140         }
141     }
142
143
144     public Reference createConnectionFactoryReference() {
145         Reference ref =
146             new Reference(getConnectionFactoryClass(),
147                           new StringRefAddr("jndiName", getName()),
148                           CONNECTOR_OBJECT_FACTORY, null);
149         return ref;
150     }
151
152
153     // this can be invoked when a component looks up a connection
154
// factory from JNDI. Need doPrivileged actions
155
public ManagedConnectionFactory
156         createManagedConnectionFactory(ClassLoader JavaDoc jcl)
157         throws PoolingException {
158
159         try {
160             if (jcl == null) {
161                 // use context class loader
162
jcl = (ClassLoader JavaDoc) AccessController.doPrivileged
163                     (new PrivilegedAction JavaDoc() {
164                         public Object JavaDoc run() {
165                             return Thread.currentThread().getContextClassLoader();
166                         }
167                     });
168             }
169                 
170             ResourceInstaller ri = Switch.getSwitch().getResourceInstaller();
171             ConnectorDescriptor desc =
172                 ri.getDescriptorByConnectorName(getApplicationName(),
173                                                 getConnectorName());
174             String JavaDoc jndiName = getName();
175             String JavaDoc mcfClass = desc.getManagedConnectionFactoryImpl();
176
177             String JavaDoc appName = getApplicationName();
178
179             /* debug
180 //IASRI 4660742 System.err.println("ClassLoader: " + jcl.getClass());
181 //IASRI 4660742 System.err.println("ClassLoader: " + jcl.getParent().getClass());
182 //IASRI 4660742 System.err.println("ClassLoader: " + jcl.getParent().getParent().getClass());
183             */

184 // START OF IASRI 4660742
185
_logger.log(Level.FINE,"ClassLoader: " + jcl.getClass());
186         _logger.log(Level.FINE,"ClassLoader: " + jcl.getParent().getClass());
187         _logger.log(Level.FINE,"ClassLoader: " + jcl.getParent().getParent().getClass());
188 // END OF IASRI 4660742
189
ManagedConnectionFactory mcf = (ManagedConnectionFactory)
190                 Class.forName(mcfClass, true, jcl).newInstance();
191             AccessController.doPrivileged
192                 (new SetMethodAction(mcf, desc));
193             return mcf;
194         } catch (PrivilegedActionException JavaDoc ex) {
195             Exception JavaDoc nested = ex.getException();
196 //IASRI 4660742 Log.err.println(nested);
197
// START OF IASRI 4660742
198
_logger.log(Level.SEVERE,"enterprise.privilegedaction_exception",nested.toString());
199 // END OF IASRI 4660742
200
Log.err.flush();
201             throw new PoolingException(nested.toString(), nested);
202         } catch (Exception JavaDoc ex) {
203 //IASRI 4660742 Log.err.println(ex);
204
// START OF IASRI 4660742
205
_logger.log(Level.SEVERE,"enterprise.managedconnfactory_exception",ex.toString());
206 // END OF IASRI 4660742
207
Log.err.flush();
208             throw new PoolingException(ex.toString(), ex);
209         }
210
211     }
212
213     // called by com.sun.enterprise.naming.ConnectorObjectFactory
214
public Object JavaDoc createConnectionFactory()
215         throws PoolingException {
216
217         ManagedConnectionFactory mcf = createManagedConnectionFactory(null);
218         try {
219             Object JavaDoc cf = mcf.createConnectionFactory
220                 (new ConnectionManagerImpl(getName()));
221             return cf;
222         } catch (ResourceException JavaDoc ex) {
223 //IASRI 4660742 Log.err.println(ex);
224
// START OF IASRI 4660742
225
_logger.log(Level.SEVERE,"enterprise.resource_exception",ex.toString());
226 // END OF IASRI 4660742
227
Log.err.flush();
228             throw new PoolingException(ex.toString(), ex);
229         }
230     }
231
232     // invokes setMethods on ManagedConnectionFactory
233
class SetMethodAction implements PrivilegedExceptionAction JavaDoc {
234         ManagedConnectionFactory mcf;
235         ConnectorDescriptor desc;
236
237         SetMethodAction(ManagedConnectionFactory mcf,
238                         ConnectorDescriptor desc) {
239             this.mcf = mcf;
240             this.desc = desc;
241         }
242
243         public Object JavaDoc run() throws Exception JavaDoc {
244
245             Iterator iter = desc.getConfigProperties().iterator();
246             while (iter.hasNext()) {
247                 EnvironmentProperty envProp =
248                     (EnvironmentProperty) iter.next();
249                 String JavaDoc propName = envProp.getName();
250                 String JavaDoc propType = envProp.getType();
251                 String JavaDoc propValue = (String JavaDoc) getProperty(propName).getValue();
252                 // use EnvironmentProperty to get valueObject
253
EnvironmentProperty newProp =
254                     new EnvironmentProperty(propName, propValue, "", propType);
255
256                 // set properties on managed connection factory
257
// change first letter to uppercase
258
String JavaDoc setMeth =
259                     "set" + propName.substring(0,1).toUpperCase() +
260                     propName.substring(1);
261                 Method JavaDoc meth = mcf.getClass().getMethod
262                     (setMeth, new Class JavaDoc[] {newProp.getValueType()});
263                 meth.invoke(mcf, new Object JavaDoc[] {newProp.getValueObject()});
264             }
265             return null;
266         }
267
268     }
269
270     public String JavaDoc toString() {
271         return "< Connector Resource : " + getName() +
272             " , " + getApplicationName() +
273             " , " + getConnectorName() + " , " + getDbuser() +
274             " , " + getDbpassword() + " >";
275             
276     }
277 }
278
Popular Tags