diff options
author | dims <dims@13f79535-47bb-0310-9956-ffa450edef68> | 2008-06-17 00:23:01 +0000 |
---|---|---|
committer | dims <dims@13f79535-47bb-0310-9956-ffa450edef68> | 2008-06-17 00:23:01 +0000 |
commit | bdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a (patch) | |
tree | 38a92061c0793434c4be189f1d70c3458b6bc41d /tags/java/sca/1.0-incubator-M2/services/maven/src/main |
Move Tuscany from Incubator to top level.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'tags/java/sca/1.0-incubator-M2/services/maven/src/main')
4 files changed, 552 insertions, 0 deletions
diff --git a/tags/java/sca/1.0-incubator-M2/services/maven/src/main/java/org/apache/tuscany/services/maven/MavenArtifactRepository.java b/tags/java/sca/1.0-incubator-M2/services/maven/src/main/java/org/apache/tuscany/services/maven/MavenArtifactRepository.java new file mode 100644 index 0000000000..b2c608da63 --- /dev/null +++ b/tags/java/sca/1.0-incubator-M2/services/maven/src/main/java/org/apache/tuscany/services/maven/MavenArtifactRepository.java @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.services.maven; + +import java.util.Collection; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.services.artifact.Artifact; +import org.apache.tuscany.spi.services.artifact.ArtifactRepository; +import org.apache.tuscany.host.RuntimeInfo; +import org.osoa.sca.annotations.Destroy; +import org.osoa.sca.annotations.Property; + +/** + * Artifact repository used for resolving artifacts. + * + * This is used by the composite loader for resolving artifacts transitively. The repository uses the Maven API for resolving dependencies and hence + * expects the artifacts to be stored in a structure similar to the Maven repository layout. The repository first looks within the deployed unit (WAR + * for example), before resorting to a local and set of remote Maven repositories. + * + * @version $Rev$ $Date$ + */ +public class MavenArtifactRepository implements ArtifactRepository { + + /** Maven helper */ + private MavenHelper mavenHelper; + + /** WAR repository helper */ + private WarRepositoryHelper warRepositoryHelper; + + /** + * Conctructs a new artifact repository. + */ + public MavenArtifactRepository(@Property(name = "remoteRepoUrl") + String remoteRepoUrl, @Autowire + RuntimeInfo runtimeInfo) { + mavenHelper = new MavenHelper(remoteRepoUrl, runtimeInfo.isOnline()); + warRepositoryHelper = new WarRepositoryHelper(runtimeInfo.getBaseURL()); + mavenHelper.start(); + } + + /** + * Resolve an artifact. This ensures that the information associated with an artifact is fully populated; Specifically, after this operation the + * URL should contain a location where the artifact can be obtained. + * + * @param artifact + * the artifact to be resolved + */ + public void resolve(Artifact rootArtifact) { + if(warRepositoryHelper.resolveTransitively(rootArtifact)) { + return; + } + if(mavenHelper.resolveTransitively(rootArtifact)) { + return; + } + TuscanyDependencyException tde = new TuscanyDependencyException("Unable to resolve artifact: " + rootArtifact); + tde.setIdentifier(rootArtifact.toString()); + throw tde; + } + + /** + * Resolve a collection of Artifacts. + * + * @param artifacts + * a collection of artifacts to be resolved + * @see #resolve(Artifact) + */ + public void resolve(Collection<? extends Artifact> artifacts) { + for (Artifact artifact : artifacts) { + resolve(artifact); + } + } + + /** + * Destroy method. + * + */ + @Destroy + public void destroy() { + mavenHelper.stop(); + } + +} diff --git a/tags/java/sca/1.0-incubator-M2/services/maven/src/main/java/org/apache/tuscany/services/maven/MavenHelper.java b/tags/java/sca/1.0-incubator-M2/services/maven/src/main/java/org/apache/tuscany/services/maven/MavenHelper.java new file mode 100644 index 0000000000..f68ad562f7 --- /dev/null +++ b/tags/java/sca/1.0-incubator-M2/services/maven/src/main/java/org/apache/tuscany/services/maven/MavenHelper.java @@ -0,0 +1,291 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.services.maven; + +import java.io.File; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.net.MalformedURLException; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; +import org.apache.maven.artifact.metadata.ArtifactMetadataSource; +import org.apache.maven.artifact.metadata.ResolutionGroup; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; +import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy; +import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; +import org.apache.maven.artifact.resolver.ArtifactNotFoundException; +import org.apache.maven.artifact.resolver.ArtifactResolutionException; +import org.apache.maven.artifact.resolver.ArtifactResolutionResult; +import org.apache.maven.artifact.resolver.ArtifactResolver; +import org.apache.tuscany.spi.services.artifact.Artifact; +import org.codehaus.classworlds.ClassWorld; +import org.codehaus.classworlds.DefaultClassRealm; +import org.codehaus.classworlds.DuplicateRealmException; +import org.codehaus.plexus.PlexusContainerException; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; +import org.codehaus.plexus.embed.Embedder; + +/** + * Utility class for embedding Maven. + * + * @version $Rev$ $Date$ + */ +public class MavenHelper { + + /** Local repository */ + private static final File LOCAL_REPO = new File(System.getProperty("user.home") + File.separator + ".m2" + File.separator + "repository"); + + /** Remote repository URLs */ + private final String[] remoteRepositoryUrls; + + /** Maven metadata source */ + private ArtifactMetadataSource metadataSource; + + /** Artifact factory */ + private ArtifactFactory artifactFactory; + + /** Local artifact repository */ + private ArtifactRepository localRepository; + + /** Remote artifact repositories */ + private List<ArtifactRepository> remoteRepositories = new LinkedList<ArtifactRepository>(); + + /** Artifact resolver */ + private ArtifactResolver artifactResolver; + + /** Online */ + private boolean online; + + /** + * Initialize the remote repository URLs. + * + * @param remoteRepositoryUrls + * Remote repository URLS. + * @param runtimeInfo + * Runtime information. + */ + public MavenHelper(String remoteRepositoryUrl, boolean online) { + this.remoteRepositoryUrls = remoteRepositoryUrl.split(","); + this.online = online; + } + + /** + * Starts the embedder. + * + * @throws TuscanyDependencyException + * If unable to start the embedder. + */ + public void start() throws TuscanyDependencyException { + + try { + + Embedder embedder = new Embedder(); + ClassWorld classWorld = new ClassWorld(); + + classWorld.newRealm("plexus.core", getClass().getClassLoader()); + + // Evil hack for Tomcat classloader issue - starts + Field realmsField = ClassWorld.class.getDeclaredField("realms"); + realmsField.setAccessible(true); + Map realms = (Map) realmsField.get(classWorld); + DefaultClassRealm realm = (DefaultClassRealm) realms.get("plexus.core"); + + Class clazz = Class.forName("org.codehaus.classworlds.RealmClassLoader"); + Constructor ctr = clazz.getDeclaredConstructor(new Class[] { DefaultClassRealm.class, ClassLoader.class }); + ctr.setAccessible(true); + Object realmClassLoader = ctr.newInstance(realm, getClass().getClassLoader()); + + Field realmClassLoaderField = DefaultClassRealm.class.getDeclaredField("classLoader"); + realmClassLoaderField.setAccessible(true); + realmClassLoaderField.set(realm, realmClassLoader); + // Evil hack for Tomcat classloader issue - ends + + embedder.start(classWorld); + + metadataSource = (ArtifactMetadataSource) embedder.lookup(ArtifactMetadataSource.ROLE); + artifactFactory = (ArtifactFactory) embedder.lookup(ArtifactFactory.ROLE); + artifactResolver = (ArtifactResolver) embedder.lookup(ArtifactResolver.ROLE); + + setUpRepositories(embedder); + + embedder.stop(); + + } catch (DuplicateRealmException ex) { + throw new TuscanyDependencyException(ex); + } catch (PlexusContainerException ex) { + throw new TuscanyDependencyException(ex); + } catch (ComponentLookupException ex) { + throw new TuscanyDependencyException(ex); + } catch (NoSuchFieldException ex) { + throw new TuscanyDependencyException(ex); + } catch (IllegalAccessException ex) { + throw new TuscanyDependencyException(ex); + } catch (ClassNotFoundException ex) { + throw new TuscanyDependencyException(ex); + } catch (NoSuchMethodException ex) { + throw new TuscanyDependencyException(ex); + } catch (InstantiationException ex) { + throw new TuscanyDependencyException(ex); + } catch (InvocationTargetException ex) { + throw new TuscanyDependencyException(ex); + } + + } + + /** + * Stops the embedder. + * + * @throws TuscanyDependencyException + * If unable to stop the embedder. + */ + public void stop() throws TuscanyDependencyException { + } + + /** + * Resolves the dependencies transitively. + * + * @param artifact + * Artifact whose dependencies need to be resolved. + * @throws TuscanyDependencyException + * If unable to resolve the dependencies. + */ + public boolean resolveTransitively(Artifact rootArtifact) throws TuscanyDependencyException { + + org.apache.maven.artifact.Artifact mavenRootArtifact = artifactFactory.createArtifact(rootArtifact.getGroup(), rootArtifact.getName(), + rootArtifact.getVersion(), org.apache.maven.artifact.Artifact.SCOPE_RUNTIME, rootArtifact.getType()); + + try { + + if (resolve(mavenRootArtifact)) { + rootArtifact.setUrl(mavenRootArtifact.getFile().toURL()); + if (resolveDependencies(rootArtifact, mavenRootArtifact)) { + return true; + } else { + return false; + } + } else { + return false; + } + } catch (MalformedURLException ex) { + throw new TuscanyDependencyException(ex); + } + + } + + /* + * Resolves the artifact. + */ + private boolean resolve(org.apache.maven.artifact.Artifact mavenRootArtifact) { + + try { + artifactResolver.resolve(mavenRootArtifact, remoteRepositories, localRepository); + return true; + } catch (ArtifactResolutionException ex) { + return false; + } catch (ArtifactNotFoundException ex) { + return false; + } + + } + + /* + * Sets up local and remote repositories. + */ + private void setUpRepositories(Embedder embedder) { + + try { + + ArtifactRepositoryFactory artifactRepositoryFactory = (ArtifactRepositoryFactory) embedder.lookup(ArtifactRepositoryFactory.ROLE); + + ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) embedder.lookup(ArtifactRepositoryLayout.ROLE, "default"); + + String updatePolicy = online ? ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS : ArtifactRepositoryPolicy.UPDATE_POLICY_NEVER; + ArtifactRepositoryPolicy snapshotsPolicy = new ArtifactRepositoryPolicy(true, updatePolicy, ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN); + ArtifactRepositoryPolicy releasesPolicy = new ArtifactRepositoryPolicy(true, updatePolicy, ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN); + + localRepository = artifactRepositoryFactory.createArtifactRepository("local", LOCAL_REPO.toURL().toExternalForm(), layout, + snapshotsPolicy, releasesPolicy); + + if (!online) { + return; + } + + for (String remoteRepositoryUrl : remoteRepositoryUrls) { + String repoid = remoteRepositoryUrl.replace(':', '_'); + repoid = repoid.replace('/', '_'); + repoid = repoid.replace('\\', '_'); + remoteRepositories.add(artifactRepositoryFactory.createArtifactRepository(repoid, remoteRepositoryUrl, layout, snapshotsPolicy, + releasesPolicy)); + } + + } catch (MalformedURLException ex) { + throw new TuscanyDependencyException(ex); + } catch (ComponentLookupException ex) { + throw new TuscanyDependencyException(ex); + } + + } + + /* + * Resolves transitive dependencies. + */ + private boolean resolveDependencies(Artifact rootArtifact, org.apache.maven.artifact.Artifact mavenRootArtifact) { + + try { + + ResolutionGroup resolutionGroup = null; + ArtifactResolutionResult result = null; + + resolutionGroup = metadataSource.retrieve(mavenRootArtifact, localRepository, remoteRepositories); + result = artifactResolver.resolveTransitively(resolutionGroup.getArtifacts(), mavenRootArtifact, remoteRepositories, localRepository, + metadataSource); + + // Add the artifacts to the deployment unit + for (Object obj : result.getArtifacts()) { + org.apache.maven.artifact.Artifact depArtifact = (org.apache.maven.artifact.Artifact) obj; + Artifact artifact = new Artifact(); + artifact.setName(mavenRootArtifact.getArtifactId()); + artifact.setGroup(mavenRootArtifact.getGroupId()); + artifact.setType(mavenRootArtifact.getType()); + artifact.setClassifier(mavenRootArtifact.getClassifier()); + artifact.setUrl(depArtifact.getFile().toURL()); + rootArtifact.addDependency(artifact); + } + + } catch (ArtifactMetadataRetrievalException ex) { + return false; + } catch (MalformedURLException ex) { + throw new TuscanyDependencyException(ex); + } catch (ArtifactResolutionException ex) { + return false; + } catch (ArtifactNotFoundException ex) { + return false; + } + + return true; + + } + +} diff --git a/tags/java/sca/1.0-incubator-M2/services/maven/src/main/java/org/apache/tuscany/services/maven/TuscanyDependencyException.java b/tags/java/sca/1.0-incubator-M2/services/maven/src/main/java/org/apache/tuscany/services/maven/TuscanyDependencyException.java new file mode 100644 index 0000000000..6e89443711 --- /dev/null +++ b/tags/java/sca/1.0-incubator-M2/services/maven/src/main/java/org/apache/tuscany/services/maven/TuscanyDependencyException.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.services.maven; + +import org.apache.tuscany.api.TuscanyRuntimeException; + +/** + * Exception thrown in case of an artifact error. + * + * @version $Rev$ $Date$ + */ +public class TuscanyDependencyException extends TuscanyRuntimeException { + + /** + * Initializes the cause. + * @param cause Cause of the exception. + */ + public TuscanyDependencyException(Throwable cause) { + super(cause); + } + + /** + * Initializes the message. + * @param message Message of the exception. + */ + public TuscanyDependencyException(String message) { + super(message); + } + +} diff --git a/tags/java/sca/1.0-incubator-M2/services/maven/src/main/java/org/apache/tuscany/services/maven/WarRepositoryHelper.java b/tags/java/sca/1.0-incubator-M2/services/maven/src/main/java/org/apache/tuscany/services/maven/WarRepositoryHelper.java new file mode 100644 index 0000000000..be4ee68bfa --- /dev/null +++ b/tags/java/sca/1.0-incubator-M2/services/maven/src/main/java/org/apache/tuscany/services/maven/WarRepositoryHelper.java @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.services.maven; + +import java.beans.XMLDecoder; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import org.apache.tuscany.spi.services.artifact.Artifact; +import org.codehaus.plexus.util.IOUtil; + +/** + * Helper class for resolving dependencies from WAR files. + * + * @author Administrator + * + */ +public class WarRepositoryHelper { + + /** WAR Repository URL */ + private URL reporsitoryUrl; + + /** Dependency metadata */ + private Map<String, Set<String>> transDependencyMap = new HashMap<String, Set<String>>(); + + /** + * Initializes the repository URL. + * @param baseUrl Base URL. + */ + @SuppressWarnings("unchecked") + public WarRepositoryHelper(URL baseUrl) { + + + InputStream transDepMapInputStream = null; + try { + + reporsitoryUrl = new URL(baseUrl, "repository/"); + URL transDependencyMapUrl = new URL(baseUrl, "repository/dependency.metadata"); + transDepMapInputStream = transDependencyMapUrl.openStream(); + + XMLDecoder decoder = new XMLDecoder(transDepMapInputStream); + transDependencyMap = (Map<String, Set<String>>)decoder.readObject(); + decoder.close(); + + } catch (MalformedURLException ex) { + // throw new TuscanyDependencyException(ex); + } catch (IOException ex) { + // throw new TuscanyDependencyException(ex); + } finally { + IOUtil.close(transDepMapInputStream); + } + + } + + /** + * Resolves the dependencies transitively. + * + * @param artifact + * Artifact whose dependencies need to be resolved. + * @throws TuscanyDependencyException + * If unable to resolve the dependencies. + */ + public boolean resolveTransitively(Artifact rootArtifact) throws TuscanyDependencyException { + + String artKey = rootArtifact.getGroup() + "/" + rootArtifact.getName() + "/" + rootArtifact.getVersion() + "/"; + if(!transDependencyMap.containsKey(artKey)) { + return false; + } + + + for(String dep : transDependencyMap.get(artKey)) { + + String[] tokens = dep.split("/"); + String artName = tokens[1]; + + try { + if(artName.equals(rootArtifact.getName())) { + rootArtifact.setUrl(new URL(reporsitoryUrl, dep)); + } else { + Artifact depArtifact = new Artifact(); + depArtifact.setGroup(tokens[0]); + depArtifact.setName(tokens[1]); + depArtifact.setVersion(tokens[2]); + depArtifact.setUrl(new URL(reporsitoryUrl, dep)); + rootArtifact.addDependency(depArtifact); + + } + } catch (MalformedURLException ex) { + throw new TuscanyDependencyException(ex); + } + } + return true; + } + +} |