KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > terracotta > dso > actions > BuildBootJarAction


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package org.terracotta.dso.actions;
5
6 import org.eclipse.core.resources.IFile;
7 import org.eclipse.core.resources.IProject;
8 import org.eclipse.core.resources.IResource;
9 import org.eclipse.core.runtime.CoreException;
10 import org.eclipse.core.runtime.IPath;
11 import org.eclipse.core.runtime.IProgressMonitor;
12 import org.eclipse.debug.core.DebugPlugin;
13 import org.eclipse.debug.core.ILaunchConfiguration;
14 import org.eclipse.debug.core.ILaunchConfigurationType;
15 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
16 import org.eclipse.debug.core.ILaunchManager;
17 import org.eclipse.debug.core.IStreamListener;
18 import org.eclipse.debug.core.Launch;
19 import org.eclipse.debug.core.model.IProcess;
20 import org.eclipse.debug.core.model.IStreamMonitor;
21 import org.eclipse.debug.core.model.IStreamsProxy;
22 import org.eclipse.jdt.core.IJavaProject;
23 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
24 import org.eclipse.jdt.launching.JavaLaunchDelegate;
25 import org.eclipse.jdt.launching.JavaRuntime;
26 import org.eclipse.jface.action.Action;
27 import org.eclipse.jface.action.IAction;
28 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
29 import org.eclipse.jface.operation.IRunnableWithProgress;
30 import org.eclipse.jface.viewers.ISelection;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32 import org.eclipse.ui.IActionDelegate;
33 import org.eclipse.ui.IWorkbench;
34 import org.eclipse.ui.IWorkbenchWindow;
35 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
36 import org.eclipse.ui.PlatformUI;
37 import org.terracotta.dso.BootJarHelper;
38 import org.terracotta.dso.ExceptionDialog;
39 import org.terracotta.dso.ProjectNature;
40 import org.terracotta.dso.TcPlugin;
41
42 import java.io.FileNotFoundException JavaDoc;
43 import java.lang.reflect.InvocationTargetException JavaDoc;
44
45 import javax.swing.SwingUtilities JavaDoc;
46
47 public class BuildBootJarAction extends Action
48   implements IActionDelegate,
49              IWorkbenchWindowActionDelegate,
50              IJavaLaunchConfigurationConstants,
51              IProjectAction
52 {
53   private IJavaProject m_javaProject;
54   private IAction m_action;
55   private String JavaDoc m_jreContainerPath;
56   private IProcess m_process;
57   
58   private static final String JavaDoc LAUNCH_LABEL = "DSO BootJar Creator";
59   private static final String JavaDoc MAIN_TYPE = "com.tc.object.tools.BootJarTool";
60   private static final String JavaDoc CLASSPATH_PROVIDER = "org.terracotta.dso.classpathProvider";
61   
62   public BuildBootJarAction() {
63     super("Build BootJar...");
64     TcPlugin.getDefault().registerProjectAction(this);
65   }
66
67   public BuildBootJarAction(IJavaProject javaProject) {
68     super("Build BootJar...");
69     m_javaProject = javaProject;
70   }
71   
72   public void setJREContainerPath(String JavaDoc path) {
73     m_jreContainerPath = path;
74   }
75   
76   public void run(IAction action) {
77     IWorkbench workbench = PlatformUI.getWorkbench();
78     
79     if(!workbench.saveAllEditors(true)) {
80       return;
81     }
82     
83     try {
84       IRunnableWithProgress op = new IRunnableWithProgress() {
85         public void run(IProgressMonitor monitor)
86           throws InvocationTargetException JavaDoc
87         {
88           try {
89             monitor.beginTask("Creating DSO BootJar...", IProgressMonitor.UNKNOWN);
90             doFinish(monitor);
91           } catch(Exception JavaDoc e) {
92             throw new InvocationTargetException JavaDoc(e);
93           } finally {
94             monitor.done();
95           }
96         }
97       };
98
99       new ProgressMonitorDialog(null).run(true, true, op);
100     }
101     catch(InterruptedException JavaDoc e) {
102       /**/
103     }
104     catch(final InvocationTargetException JavaDoc ite) {
105       SwingUtilities.invokeLater(new Runnable JavaDoc() {
106         public void run() {
107           Throwable JavaDoc t = ite.getCause();
108           ExceptionDialog d = new ExceptionDialog();
109           
110           d.setTitle("Terracotta DSO");
111           d.setMessage("Problem building BootJar");
112           d.setErrorText(t.getMessage());
113           d.center();
114           d.setVisible(true);
115         }
116       });
117     }
118     catch(final Exception JavaDoc e) {
119       SwingUtilities.invokeLater(new Runnable JavaDoc() {
120         public void run() {
121           ExceptionDialog d = new ExceptionDialog();
122           
123           d.setTitle("Terracotta DSO");
124           d.setMessage("Problem building BootJar");
125           d.setError(e);
126           d.center();
127           d.setVisible(true);
128         }
129       });
130     }
131   }
132
133   private void doFinish(final IProgressMonitor monitor) throws Exception JavaDoc {
134     ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
135     ILaunchConfigurationType type = manager.getLaunchConfigurationType(ID_JAVA_APPLICATION);
136     ILaunchConfiguration[] configs = manager.getLaunchConfigurations(type);
137     
138     checkCancel(monitor);
139     monitor.subTask("Please wait...");
140
141     for(int i = 0; i < configs.length; i++) {
142       ILaunchConfiguration config = configs[i];
143       
144       if(config.getName().equals(LAUNCH_LABEL)) {
145         config.delete();
146         break;
147       }
148     }
149     
150     ILaunchConfigurationWorkingCopy wc = type.newInstance(null, LAUNCH_LABEL);
151     IProject project = m_javaProject.getProject();
152     
153     String JavaDoc portablePath = m_jreContainerPath;
154     if(portablePath == null) {
155       IPath jrePath = JavaRuntime.computeJREEntry(m_javaProject).getPath();
156       if(jrePath != null) {
157         portablePath = jrePath.makeAbsolute().toPortableString();
158       }
159     }
160
161     TcPlugin plugin = TcPlugin.getDefault();
162     IFile configFile = plugin.getConfigurationFile(project);
163     IPath configPath = configFile.getLocation();
164     String JavaDoc bootJarName = BootJarHelper.getHelper().getBootJarName(portablePath);
165     IPath outPath = project.getLocation().append(bootJarName);
166     String JavaDoc args = "-o " + toOSString(outPath) + " -f " + toOSString(configPath);
167     
168     if(configFile == null) {
169       throw new RuntimeException JavaDoc("No config file");
170     }
171       
172     if(!configFile.exists()) {
173       throw new FileNotFoundException JavaDoc(toOSString(configPath));
174     }
175     
176     wc.setAttribute(ATTR_MAIN_TYPE_NAME, MAIN_TYPE);
177     wc.setAttribute(ATTR_PROGRAM_ARGUMENTS, args);
178     wc.setAttribute(ATTR_CLASSPATH_PROVIDER, CLASSPATH_PROVIDER);
179     wc.setAttribute(ATTR_JRE_CONTAINER_PATH, portablePath);
180     
181     String JavaDoc runMode = ILaunchManager.RUN_MODE;
182     JavaLaunchDelegate delegate = new JavaLaunchDelegate();
183     Launch launch = new Launch(wc, runMode, null);
184
185     checkCancel(monitor);
186     delegate.launch(wc, runMode, launch, null);
187     checkCancel(monitor);
188     
189     m_process = launch.getProcesses()[0];
190     
191     IStreamsProxy streamsProxy = m_process.getStreamsProxy();
192     IStreamMonitor outMonitor = streamsProxy.getOutputStreamMonitor();
193     IStreamMonitor errMonitor = streamsProxy.getErrorStreamMonitor();
194     
195     outMonitor.addListener(new IStreamListener() {
196       public void streamAppended(final String JavaDoc text, IStreamMonitor streamMonitor) {
197         monitor.subTask(text);
198         monitor.worked(1);
199       }
200     });
201     
202     checkCancel(monitor);
203     while(!m_process.isTerminated()) {
204       checkCancel(monitor);
205       try {
206         Thread.sleep(100);
207       } catch(Exception JavaDoc e) {/**/}
208     }
209     
210     if(m_process.getExitValue() != 0) {
211       m_process = null;
212       throw new RuntimeException JavaDoc(errMonitor.getContents());
213     }
214     else {
215       project.refreshLocal(IResource.DEPTH_INFINITE, null);
216     }
217     
218     m_process = null;
219   }
220   
221   private void checkCancel(IProgressMonitor monitor) throws InterruptedException JavaDoc {
222     if(monitor.isCanceled()) {
223       try {
224         if(m_process != null && !m_process.isTerminated()) {
225           m_process.terminate();
226         }
227         m_process = null;
228       } catch(Exception JavaDoc e) {/**/}
229       throw new InterruptedException JavaDoc("BootJar creation cancelled.");
230     }
231   }
232
233   private static String JavaDoc toOSString(IPath path) {
234     return "\"" + path.makeAbsolute().toOSString() + "\"";
235   }
236   
237   public void selectionChanged(IAction action, ISelection selection) {
238     m_action = action;
239     
240     if(m_javaProject == null || selection instanceof IStructuredSelection) {
241       update(ActionUtil.locateSelectedJavaProject(selection));
242     }
243     else {
244       action.setEnabled(true);
245     }
246   }
247
248   private void update(IJavaProject javaProject) {
249     if(javaProject != null) {
250       try {
251         if(javaProject.getProject().hasNature(ProjectNature.NATURE_ID)) {
252           m_javaProject = javaProject;
253         }
254         else {
255           m_javaProject = null;
256         }
257       } catch(CoreException ce) {/**/}
258     }
259     else {
260       m_javaProject = null;
261     }
262     
263     m_action.setEnabled(m_javaProject != null);
264   }
265   
266   public void update(IProject project) {
267     update(ActionUtil.findJavaProject(project));
268   }
269   
270   public void dispose() {
271     /**/
272   }
273
274   public void init(IWorkbenchWindow window) {
275     /**/
276   }
277 }
278
Popular Tags