KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Hashtable JavaDoc;
27 import java.util.logging.*;
28 import javax.naming.*;
29
30 import com.sun.enterprise.*;
31 import com.sun.enterprise.deployment.*;
32 import com.sun.enterprise.server.*;
33
34 /**
35  * This is connector resource admin service. It creates and deletes the
36  * connector resources.
37  * @author Srikanth P
38  */

39
40
41 public class ConnectorResourceAdminServiceImpl extends
42                      ConnectorServiceImpl implements ConnectorAdminService {
43      
44     /**
45      * Default constructor
46      */

47
48      public ConnectorResourceAdminServiceImpl() {
49          super();
50      }
51
52     /**
53      * Creates the connector resource on a given connection pool
54      * @param jndiName JNDI name of the resource to be created
55      * @poolName PoolName to which the connector resource belongs.
56      * @resourceType Resource type Unused.
57      * @throws ConnectorRuntimeException If the resouce creation fails.
58      */

59
60     public void createConnectorResource(String JavaDoc jndiName, String JavaDoc poolName,
61                     String JavaDoc resourceType) throws ConnectorRuntimeException
62     {
63
64         String JavaDoc errMsg = "rardeployment.jndi_lookup_failed";
65         String JavaDoc name = poolName;
66         try {
67         ConnectorConnectionPool connectorConnectionPool = null;
68             String JavaDoc jndiNameForPool = ConnectorAdminServiceUtils.
69                 getReservePrefixedJNDINameForPool(poolName);
70             InitialContext ic = new InitialContext();
71             try {
72                 connectorConnectionPool =
73                     (ConnectorConnectionPool) ic.lookup(jndiNameForPool);
74             } catch(NamingException ne) {
75                 checkAndLoadPoolResource(poolName);
76             }
77
78             connectorConnectionPool =
79                     (ConnectorConnectionPool) ic.lookup(jndiNameForPool);
80             ConnectorDescriptorInfo cdi = connectorConnectionPool.
81                 getConnectorDescriptorInfo();
82
83             javax.naming.Reference JavaDoc ref=new javax.naming.Reference JavaDoc(
84                    connectorConnectionPool.getConnectorDescriptorInfo().
85                    getConnectionFactoryClass(),
86                    "com.sun.enterprise.naming.factory.ConnectorObjectFactory",
87                    null);
88             StringRefAddr addr = new StringRefAddr("poolName",poolName);
89             ref.add(addr);
90             addr = new StringRefAddr("rarName", cdi.getRarName() );
91             ref.add(addr);
92
93             errMsg = "Failed to bind connector resource in JNDI";
94             name = jndiName;
95             Switch.getSwitch().getNamingManager().publishObject(
96                           jndiName,ref,true);
97             //To notify that a connector resource rebind has happened.
98
ConnectorResourceNamingEventNotifier.getInstance().
99                     notifyListeners(
100                             new ConnectorNamingEvent(
101                                     jndiName,ConnectorNamingEvent.EVENT_OBJECT_REBIND));
102
103         } catch(NamingException ne) {
104             ConnectorRuntimeException cre =
105                   new ConnectorRuntimeException(errMsg);
106             cre.initCause(ne);
107             _logger.log(Level.SEVERE,errMsg, name);
108             _logger.log(Level.SEVERE,"", cre);
109             throw cre;
110         }
111     }
112
113     /**
114      * Deletes the connector resource.
115      * @param jndiName JNDI name of the resource to delete.
116      * @throws ConnectorRuntimeException if connector resource deletion fails.
117      */

118
119     public void deleteConnectorResource(String JavaDoc jndiName)
120                        throws ConnectorRuntimeException
121     {
122
123         try {
124             InitialContext ic = new InitialContext();
125             ic.unbind(jndiName);
126         } catch(NamingException ne) {
127             ResourcesUtil resUtil = ResourcesUtil.getInstance();
128             if(resUtil.resourceBelongsToSystemRar(jndiName)) {
129                 return;
130             }
131             if(ne instanceof NameNotFoundException){
132                 _logger.log(Level.FINE,
133                     "rardeployment.connectorresource_removal_from_jndi_error",
134                     jndiName);
135                 _logger.log(Level.FINE,"", ne);
136                 return;
137             }
138             ConnectorRuntimeException cre = new ConnectorRuntimeException(
139                             "Failed to delete connector resource from jndi");
140             cre.initCause(ne);
141             _logger.log(Level.SEVERE,
142                     "rardeployment.connectorresource_removal_from_jndi_error",
143                     jndiName);
144             _logger.log(Level.SEVERE,"", cre);
145             throw cre;
146         }
147     }
148
149     /**
150      * If the suffix is one of the valid context return true.
151      * Return false, if that is not the case.
152      */

153     public boolean isValidJndiSuffix(String JavaDoc suffix) {
154         if (suffix != null) {
155             for (String JavaDoc validSuffix : ConnectorConstants.JNDI_SUFFIX_VALUES) {
156                 if (validSuffix.equals(suffix)) {
157                     return true;
158                 }
159             }
160         }
161
162         return false;
163     }
164
165     /**
166      * Look up the JNDI name with appropriate suffix.
167      * Suffix can be either __pm or __nontx.
168      */

169     public Object JavaDoc lookup(String JavaDoc name) throws NamingException {
170         Hashtable JavaDoc ht = null;
171         String JavaDoc suffix = getValidSuffix(name);
172         if (suffix != null) {
173             ht = new Hashtable JavaDoc();
174             ht.put(ConnectorConstants.JNDI_SUFFIX_PROPERTY, suffix);
175             name = name.substring(0, name.lastIndexOf(suffix));
176         }
177         InitialContext ic = new InitialContext(ht);
178         return ic.lookup(name);
179     }
180
181     /**
182      * Gets Connector Resource Rebind Event notifier.
183      * @return ConnectorNamingEventNotifier
184      */

185     public ConnectorNamingEventNotifier getResourceRebindEventNotifier(){
186         return ConnectorResourceNamingEventNotifier.getInstance();
187     }
188
189     private String JavaDoc getValidSuffix(String JavaDoc name) {
190         if (name != null) {
191             for (String JavaDoc validSuffix : ConnectorConstants.JNDI_SUFFIX_VALUES) {
192                 if (name.endsWith(validSuffix)) {
193                     return validSuffix;
194                 }
195             }
196         }
197         return null;
198     }
199 }
200
Popular Tags