diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-09 23:34:56 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-09 23:34:56 +0000 |
commit | d5319fc19f29bad7971ac369f1dc365441ae35b6 (patch) | |
tree | 2f940b5bfe8d6afe8c3b491be4a8719589ef6520 /sandbox/rfeng/tuscany-provisioning/src/main | |
parent | 80b0aff41c6d21c5faa91f5e37e247b89ca0d404 (diff) |
Prototyping the provisioning support for Tuscany SCA:
1) Find the "extension" by XML qnames or Java types
2) Resolve the maven dependencies for the "extension"
3) Traverse the contribution/composite model to identify required "extensions"
4) Provision the Node to a runtime that is capable of handling the "extensions"
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@834277 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sandbox/rfeng/tuscany-provisioning/src/main')
-rw-r--r-- | sandbox/rfeng/tuscany-provisioning/src/main/java/org/apache/tuscany/sca/provision/ExtensionIntrospector.java | 249 |
1 files changed, 249 insertions, 0 deletions
diff --git a/sandbox/rfeng/tuscany-provisioning/src/main/java/org/apache/tuscany/sca/provision/ExtensionIntrospector.java b/sandbox/rfeng/tuscany-provisioning/src/main/java/org/apache/tuscany/sca/provision/ExtensionIntrospector.java new file mode 100644 index 0000000000..f3cb381faf --- /dev/null +++ b/sandbox/rfeng/tuscany-provisioning/src/main/java/org/apache/tuscany/sca/provision/ExtensionIntrospector.java @@ -0,0 +1,249 @@ +/* + * 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.sca.provision; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.xml.namespace.QName; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.metadata.ArtifactMetadataSource; +import org.apache.maven.artifact.repository.ArtifactRepository; +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.maven.embedder.MavenEmbedder; +import org.apache.maven.project.MavenProject; +import org.apache.maven.shared.runtime.DefaultMavenRuntime; +import org.apache.maven.shared.runtime.MavenRuntime; +import org.apache.maven.shared.runtime.MavenRuntimeException; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.extensibility.ServiceDeclaration; +import org.apache.tuscany.sca.extensibility.ServiceDeclarationParser; +import org.apache.tuscany.sca.extensibility.ServiceDiscovery; +import org.codehaus.plexus.embed.Embedder; + +/** + * + */ +public class ExtensionIntrospector { + private ExtensionPointRegistry registry; + private boolean loaded; + private MavenRuntime mavenRuntime = new DefaultMavenRuntime(); + private Map<String, MavenProject> modules = new HashMap<String, MavenProject>(); + private Map<String, QName> qnames = new HashMap<String, QName>(); + + /** + * @param registry + */ + public ExtensionIntrospector(ExtensionPointRegistry registry) { + super(); + this.registry = (registry == null) ? new DefaultExtensionPointRegistry() : registry; + } + + public MavenProject findModule(String extensionType) { + return getModules().get(extensionType); + } + + public Map<String, MavenProject> getModules() { + loadProviderFactories(); + return modules; + } + + public Map<String, QName> getExtensionTypes() { + return qnames; + } + + /** + * Load provider factories declared under META-INF/services. + * @param registry + */ + private synchronized void loadProviderFactories() { + if (loaded) + return; + + this.qnames.putAll(mapJavaTypeToQName()); + + List<ServiceDeclaration> factories = new ArrayList<ServiceDeclaration>(); + factories.addAll(discover("org.apache.tuscany.sca.provider.BindingProviderFactory")); + factories.addAll(discover("org.apache.tuscany.sca.provider.ImplementationProviderFactory")); + factories.addAll(discover("org.apache.tuscany.sca.provider.PolicyProviderFactory")); + factories.addAll(discover("org.apache.tuscany.sca.provider.WireFormatProviderFactory")); + factories.addAll(discover("org.apache.tuscany.sca.provider.OperationSelectorProviderFactory")); + + for (ServiceDeclaration sd : factories) { + MavenProject project = null; + try { + project = mavenRuntime.getProject(sd.getLocation()); + } catch (MavenRuntimeException e) { + e.printStackTrace(); + } + if (project != null) { + String model = sd.getAttributes().get("model"); + modules.put(model, project); + } + } + + loaded = true; + } + + public Map<String, QName> mapJavaTypeToQName() { + Collection<ServiceDeclaration> processors = + discover("org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor"); + Map<String, QName> qnames = new HashMap<String, QName>(); + for (ServiceDeclaration sd : processors) { + String qname = sd.getAttributes().get("qname"); + String model = sd.getAttributes().get("model"); + qnames.put(model, ServiceDeclarationParser.getQName(qname)); + } + return qnames; + } + + /** + * Load provider factories declared under META-INF/services. + * @param registry + * @param factoryClass + * @return + */ + private Collection<ServiceDeclaration> discover(String name) { + + // Get the provider factory service declarations + Collection<ServiceDeclaration> factoryDeclarations; + ServiceDiscovery serviceDiscovery = registry.getServiceDiscovery(); + try { + factoryDeclarations = serviceDiscovery.getServiceDeclarations(name, false); + } catch (Exception e) { + throw new IllegalStateException(e); + } + + return factoryDeclarations; + } + + private MavenEmbedder embedder; + private ArtifactResolver resolver; + private Embedder internalEmbedder; + ArtifactMetadataSource artifactMetadataSource; + private ArtifactRepository remoteRepo; + + public void start() { + try { + embedder = new MavenEmbedder(); + embedder.setClassLoader(Thread.currentThread().getContextClassLoader()); + embedder.start(); + + resolver = getField(embedder, "artifactResolver"); + internalEmbedder = getField(embedder, "embedder"); + + artifactMetadataSource = + (ArtifactMetadataSource)internalEmbedder + .lookup("org.apache.maven.artifact.metadata.ArtifactMetadataSource"); + + remoteRepo = embedder.createRepository("http://repo2.maven.org/maven2/", "maven"); + } catch (Throwable e) { + throw new IllegalStateException(e); + } + } + + public void stop() { + try { + if (embedder != null) { + internalEmbedder.release(resolver); + internalEmbedder.release(artifactMetadataSource); + embedder.stop(); + embedder = null; + } + } catch (Throwable e) { + throw new IllegalStateException(e); + } + + } + + public Map<String, Collection<Artifact>> getDependencies(String args[]) throws ArtifactResolutionException, + ArtifactNotFoundException { + Map<String, Collection<Artifact>> artifacts = new HashMap<String, Collection<Artifact>>(); + + Artifact originatingArtifact = embedder.createArtifact("dummy", "dummy", "1.0", Artifact.SCOPE_RUNTIME, "jar"); + + for (Map.Entry<String, MavenProject> e : getModules().entrySet()) { + String type = e.getKey(); + QName qname = qnames.get(e.getKey()); + if (qname != null) { + type = qname.toString(); + } + boolean matched = (args.length == 0); + for (String arg : args) { + if (type.contains(arg)) { + matched = true; + break; + } + } + if (!matched) { + continue; + } + // System.out.println(type + ": " + e.getValue()); + + MavenProject project = e.getValue(); + Artifact artifact = + embedder.createArtifact(project.getGroupId(), + project.getArtifactId(), + project.getVersion(), + Artifact.SCOPE_COMPILE, + project.getPackaging()); + + ArtifactResolutionResult result = + resolver.resolveTransitively(Collections.singleton(artifact), originatingArtifact, embedder + .getLocalRepository(), Arrays.asList(remoteRepo), artifactMetadataSource, null); + + // System.out.println("*** " + artifact); + artifacts.put(type, result.getArtifacts()); + } + return artifacts; + } + + private static <T> T getField(Object object, String name) throws Exception { + Field field = object.getClass().getDeclaredField(name); + field.setAccessible(true); + return (T)field.get(object); + } + + public static void main(String args[]) throws Exception { + ExtensionIntrospector introspector = new ExtensionIntrospector(null); + introspector.start(); + for (Map.Entry<String, Collection<Artifact>> entry : introspector.getDependencies(args).entrySet()) { + System.out.println("\n------------------------------------------------------------------------"); + System.out.println(entry.getKey()); + System.out.println("------------------------------------------------------------------------"); + for (Artifact a : entry.getValue()) { + System.out.println(a); + } + System.out.println("------------------------------------------------------------------------"); + } + introspector.stop(); + } +} |