PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/netbeans-7.3/tomcat5/src/org/netbeans/modules/tomcat5/nodes/TomcatWebModule.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 339 lines | 190 code | 47 blank | 102 comment | 27 complexity | ab8bd6b321f36956c1dfcd0dbe4ef68a MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.netbeans.modules.tomcat5.nodes;
  45. import java.util.Comparator;
  46. import javax.enterprise.deploy.spi.DeploymentManager;
  47. import javax.enterprise.deploy.spi.TargetModuleID;
  48. import javax.enterprise.deploy.spi.status.ProgressListener;
  49. import javax.enterprise.deploy.spi.status.ProgressObject;
  50. import javax.enterprise.deploy.shared.StateType;
  51. import javax.enterprise.deploy.shared.CommandType;
  52. import org.netbeans.modules.tomcat5.TomcatModule;
  53. import org.netbeans.modules.tomcat5.nodes.actions.TomcatWebModuleCookie;
  54. import org.openide.awt.StatusDisplayer;
  55. import org.openide.nodes.Node;
  56. import org.openide.util.RequestProcessor.Task;
  57. import org.openide.util.NbBundle;
  58. import org.openide.util.RequestProcessor;
  59. import org.openide.NotifyDescriptor;
  60. import org.openide.DialogDisplayer;
  61. import javax.enterprise.deploy.spi.status.ProgressEvent;
  62. import javax.enterprise.deploy.spi.status.DeploymentStatus;
  63. import org.netbeans.modules.tomcat5.TomcatManager;
  64. import org.openide.util.Parameters;
  65. /**
  66. *
  67. * @author Petr Pisl
  68. * @author Petr Hejl
  69. */
  70. public class TomcatWebModule implements TomcatWebModuleCookie {
  71. /** RequestProcessor processor that serializes management tasks. */
  72. private static RequestProcessor rp;
  73. /** Returns shared RequestProcessor. */
  74. private static synchronized RequestProcessor rp () {
  75. if (rp == null) {
  76. rp = new RequestProcessor ("Tomcat web module", 1); // NOI18N
  77. }
  78. return rp;
  79. }
  80. /** Simple comparator for sorting nodes by name. */
  81. public static final Comparator<TomcatWebModule> TOMCAT_WEB_MODULE_COMPARATOR = new Comparator<TomcatWebModule>() {
  82. public int compare(TomcatWebModule wm1, TomcatWebModule wm2) {
  83. return wm1.getTomcatModule ().getModuleID().compareTo(wm2.getTomcatModule ().getModuleID());
  84. }
  85. };
  86. private final TomcatModule tomcatModule;
  87. private final TomcatManager manager;
  88. private volatile boolean isRunning;
  89. private Node node;
  90. private final TargetModuleID[] target;
  91. /** Creates a new instance of TomcatWebModule */
  92. public TomcatWebModule(DeploymentManager manager, TomcatModule tomcatModule, boolean isRunning) {
  93. this.tomcatModule = tomcatModule;
  94. this.manager = (TomcatManager)manager;
  95. this.isRunning = isRunning;
  96. target = new TargetModuleID[]{tomcatModule};
  97. }
  98. public TomcatModule getTomcatModule () {
  99. return tomcatModule;
  100. }
  101. public void setRepresentedNode(Node node) {
  102. this.node = node;
  103. }
  104. public Node getRepresentedNode () {
  105. return node;
  106. }
  107. public DeploymentManager getDeploymentManager() {
  108. return manager;
  109. }
  110. /**
  111. * Undeploys the web application described by this module.
  112. *
  113. * @return task in which the undeployment itself is processed. When the
  114. * task is finished it implicate that undeployment is finished
  115. * (failed or completed).
  116. */
  117. public Task undeploy() {
  118. return rp().post(new Runnable() {
  119. public void run () {
  120. StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(TomcatWebModule.class, "MSG_START_UNDEPLOY", // NOI18N
  121. new Object [] { getTomcatModule().getPath() }));
  122. ProgressObject po = manager.undeploy(target);
  123. TomcatProgressListener listener = new TomcatProgressListener(po);
  124. po.addProgressListener(listener);
  125. listener.updateState();
  126. CompletionWait wait = new CompletionWait(po);
  127. wait.init();
  128. wait.waitFinished();
  129. }
  130. }, 0);
  131. }
  132. public void start() {
  133. rp().post(new Runnable() {
  134. public void run () {
  135. StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(TomcatWebModule.class, "MSG_START_STARTING", // NOI18N
  136. new Object [] { getTomcatModule().getPath() }));
  137. ProgressObject po = manager.start(target);
  138. TomcatProgressListener listener = new TomcatProgressListener(po);
  139. po.addProgressListener(listener);
  140. listener.updateState();
  141. }
  142. }, 0);
  143. }
  144. public void stop() {
  145. rp().post(new Runnable() {
  146. public void run () {
  147. StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(TomcatWebModule.class, "MSG_START_STOPPING", // NOI18N
  148. new Object [] { getTomcatModule ().getPath() }));
  149. ProgressObject po = manager.stop(target);
  150. TomcatProgressListener listener = new TomcatProgressListener(po);
  151. po.addProgressListener(listener);
  152. listener.updateState();
  153. }
  154. }, 0);
  155. }
  156. public boolean isRunning() {
  157. return isRunning;
  158. }
  159. private String constructDisplayName(){
  160. if (isRunning())
  161. return getTomcatModule ().getPath();
  162. else
  163. return getTomcatModule ().getPath() + " [" + NbBundle.getMessage(TomcatWebModuleNode.class, "LBL_Stopped") // NOI18N
  164. + "]";
  165. }
  166. /**
  167. * Opens the log file defined for this web moudel in the ouput window.
  168. */
  169. public void openLog() {
  170. manager.logManager().openContextLog(tomcatModule);
  171. }
  172. /**
  173. * Returns <code>true</code> if there is a logger defined for this module,
  174. * <code>false</code> otherwise.
  175. *
  176. * @return <code>true</code> if there is a logger defined for this module,
  177. * <code>false</code> otherwise.
  178. */
  179. public boolean hasLogger() {
  180. return manager.logManager().hasContextLogger(tomcatModule);
  181. }
  182. private class TomcatProgressListener implements ProgressListener {
  183. private final ProgressObject progressObject;
  184. private boolean finished;
  185. public TomcatProgressListener(ProgressObject progressObject) {
  186. this.progressObject = progressObject;
  187. }
  188. public void handleProgressEvent(ProgressEvent progressEvent) {
  189. updateState();
  190. }
  191. public void updateState() {
  192. DeploymentStatus deployStatus = progressObject.getDeploymentStatus();
  193. synchronized (this) {
  194. if (finished || deployStatus == null) {
  195. return;
  196. }
  197. if (deployStatus.isCompleted() || deployStatus.isFailed()) {
  198. finished = true;
  199. }
  200. if (deployStatus.getState() == StateType.COMPLETED) {
  201. CommandType command = deployStatus.getCommand();
  202. if (command == CommandType.START || command == CommandType.STOP) {
  203. StatusDisplayer.getDefault().setStatusText(deployStatus.getMessage());
  204. if (command == CommandType.START) {
  205. isRunning = true;
  206. } else {
  207. isRunning = false;
  208. }
  209. node.setDisplayName(constructDisplayName());
  210. } else if (command == CommandType.UNDEPLOY) {
  211. StatusDisplayer.getDefault().setStatusText(deployStatus.getMessage());
  212. }
  213. } else if (deployStatus.getState() == StateType.FAILED) {
  214. NotifyDescriptor notDesc = new NotifyDescriptor.Message(
  215. deployStatus.getMessage(),
  216. NotifyDescriptor.ERROR_MESSAGE);
  217. DialogDisplayer.getDefault().notify(notDesc);
  218. StatusDisplayer.getDefault().setStatusText(deployStatus.getMessage());
  219. }
  220. }
  221. }
  222. }
  223. /**
  224. * Helper class for blocking wait until the deployment manager operation
  225. * gets finished.
  226. * <p>
  227. * The class is <i>thread safe</i>.
  228. *
  229. * @author Petr Hejl
  230. */
  231. private static class CompletionWait implements ProgressListener {
  232. private final ProgressObject progressObject;
  233. private boolean completed;
  234. /**
  235. * Constructs the CompletionWait object that will wait for
  236. * given ProgressObject.
  237. *
  238. * @param progressObject object that we want to wait for
  239. * must not be <code>null</code>
  240. */
  241. public CompletionWait(ProgressObject progressObject) {
  242. Parameters.notNull("progressObject", progressObject);
  243. this.progressObject = progressObject;
  244. }
  245. /**
  246. * Initialize this object. Until calling this method any thread that
  247. * has called {@link #waitFinished()} will wait unconditionaly (does not
  248. * matter what is the state of the ProgressObject.
  249. */
  250. public void init() {
  251. synchronized (this) {
  252. progressObject.addProgressListener(this);
  253. // to be sure we didn't missed the state
  254. handleProgressEvent(null);
  255. }
  256. }
  257. /**
  258. * Handles the progress. May lead to notifying threads waiting in
  259. * {@link #waitFinished()}.
  260. *
  261. * @param evt event to handle
  262. */
  263. public void handleProgressEvent(ProgressEvent evt) {
  264. synchronized (this) {
  265. DeploymentStatus status = progressObject.getDeploymentStatus();
  266. if (status.isCompleted() || status.isFailed()) {
  267. completed = true;
  268. notifyAll();
  269. }
  270. }
  271. }
  272. /**
  273. * Block the calling thread until the progress object indicates the
  274. * competion or failure. If the task described by ProgressObject is
  275. * already finished returns immediately.
  276. */
  277. public void waitFinished() {
  278. synchronized (this) {
  279. if (completed) {
  280. return;
  281. }
  282. while (!completed) {
  283. try {
  284. wait();
  285. } catch (InterruptedException ex) {
  286. // don't response to interrupt
  287. }
  288. }
  289. }
  290. }
  291. }
  292. }