KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tomcat5 > nodes > TomcatWebModule


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tomcat5.nodes;
21
22 import java.util.Comparator JavaDoc;
23 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
24 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
25 import javax.enterprise.deploy.spi.status.ProgressListener JavaDoc;
26 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
27 import javax.enterprise.deploy.shared.StateType JavaDoc;
28 import javax.enterprise.deploy.shared.CommandType JavaDoc;
29 import org.netbeans.modules.tomcat5.TomcatModule;
30 import org.netbeans.modules.tomcat5.nodes.actions.TomcatWebModuleCookie;
31 import org.openide.awt.StatusDisplayer;
32 import org.openide.nodes.Children;
33 import org.openide.nodes.Node;
34 import org.openide.util.NbBundle;
35 import org.openide.util.RequestProcessor;
36 import org.openide.NotifyDescriptor;
37 import org.openide.DialogDisplayer;
38 import javax.enterprise.deploy.spi.status.ProgressEvent JavaDoc;
39 import javax.enterprise.deploy.spi.status.DeploymentStatus JavaDoc;
40 import org.netbeans.modules.tomcat5.TomcatManager;
41
42 /**
43  *
44  * @author Petr Pisl
45  */

46 public class TomcatWebModule implements TomcatWebModuleCookie {
47  
48     private final TomcatModule tomcatModule;
49     private final TomcatManager manager;
50     
51     private boolean isRunning;
52     
53     private Node node;
54     
55     private final TargetModuleID JavaDoc[] target;
56
57     
58     /** Creates a new instance of TomcatWebModule */
59     public TomcatWebModule(DeploymentManager JavaDoc manager, TomcatModule tomcatModule, boolean isRunning) {
60         this.tomcatModule = tomcatModule;
61         this.manager = (TomcatManager)manager;
62         this.isRunning = isRunning;
63         target = new TargetModuleID JavaDoc[]{tomcatModule};
64     }
65     
66     public TomcatModule getTomcatModule () {
67         return tomcatModule;
68     }
69     
70     public void setRepresentedNode(Node node){
71         this.node = node;
72     }
73     
74     public Node getRepresentedNode (){
75         return node;
76     }
77     
78     public DeploymentManager JavaDoc getDeploymentManager() {
79         return manager;
80     }
81     
82     public void undeploy() {
83         RequestProcessor.getDefault().post(new Runnable JavaDoc() {
84             public void run () {
85                 StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(TomcatWebModule.class, "MSG_START_UNDEPLOY", // NOI18N
86
new Object JavaDoc []{getTomcatModule ().getPath()}));
87                 ProgressObject JavaDoc po = manager.undeploy(target);
88                 po.addProgressListener(new TomcatProgressListener());
89             }
90         }, 0);
91     }
92
93     public void start() {
94         RequestProcessor.getDefault().post(new Runnable JavaDoc() {
95             public void run () {
96                 StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(TomcatWebModule.class, "MSG_START_STARTING", // NOI18N
97
new Object JavaDoc []{getTomcatModule ().getPath()}));
98                 ProgressObject JavaDoc po = manager.start(target);
99                 po.addProgressListener(new TomcatProgressListener());
100             }
101         }, 0);
102     }
103
104     public void stop() {
105         RequestProcessor.getDefault().post(new Runnable JavaDoc() {
106             public void run () {
107                 StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(TomcatWebModule.class, "MSG_START_STOPPING", // NOI18N
108
new Object JavaDoc []{getTomcatModule ().getPath()}));
109                 ProgressObject JavaDoc po = manager.stop(target);
110                 po.addProgressListener(new TomcatProgressListener());
111             }
112         }, 0);
113     }
114
115     public boolean isRunning() {
116         return isRunning;
117     }
118     
119     
120     private String JavaDoc constructDisplayName(){
121         if (isRunning())
122             return getTomcatModule ().getPath();
123         else
124             return getTomcatModule ().getPath() + " [" + NbBundle.getMessage(TomcatWebModuleNode.class, "LBL_Stopped") // NOI18N
125
+ "]";
126     }
127        
128     /**
129      * Opens the log file defined for this web moudel in the ouput window.
130      */

131     public void openLog() {
132         manager.logManager().openContextLog(tomcatModule);
133     }
134     
135     /**
136      * Returns <code>true</code> if there is a logger defined for this module,
137      * <code>false</code> otherwise.
138      *
139      * @return <code>true</code> if there is a logger defined for this module,
140      * <code>false</code> otherwise.
141      */

142     public boolean hasLogger() {
143          return manager.logManager().hasContextLogger(tomcatModule);
144     }
145     
146     private class TomcatProgressListener implements ProgressListener JavaDoc {
147         public void handleProgressEvent(ProgressEvent JavaDoc progressEvent) {
148             DeploymentStatus JavaDoc deployStatus = progressEvent.getDeploymentStatus();
149             if (deployStatus.getState() == StateType.COMPLETED) {
150                 CommandType JavaDoc command = deployStatus.getCommand();
151                 if (command == CommandType.START || command == CommandType.STOP) {
152                         StatusDisplayer.getDefault().setStatusText(deployStatus.getMessage());
153                         if (command == CommandType.START) isRunning = true; else isRunning = false;
154                         node.setDisplayName(constructDisplayName());
155                 } else if (command == CommandType.UNDEPLOY) {
156                         Node parent = node.getParentNode();
157                         if (parent != null) {
158                             Children children = parent.getChildren();
159                             if (children instanceof TomcatWebModuleChildren){
160                                 ((TomcatWebModuleChildren)children).updateKeys();
161                                 StatusDisplayer.getDefault().setStatusText(deployStatus.getMessage());
162                             }
163                         }
164                 }
165             } else if (deployStatus.getState() == StateType.FAILED) {
166                 NotifyDescriptor notDesc = new NotifyDescriptor.Message(
167                         deployStatus.getMessage(),
168                         NotifyDescriptor.ERROR_MESSAGE);
169                 DialogDisplayer.getDefault().notify(notDesc);
170                 StatusDisplayer.getDefault().setStatusText(deployStatus.getMessage());
171             }
172         }
173     }
174     
175     public static class TomcatWebModuleComparator implements Comparator JavaDoc {
176         
177         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
178             TomcatWebModule wm1 = (TomcatWebModule) o1;
179             TomcatWebModule wm2 = (TomcatWebModule) o2;
180             
181             return wm1.getTomcatModule ().getModuleID().compareTo(wm2.getTomcatModule ().getModuleID());
182         }
183         
184     }
185 }
186
Popular Tags