KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.sun.enterprise.resource;
26
27 import com.sun.enterprise.server.ResourceDeployer;
28 import com.sun.enterprise.server.ResourcesUtil;
29
30 import com.sun.enterprise.config.serverbeans.Resources;
31 import com.sun.enterprise.config.serverbeans.ConnectorResource;
32 import com.sun.enterprise.connectors.ConnectorAdminServiceUtils;
33 import com.sun.enterprise.connectors.ConnectorRuntime;
34 import com.sun.enterprise.connectors.ConnectorConnectionPool;
35 import com.sun.enterprise.deployment.ConnectionDefDescriptor;
36 import com.sun.logging.LogDomains;
37 import com.sun.enterprise.config.serverbeans.Domain;
38 import com.sun.enterprise.connectors.ConnectorConstants;
39 import javax.naming.InitialContext JavaDoc;
40 import com.sun.enterprise.config.ConfigBean;
41 import com.sun.enterprise.config.ConfigException;
42 import com.sun.enterprise.connectors.util.ConnectionPoolObjectsUtils;
43
44 import java.util.logging.*;
45 import java.util.regex.Matcher JavaDoc;
46 import java.util.regex.Pattern JavaDoc;
47
48 /**
49   *
50   * @author Srikanth P
51   * @version
52  */

53
54 public class ConnectorResourceDeployer implements ResourceDeployer {
55
56     static Logger _logger = LogDomains.getLogger(LogDomains.CORE_LOGGER);
57     public synchronized void deployResource(Object JavaDoc resource) throws Exception JavaDoc {
58
59         ConnectorResource domainResource =
60             (com.sun.enterprise.config.serverbeans.ConnectorResource)resource;
61         String JavaDoc jndiName = domainResource.getJndiName();
62         String JavaDoc poolName = domainResource.getPoolName();
63         ConnectorRuntime crt = ConnectorRuntime.getRuntime();
64         _logger.log(Level.FINE,
65                    "Calling backend to add connector resource",jndiName);
66
67         // load connection pool if not loaded from the ctx in event
68
loadPool(domainResource);
69
70         crt.createConnectorResource(jndiName,poolName,null);
71         _logger.log(Level.FINE,
72                    "Added connector resource in backend",jndiName);
73     }
74
75     public synchronized void undeployResource(Object JavaDoc resource)
76                   throws Exception JavaDoc {
77         ConnectorResource domainResource =
78            (com.sun.enterprise.config.serverbeans.ConnectorResource)resource;
79         String JavaDoc jndiName = domainResource.getJndiName();
80         ConnectorRuntime crt = ConnectorRuntime.getRuntime();
81         crt.deleteConnectorResource(jndiName);
82         
83         //Since 8.1 PE/SE/EE - if no more resource-ref to the pool
84
//of this resource in this server instance, remove pool from connector
85
//runtime
86
checkAndDeletePool(domainResource);
87         
88     }
89
90
91     public synchronized void redeployResource(Object JavaDoc resource)
92                   throws Exception JavaDoc {
93     }
94
95     public synchronized void disableResource(Object JavaDoc resource)
96                   throws Exception JavaDoc {
97         undeployResource(resource);
98     }
99
100     public synchronized void enableResource(Object JavaDoc resource) throws Exception JavaDoc {
101         deployResource(resource);
102     }
103
104     public Object JavaDoc getResource(String JavaDoc name, Resources rbeans) throws Exception JavaDoc {
105
106         Object JavaDoc res = rbeans.getConnectorResourceByJndiName(name);
107         if (res == null) {
108             Exception JavaDoc ex = new Exception JavaDoc("No such resource");
109             _logger.log(Level.SEVERE,"no_resource",name);
110             _logger.log(Level.SEVERE,"",ex);
111             throw ex;
112         }
113         return res;
114
115     }
116
117     private void loadPool(ConnectorResource cr) throws Exception JavaDoc {
118
119         String JavaDoc poolName = cr.getPoolName();
120         Resources resources = (Resources) cr.parent();
121         ConfigBean cb = resources.getConnectorConnectionPoolByName(poolName);
122         com.sun.enterprise.config.serverbeans.ConnectorConnectionPool cp =
123             (com.sun.enterprise.config.serverbeans.ConnectorConnectionPool)cb;
124         if (cb != null) {
125             if (ConnectionPoolObjectsUtils.isPoolSystemPool(poolName)){
126                 createPool(cp);
127             } else {
128                 try {
129                     InitialContext JavaDoc ic = new InitialContext JavaDoc();
130                     ic.lookup(ConnectorAdminServiceUtils.getReservePrefixedJNDINameForPool(poolName));
131                 } catch (Exception JavaDoc e) {
132                     // pool is not loaded
133
createPool(cp);
134                 }
135             }
136         }
137     }
138
139     /**
140      * Checks if no more resource-refs to resources exists for the
141      * connector connection pool and then deletes the pool
142      * @param domainResource
143      * @throws Exception
144      * @since 8.1 pe/se/ee
145      */

146     private void checkAndDeletePool(ConnectorResource cr) throws Exception JavaDoc {
147         try {
148             String JavaDoc poolName = cr.getPoolName();
149             Resources res = (Resources) cr.parent();
150
151             boolean poolReferred =
152                 ResourcesUtil.getInstance().isPoolReferredInServerInstance(poolName);
153             if (!poolReferred) {
154                 _logger.fine("Deleting pool " + poolName + "as there is no more " +
155                         "resource-refs to the pool in this server instance");
156                 com.sun.enterprise.config.serverbeans.ConnectorConnectionPool ccp
157                                     = res.getConnectorConnectionPoolByName(poolName);
158                 //Delete/Undeploy Pool
159
ConnectorConnectionPoolDeployer deployer =
160                         new ConnectorConnectionPoolDeployer();
161                 deployer.undeployResource(ccp);
162             }
163             
164         } catch (ConfigException ce) {
165             _logger.warning(ce.getMessage());
166             _logger.fine("Exception while deleting pool : " + ce );
167             throw ce;
168         }
169     }
170     
171     private void createPool(com.sun.enterprise.config.serverbeans.ConnectorConnectionPool cp)
172     throws Exception JavaDoc {
173         ConnectorConnectionPoolDeployer deployer =
174             new ConnectorConnectionPoolDeployer();
175         
176         deployer.deployResource(cp);
177     }
178 }
179
Popular Tags