KickJava   Java API By Example, From Geeks To Geeks.

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


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  * @(#) CustomResourceDeployer.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.config.serverbeans.Resources;
41
42 import com.sun.enterprise.Switch;
43 import com.sun.enterprise.NamingManager;
44 import com.sun.enterprise.resource.ResourceInstaller;
45 import com.sun.enterprise.repository.J2EEResource;
46 import com.sun.enterprise.repository.CustomResource;
47 import com.sun.enterprise.repository.IASJ2EEResourceFactoryImpl;
48
49 import java.util.logging.Logger JavaDoc;
50 import java.util.logging.Level JavaDoc;
51 import com.sun.logging.LogDomains;
52 import com.sun.enterprise.util.i18n.StringManager;
53
54 /**
55  * Handles custom resource events in the server instance.
56  *
57  * The custom resource events from the admin instance are propagated
58  * to this object.
59  *
60  * The methods can potentially be called concurrently, therefore implementation
61  * need to be synchronized.
62  *
63  * <P>
64  * Note: Since a notification is not sent to the user of the custom
65  * resources upon undeploy, it is possible that there would be
66  * stale objects not being garbage collected. Future versions
67  * should take care of this problem.
68  *
69  * @author Nazrul Islam
70  * @since JDK1.4
71  */

72 public class CustomResourceDeployer implements ResourceDeployer {
73
74     /** Stringmanager for this deployer */
75     private static final StringManager localStrings =
76         StringManager.getManager("com.sun.enterprise.resource");
77
78     /** logger for this deployer */
79     private static Logger JavaDoc _logger=LogDomains.getLogger(LogDomains.CORE_LOGGER);
80
81     /**
82      * Deploy the resource into the server's runtime naming context
83      *
84      * @param resoure a resource object (eg. JmsResource)
85      * @exception Exception thrown if fail
86      */

87     public synchronized void deployResource(Object JavaDoc resource) throws Exception JavaDoc {
88         
89         com.sun.enterprise.config.serverbeans.CustomResource customRes =
90             (com.sun.enterprise.config.serverbeans.CustomResource) resource;
91         
92         if (customRes.isEnabled()) {
93             // converts the config data to j2ee resource
94
J2EEResource j2eeResource =
95                 IASJ2EEResourceFactoryImpl.toCustomJ2EEResource(customRes);
96
97             // resource installer
98
ResourceInstaller installer =
99                 Switch.getSwitch().getResourceInstaller();
100
101             // installs the resource
102
installer.installCustomResource((CustomResource) j2eeResource);
103
104             // adds the resource to the resource collection
105
installer.addResource(j2eeResource);
106         } else {
107             _logger.log(Level.INFO, "core.resource_disabled",
108                 new Object JavaDoc[] {customRes.getJndiName(),
109                               IASJ2EEResourceFactoryImpl.CUSTOM_RES_TYPE});
110         }
111     }
112
113     /**
114      * Undeploy the resource from the server's runtime naming context
115      *
116      * @param resoure a resource object (eg. JmsResource)
117      * @exception Exception thrown if fail
118      */

119     public synchronized void undeployResource(Object JavaDoc resource)
120             throws Exception JavaDoc {
121
122         // naming manager - provides jndi support
123
NamingManager namingMgr = Switch.getSwitch().getNamingManager();
124
125         com.sun.enterprise.config.serverbeans.CustomResource customRes =
126             (com.sun.enterprise.config.serverbeans.CustomResource) resource;
127
128         // converts the config data to j2ee resource
129
J2EEResource j2eeResource =
130             IASJ2EEResourceFactoryImpl.toCustomJ2EEResource(customRes);
131
132         // removes the resource from jndi naming
133
namingMgr.unpublishObject( j2eeResource.getName() );
134
135         // resource installer
136
ResourceInstaller installer = Switch.getSwitch().getResourceInstaller();
137
138         // removes the resource from the collection
139
installer.removeResource(j2eeResource);
140     }
141
142     /**
143      * Redeploy the resource into the server's runtime naming context
144      *
145      * @param resoure a resource object (eg. JmsResource)
146      * @exception Exception thrown if fail
147      */

148     public synchronized void redeployResource(Object JavaDoc resource)
149             throws Exception JavaDoc {
150
151         undeployResource(resource);
152         deployResource(resource);
153     }
154
155     /**
156      * Enable the resource in the server's runtime naming context
157      *
158      * @param resoure a resource object (eg. JmsResource)
159      * @exception Exception thrown if fail
160      */

161     public synchronized void enableResource(Object JavaDoc resource) throws Exception JavaDoc {
162         deployResource(resource);
163     }
164
165     /**
166      * Disable 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 disableResource(Object JavaDoc resource) throws Exception JavaDoc {
172         undeployResource(resource);
173     }
174
175
176     /**
177      * Utility method to find a resource from Resources beans and converte
178      * it to a resource object to be used by the implemented ResourceDeployer
179      *
180      * @param name resource name (normally the jndi-name)
181      * @param rbeans Resources config-beans
182      * @exception Exception thrown if fail
183      */

184     public Object JavaDoc getResource(String JavaDoc name, Resources rbeans) throws Exception JavaDoc {
185
186         Object JavaDoc res = rbeans.getCustomResourceByJndiName(name);
187
188         if (res == null) {
189             String JavaDoc msg = localStrings.getString(
190                          "resource.no_resource",name);
191             throw new Exception JavaDoc(msg);
192         }
193
194         return res;
195     }
196 }
197
Popular Tags