//
// This file is part of the Prose Development Tools for Eclipse package.
//
// The contents of this file are subject to the Mozilla Public License
// Version 1.1 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
// for the specific language governing rights and limitations under the
// License.
//
// The Original Code is Prose Development Tools for Eclipse.
//
// The Initial Developer of the Original Code is Angela Nicoara. Portions
// created by Angela Nicoara are Copyright (C) 2006 Angela Nicoara.
// All Rights Reserved.
//
// Contributor(s):
// $Id: ProseClientRunner.java,v 1.1 2008/11/18 12:28:36 anicoara Exp $
// ==============================================================================
//
package ch.ethz.prose.eclipse.internal.core;
import java.io.File;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.Launch;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.jdt.launching.IVMRunner;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.launching.VMRunnerConfiguration;
/**
* Client runner to insert an aspect by starting another VM and launching the
* Prose command line client.
*
* @author Angela Nicoara
* @author Johann Gyger
* @version $Id: ProseClientRunner.java,v 1.1 2008/11/18 12:28:36 anicoara Exp $
*/
public class ProseClientRunner {
/**
* Main class that will be started in a new VM.
*/
protected static final String MAIN_CLASS = "ch.ethz.prose.tools.CommandlineProseClient"; //$NON-NLS-1$
/**
* Run another VM that starts CommandlineProseClient and passes the aspect
* to insert as an command line argument.
*
* @param aspect Aspect to insert
* @param address Prose server address
* @param active Active or testing aspect manager instance?
* @param txId transaction identifier, or null if non-transacted
* @throws CoreException
*/
public void insertAspect(IType aspect, String address, boolean active, String txId) throws CoreException {
IJavaProject project = aspect.getJavaProject();
IVMInstall vmInstall = getVMInstall(project);
if (vmInstall == null) return;
IVMRunner vmRunner = vmInstall.getVMRunner(ILaunchManager.RUN_MODE);
if (vmRunner == null) return;
String[] classPath = computeClasspath(project);
VMRunnerConfiguration vmConfig = new VMRunnerConfiguration(MAIN_CLASS, classPath);
List args = new ArrayList();
String path = ProsePlugin.getDefault().getProsePath();
args.add("-Dch.ethz.inf.project.home=" + path);
args.add("-Djava.security.policy=" + path + "testpolicy");
args.add(getRmiCodebase(classPath));
args.add("-Dprose.instance=" + (active? "activeInstance" : "testInstance"));
args.add("-Dprose.address=" + address);
args.add("-Dinsert=" + aspect.getFullyQualifiedName());
if (txId != null) args.add("-DtxId=" + txId);
vmConfig.setVMArguments((String[]) args.toArray(new String[] {}));
ILaunch launch = new Launch(null, ILaunchManager.RUN_MODE, null);
vmRunner.run(vmConfig, launch, null);
DebugPlugin.getDefault().getLaunchManager().addLaunch(launch);
}
/**
* @param project Project for which the classpath is computed
* @return Classpath of project that contains the aspect
* @throws CoreException
*/
protected String[] computeClasspath(IJavaProject project) throws CoreException {
String[] defaultPath = JavaRuntime.computeDefaultRuntimeClassPath(project);
return defaultPath;
}
/**
* @param project Java project for which the VM install is returned
* @return Default Java VM install
* @throws CoreException
*/
protected IVMInstall getVMInstall(IJavaProject project) throws CoreException {
IVMInstall vmInstall = JavaRuntime.getVMInstall(project);
if (vmInstall == null) vmInstall = JavaRuntime.getDefaultVMInstall();
return vmInstall;
}
/**
* Convert project classpath into RMI codebase URLs.
*
* @param classpath Project classpath
* @return Codebase argument
*/
protected String getRmiCodebase(String[] classpath) {
StringBuffer buffer = new StringBuffer();
buffer.append("-Djava.rmi.server.codebase=");
for (int i = 0; i < classpath.length; i++) {
String cp = classpath[i];
if (cp.endsWith("jdk-jvmai-loc.jar") || cp.endsWith("jdk-prose-loc.jar") || cp.endsWith("prose-tools-loc.jar"))
continue;
try {
File file = new File(cp);
//buffer.append(file.toURL().toString()); //before(bug)
//BugFix: The file url for java.rmi.server.codebase was not correct generated (file:///)
String url = file.toURL().toString();
String fileUrl = url.substring(0, url.indexOf('/'));
String codebase = fileUrl + "//" + url.substring(url.indexOf('/'), url.length());
buffer.append(codebase);
buffer.append(' ');
} catch (MalformedURLException e) {
ProsePlugin.log(e);
}
}
return buffer.toString();
}
}