KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > server > ResourceManager


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  * @(#) ResourceManager.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.server;
38
39 import com.sun.enterprise.config.serverbeans.*;
40 import com.sun.enterprise.config.*;
41 import com.sun.enterprise.config.ConfigException;
42 import com.sun.enterprise.admin.event.ResourceDeployEvent;
43 import com.sun.enterprise.admin.event.ResourceDeployEventListener;
44 import com.sun.enterprise.admin.event.AdminEventListenerException;
45 import com.sun.enterprise.admin.event.AdminEventMulticaster;
46 import com.sun.enterprise.admin.event.AdminEventResult;
47 import com.sun.enterprise.admin.event.BaseDeployEvent;
48 import com.sun.enterprise.util.i18n.StringManager;
49
50
51 import java.util.logging.Level JavaDoc;
52 import java.util.logging.Logger JavaDoc;
53 import com.sun.logging.LogDomains;
54
55
56 /**
57  * Handles resource deploy events. When a resource is updated/added,
58  * admin server sends a notification to the server instance. This object
59  * is the listener for all resource events.
60  *
61  * Resource deployers throws UnsupportedOperationException when
62  * an operation is not supported and requires a server restart. It
63  * catches the UnsupportedOperationException and notifies
64  * admin event multicaster with RESTART_NEEDED flag.
65  */

66 class ResourceManager implements ResourceDeployEventListener {
67
68     /** logger for this manager */
69     static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.CORE_LOGGER);
70
71     /** context of the server instance runtime */
72     private ServerContext serverContext_;
73
74     /** resource deployer factory */
75     private ResourceDeployerFactory factory_ = null;
76     private static StringManager localStrings = StringManager.getManager(
77                 "com.sun.enterprise.server");
78
79     /**
80      * Constructor.
81      *
82      * @param sc server context
83      */

84     public ResourceManager(ServerContext sc) {
85         serverContext_ = sc;
86         factory_ = new ResourceDeployerFactory();
87     }
88
89     // ---- START OF ResourceDeployEventListener METHODS ---------------------
90

91     public void resourceDeployed(ResourceDeployEvent event)
92             throws AdminEventListenerException {
93
94         assert(event.getJ2EEComponentType() == BaseDeployEvent.RESOURCE);
95
96         try {
97             Resources rbeans = getResources(event.getConfigContext());
98             String JavaDoc type = event.getResourceType();
99             
100             if (type.equals(ResourceDeployEvent.RES_TYPE_JDBC)){
101                 ResourcesUtil.getInstance().setConfigContext(event.getConfigContext());
102                 ResourcesUtil.getInstance().setReinitConfigContext(false);
103             }
104             
105             Object JavaDoc resource =
106                 factory_.getResource(type, event.getResourceName(), rbeans);
107             if (ResourcesUtil.getInstance().isEnabled(event.getConfigContext(), (ConfigBean)resource)) {
108                 factory_.getResourceDeployer(type).deployResource(resource);
109             }
110
111             _logger.log(Level.INFO, "core.resourcedeployed",
112                         type + ":" + event.getResourceName());
113         } catch (Exception JavaDoc e) {
114             _logger.log(Level.SEVERE,"core.resourcedeploy_error", e);
115             throw new AdminEventListenerException(e.getMessage());
116         } finally {
117             ResourcesUtil.getInstance().setReinitConfigContext(true);
118         }
119     }
120
121     public void resourceUndeployed(ResourceDeployEvent event)
122             throws AdminEventListenerException {
123
124         assert(event.getJ2EEComponentType() == BaseDeployEvent.RESOURCE);
125         
126         try {
127             // use config context prior to applying any changes
128
Resources rbeans = getResources(event.getOldConfigContext());
129             String JavaDoc type = event.getResourceType();
130             Object JavaDoc resource =
131                 factory_.getResource(type, event.getResourceName(), rbeans);
132             ResourcesUtil.getInstance().setConfigContext(event.getConfigContext());
133             ResourcesUtil.getInstance().setReinitConfigContext(false);
134             
135             factory_.getResourceDeployer(type).undeployResource(resource);
136
137             _logger.log(Level.INFO, "core.resourceundeployed",
138                         type + ":" + event.getResourceName());
139         } catch (UnsupportedOperationException JavaDoc nse) {
140               AdminEventMulticaster.notifyFailure(event, AdminEventResult.RESTART_NEEDED);
141             _logger.log(Level.INFO,"core.resource_undeployed_restart_needed", event.getResourceName());
142         } catch (Exception JavaDoc e) {
143             _logger.log(Level.SEVERE,"core.resourceundeploy_error",e);
144             throw new AdminEventListenerException(e.getMessage());
145         } finally {
146             ResourcesUtil.getInstance().setReinitConfigContext(true);
147         }
148     }
149
150     public void resourceRedeployed(ResourceDeployEvent event)
151             throws AdminEventListenerException {
152
153         assert(event.getJ2EEComponentType() == BaseDeployEvent.RESOURCE);
154
155         try {
156             Resources rbeans = getResources(event.getConfigContext());
157             String JavaDoc type = event.getResourceType();
158             Object JavaDoc resource =
159                 factory_.getResource(type, event.getResourceName(), rbeans);
160             factory_.getResourceDeployer(type).redeployResource(resource);
161
162             _logger.log(Level.INFO, "core.resourceredeployed",
163                         type + ":" + event.getResourceName());
164         } catch (UnsupportedOperationException JavaDoc nse) {
165               AdminEventMulticaster.notifyFailure(event, AdminEventResult.RESTART_NEEDED);
166             _logger.log(Level.INFO,"core.resource_redeployed_restart_needed", event.getResourceName());
167         } catch (Exception JavaDoc e) {
168             _logger.log(Level.SEVERE,"core.resourceredeploy_error",e);
169             throw new AdminEventListenerException(e.getMessage());
170         }
171     }
172
173     public void resourceEnabled(ResourceDeployEvent event)
174             throws AdminEventListenerException {
175
176         assert(event.getJ2EEComponentType() == BaseDeployEvent.RESOURCE);
177
178         try {
179             Resources rbeans = getResources(event.getConfigContext());
180             String JavaDoc type = event.getResourceType();
181             Object JavaDoc resource =
182                 factory_.getResource(type, event.getResourceName(), rbeans);
183             factory_.getResourceDeployer(type).enableResource(resource);
184
185             _logger.log(Level.INFO, "core.resourceenabled",
186                         type + ":" + event.getResourceName());
187         } catch (UnsupportedOperationException JavaDoc nse) {
188               AdminEventMulticaster.notifyFailure(event, AdminEventResult.RESTART_NEEDED);
189             _logger.log(Level.INFO,"core.resource_enabled_restart_needed", event.getResourceName());
190         } catch (Exception JavaDoc e) {
191             _logger.log(Level.SEVERE,"core.resourceenabled_error",e);
192             throw new AdminEventListenerException(e.getMessage());
193         }
194     }
195
196     public void resourceDisabled(ResourceDeployEvent event)
197             throws AdminEventListenerException {
198
199         assert(event.getJ2EEComponentType() == BaseDeployEvent.RESOURCE);
200
201         try {
202             Resources rbeans = getResources(event.getConfigContext());
203             String JavaDoc type = event.getResourceType();
204             Object JavaDoc resource =
205                 factory_.getResource(type, event.getResourceName(), rbeans);
206             factory_.getResourceDeployer(type).disableResource(resource);
207
208             _logger.log(Level.INFO, "core.resourcedisabled",
209                         type + ":" + event.getResourceName());
210         } catch (UnsupportedOperationException JavaDoc nse) {
211               AdminEventMulticaster.notifyFailure(event, AdminEventResult.RESTART_NEEDED);
212             _logger.log(Level.INFO,"core.resource_disabled_restart_needed", event.getResourceName());
213         } catch (Exception JavaDoc e) {
214             _logger.log(Level.SEVERE,"core.resourcedisabled_error",e);
215             throw new AdminEventListenerException(e.getMessage());
216         }
217     }
218
219     public void resourceReferenceAdded(ResourceDeployEvent event)
220             throws AdminEventListenerException {
221
222         resourceDeployed(event);
223     }
224
225     public void resourceReferenceRemoved(ResourceDeployEvent event)
226             throws AdminEventListenerException {
227
228         resourceUndeployed(event);
229     }
230
231     // ---- END OF ResourceDeployEventListener METHODS ---------------------
232

233     /**
234      * Returns the config resources bean from the given config context.
235      *
236      * @param ctx config context
237      *
238      * @throws ConfigException if an error while getting the resources bean
239      */

240     private Resources getResources(ConfigContext ctx) throws ConfigException {
241
242         //Resources rbeans = ServerBeansFactory.getServerBean(ctx).getResources();
243
Resources rbeans = ServerBeansFactory.getDomainBean(ctx).getResources();
244
245         if (rbeans == null) {
246             String JavaDoc msg = localStrings.getString("resourceManager.resource_not_found");
247                 throw new ConfigException(msg); //XXX
248
}
249         return rbeans;
250     }
251 }
252
Popular Tags