001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package org.terracotta.dso.actions;
005:
006: import org.apache.commons.io.IOUtils;
007: import org.eclipse.core.resources.IProject;
008: import org.eclipse.core.runtime.CoreException;
009: import org.eclipse.core.runtime.IPath;
010: import org.eclipse.jdt.core.IJavaProject;
011: import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
012: import org.eclipse.jface.action.Action;
013: import org.eclipse.jface.action.IAction;
014: import org.eclipse.jface.viewers.ISelection;
015: import org.eclipse.jface.viewers.IStructuredSelection;
016: import org.eclipse.ui.IActionDelegate;
017: import org.eclipse.ui.IWorkbenchWindow;
018: import org.eclipse.ui.IWorkbenchWindowActionDelegate;
019: import org.terracotta.dso.ClasspathProvider;
020: import org.terracotta.dso.ProjectNature;
021: import org.terracotta.dso.TcPlugin;
022:
023: import com.tc.admin.common.InputStreamDrainer;
024: import com.tc.util.runtime.Os;
025:
026: import java.io.File;
027: import java.io.IOException;
028:
029: public class ShowAdminConsoleAction extends Action implements
030: IActionDelegate, IWorkbenchWindowActionDelegate,
031: IJavaLaunchConfigurationConstants, IProjectAction {
032: private IJavaProject m_javaProject;
033: private IAction m_action;
034:
035: public ShowAdminConsoleAction() {
036: super ("Show AdminConsole...");
037: TcPlugin.getDefault().registerProjectAction(this );
038: }
039:
040: public ShowAdminConsoleAction(IJavaProject javaProject) {
041: super ("Show AdminConsole...");
042: m_javaProject = javaProject;
043: }
044:
045: public void run(IAction action) {
046: String[] cmdarray = {
047: getJavaCmd().getAbsolutePath(),
048: "-Dtc.install-root="
049: + TcPlugin.getDefault().getLocation()
050: .toOSString(), "-cp", getClasspath(),
051: "com.tc.admin.AdminClient" };
052:
053: Process p = exec(cmdarray, null, new File(System
054: .getProperty("user.dir")));
055:
056: new InputStreamDrainer(p.getErrorStream()).start();
057: new InputStreamDrainer(p.getInputStream()).start();
058: IOUtils.closeQuietly(p.getOutputStream());
059: }
060:
061: private String getClasspath() {
062: TcPlugin plugin = TcPlugin.getDefault();
063: IPath tcJarPath = plugin.getLibDirPath().append("tc.jar");
064:
065: if (tcJarPath.toFile().exists()) {
066: return tcJarPath.toOSString();
067: } else {
068: return ClasspathProvider.makeDevClasspath();
069: }
070: }
071:
072: private File getJavaCmd() {
073: File javaBin = new File(System.getProperty("java.home"), "bin");
074: return new File(javaBin, "java"
075: + (Os.isWindows() ? ".exe" : ""));
076: }
077:
078: protected Process exec(String[] cmdarray, String[] envp, File dir) {
079: try {
080: return Runtime.getRuntime().exec(cmdarray, envp, dir);
081: } catch (IOException ioe) {
082: TcPlugin.getDefault().openError(
083: "Unable to show AdminConsole", ioe);
084: }
085:
086: return null;
087: }
088:
089: public void selectionChanged(IAction action, ISelection selection) {
090: m_action = action;
091:
092: if (m_javaProject == null
093: || selection instanceof IStructuredSelection) {
094: update(ActionUtil.locateSelectedJavaProject(selection));
095: } else {
096: action.setEnabled(true);
097: }
098: }
099:
100: private void update(IJavaProject javaProject) {
101: if (javaProject != null) {
102: try {
103: if (javaProject.getProject().hasNature(
104: ProjectNature.NATURE_ID)) {
105: m_javaProject = javaProject;
106: } else {
107: m_javaProject = null;
108: }
109: } catch (CoreException ce) {/**/
110: }
111: } else {
112: m_javaProject = null;
113: }
114:
115: m_action.setEnabled(m_javaProject != null);
116: }
117:
118: public void update(IProject project) {
119: update(ActionUtil.findJavaProject(project));
120: }
121:
122: public void dispose() {
123: /**/
124: }
125:
126: public void init(IWorkbenchWindow window) {
127: /**/
128: }
129: }
|