001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package org.terracotta.dso.actions;
006:
007: import org.eclipse.core.resources.IFile;
008: import org.eclipse.core.resources.IProject;
009: import org.eclipse.core.resources.IResource;
010: import org.eclipse.core.runtime.CoreException;
011: import org.eclipse.core.runtime.IPath;
012: import org.eclipse.core.runtime.IProgressMonitor;
013: import org.eclipse.debug.core.DebugPlugin;
014: import org.eclipse.debug.core.ILaunchConfiguration;
015: import org.eclipse.debug.core.ILaunchConfigurationType;
016: import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
017: import org.eclipse.debug.core.ILaunchManager;
018: import org.eclipse.debug.core.IStreamListener;
019: import org.eclipse.debug.core.Launch;
020: import org.eclipse.debug.core.model.IProcess;
021: import org.eclipse.debug.core.model.IStreamMonitor;
022: import org.eclipse.debug.core.model.IStreamsProxy;
023: import org.eclipse.jdt.core.IJavaProject;
024: import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
025: import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
026: import org.eclipse.jdt.launching.JavaLaunchDelegate;
027: import org.eclipse.jdt.launching.JavaRuntime;
028: import org.eclipse.jface.action.Action;
029: import org.eclipse.jface.action.IAction;
030: import org.eclipse.jface.operation.IRunnableWithProgress;
031: import org.eclipse.jface.viewers.ISelection;
032: import org.eclipse.jface.viewers.IStructuredSelection;
033: import org.eclipse.ui.IActionDelegate;
034: import org.eclipse.ui.IWorkbenchWindow;
035: import org.eclipse.ui.IWorkbenchWindowActionDelegate;
036: import org.eclipse.ui.PlatformUI;
037: import org.terracotta.dso.BootJarHelper;
038: import org.terracotta.dso.ClasspathProvider;
039: import org.terracotta.dso.ProjectNature;
040: import org.terracotta.dso.TcPlugin;
041: import org.terracotta.dso.dialogs.ExceptionDialog;
042:
043: import com.tc.util.concurrent.ThreadUtil;
044:
045: import java.lang.reflect.InvocationTargetException;
046:
047: public class BuildBootJarAction extends Action implements
048: IActionDelegate, IWorkbenchWindowActionDelegate,
049: IJavaLaunchConfigurationConstants, IProjectAction,
050: IRunnableWithProgress {
051: private IJavaProject m_javaProject;
052: private IAction m_action;
053: private String m_jreContainerPath;
054: private IProcess m_process;
055:
056: private static final String LAUNCH_LABEL = "DSO BootJar Creator";
057: private static final String MAIN_TYPE = "com.tc.object.tools.BootJarTool";
058: private static final String CLASSPATH_PROVIDER = "org.terracotta.dso.classpathProvider";
059: private static final String EXCEPTION_TITLE = "Terracotta DSO";
060: private static final String EXCEPTION_MESSAGE = "Problem Building BootJar";
061:
062: public BuildBootJarAction() {
063: super ("Build BootJar...");
064: TcPlugin.getDefault().registerProjectAction(this );
065: }
066:
067: public BuildBootJarAction(IJavaProject javaProject) {
068: super ("Build BootJar...");
069: m_javaProject = javaProject;
070: }
071:
072: public void setJREContainerPath(String path) {
073: m_jreContainerPath = path;
074: }
075:
076: public void run(IAction action) {
077: try {
078: IWorkbenchWindow window = PlatformUI.getWorkbench()
079: .getActiveWorkbenchWindow();
080: window.run(true, true, this );
081: } catch (Exception e) {
082: Throwable t = e.getCause();
083: ExceptionDialog dialog = new ExceptionDialog(TcPlugin
084: .getStandardDisplay().getActiveShell(),
085: EXCEPTION_TITLE, EXCEPTION_MESSAGE, t.getMessage());
086: dialog.open();
087: }
088: }
089:
090: public void run(IProgressMonitor monitor)
091: throws InvocationTargetException {
092: try {
093: monitor.beginTask("Creating DSO BootJar...",
094: IProgressMonitor.UNKNOWN);
095: doFinish(monitor);
096: monitor.done();
097: } catch (Exception e) {
098: throw new InvocationTargetException(e);
099: }
100: }
101:
102: private void doFinish(final IProgressMonitor monitor)
103: throws Exception {
104: ILaunchManager manager = DebugPlugin.getDefault()
105: .getLaunchManager();
106: ILaunchConfigurationType type = manager
107: .getLaunchConfigurationType(ID_JAVA_APPLICATION);
108: ILaunchConfiguration[] configs = manager
109: .getLaunchConfigurations(type);
110:
111: checkCancel(monitor);
112: monitor.subTask("Please wait...");
113:
114: for (int i = 0; i < configs.length; i++) {
115: ILaunchConfiguration config = configs[i];
116:
117: if (config.getName().equals(LAUNCH_LABEL)) {
118: config.delete();
119: break;
120: }
121: }
122:
123: ILaunchConfigurationWorkingCopy wc = type.newInstance(null,
124: LAUNCH_LABEL);
125: IProject project = m_javaProject.getProject();
126:
127: String portablePath = m_jreContainerPath;
128: if (portablePath == null) {
129: IRuntimeClasspathEntry jreEntry = JavaRuntime
130: .computeJREEntry(m_javaProject);
131: if (jreEntry != null) {
132: IPath jrePath = jreEntry.getPath();
133: if (jrePath != null) {
134: portablePath = jrePath.makeAbsolute()
135: .toPortableString();
136: }
137: }
138: }
139:
140: TcPlugin plugin = TcPlugin.getDefault();
141: IFile configFile = plugin.getConfigurationFile(project);
142: IPath configPath = configFile != null ? configFile
143: .getLocation() : null;
144: String bootJarName = BootJarHelper.getHelper().getBootJarName(
145: portablePath);
146: IPath outPath = project.getLocation().append(bootJarName);
147: String args = "-v -w -o " + toOSString(outPath);
148:
149: if (configPath != null)
150: args += " -f " + toOSString(configPath);
151:
152: String origVMArgs = wc.getAttribute(ATTR_VM_ARGUMENTS, "")
153: + " ";
154: String vmargs;
155: IPath jarPath = TcPlugin.getDefault().getLibDirPath().append(
156: "tc.jar");
157: if (jarPath.toFile().exists()) {
158: String installPath = plugin.getLocation().makeAbsolute()
159: .toOSString();
160: vmargs = "-Dtc.install-root=\"" + installPath + "\"";
161: } else {
162: vmargs = "-Dtc.classpath=\""
163: + ClasspathProvider.makeDevClasspath()
164: + "\" -Dtc.install-root=\""
165: + System.getProperty("tc.install-root") + "\"";
166: }
167:
168: wc.setAttribute(ATTR_VM_ARGUMENTS, vmargs + origVMArgs);
169: wc.setAttribute(ATTR_CLASSPATH_PROVIDER, CLASSPATH_PROVIDER);
170: wc.setAttribute(ATTR_MAIN_TYPE_NAME, MAIN_TYPE);
171: wc.setAttribute(ATTR_PROGRAM_ARGUMENTS, args);
172: wc.setAttribute(ATTR_JRE_CONTAINER_PATH, portablePath);
173:
174: String runMode = ILaunchManager.RUN_MODE;
175: JavaLaunchDelegate delegate = new JavaLaunchDelegate();
176: Launch launch = new Launch(wc, runMode, null);
177:
178: checkCancel(monitor);
179: delegate.launch(wc, runMode, launch, null);
180: checkCancel(monitor);
181:
182: m_process = launch.getProcesses()[0];
183:
184: IStreamsProxy streamsProxy = m_process.getStreamsProxy();
185: IStreamMonitor outMonitor = streamsProxy
186: .getOutputStreamMonitor();
187: IStreamMonitor errMonitor = streamsProxy
188: .getErrorStreamMonitor();
189:
190: outMonitor.addListener(new IStreamListener() {
191: public void streamAppended(final String text,
192: IStreamMonitor streamMonitor) {
193: System.err.print(text);
194: monitor.subTask(text);
195: monitor.worked(1);
196: }
197: });
198:
199: checkCancel(monitor);
200: while (!m_process.isTerminated()) {
201: checkCancel(monitor);
202: ThreadUtil.reallySleep(100);
203: }
204:
205: if (m_process.getExitValue() != 0) {
206: m_process = null;
207: monitor.done();
208: throw new RuntimeException(errMonitor.getContents());
209: } else {
210: project.refreshLocal(IResource.DEPTH_INFINITE, null);
211: }
212:
213: m_process = null;
214: }
215:
216: private void checkCancel(IProgressMonitor monitor)
217: throws InterruptedException {
218: if (monitor.isCanceled()) {
219: try {
220: if (m_process != null && !m_process.isTerminated()) {
221: m_process.terminate();
222: }
223: m_process = null;
224: } catch (Exception e) {/**/
225: }
226: throw new InterruptedException(
227: "BootJar creation cancelled.");
228: }
229: }
230:
231: private static String toOSString(IPath path) {
232: return "\"" + path.makeAbsolute().toOSString() + "\"";
233: }
234:
235: public void selectionChanged(IAction action, ISelection selection) {
236: m_action = action;
237:
238: if (m_javaProject == null
239: || selection instanceof IStructuredSelection) {
240: update(ActionUtil.locateSelectedJavaProject(selection));
241: } else {
242: action.setEnabled(true);
243: }
244: }
245:
246: private void update(IJavaProject javaProject) {
247: if (javaProject != null) {
248: try {
249: if (javaProject.getProject().hasNature(
250: ProjectNature.NATURE_ID)) {
251: m_javaProject = javaProject;
252: } else {
253: m_javaProject = null;
254: }
255: } catch (CoreException ce) {/**/
256: }
257: } else {
258: m_javaProject = null;
259: }
260:
261: m_action.setEnabled(m_javaProject != null);
262: }
263:
264: public void update(IProject project) {
265: update(ActionUtil.findJavaProject(project));
266: }
267:
268: public void dispose() {
269: /**/
270: }
271:
272: public void init(IWorkbenchWindow window) {
273: /**/
274: }
275: }
|