View Javadoc
1   /*
2    * Copyright (c) 2003, Andreas Pataki 
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    *
9    * Redistributions of source code must retain the above copyright notice,
10   * this list of conditions and the following disclaimer.
11   * 
12   * Redistributions in binary form must reproduce the above copyright notice,
13   * this list of conditions and the following disclaimer in the documentation
14   * and/or other materials provided with the distribution.
15   * 
16   * Neither the name of the author nor the names of its contributors
17   * may be used to endorse or promote products derived from this software
18   * without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS
21   * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   */
32  
33  package net.sf.tm.plugin.actions;
34  
35  import org.eclipse.jface.action.IAction;
36  import org.eclipse.jface.viewers.ISelection;
37  import org.eclipse.ui.IWorkbenchWindow;
38  import org.eclipse.ui.IWorkbenchWindowActionDelegate;
39  
40  import net.sf.tm.ContextManagerException;
41  import net.sf.tm.IContextManager;
42  import net.sf.tm.WebContext;
43  import net.sf.tm.WebContextStatus;
44  import net.sf.tm.plugin.ContextManagerPlugin;
45  import net.sf.tm.plugin.Logger;
46  import net.sf.tm.plugin.TreeNode;
47  import net.sf.tm.plugin.ui.WebContextView;
48  import net.sf.tm.tomcat.TomcatContextManager;
49  import net.sf.tm.tomcat.TomcatServer;
50  
51  /***
52   * Action for reloading a context on a server. The context has to be started
53   * to be reloaded.
54   * 
55   * @author Andreas Pataki
56   * @version $Id: ReloadContextAction.java,v 1.10 2004/02/23 19:19:35 apataki Exp $
57   */
58  public class ReloadContextAction
59      extends AbstractBackgroundAction
60      implements IWorkbenchWindowActionDelegate {
61  
62      /***
63       * View which this action is attached with.
64       */
65      private WebContextView view;
66  
67      /***
68       * Constructor for this action. The text and image settings are initialized.
69       * 
70       * @param parentView Parent view which created this action.
71       */
72      public ReloadContextAction(WebContextView parentView) {
73          view = parentView;
74  
75          setText(getLocalizedString("action.reload.text"));
76          setToolTipText(getLocalizedString("action.reload.tooltip"));
77          setImageDescriptor(
78              ContextManagerPlugin.getDefault().getImageDescriptor(
79                  "icons/refresh.gif"));
80      }
81  
82      /***
83       * On excecution the run method changes the icon state, then splits of 
84       * another thread to do the actual excecution with a busy indicator.
85       * 
86       * @see org.eclipse.jface.action.IAction#run()
87       */
88      public void run() {
89          TreeNode node = (TreeNode) view.getSelected();
90          WebContext context = (WebContext) node.getContent();
91  
92          TomcatServer server = (TomcatServer) node.getParent().getContent();
93          if (server.getManager().equals(context.getPath())) {
94              ContextManagerPlugin.getDefault().showError(
95                  getLocalizedString("error.managerReload"));
96          } else {
97  
98              context.setStatus(WebContextStatus.PENDING);
99              view.updateContextNode(node);
100 
101             final TreeNode restartNode = (TreeNode) view.getSelected();
102 
103             runInBackground(new Runnable() {
104                 public void run() {
105                     restart(restartNode);
106                 }
107             });
108 
109         }
110     }
111 
112     /***
113      * @param node The node which should be restarted
114      */
115     private void restart(final TreeNode node) {
116         final WebContext context = (WebContext) node.getContent();
117 
118         IContextManager manager =
119             new TomcatContextManager(
120                 (TomcatServer) node.getParent().getContent());
121 
122         try {
123 
124             manager.reload(context.getPath());
125 
126             if (Logger.isDebugEnabled()) {
127                 Logger.debug("reload of context " + context.getPath());
128             }
129 
130             runInUIThread(new Runnable() {
131 
132                 public void run() {
133                     view.update();
134                 }
135 
136             });
137 
138         } catch (final ContextManagerException e) {
139 
140             Logger.error("error reloading " + context, e);
141 
142             // restore old status
143             runInUIThread(new Runnable() {
144 
145                 public void run() {
146                     ContextManagerPlugin.getDefault().showError(
147                         (getLocalizedString("error.reloadContext",
148                             context.getPath())),
149                         e);
150 
151                     context.setStatus(WebContextStatus.STOPPED);
152                     view.updateContextNode(node);
153                 }
154 
155             });
156 
157         }
158     }
159 
160     /***
161      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
162      */
163     public void dispose() {
164         System.out.println("dispose");
165     }
166 
167     /***
168      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
169      */
170     public void init(IWorkbenchWindow window) {
171         System.out.println("init " + window);
172 
173     }
174 
175     /***
176      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
177      */
178     public void run(IAction action) {
179         System.out.println("run " + action);
180     }
181 
182     /***
183      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
184      */
185     public void selectionChanged(IAction action, ISelection selection) {
186     }
187 
188 }