KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > naming > factory > ConnectorObjectFactory


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.naming.factory;
24
25 import java.util.*;
26 import javax.naming.*;
27 import javax.naming.spi.*;
28
29 import java.util.Hashtable JavaDoc;
30 import javax.resource.spi.ConnectionManager JavaDoc;
31 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
32
33 import com.sun.enterprise.*;
34 import com.sun.enterprise.util.LocalStringManagerImpl;
35 import com.sun.enterprise.connectors.*;
36 import com.sun.enterprise.deployment.ConnectorDescriptor;
37
38 import java.util.logging.*;
39 import com.sun.logging.*;
40
41 /**
42  * An object factory to handle creation of Connection Factories
43  *
44  * @author Tony Ng
45  *
46  */

47 public class ConnectorObjectFactory implements ObjectFactory {
48
49     static Logger _logger=LogDomains.getLogger(LogDomains.JNDI_LOGGER);
50
51     private static LocalStringManagerImpl localStrings =
52     new LocalStringManagerImpl(ConnectorObjectFactory.class);
53     private ConnectorRuntime runtime = ConnectorRuntime.getRuntime();
54
55     public ConnectorObjectFactory() {
56     }
57
58     public Object JavaDoc getObjectInstance(Object JavaDoc obj,
59                     Name name,
60                     Context nameCtx,
61                     Hashtable JavaDoc env) throws Exception JavaDoc
62     {
63     Reference ref = (Reference) obj;
64     if(_logger.isLoggable(Level.FINE)) {
65         _logger.log(Level.FINE,"ConnectorObjectFactory: " + ref +
66             " Name:" + name);
67     }
68         String JavaDoc poolName = (String JavaDoc) ref.get(0).getContent();
69         String JavaDoc moduleName = (String JavaDoc) ref.get(1).getContent();
70
71         Switch sw = Switch.getSwitch();
72
73         if(runtime.getEnviron() == ConnectorRuntime.CLIENT) {
74             ConnectorDescriptor connectorDescriptor = null;
75             try {
76                 Context ic = new InitialContext();
77                 String JavaDoc descriptorJNDIName = ConnectorAdminServiceUtils.
78                     getReservePrefixedJNDINameForDescriptor(moduleName);
79                 connectorDescriptor = (ConnectorDescriptor)ic.lookup(descriptorJNDIName);
80             }
81             catch(NamingException ne) {
82                 _logger.log(Level.FINE,
83                 "Failed to look up ConnectorDescriptor from JNDI",
84                 moduleName);
85                 throw new ConnectorRuntimeException(
86                 "Failed to look up ConnectorDescriptor from JNDI");
87             }
88             runtime.createActiveResourceAdapter(connectorDescriptor,
89                         moduleName,null,false);
90         }
91
92         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
93         if (runtime.checkAccessibility(moduleName, loader) == false) {
94         throw new NamingException(
95               "Only the application that has the embedded resource" +
96               "adapter can access the resource adapter");
97     }
98     
99
100         ManagedConnectionFactory JavaDoc mcf = runtime.obtainManagedConnectionFactory(poolName);
101         if(mcf == null) {
102                 _logger.log(Level.FINE,"Failed to create MCF ",poolName);
103                 throw new ConnectorRuntimeException("Failed to create MCF");
104         }
105
106         String JavaDoc jndiName = name.toString();
107         boolean forceNoLazyAssoc = false;
108         if ( jndiName.endsWith( ConnectorConstants.PM_JNDI_SUFFIX ) ) {
109             forceNoLazyAssoc = true;
110         }
111         ConnectionManagerImpl mgr = (ConnectionManagerImpl)
112             runtime.obtainConnectionManager(poolName, forceNoLazyAssoc);
113         mgr.setJndiName(deriveJndiName(jndiName, env));
114         mgr.setRarName( moduleName );
115         mgr.initialize();
116
117         Object JavaDoc cf = mcf.createConnectionFactory(mgr);
118         if (cf == null) {
119             String JavaDoc msg = localStrings.getLocalString
120                 ("no.resource.adapter", "");
121             throw new ConfigurationException(msg);
122         }
123     if(_logger.isLoggable(Level.FINE)) {
124         _logger.log(Level.FINE,"Connection Factory:" + cf);
125     }
126
127     return cf;
128     }
129
130     private String JavaDoc deriveJndiName(String JavaDoc name, Hashtable JavaDoc env) {
131         String JavaDoc suffix = (String JavaDoc) env.get(ConnectorConstants.JNDI_SUFFIX_PROPERTY);
132         if (runtime.isValidJndiSuffix(suffix)) {
133             _logger.log(Level.FINE, "JNDI name will be suffixed with :" + suffix);
134             return name + suffix;
135         }
136         return name;
137     }
138
139 }
140
Popular Tags