001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.tomcat5.nodes.actions;
043:
044: import java.util.HashMap;
045: import java.util.HashSet;
046: import java.util.Map;
047: import java.util.Set;
048: import org.netbeans.modules.tomcat5.nodes.TomcatWebModule;
049: import org.openide.nodes.Node;
050: import org.openide.util.HelpCtx;
051: import org.openide.util.NbBundle;
052: import org.openide.util.Parameters;
053: import org.openide.util.RequestProcessor;
054: import org.openide.util.RequestProcessor.Task;
055: import org.openide.util.actions.NodeAction;
056:
057: /**
058: *
059: * @author Petr Pisl
060: * @author Petr Hejl
061: */
062: public class UndeployAction extends NodeAction {
063:
064: /** Creates a new instance of Undeploy */
065: public UndeployAction() {
066: }
067:
068: public String getName() {
069: return NbBundle.getMessage(UndeployAction.class,
070: "LBL_UndeployAction"); //NOI18N
071: }
072:
073: protected void performAction(Node[] nodes) {
074: NodeRefreshTask refresh = new NodeRefreshTask(RequestProcessor
075: .getDefault());
076: for (int i = 0; i < nodes.length; i++) {
077: TomcatWebModuleCookie cookie = (TomcatWebModuleCookie) nodes[i]
078: .getCookie(TomcatWebModuleCookie.class);
079: if (cookie != null) {
080: Task task = cookie.undeploy();
081:
082: refresh.addPrerequisity(nodes[i].getParentNode(), task);
083: }
084: }
085:
086: RequestProcessor.getDefault().post(refresh);
087: }
088:
089: protected boolean asynchronous() {
090: return false;
091: }
092:
093: public HelpCtx getHelpCtx() {
094: return null;
095: }
096:
097: protected boolean enable(Node[] nodes) {
098: for (int i = 0; i < nodes.length; i++) {
099: TomcatWebModule module = (TomcatWebModule) nodes[i]
100: .getLookup().lookup(TomcatWebModule.class);
101: if (module != null) {
102: // it should not be allowed to undeploy the /manager application
103: if ("/manager".equals(module.getTomcatModule()
104: .getPath())) { // NOI18N
105: return false;
106: }
107: }
108: }
109: return true;
110: }
111:
112: /**
113: * Helper class supporting the node(s) refresh after the set of prerequisity
114: * tasks is finished.
115: * <p>
116: * Class itself is <i>thread safe</i> (uses intrinsic lock). Refresh
117: * itself is performed from dedicated thread so, the refresh must be
118: * implemented in thread safe way.
119: *
120: * @author Petr Hejl
121: */
122: private static class NodeRefreshTask implements Runnable {
123:
124: private final RequestProcessor requestProcessor;
125:
126: private Map<Node, Set<Task>> taskMap = new HashMap<Node, Set<Task>>();
127:
128: /**
129: * Constructs the NodeRefreshTask using the given RequestProcessor.
130: *
131: * @param requestProcessor will be used for scheduling the refresh tasks
132: */
133: public NodeRefreshTask(RequestProcessor requestProcessor) {
134: Parameters.notNull("requestProcessor", taskMap);
135:
136: this .requestProcessor = requestProcessor;
137: }
138:
139: /**
140: * Adds prerequisity task. Defines that the node should be refreshed
141: * after the task (and all already added tasks) is finished.
142: *
143: * @param node node to refresh when the task is finished
144: * @param task task to wait for (multiple task can be assigned) by calling this method
145: */
146: public synchronized void addPrerequisity(Node node, Task task) {
147: Parameters.notNull("node", node);
148: Parameters.notNull("task", task);
149:
150: Set<Task> tasks = taskMap.get(node);
151: if (tasks == null) {
152: tasks = new HashSet<Task>();
153: taskMap.put(node, tasks);
154: }
155:
156: tasks.add(task);
157: }
158:
159: /**
160: * Executes this task. For each node added with {@link #addPrerequisity(Node, Task)}
161: * it post a new task that waits until all tasks asscociated with the node
162: * are finished and after that refreshes the node.
163: */
164: public synchronized void run() {
165: for (Map.Entry<Node, Set<Task>> entry : taskMap.entrySet()) {
166:
167: final Node node = entry.getKey();
168: final Set<Task> tasks = entry.getValue();
169:
170: requestProcessor.post(new Runnable() {
171: public void run() {
172: for (Task task : tasks) {
173: task.waitFinished();
174: }
175: NodeRefreshTask.this .refresh(node);
176: }
177: });
178: }
179: }
180:
181: private void refresh(Node node) {
182: if (node == null) {
183: return;
184: }
185:
186: RefreshWebModulesCookie cookie = node.getLookup().lookup(
187: RefreshWebModulesCookie.class);
188: if (cookie != null) {
189: cookie.refresh();
190: }
191: }
192:
193: }
194:
195: }
|