blob: 16f787927d4ad0fe50fe0a9de8c2e6ebc2ad93a2 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2008-2010 Sonatype, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Sonatype, Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.m2e.core.internal.embedder;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.namespace.BundleNamespace;
import org.osgi.framework.namespace.PackageNamespace;
import org.osgi.framework.wiring.BundleWire;
import org.osgi.framework.wiring.BundleWiring;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osgi.util.ManifestElement;
import org.codehaus.plexus.util.IOUtil;
import org.eclipse.m2e.core.embedder.IMavenLauncherConfiguration;
import org.eclipse.m2e.core.embedder.MavenRuntime;
import org.eclipse.m2e.core.embedder.MavenRuntimeManager;
import org.eclipse.m2e.core.internal.Messages;
import org.eclipse.m2e.core.internal.e44.DevClassPathHelper;
/**
* Embedded Maven runtime
*
* @author Eugene Kuleshov
* @author Igor Fedorenko
*/
public class MavenEmbeddedRuntime implements MavenRuntime {
private static final String MAVEN_CORE_POM_PROPERTIES = "META-INF/maven/org.apache.maven/maven-core/pom.properties"; //$NON-NLS-1$
private static final Logger log = LoggerFactory.getLogger(MavenEmbeddedRuntime.class);
private static final String MAVEN_EMBEDDER_BUNDLE_SYMBOLICNAME = "org.eclipse.m2e.maven.runtime"; //$NON-NLS-1$
private static final String MAVEN_EXECUTOR_CLASS = org.apache.maven.cli.MavenCli.class.getName();
public static final String PLEXUS_CLASSWORLD_NAME = "plexus.core"; //$NON-NLS-1$
private static String[] LAUNCHER_CLASSPATH;
private static String[] CLASSPATH;
private static volatile String mavenVersion;
private Bundle m2eCore;
public MavenEmbeddedRuntime(Bundle m2eCore) {
this.m2eCore = m2eCore;
}
public boolean isEditable() {
return false;
}
public String getLocation() {
return MavenRuntimeManager.EMBEDDED;
}
public String getSettings() {
return null;
}
public boolean isAvailable() {
return true;
}
public void createLauncherConfiguration(IMavenLauncherConfiguration collector, IProgressMonitor monitor)
throws CoreException {
collector.setMainType(MAVEN_EXECUTOR_CLASS, PLEXUS_CLASSWORLD_NAME);
initClasspath(findMavenEmbedderBundle());
collector.addRealm(IMavenLauncherConfiguration.LAUNCHER_REALM);
for(String entry : LAUNCHER_CLASSPATH) {
collector.addArchiveEntry(entry);
}
collector.addRealm(PLEXUS_CLASSWORLD_NAME);
for(String entry : CLASSPATH) {
// https://issues.sonatype.org/browse/MNGECLIPSE-2507
if(!entry.contains("plexus-build-api")) {
collector.addArchiveEntry(entry);
}
}
}
private synchronized void initClasspath(Bundle mavenRuntimeBundle) {
if(CLASSPATH == null) {
LinkedHashSet<String> allentries = new LinkedHashSet<String>();
addBundleClasspathEntries(allentries, mavenRuntimeBundle);
// find and add more bundles
for(String sname : new String[] {"com.ning.async-http-client", "org.jboss.netty", "org.slf4j.api"}) {
Bundle dependency = findDependencyBundle(mavenRuntimeBundle, sname, new HashSet<Bundle>());
if(dependency != null) {
addBundleClasspathEntries(allentries, dependency);
} else {
log.warn(
"Could not find OSGi bundle with symbolic name ''{}'' required to launch embedded maven runtime in external process",
sname);
}
}
List<String> cp = new ArrayList<String>();
List<String> lcp = new ArrayList<String>();
for(String entry : allentries) {
if(entry.contains("plexus-classworlds")) { //$NON-NLS-1$
lcp.add(entry);
} else {
cp.add(entry);
}
}
CLASSPATH = cp.toArray(new String[cp.size()]);
LAUNCHER_CLASSPATH = lcp.toArray(new String[lcp.size()]);
}
}
private Bundle findDependencyBundle(Bundle bundle, String dependencyName, Set<Bundle> visited) {
BundleWiring bundleWiring = bundle.adapt(BundleWiring.class);
if(bundleWiring == null) {
return null;
}
ArrayList<BundleWire> dependencies = new ArrayList<BundleWire>();
dependencies.addAll(bundleWiring.getRequiredWires(BundleNamespace.BUNDLE_NAMESPACE));
dependencies.addAll(bundleWiring.getRequiredWires(PackageNamespace.PACKAGE_NAMESPACE));
for(BundleWire wire : dependencies) {
Bundle requiredBundle = wire.getProviderWiring().getBundle();
if(requiredBundle != null && visited.add(requiredBundle)) {
if(dependencyName.equals(requiredBundle.getSymbolicName())) {
return requiredBundle;
}
Bundle required = findDependencyBundle(requiredBundle, dependencyName, visited);
if(required != null) {
return required;
}
}
}
return null;
}
private void addBundleClasspathEntries(Set<String> entries, Bundle bundle) {
log.debug("addBundleClasspathEntries(Bundle={})", bundle.toString());
Set<String> cp = new LinkedHashSet<String>();
if(DevClassPathHelper.inDevelopmentMode()) {
cp.addAll(Arrays.asList(DevClassPathHelper.getDevClassPath(bundle.getSymbolicName())));
}
cp.addAll(Arrays.asList(parseBundleClasspath(bundle)));
for(String cpe : cp) {
String entry;
if(".".equals(cpe)) {
entry = getNestedJarOrDir(bundle, "/");
} else {
entry = getNestedJarOrDir(bundle, cpe);
}
if(entry != null) {
log.debug("\tEntry:{}", entry);
entries.add(entry);
}
}
}
private String[] parseBundleClasspath(Bundle bundle) {
String[] result = new String[] {"."};
String header = (String) bundle.getHeaders().get(Constants.BUNDLE_CLASSPATH);
ManifestElement[] classpathEntries = null;
try {
classpathEntries = ManifestElement.parseHeader(Constants.BUNDLE_CLASSPATH, header);
} catch(BundleException ex) {
log.warn("Could not parse bundle classpath of {}", bundle.toString(), ex);
}
if(classpathEntries != null) {
result = new String[classpathEntries.length];
for(int i = 0; i < classpathEntries.length; i++ ) {
result[i] = classpathEntries[i].getValue();
}
}
return result;
}
private String getNestedJarOrDir(Bundle bundle, String cp) {
// try embeded entries first
URL url = bundle.getEntry(cp);
if(url != null) {
try {
return FileLocator.toFileURL(url).getFile();
} catch(IOException ex) {
log.warn("Could not get entry {} for bundle {}", new Object[] {cp, bundle.toString(), ex});
}
}
// in development mode entries can be absolute paths outside of bundle basedir
if(DevClassPathHelper.inDevelopmentMode()) {
File file = new File(cp);
if(file.exists() && file.isAbsolute()) {
return file.getAbsolutePath();
}
}
log.debug("Bundle {} does not have entry {}", bundle.toString(), cp);
return null;
}
private Bundle findMavenEmbedderBundle() {
return findDependencyBundle(m2eCore, MAVEN_EMBEDDER_BUNDLE_SYMBOLICNAME, new HashSet<Bundle>());
}
public String toString() {
Bundle embedder = findMavenEmbedderBundle();
StringBuilder sb = new StringBuilder();
sb.append("Embedded (").append(getVersion()); //$NON-NLS-1$
if(embedder != null) {
String version = (String) embedder.getHeaders().get(Constants.BUNDLE_VERSION);
sb.append('/').append(version);
}
sb.append(')');
return sb.toString();
}
private synchronized String getVersion(Bundle bundle) {
if(mavenVersion != null) {
return mavenVersion;
}
initClasspath(bundle);
try {
String mavenCoreJarPath = null;
for(String path : CLASSPATH) {
if(path.contains("maven-core")) {
mavenCoreJarPath = path;
break;
}
}
if(mavenCoreJarPath == null) {
throw new RuntimeException("Could not find maven core jar file");
}
Properties pomProperties = new Properties();
File mavenCoreJar = new File(mavenCoreJarPath);
if(mavenCoreJar.isFile()) {
ZipFile zip = new ZipFile(mavenCoreJarPath);
try {
ZipEntry zipEntry = zip.getEntry(MAVEN_CORE_POM_PROPERTIES);
if(zipEntry != null) {
pomProperties.load(zip.getInputStream(zipEntry));
}
} finally {
zip.close();
}
} else if(mavenCoreJar.isDirectory()) {
InputStream is = new BufferedInputStream(new FileInputStream(new File(mavenCoreJar, MAVEN_CORE_POM_PROPERTIES)));
try {
pomProperties.load(is);
} finally {
IOUtil.close(is);
}
}
String version = pomProperties.getProperty("version"); //$NON-NLS-1$
if(version != null) {
mavenVersion = version;
return mavenVersion;
}
} catch(Exception e) {
log.warn("Could not determine embedded maven version", e);
}
return Messages.MavenEmbeddedRuntime_unknown;
}
public String getVersion() {
Bundle bundle = findMavenEmbedderBundle();
return getVersion(bundle);
}
}