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;
005:
006: import org.eclipse.core.runtime.CoreException;
007: import org.eclipse.core.runtime.IPath;
008: import org.eclipse.debug.core.DebugPlugin;
009: import org.eclipse.debug.core.ILaunchConfiguration;
010: import org.eclipse.debug.core.ILaunchConfigurationType;
011: import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
012: import org.eclipse.debug.core.ILaunchManager;
013: import org.eclipse.debug.core.Launch;
014: import org.eclipse.debug.core.model.IProcess;
015: import org.eclipse.debug.core.model.IStreamMonitor;
016: import org.eclipse.debug.core.model.IStreamsProxy;
017: import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
018: import org.eclipse.jdt.launching.JavaLaunchDelegate;
019:
020: import com.tc.object.tools.BootJarSignature;
021: import com.tc.object.tools.UnsupportedVMException;
022: import com.tc.util.concurrent.ThreadUtil;
023:
024: import java.io.File;
025:
026: public class BootJarHelper implements IJavaLaunchConfigurationConstants {
027: private static BootJarHelper m_helper;
028:
029: private static final String LAUNCH_LABEL = "DSO BootJar Namer";
030:
031: private static final String CLASSPATH_PROVIDER = "org.terracotta.dso.classpathProvider";
032:
033: private static final String BOOT_JAR_NAMER = "com.tc.object.tools.BootJarSignature";
034:
035: public static synchronized BootJarHelper getHelper() {
036: if (m_helper == null) {
037: m_helper = new BootJarHelper();
038: }
039: return m_helper;
040: }
041:
042: private BootJarHelper() {
043: super ();
044: }
045:
046: /**
047: * Retrieve the name of the default bootjar.
048: */
049: public String getBootJarName() throws CoreException {
050: return getBootJarName(null);
051: }
052:
053: /**
054: * Retrieve the name of the default bootjar.
055: */
056: public String getBootJarName(String jreContainerPath)
057: throws CoreException {
058: ILaunchManager manager = DebugPlugin.getDefault()
059: .getLaunchManager();
060: ILaunchConfigurationType type = manager
061: .getLaunchConfigurationType(ID_JAVA_APPLICATION);
062: ILaunchConfiguration[] configs = manager
063: .getLaunchConfigurations(type);
064:
065: for (int i = 0; i < configs.length; i++) {
066: ILaunchConfiguration config = configs[i];
067:
068: if (config.getName().equals(LAUNCH_LABEL)) {
069: config.delete();
070: break;
071: }
072: }
073:
074: ILaunchConfigurationWorkingCopy wc = type.newInstance(null,
075: LAUNCH_LABEL);
076: String runMode = ILaunchManager.RUN_MODE;
077: JavaLaunchDelegate delegate = new JavaLaunchDelegate();
078: Launch launch = new Launch(wc, runMode, null);
079:
080: wc.setAttribute(ATTR_CLASSPATH_PROVIDER, CLASSPATH_PROVIDER);
081: wc.setAttribute(ATTR_MAIN_TYPE_NAME, BOOT_JAR_NAMER);
082:
083: if (jreContainerPath != null) {
084: wc.setAttribute(ATTR_JRE_CONTAINER_PATH, jreContainerPath);
085: }
086:
087: delegate.launch(wc, runMode, launch, null);
088:
089: IProcess process = launch.getProcesses()[0];
090: IStreamsProxy streamsProxy = process.getStreamsProxy();
091: IStreamMonitor outMonitor = streamsProxy
092: .getOutputStreamMonitor();
093:
094: while (!process.isTerminated()) {
095: ThreadUtil.reallySleep(100);
096: }
097:
098: return outMonitor.getContents().trim();
099: }
100:
101: /**
102: * Retrieve the bootjar path.
103: */
104: public IPath getBootJarPath() throws CoreException {
105: return getBootJarPath(getBootJarName());
106: }
107:
108: /**
109: * Retrieve the bootjar path for the current VM.
110: */
111: public IPath getBootJarPathForThisVM()
112: throws UnsupportedVMException {
113: return getBootJarPath(BootJarSignature
114: .getBootJarNameForThisVM());
115: }
116:
117: /**
118: * Retrieve the bootjar path given the bootjar name.
119: */
120: public IPath getBootJarPath(String bootJarName) {
121: IPath libDirPath = TcPlugin.getDefault().getLibDirPath();
122: IPath bootJarPath = libDirPath.append("dso-boot").append(
123: bootJarName);
124:
125: return bootJarPath;
126: }
127:
128: public File getBootJarFileForThisVM() throws UnsupportedVMException {
129: return getBootJarPathForThisVM().toFile();
130: }
131:
132: /**
133: * Retrieve the bootjar file.
134: */
135: public File getBootJarFile() throws CoreException {
136: return getBootJarPath().toFile();
137: }
138: }
|