From d5319fc19f29bad7971ac369f1dc365441ae35b6 Mon Sep 17 00:00:00 2001 From: rfeng Date: Mon, 9 Nov 2009 23:34:56 +0000 Subject: 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 --- .../sca/provision/ExtensionIntrospector.java | 249 +++++++++++++++++++++ 1 file changed, 249 insertions(+) create mode 100644 sandbox/rfeng/tuscany-provisioning/src/main/java/org/apache/tuscany/sca/provision/ExtensionIntrospector.java (limited to 'sandbox/rfeng/tuscany-provisioning/src/main/java/org/apache/tuscany') 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 modules = new HashMap(); + private Map qnames = new HashMap(); + + /** + * @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 getModules() { + loadProviderFactories(); + return modules; + } + + public Map 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 factories = new ArrayList(); + 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 mapJavaTypeToQName() { + Collection processors = + discover("org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor"); + Map qnames = new HashMap(); + 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 discover(String name) { + + // Get the provider factory service declarations + Collection 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> getDependencies(String args[]) throws ArtifactResolutionException, + ArtifactNotFoundException { + Map> artifacts = new HashMap>(); + + Artifact originatingArtifact = embedder.createArtifact("dummy", "dummy", "1.0", Artifact.SCOPE_RUNTIME, "jar"); + + for (Map.Entry 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 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> 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(); + } +} -- cgit v1.2.3