KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > core > StandardHostDeployer


1
2
3 /*
4  * The contents of this file are subject to the terms
5  * of the Common Development and Distribution License
6  * (the "License"). You may not use this file except
7  * in compliance with the License.
8  *
9  * You can obtain a copy of the license at
10  * glassfish/bootstrap/legal/CDDLv1.0.txt or
11  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
12  * See the License for the specific language governing
13  * permissions and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL
16  * HEADER in each file and include the License file at
17  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
18  * add the following below this CDDL HEADER, with the
19  * fields enclosed by brackets "[]" replaced with your
20  * own identifying information: Portions Copyright [yyyy]
21  * [name of copyright owner]
22  *
23  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24  *
25  * Portions Copyright Apache Software Foundation.
26  */

27
28
29 package org.apache.catalina.core;
30
31
32 import java.io.BufferedOutputStream JavaDoc;
33 import java.io.File JavaDoc;
34 import java.io.FileOutputStream JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.net.URL JavaDoc;
38 import java.util.Enumeration JavaDoc;
39 import org.apache.catalina.Container;
40 import org.apache.catalina.Context;
41 import org.apache.catalina.Deployer;
42 import org.apache.catalina.Engine;
43 import org.apache.catalina.Globals;
44 import org.apache.catalina.Lifecycle;
45 import org.apache.catalina.LifecycleException;
46 import org.apache.catalina.LifecycleListener;
47 import org.apache.catalina.Host;
48 import org.apache.catalina.core.StandardServer;
49 import org.apache.catalina.startup.ContextRuleSet;
50 import org.apache.catalina.startup.ExpandWar;
51 import org.apache.catalina.startup.NamingRuleSet;
52 import org.apache.catalina.util.StringManager;
53 import com.sun.org.apache.commons.digester.Digester;
54 import org.xml.sax.SAXParseException JavaDoc;
55
56
57 /**
58  * <p>Implementation of <b>Deployer</b> that is delegated to by the
59  * <code>StandardHost</code> implementation class.</p>
60  *
61  * @author Craig R. McClanahan
62  * @version $Revision: 1.4 $ $Date: 2006/03/12 01:27:01 $
63  */

64
65 public class StandardHostDeployer implements Deployer {
66
67     private static com.sun.org.apache.commons.logging.Log log=
68         com.sun.org.apache.commons.logging.LogFactory.getLog( StandardHostDeployer.class );
69
70     // ----------------------------------------------------------- Constructors
71

72     public StandardHostDeployer() {
73     }
74
75     /**
76      * Create a new StandardHostDeployer associated with the specified
77      * StandardHost.
78      *
79      * @param host The StandardHost we are associated with
80      */

81     public StandardHostDeployer(StandardHost host) {
82
83         super();
84         this.host = host;
85
86     }
87
88
89     // ----------------------------------------------------- Instance Variables
90

91
92     /**
93      * The <code>ContextRuleSet</code> associated with our
94      * <code>digester</code> instance.
95      */

96     private ContextRuleSet contextRuleSet = null;
97
98
99      /**
100      * The <code>Digester</code> instance to use for deploying web applications
101      * to this <code>Host</code>. <strong>WARNING</strong> - Usage of this
102      * instance must be appropriately synchronized to prevent simultaneous
103      * access by multiple threads.
104      */

105     private Digester digester = null;
106
107
108     /**
109      * The <code>StandardHost</code> instance we are associated with.
110      */

111     protected StandardHost host = null;
112
113
114     /**
115      * The <code>NamingRuleSet</code> associated with our
116      * <code>digester</code> instance.
117      */

118     private NamingRuleSet namingRuleSet = null;
119
120
121     /**
122      * The document base which should replace the value specified in the
123      * <code>Context</code> being added in the <code>addChild()</code> method,
124      * or <code>null</code> if the original value should remain untouched.
125      */

126     private String JavaDoc overrideDocBase = null;
127
128
129     /**
130      * The config file which should replace the value set for the config file
131      * of the <code>Context</code>being added in the <code>addChild()</code>
132      * method, or <code>null</code> if the original value should remain
133      * untouched.
134      */

135     private String JavaDoc overrideConfigFile = null;
136
137
138     /**
139      * The string manager for this package.
140      */

141     protected static final StringManager sm =
142         StringManager.getManager(Constants.Package);
143
144
145     // -------------------------------------------------------- Depoyer Methods
146

147     public Host getHost() {
148         return host;
149     }
150
151     public void setHost(Host host) {
152         this.host = (StandardHost)host;
153     }
154
155     /**
156      * Return the name of the Container with which this Deployer is associated.
157      */

158     public String JavaDoc getName() {
159
160         return (host.getName());
161
162     }
163
164
165     /**
166      * Install a new web application, whose web application archive is at the
167      * specified URL, into this container with the specified context path.
168      * A context path of "" (the empty string) should be used for the root
169      * application for this container. Otherwise, the context path must
170      * start with a slash.
171      * <p>
172      * If this application is successfully installed, a ContainerEvent of type
173      * <code>PRE_INSTALL_EVENT</code> will be sent to registered listeners
174      * before the associated Context is started, and a ContainerEvent of type
175      * <code>INSTALL_EVENT</code> will be sent to all registered listeners
176      * after the associated Context is started, with the newly created
177      * <code>Context</code> as an argument.
178      *
179      * @param contextPath The context path to which this application should
180      * be installed (must be unique)
181      * @param war A URL of type "jar:" that points to a WAR file, or type
182      * "file:" that points to an unpacked directory structure containing
183      * the web application to be installed
184      *
185      * @exception IllegalArgumentException if the specified context path
186      * is malformed (it must be "" or start with a slash)
187      * @exception IllegalStateException if the specified context path
188      * is already attached to an existing web application
189      * @exception IOException if an input/output error was encountered
190      * during installation
191      */

192     public synchronized void install(String JavaDoc contextPath, URL JavaDoc war)
193         throws IOException JavaDoc {
194
195         // Validate the format and state of our arguments
196
if (contextPath == null)
197             throw new IllegalArgumentException JavaDoc
198                 (sm.getString("standardHost.pathRequired"));
199         if (!contextPath.equals("") && !contextPath.startsWith("/"))
200             throw new IllegalArgumentException JavaDoc
201                 (sm.getString("standardHost.pathFormat", contextPath));
202         if (findDeployedApp(contextPath) != null)
203             throw new IllegalStateException JavaDoc
204                 (sm.getString("standardHost.pathUsed", contextPath));
205         if (war == null)
206             throw new IllegalArgumentException JavaDoc
207                 (sm.getString("standardHost.warRequired"));
208
209         // Calculate the document base for the new web application
210
log.info(sm.getString("standardHost.installing",
211                               contextPath, war.toString()));
212         String JavaDoc url = war.toString();
213         String JavaDoc docBase = null;
214         boolean isWAR = false;
215         if (url.startsWith("jar:")) {
216             url = url.substring(4, url.length() - 2);
217             if (!url.toLowerCase().endsWith(".war")) {
218                 throw new IllegalArgumentException JavaDoc
219                     (sm.getString("standardHost.warURL", url));
220             }
221             isWAR = true;
222         }
223         if (url.startsWith("file://"))
224             docBase = url.substring(7);
225         else if (url.startsWith("file:"))
226             docBase = url.substring(5);
227         else
228             throw new IllegalArgumentException JavaDoc
229                 (sm.getString("standardHost.warURL", url));
230
231         // Determine if directory/war to install is in the host appBase
232
boolean isAppBase = false;
233         File JavaDoc appBase = new File JavaDoc(host.getAppBase());
234         if (!appBase.isAbsolute())
235             appBase = new File JavaDoc(System.getProperty("catalina.base"),
236                             host.getAppBase());
237         File JavaDoc contextFile = new File JavaDoc(docBase);
238         File JavaDoc baseDir = contextFile.getParentFile();
239         if (appBase.getCanonicalPath().equals(baseDir.getCanonicalPath())) {
240             isAppBase = true;
241         }
242
243         // For security, if deployXML is false only allow directories
244
// and war files from the hosts appBase
245
if (!host.isDeployXML() && !isAppBase) {
246             throw new IllegalArgumentException JavaDoc
247                 (sm.getString("standardHost.installBase", url));
248         }
249
250         // Make sure contextPath and directory/war names match when
251
// installing from the host appBase
252
if (isAppBase && host.getAutoDeploy()) {
253             String JavaDoc filename = contextFile.getName();
254             if (isWAR) {
255                 filename = filename.substring(0,filename.length()-4);
256             }
257             if (contextPath.length() == 0) {
258                 if (!filename.equals("ROOT")) {
259                     throw new IllegalArgumentException JavaDoc
260                         (sm.getString("standardHost.pathMatch", "/", "ROOT"));
261                 }
262             } else if (!filename.equals(contextPath.substring(1))) {
263                 throw new IllegalArgumentException JavaDoc
264                     (sm.getString("standardHost.pathMatch", contextPath, filename));
265             }
266         }
267
268         // Expand war file if host wants wars unpacked
269
if (isWAR && host.isUnpackWARs()) {
270             docBase = ExpandWar.expand(host, war, contextPath);
271         }
272
273         // Install the new web application
274
try {
275             Class JavaDoc clazz = Class.forName(host.getContextClass());
276             Context context = (Context) clazz.newInstance();
277             context.setPath(contextPath);
278             context.setDocBase(docBase);
279             if (context instanceof Lifecycle) {
280                 clazz = Class.forName(host.getConfigClass());
281                 LifecycleListener listener =
282                     (LifecycleListener) clazz.newInstance();
283                 ((Lifecycle) context).addLifecycleListener(listener);
284             }
285             host.fireContainerEvent(PRE_INSTALL_EVENT, context);
286             host.addChild(context);
287             host.fireContainerEvent(INSTALL_EVENT, context);
288         } catch (ClassNotFoundException JavaDoc e) {
289             log.info("", e);
290         } catch (Exception JavaDoc e) {
291             log.info("Error installing", e);
292             throw new IOException JavaDoc(e.toString());
293         }
294
295     }
296
297
298     /**
299      * Install a new web application, whose web application archive is at the
300      * specified URL, into this container with the specified context path.
301      * A context path of "" (the empty string) should be used for the root
302      * application for this container. Otherwise, the context path must
303      * start with a slash.
304      * <p>
305      * If this application is successfully installed, a ContainerEvent of type
306      * <code>PRE_INSTALL_EVENT</code> will be sent to registered listeners
307      * before the associated Context is started, and a ContainerEvent of type
308      * <code>INSTALL_EVENT</code> will be sent to all registered listeners
309      * after the associated Context is started, with the newly created
310      * <code>Context</code> as an argument.
311      *
312      * @param contextPath The context path to which this application should
313      * be installed (must be unique)
314      * @param war A URL of type "jar:" that points to a WAR file, or type
315      * "file:" that points to an unpacked directory structure containing
316      * the web application to be installed
317      * @param configFile The path to a file to save the Context information.
318      * If configFile is null, the Context information is saved in server.xml;
319      * if it is NOT null, the Context information is saved in configFile.
320      *
321      * @exception IllegalArgumentException if the specified context path
322      * is malformed (it must be "" or start with a slash)
323      * @exception IllegalStateException if the specified context path
324      * is already attached to an existing web application
325      * @exception IOException if an input/output error was encountered
326      * during installation
327      */

328     public synchronized void install(String JavaDoc contextPath, URL JavaDoc war,
329         String JavaDoc configFile) throws IOException JavaDoc {
330
331         // Validate the format and state of our arguments
332
if (contextPath == null)
333             throw new IllegalArgumentException JavaDoc
334                 (sm.getString("standardHost.pathRequired"));
335         if (!contextPath.equals("") && !contextPath.startsWith("/"))
336             throw new IllegalArgumentException JavaDoc
337                 (sm.getString("standardHost.pathFormat", contextPath));
338         if (findDeployedApp(contextPath) != null)
339             throw new IllegalStateException JavaDoc
340                 (sm.getString("standardHost.pathUsed", contextPath));
341         if (war == null)
342             throw new IllegalArgumentException JavaDoc
343                 (sm.getString("standardHost.warRequired"));
344
345         // Calculate the document base for the new web application
346
log.info(sm.getString("standardHost.installing",
347                               contextPath, war.toString()));
348         String JavaDoc url = war.toString();
349         String JavaDoc docBase = null;
350         boolean isWAR = false;
351         if (url.startsWith("jar:")) {
352             url = url.substring(4, url.length() - 2);
353             if (!url.toLowerCase().endsWith(".war")) {
354                 throw new IllegalArgumentException JavaDoc
355                     (sm.getString("standardHost.warURL", url));
356             }
357             isWAR = true;
358         }
359         if (url.startsWith("file://"))
360             docBase = url.substring(7);
361         else if (url.startsWith("file:"))
362             docBase = url.substring(5);
363         else
364             throw new IllegalArgumentException JavaDoc
365                 (sm.getString("standardHost.warURL", url));
366
367         // Expand war file if host wants wars unpacked
368
if (isWAR && host.isUnpackWARs()) {
369             docBase = ExpandWar.expand(host, war, contextPath);
370         }
371
372         // Install the new web application
373
try {
374             Class JavaDoc clazz = Class.forName(host.getContextClass());
375             Context context = (Context) clazz.newInstance();
376             context.setPath(contextPath);
377             context.setDocBase(docBase);
378             context.setConfigFile(configFile);
379             if (context instanceof Lifecycle) {
380                 clazz = Class.forName(host.getConfigClass());
381                 LifecycleListener listener =
382                     (LifecycleListener) clazz.newInstance();
383                 ((Lifecycle) context).addLifecycleListener(listener);
384             }
385             host.fireContainerEvent(PRE_INSTALL_EVENT, context);
386             host.addChild(context);
387             host.fireContainerEvent(INSTALL_EVENT, context);
388
389             // save context info into configFile
390
Engine engine = (Engine)host.getParent();
391             StandardServer server = (StandardServer) engine.getService().getServer();
392             //server.storeContext(context);
393
} catch (Exception JavaDoc e) {
394             log.error(sm.getString("standardHost.installError", contextPath),
395                       e);
396             throw new IOException JavaDoc(e.toString());
397         }
398
399     }
400
401
402     /**
403      * <p>Install a new web application, whose context configuration file
404      * (consisting of a <code>&lt;Context&gt;</code> element) and (optional)
405      * web application archive are at the specified URLs.</p>
406      *
407      * If this application is successfully installed, a ContainerEvent of type
408      * <code>PRE_INSTALL_EVENT</code> will be sent to registered listeners
409      * before the associated Context is started, and a ContainerEvent of type
410      * <code>INSTALL_EVENT</code> will be sent to all registered listeners
411      * after the associated Context is started, with the newly created
412      * <code>Context</code> as an argument.
413      *
414      * @param config A URL that points to the context configuration descriptor
415      * to be used for configuring the new Context
416      * @param war A URL of type "jar:" that points to a WAR file, or type
417      * "file:" that points to an unpacked directory structure containing
418      * the web application to be installed, or <code>null</code> to use
419      * the <code>docBase</code> attribute from the configuration descriptor
420      *
421      * @exception IllegalArgumentException if one of the specified URLs is
422      * null
423      * @exception IllegalStateException if the context path specified in the
424      * context configuration file is already attached to an existing web
425      * application
426      * @exception IOException if an input/output error was encountered
427      * during installation
428      */

429     public synchronized void install(URL JavaDoc config, URL JavaDoc war) throws IOException JavaDoc {
430
431         // Validate the format and state of our arguments
432
if (config == null)
433             throw new IllegalArgumentException JavaDoc
434                 (sm.getString("standardHost.configRequired"));
435
436         if (!host.isDeployXML())
437             throw new IllegalArgumentException JavaDoc
438                 (sm.getString("standardHost.configNotAllowed"));
439
440         log.info(sm.getString("standardHost.installingXML", config));
441
442         // Calculate the document base for the new web application (if needed)
443
String JavaDoc docBase = null; // Optional override for value in config file
444
boolean isWAR = false;
445         if (war != null) {
446             String JavaDoc url = war.toString();
447             log.info(sm.getString("standardHost.installingWAR", url));
448             // Calculate the WAR file absolute pathname
449
if (url.startsWith("jar:")) {
450                 url = url.substring(4, url.length() - 2);
451                 isWAR = true;
452             }
453             if (url.startsWith("file://"))
454                 docBase = url.substring(7);
455             else if (url.startsWith("file:"))
456                 docBase = url.substring(5);
457             else
458                 throw new IllegalArgumentException JavaDoc
459                     (sm.getString("standardHost.warURL", url));
460
461         }
462
463         // Expand war file if host wants wars unpacked
464
if (isWAR && host.isUnpackWARs()) {
465             docBase = ExpandWar.expand(host, war);
466         }
467
468         // Install the new web application
469
this.overrideDocBase = docBase;
470         if (config.toString().startsWith("file:")) {
471             this.overrideConfigFile = config.getFile();
472         }
473
474         InputStream JavaDoc stream = null;
475         try {
476             stream = config.openStream();
477             Digester digester = createDigester();
478             digester.setDebug(host.getDebug());
479             digester.setClassLoader(this.getClass().getClassLoader());
480             digester.clear();
481             digester.push(this);
482             digester.parse(stream);
483             stream.close();
484             stream = null;
485         } catch (Exception JavaDoc e) {
486             host.log
487                 (sm.getString("standardHost.installError", docBase), e);
488             throw new IOException JavaDoc(e.toString());
489         } finally {
490             if (stream != null) {
491                 try {
492                     stream.close();
493                 } catch (Throwable JavaDoc t) {
494                     ;
495                 }
496             }
497             this.overrideDocBase = null;
498             this.overrideConfigFile = null;
499         }
500
501     }
502
503
504     /**
505      * Return the Context for the deployed application that is associated
506      * with the specified context path (if any); otherwise return
507      * <code>null</code>.
508      *
509      * @param contextPath The context path of the requested web application
510      */

511     public Context findDeployedApp(String JavaDoc contextPath) {
512
513         return ((Context) host.findChild(contextPath));
514
515     }
516
517
518     /**
519      * Return the context paths of all deployed web applications in this
520      * Container. If there are no deployed applications, a zero-length
521      * array is returned.
522      */

523     public String JavaDoc[] findDeployedApps() {
524
525         Container children[] = host.findChildren();
526         String JavaDoc results[] = new String JavaDoc[children.length];
527         for (int i = 0; i < children.length; i++)
528             results[i] = children[i].getName();
529         return (results);
530
531     }
532
533
534     /**
535      * Remove an existing web application, attached to the specified context
536      * path. If this application is successfully removed, a
537      * ContainerEvent of type <code>REMOVE_EVENT</code> will be sent to all
538      * registered listeners, with the removed <code>Context</code> as
539      * an argument.
540      *
541      * @param contextPath The context path of the application to be removed
542      *
543      * @exception IllegalArgumentException if the specified context path
544      * is malformed (it must be "" or start with a slash)
545      * @exception IllegalArgumentException if the specified context path does
546      * not identify a currently installed web application
547      * @exception IOException if an input/output error occurs during
548      * removal
549      */

550     public void remove(String JavaDoc contextPath) throws IOException JavaDoc {
551
552         // Validate the format and state of our arguments
553
if (contextPath == null)
554             throw new IllegalArgumentException JavaDoc
555                 (sm.getString("standardHost.pathRequired"));
556         if (!contextPath.equals("") && !contextPath.startsWith("/"))
557             throw new IllegalArgumentException JavaDoc
558                 (sm.getString("standardHost.pathFormat", contextPath));
559
560         // Locate the context and associated work directory
561
Context context = findDeployedApp(contextPath);
562         if (context == null)
563             throw new IllegalArgumentException JavaDoc
564                 (sm.getString("standardHost.pathMissing", contextPath));
565
566         // Remove this web application
567
log.info(sm.getString("standardHost.removing", contextPath));
568         try {
569             host.removeChild(context);
570             host.fireContainerEvent(REMOVE_EVENT, context);
571         } catch (Exception JavaDoc e) {
572             log.error(sm.getString("standardHost.removeError", contextPath), e);
573             throw new IOException JavaDoc(e.toString());
574         }
575
576     }
577
578
579     /**
580      * Remove an existing web application, attached to the specified context
581      * path. If this application is successfully removed, a
582      * ContainerEvent of type <code>REMOVE_EVENT</code> will be sent to all
583      * registered listeners, with the removed <code>Context</code> as
584      * an argument. Deletes the web application war file and/or directory
585      * if they exist in the Host's appBase.
586      *
587      * @param contextPath The context path of the application to be removed
588      * @param undeploy boolean flag to remove web application from server
589      *
590      * @exception IllegalArgumentException if the specified context path
591      * is malformed (it must be "" or start with a slash)
592      * @exception IllegalArgumentException if the specified context path does
593      * not identify a currently installed web application
594      * @exception IOException if an input/output error occurs during
595      * removal
596      */

597     public void remove(String JavaDoc contextPath, boolean undeploy)
598         throws IOException JavaDoc {
599
600         // Validate the format and state of our arguments
601
if (contextPath == null)
602             throw new IllegalArgumentException JavaDoc
603                 (sm.getString("standardHost.pathRequired"));
604         if (!contextPath.equals("") && !contextPath.startsWith("/"))
605             throw new IllegalArgumentException JavaDoc
606                 (sm.getString("standardHost.pathFormat", contextPath));
607
608         // Locate the context and associated work directory
609
Context context = findDeployedApp(contextPath);
610         if (context == null)
611             throw new IllegalArgumentException JavaDoc
612                 (sm.getString("standardHost.pathMissing", contextPath));
613
614         // Remove this web application
615
host.log(sm.getString("standardHost.removing", contextPath));
616         try {
617             // Get the work directory for the Context
618
File JavaDoc workDir =
619                 (File JavaDoc) context.getServletContext().getAttribute
620                 (Globals.WORK_DIR_ATTR);
621             String JavaDoc configFile = context.getConfigFile();
622             host.removeChild(context);
623
624             if (undeploy) {
625                 // Remove the web application directory and/or war file if it
626
// exists in the Host's appBase directory.
627

628                 // Determine if directory/war to remove is in the host appBase
629
boolean isAppBase = false;
630                 File JavaDoc appBase = new File JavaDoc(host.getAppBase());
631                 if (!appBase.isAbsolute())
632                     appBase = new File JavaDoc(System.getProperty("catalina.base"),
633                                        host.getAppBase());
634                 File JavaDoc contextFile = new File JavaDoc(context.getDocBase());
635                 File JavaDoc baseDir = contextFile.getParentFile();
636                 if ((baseDir == null)
637                     || (appBase.getCanonicalPath().equals
638                         (baseDir.getCanonicalPath()))) {
639                     isAppBase = true;
640                 }
641
642                 boolean isWAR = false;
643                 if (contextFile.getName().toLowerCase().endsWith(".war")) {
644                     isWAR = true;
645                 }
646                 // Only remove directory and/or war if they are located in the
647
// Host's appBase autoDeploy is true
648
if (isAppBase && host.getAutoDeploy()) {
649                     String JavaDoc filename = contextFile.getName();
650                     if (isWAR) {
651                         filename = filename.substring(0,filename.length()-4);
652                     }
653                     if (contextPath.length() == 0 && filename.equals("ROOT") ||
654                         filename.equals(contextPath.substring(1))) {
655                         if (!isWAR) {
656                             long contextLastModified =
657                                 contextFile.lastModified();
658                             if (contextFile.isDirectory()) {
659                                 deleteDir(contextFile);
660                             }
661                             if (host.isUnpackWARs()) {
662                                 File JavaDoc contextWAR =
663                                     new File JavaDoc(context.getDocBase() + ".war");
664                                 if (contextWAR.exists()) {
665                                     if (contextLastModified
666                                         > contextWAR.lastModified()) {
667                                         contextWAR.delete();
668                                     }
669                                 }
670                             }
671                         } else {
672                             contextFile.delete();
673                         }
674                     }
675                     if (host.isDeployXML() && (configFile != null)) {
676                         File JavaDoc docBaseXml = new File JavaDoc(configFile);
677                         docBaseXml.delete();
678                     }
679                 }
680
681                 // Remove the work directory for the Context
682
if (workDir == null &&
683                     context instanceof StandardContext &&
684                     ((StandardContext)context).getWorkDir() != null) {
685                     workDir = new File JavaDoc(((StandardContext)context).getWorkPath());
686                 }
687                 if (workDir != null && workDir.exists()) {
688                     deleteDir(workDir);
689                 }
690             }
691
692             host.fireContainerEvent(REMOVE_EVENT, context);
693         } catch (Exception JavaDoc e) {
694             host.log(sm.getString("standardHost.removeError", contextPath), e);
695             throw new IOException JavaDoc(e.toString());
696         }
697
698     }
699
700
701     /**
702      * Start an existing web application, attached to the specified context
703      * path. Only starts a web application if it is not running.
704      *
705      * @param contextPath The context path of the application to be started
706      *
707      * @exception IllegalArgumentException if the specified context path
708      * is malformed (it must be "" or start with a slash)
709      * @exception IllegalArgumentException if the specified context path does
710      * not identify a currently installed web application
711      * @exception IOException if an input/output error occurs during
712      * startup
713      */

714     public void start(String JavaDoc contextPath) throws IOException JavaDoc {
715
716         // Validate the format and state of our arguments
717
if (contextPath == null)
718             throw new IllegalArgumentException JavaDoc
719                 (sm.getString("standardHost.pathRequired"));
720         if (!contextPath.equals("") && !contextPath.startsWith("/"))
721             throw new IllegalArgumentException JavaDoc
722                 (sm.getString("standardHost.pathFormat", contextPath));
723         Context context = findDeployedApp(contextPath);
724         if (context == null)
725             throw new IllegalArgumentException JavaDoc
726                 (sm.getString("standardHost.pathMissing", contextPath));
727         log.info("standardHost.start " + contextPath);
728         try {
729             ((Lifecycle) context).start();
730         } catch (LifecycleException e) {
731             log.info("standardHost.start " + contextPath + ": ", e);
732             throw new IllegalStateException JavaDoc
733                 ("standardHost.start " + contextPath + ": " + e);
734         }
735     }
736
737
738     /**
739      * Stop an existing web application, attached to the specified context
740      * path. Only stops a web application if it is running.
741      *
742      * @param contextPath The context path of the application to be stopped
743      *
744      * @exception IllegalArgumentException if the specified context path
745      * is malformed (it must be "" or start with a slash)
746      * @exception IllegalArgumentException if the specified context path does
747      * not identify a currently installed web application
748      * @exception IOException if an input/output error occurs while stopping
749      * the web application
750      */

751     public void stop(String JavaDoc contextPath) throws IOException JavaDoc {
752
753         // Validate the format and state of our arguments
754
if (contextPath == null)
755             throw new IllegalArgumentException JavaDoc
756                 (sm.getString("standardHost.pathRequired"));
757         if (!contextPath.equals("") && !contextPath.startsWith("/"))
758             throw new IllegalArgumentException JavaDoc
759                 (sm.getString("standardHost.pathFormat", contextPath));
760         Context context = findDeployedApp(contextPath);
761         if (context == null)
762             throw new IllegalArgumentException JavaDoc
763                 (sm.getString("standardHost.pathMissing", contextPath));
764         log.info("standardHost.stop " + contextPath);
765         try {
766             ((Lifecycle) context).stop();
767         } catch (LifecycleException e) {
768             log.error("standardHost.stop " + contextPath + ": ", e);
769             throw new IllegalStateException JavaDoc
770                 ("standardHost.stop " + contextPath + ": " + e);
771         }
772
773     }
774
775
776     // ------------------------------------------------------ Delegated Methods
777

778
779     /**
780      * Delegate a request to add a child Context to our associated Host.
781      *
782      * @param child The child Context to be added
783      */

784     public void addChild(Container child) {
785
786         Context context = (Context) child;
787         String JavaDoc contextPath = context.getPath();
788         if (contextPath == null)
789             throw new IllegalArgumentException JavaDoc
790                 (sm.getString("standardHost.pathRequired"));
791         else if (!contextPath.equals("") && !contextPath.startsWith("/"))
792             throw new IllegalArgumentException JavaDoc
793                 (sm.getString("standardHost.pathFormat", contextPath));
794         if (host.findChild(contextPath) != null)
795             throw new IllegalStateException JavaDoc
796                 (sm.getString("standardHost.pathUsed", contextPath));
797         if (this.overrideDocBase != null)
798             context.setDocBase(this.overrideDocBase);
799         if (this.overrideConfigFile != null)
800             context.setConfigFile(this.overrideConfigFile);
801         host.fireContainerEvent(PRE_INSTALL_EVENT, context);
802         host.addChild(child);
803         host.fireContainerEvent(INSTALL_EVENT, context);
804
805     }
806
807
808     /**
809      * Delegate a request for the parent class loader to our associated Host.
810      */

811     public ClassLoader JavaDoc getParentClassLoader() {
812
813         return (host.getParentClassLoader());
814
815     }
816
817
818     // ------------------------------------------------------ Protected Methods
819

820
821     /**
822      * Create (if necessary) and return a Digester configured to process the
823      * context configuration descriptor for an application.
824      */

825     protected Digester createDigester() {
826         if (digester == null) {
827             digester = new Digester();
828             if (host.getDebug() > 0)
829                 digester.setDebug(3);
830             digester.setValidating(false);
831             contextRuleSet = new ContextRuleSet("");
832             digester.addRuleSet(contextRuleSet);
833             namingRuleSet = new NamingRuleSet("Context/");
834             digester.addRuleSet(namingRuleSet);
835         }
836         return (digester);
837
838     }
839
840
841     /**
842      * Delete the specified directory, including all of its contents and
843      * subdirectories recursively.
844      *
845      * @param dir File object representing the directory to be deleted
846      */

847     protected void deleteDir(File JavaDoc dir) {
848
849         String JavaDoc files[] = dir.list();
850         if (files == null) {
851             files = new String JavaDoc[0];
852         }
853         for (int i = 0; i < files.length; i++) {
854             File JavaDoc file = new File JavaDoc(dir, files[i]);
855             if (file.isDirectory()) {
856                 deleteDir(file);
857             } else {
858                 file.delete();
859             }
860         }
861         dir.delete();
862
863     }
864
865 }
866
Popular Tags