KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
25  * @(#) JdbcResourceDeployer.java
26  *
27  * Copyright 2000-2001 by iPlanet/Sun Microsystems, Inc.,
28  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
29  * All rights reserved.
30  *
31  * This software is the confidential and proprietary information
32  * of iPlanet/Sun Microsystems, Inc. ("Confidential Information").
33  * You shall not disclose such Confidential Information and shall
34  * use it only in accordance with the terms of the license
35  * agreement you entered into with iPlanet/Sun Microsystems.
36  */

37 package com.sun.enterprise.resource;
38
39 import com.sun.enterprise.server.ResourceDeployer;
40 import com.sun.enterprise.server.ResourcesUtil;
41
42 import com.sun.enterprise.config.serverbeans.Resources;
43 //import com.sun.enterprise.config.serverbeans.JdbcResource;
44
import com.sun.enterprise.connectors.ConnectorAdminServiceUtils;
45 import com.sun.enterprise.connectors.ConnectorRuntime;
46
47 import com.sun.enterprise.Switch;
48 import com.sun.enterprise.NamingManager;
49 import com.sun.enterprise.resource.ResourceInstaller;
50 import com.sun.enterprise.repository.JdbcConnectionPool;
51 import com.sun.enterprise.repository.IASJdbcResource;
52 import com.sun.enterprise.repository.JdbcResource;
53 import com.sun.enterprise.repository.JdbcXAResource;
54 import com.sun.enterprise.repository.IASJdbcXAResource;
55 import com.sun.enterprise.repository.J2EEResource;
56 import com.sun.enterprise.repository.IASJ2EEResourceFactoryImpl;
57
58 import java.util.logging.Logger JavaDoc;
59 import java.util.logging.Level JavaDoc;
60 import com.sun.logging.LogDomains;
61 import com.sun.enterprise.util.i18n.StringManager;
62
63 import com.sun.enterprise.ManagementObjectManager;
64
65 import com.sun.enterprise.connectors.ConnectorConstants;
66 import javax.naming.InitialContext JavaDoc;
67 import com.sun.enterprise.config.ConfigBean;
68 import com.sun.enterprise.config.ConfigException;
69
70
71 /**
72  * Handles Jdbc resource events in the server instance. When user adds a
73  * jdbc resource, the admin instance emits resource event. The jdbc
74  * resource events are propagated to this object.
75  *
76  * The methods can potentially be called concurrently, therefore implementation
77  * need to be synchronized.
78  *
79  * @author Nazrul Islam
80  * @since JDK1.4
81  */

82 public class JdbcResourceDeployer implements ResourceDeployer {
83     
84     private static final StringManager localStrings =
85         StringManager.getManager("com.sun.enterprise.resource");
86     /** logger for this deployer */
87     private static Logger JavaDoc _logger=LogDomains.getLogger(LogDomains.CORE_LOGGER);
88     private static final String JavaDoc PM_JNDI_EXTENSION = "__pm";
89     /**
90      * Deploy the resource into the server's runtime naming context
91      *
92      * @param resoure a resource object (eg. JmsResource)
93      * @exception Exception thrown if fail
94      */

95     public synchronized void deployResource(Object JavaDoc resource) throws Exception JavaDoc {
96         
97         com.sun.enterprise.config.serverbeans.JdbcResource jdbcRes =
98             (com.sun.enterprise.config.serverbeans.JdbcResource) resource;
99         
100         if (jdbcRes.isEnabled()) {
101             String JavaDoc jndiName = jdbcRes.getJndiName();
102         String JavaDoc poolName = jdbcRes.getPoolName();
103
104         // loads dependent jdbc connection pool if not loaded
105
loadPool(jdbcRes);
106
107         ManagementObjectManager mgr = Switch.getSwitch().getManagementObjectManager();
108         mgr.registerJDBCResource( jndiName );
109         ConnectorRuntime runtime = ConnectorRuntime.getRuntime();
110         runtime.createConnectorResource( jndiName, poolName, null);
111         runtime.createConnectorResource( getPMJndiName( jndiName),
112                 poolName, null);
113         _logger.finest("deployed resource " + jndiName );
114         } else {
115             _logger.log(Level.INFO, "core.resource_disabled",
116                 new Object JavaDoc[] {jdbcRes.getJndiName(),
117                               IASJ2EEResourceFactoryImpl.JDBC_RES_TYPE});
118         }
119     }
120
121     /**
122      * Undeploy the resource from the server's runtime naming context
123      *
124      * @param resoure a resource object (eg. JmsResource)
125      * @exception Exception thrown if fail
126      */

127     public synchronized void undeployResource(Object JavaDoc resource)
128             throws Exception JavaDoc {
129
130         com.sun.enterprise.config.serverbeans.JdbcResource jdbcRes =
131             (com.sun.enterprise.config.serverbeans.JdbcResource) resource;
132         
133     String JavaDoc jndiName = jdbcRes.getJndiName();
134     String JavaDoc pmJndiName = getPMJndiName( jdbcRes.getJndiName() );
135
136     ConnectorRuntime runtime = ConnectorRuntime.getRuntime();
137     runtime.deleteConnectorResource( jndiName );
138     runtime.deleteConnectorResource( pmJndiName );
139     
140         ManagementObjectManager mgr =
141                 Switch.getSwitch().getManagementObjectManager();
142         mgr.unregisterJDBCResource( jndiName );
143
144         //Since 8.1 PE/SE/EE - if no more resource-ref to the pool
145
//of this resource exists in this server instance, remove
146
//pool from connector runtime
147
checkAndDeletePool(jdbcRes);
148         
149
150     }
151
152     /**
153      * Redeploy the resource into the server's runtime naming context
154      *
155      * @param resoure a resource object (eg. JmsResource)
156      * @exception Exception thrown if fail
157      */

158     public synchronized void redeployResource(Object JavaDoc resource)
159             throws Exception JavaDoc {
160
161         undeployResource(resource);
162         deployResource(resource);
163     }
164
165     /**
166      * Enable the resource in the server's runtime naming context
167      *
168      * @param resoure a resource object (eg. JmsResource)
169      * @exception Exception thrown if fail
170      */

171     public synchronized void enableResource(Object JavaDoc resource) throws Exception JavaDoc {
172         deployResource(resource);
173     }
174
175     /**
176      * Disable the resource in the server's runtime naming context
177      *
178      * @param resoure a resource object (eg. JmsResource)
179      * @exception Exception thrown if fail
180      */

181     public synchronized void disableResource(Object JavaDoc resource) throws Exception JavaDoc {
182         undeployResource(resource);
183     }
184
185
186     /**
187      * Utility method to find a resource from Resources beans and converte
188      * it to a resource object to be used by the implemented ResourceDeployer
189      *
190      * @param name resource name (normally the jndi-name)
191      * @param rbeans Resources config-beans
192      * @exception Exception thrown if fail
193      */

194     public Object JavaDoc getResource(String JavaDoc name, Resources rbeans) throws Exception JavaDoc {
195
196         Object JavaDoc res = rbeans.getJdbcResourceByJndiName(name);
197
198         if (res == null) {
199             String JavaDoc msg = localStrings.getString("resource.no_resource",name);
200             throw new Exception JavaDoc(msg);
201         }
202
203         return res;
204     }
205
206
207     /* Return the system PM name for the JNDI name*/
208     private String JavaDoc getPMJndiName( String JavaDoc jndiName ) {
209         return jndiName + PM_JNDI_EXTENSION;
210     }
211
212     private void loadPool(com.sun.enterprise.config.serverbeans.JdbcResource jr)
213             throws Exception JavaDoc {
214
215         String JavaDoc poolName = jr.getPoolName();
216         Resources resources = (Resources) jr.parent();
217         ConfigBean cb = resources.getJdbcConnectionPoolByName(poolName);
218         if (cb != null) {
219             try {
220                 InitialContext JavaDoc ic = new InitialContext JavaDoc();
221                 ic.lookup(ConnectorAdminServiceUtils.
222                                 getReservePrefixedJNDINameForPool(poolName));
223             } catch (Exception JavaDoc e) {
224                 // pool is not loaded
225
JdbcConnectionPoolDeployer deployer =
226                         new JdbcConnectionPoolDeployer();
227                 deployer.actualDeployResource(
228                  (com.sun.enterprise.config.serverbeans.JdbcConnectionPool)
229                  cb);
230             }
231         }
232     }
233
234     /**
235      * Checks if no more resource-refs to resources exists for the
236      * JDBC connection pool and then deletes the pool
237      * @param domainResource
238      * @throws Exception
239      * @since 8.1 pe/se/ee
240      */

241     private void checkAndDeletePool(com.sun.enterprise.config.serverbeans.JdbcResource
242                     cr) throws Exception JavaDoc {
243         String JavaDoc poolName = cr.getPoolName();
244         Resources res = (Resources) cr.parent();
245         
246         try {
247             boolean poolReferred =
248                 ResourcesUtil.getInstance().isJdbcPoolReferredInServerInstance(poolName);
249             if (!poolReferred) {
250                 _logger.fine("Deleting JDBC pool " + poolName + "as there is no more " +
251                         "resource-refs to the pool in this server instance");
252                 com.sun.enterprise.config.serverbeans.JdbcConnectionPool jcp
253                                     = res.getJdbcConnectionPoolByName(poolName);
254                 //Delete/Undeploy Pool
255
JdbcConnectionPoolDeployer deployer =
256                         new JdbcConnectionPoolDeployer();
257                 deployer.actualUndeployResource(jcp);
258             }
259         } catch (ConfigException ce) {
260             _logger.warning(ce.getMessage());
261             _logger.fine("Exception while deleting pool : " + ce );
262             throw ce;
263         }
264     }
265     
266 }
267
Popular Tags