KickJava   Java API By Example, From Geeks To Geeks.

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


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.server;
25
26 import java.io.*;
27 import java.util.*;
28 import java.util.logging.*;
29
30 import com.sun.enterprise.config.serverbeans.WebModule;
31 import com.sun.enterprise.config.serverbeans.ApplicationHelper;
32 import com.sun.enterprise.config.ConfigContext;
33 import com.sun.enterprise.config.ConfigBean;
34 import com.sun.enterprise.config.ConfigException;
35 import com.sun.enterprise.config.serverbeans.ApplicationRef;
36 import com.sun.enterprise.config.serverbeans.Server;
37 import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
38 import com.sun.enterprise.admin.server.core.AdminService;
39
40 import com.sun.enterprise.deployment.backend.DeploymentRequest;
41 import com.sun.enterprise.deployment.backend.DeploymentCommand;
42 import com.sun.enterprise.deployment.backend.DeployableObjectType;
43 import com.sun.enterprise.instance.BaseManager;
44 import com.sun.enterprise.instance.InstanceEnvironment;
45 import com.sun.enterprise.deployment.autodeploy.AutoDirReDeployer;
46 import com.sun.enterprise.deployment.backend.IASDeploymentException;
47 import com.sun.enterprise.util.i18n.StringManager;
48 import com.sun.logging.LogDomains;
49 import com.sun.enterprise.instance.WebModulesManager;
50 import com.sun.enterprise.instance.InstanceFactory;
51
52 /**
53  * Implements the reload callbacks for standalone web modules.
54  *
55  * When dynamic reloading is enabled, this object adds the list of enabled
56  * standalone web modules to the reload monitor thread, so that web module
57  * reload via 'touch .reload' works.
58  *
59  * This class lives in the com.sun.enterprise.server package and not the
60  * com.sun.enterprise.web package because of the scope of the interface/methods
61  * in - ReloadMonitor, MonitorableEntry and MonitorListener.
62  */

63 public final class StandaloneWebModulesManager implements MonitorListener {
64     
65
66     ReloadMonitor reloadMonitor;
67
68     
69     // ------------------------------------------------------------ Constructor
70

71     /**
72      * Standard constructor.
73      *
74      * @param id The web container object identifier
75      * @param modulesRoot The top level directory under which all standalone
76      * modules are deployed at.
77      * @param pollInterval The interval at which dynamic reloading is performed
78      */

79     public StandaloneWebModulesManager(String JavaDoc id, String JavaDoc modulesRoot,
80                                        long pollInterval) {
81         _id = id;
82         _modulesRoot = modulesRoot;
83         start(pollInterval);
84     }
85     
86     
87     // ----------------------------------------------------- Instance Variables
88

89     /**
90      * The id of this object (this is the same as that of the id of the
91      * associated web container object).
92      */

93     private String JavaDoc _id = null;
94     
95     /**
96      * Absolute path for location where all the deployed
97      * standalone modules are stored for this Server Instance.
98      */

99     private String JavaDoc _modulesRoot = null;
100     
101     /**
102      * List of web module ids registered with the reload monitor thread.
103      */

104     private ArrayList _reloadables = new ArrayList();
105     
106     
107     // --------------------------------------------------------- Public Methods
108

109     /**
110      * Enable dynamic reloading (via the .reload file) for all the
111      * standalone web-modules that are marked as enabled
112      *
113      * This method is invoked be WebContainer.start() only when
114      * dynamic reloading has been enabled in server.xml.
115      */

116     public void start(long pollInterval) {
117        
118         reloadMonitor = ReloadMonitor.getInstance(pollInterval * 1000);
119     }
120     
121     /**
122      * Remove the modules (that were previously registered by start()) from
123      * the reload monitor thread.
124      *
125      * This method is invoked be WebContainer.stop() only when
126      * dynamic reloading has been enabled in server.xml.
127      */

128     public void stop() {
129         ReloadMonitor reloadMonitor = ReloadMonitor.getInstance(1);
130         for (int i = 0; i < _reloadables.size(); i++) {
131             String JavaDoc id = (String JavaDoc) _reloadables.get(i);
132             reloadMonitor.removeMonitoredEntry(id);
133         }
134         _reloadables.clear();
135         _reloadables = null;
136     }
137     
138     // ------------------------------------------------ MonitorListener Methods
139

140     /**
141      * Callback from the reload monitor thread for a web module.
142      *
143      * This is done when the user updates the $MODULE_ROOT/$MODULE/.reload
144      * file indicating the server runtime for a dynamic reload.
145      *
146      * @param entry entry thats being monitored
147      *
148      * @return true if application was reloaded successfully
149      */

150     public boolean reload(MonitorableEntry entry) {
151         // The actual reload is done by the NSAPI reconfiguration logic which
152
// the reload monitor thread invokes (at the end), so simply return
153
// true here.
154

155         InstanceEnvironment ienv = ApplicationServer.getServerContext().getInstanceEnvironment();
156         
157         //4926513 work-around --- use displayname
158
// for "foo", the id will be "foo[0]"
159
//String moduleName = entry.getId();
160
String JavaDoc moduleName = entry.getDisplayName();
161         boolean status = false;
162         
163         try {
164             DeploymentRequest req = new DeploymentRequest(ienv, DeployableObjectType.WEB, DeploymentCommand.DEPLOY);
165             
166             // monitored file points to $APP_ROOT/.reload
167
req.setFileSource(entry.getMonitoredFile().getParentFile());
168             
169             // application registration name
170
req.setName(moduleName);
171             
172             // we are always trying a redeployment
173
req.setForced(true);
174                         
175             AutoDirReDeployer deployer = new AutoDirReDeployer(req);
176             status = deployer.redeploy();
177             
178         } catch (IASDeploymentException de) {
179             _logger.log(Level.WARNING,"core.error_in_reload_war_module",de);
180             return false;
181         }
182         return status;
183     }
184     
185     /**
186      * Callback from the auto deploy monitor when a new archive is detected.
187      *
188      * @param entry entry thats being monitored
189      * @param archive newly detected archive under the auto deploy directory
190      *
191      * @return true if archive was deployed successfully
192      */

193     public boolean deploy(MonitorableEntry entry, File archive) {
194         // auto-deploy has not been implemented in S1AS7
195
return false;
196     }
197
198     /**
199      * Adds the given WebModule to the list of monitorable entries for
200      * dynamic reloading.
201      *
202      * @param wm The WebModule to add to the list of monitorable entries for
203      * dynamic reloading
204      */

205     public void addWebModule(WebModule wm) {
206         if (wm != null && isEnabled(wm.getConfigContext(), wm.getName())) {
207             String JavaDoc name = wm.getName();
208             String JavaDoc id = name + "[" + _id + "]";
209             String JavaDoc fileName = getReloadFilename(wm);
210             MonitorableEntry entry = new MonitorableEntry(id, name,
211                                                           new File(fileName),
212                                                           this);
213             _reloadables.add(id);
214             reloadMonitor.addMonitorableEntry(entry);
215         }
216     }
217
218     /**
219      * Whether or not a component (either an application or a module) should be
220      * enabled is defined by the "enable" attribute on both the
221      * application/module element and the application-ref element.
222      *
223      * @param config The dynamic ConfigContext
224      * @param moduleName The name of the component
225      * @return boolean
226      */

227     protected boolean isEnabled (ConfigContext config, String JavaDoc moduleName) {
228         try {
229             if (config == null) {
230                 config = AdminService.getAdminService().getAdminContext().getAdminConfigContext();
231             }
232             ConfigBean app = ApplicationHelper.findApplication(config,
233                 moduleName);
234             Server server = ServerBeansFactory.getServerBean(config);
235             ApplicationRef appRef = server.getApplicationRefByRef(moduleName);
236
237             return ((app != null && app.isEnabled()) &&
238                         (appRef != null && appRef.isEnabled()));
239         } catch (ConfigException e) {
240             _logger.log(Level.FINE, "Error in finding " + moduleName, e);
241
242             //If there is anything wrong, do not enable the module
243
return false;
244         }
245     }
246
247
248     /**
249      * Adds the given WebModule instances to the list of monitorable entries
250      * for dynamic reloading.
251      *
252      * @param modules The array of WebModule instances to add to the list of
253      * monitorable entries for dynamic reloading
254      */

255     public void addWebModules(WebModule[] modules) {
256         if (modules != null && modules.length > 0) {
257             for (int i = 0; i < modules.length; i++) {
258                 addWebModule(modules[i]);
259             }
260         }
261     }
262        
263     /**
264      * Returns the absolute pathname to the .reload file in the
265      * specified web module's directory.
266      */

267     private String JavaDoc getReloadFilename(WebModule wm) {
268         String JavaDoc path = wm.getLocation();
269         File dir = new File(path);
270         if (!dir.isAbsolute())
271             path = _modulesRoot + "/" + path;
272         return path + "/" + ReloadMonitor.RELOAD_FILE;
273     }
274     
275     /** logger to log core messages */
276     static Logger _logger = LogDomains.getLogger(LogDomains.CORE_LOGGER);
277     
278     /** local string manager */
279     private static StringManager localStrings =
280     StringManager.getManager(StandAloneEJBModulesManager.class);
281     
282 }
283
Popular Tags