diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-05-27 01:21:22 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-05-27 01:21:22 +0000 |
commit | 3426488b8797730981da3aa0deb6b0b9f02bd9d1 (patch) | |
tree | 75fcd5b3ce847c7ef8879236b769a95a918440a8 /java | |
parent | 3842c8e1357e155ba9c780ec7a040c49b3e1d7d8 (diff) |
Refactor the contribution scanner to take Contribution as an argument
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@778953 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
15 files changed, 219 insertions, 221 deletions
diff --git a/java/sca/modules/contribution-osgi/src/main/java/org/apache/tuscany/sca/contribution/osgi/impl/OSGiBundleContributionScanner.java b/java/sca/modules/contribution-osgi/src/main/java/org/apache/tuscany/sca/contribution/osgi/impl/OSGiBundleContributionScanner.java index 0b32d7ac10..3d20d968a3 100644 --- a/java/sca/modules/contribution-osgi/src/main/java/org/apache/tuscany/sca/contribution/osgi/impl/OSGiBundleContributionScanner.java +++ b/java/sca/modules/contribution-osgi/src/main/java/org/apache/tuscany/sca/contribution/osgi/impl/OSGiBundleContributionScanner.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
@@ -30,6 +31,7 @@ import java.util.Set; import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
+import org.apache.tuscany.sca.contribution.Contribution;
import org.apache.tuscany.sca.contribution.PackageType;
import org.apache.tuscany.sca.contribution.processor.ContributionException;
import org.apache.tuscany.sca.contribution.processor.ContributionReadException;
@@ -50,10 +52,10 @@ public class OSGiBundleContributionScanner implements ContributionScanner { return PackageType.BUNDLE;
}
- public URL getArtifactURL(URL sourceURL, String artifact) throws ContributionReadException {
+ public URL getArtifactURL(Contribution contribution, String artifact) throws ContributionReadException {
Bundle bundle = null;
try {
- bundle = OSGiBundleActivator.findBundle(sourceURL);
+ bundle = OSGiBundleActivator.findBundle(contribution.getLocation());
if (bundle != null) {
URL url = bundle.getResource(artifact);
return url;
@@ -113,15 +115,11 @@ public class OSGiBundleContributionScanner implements ContributionScanner { }
}
- public List<String> getArtifacts(URL packageSourceURL) throws ContributionReadException {
-
- if (packageSourceURL == null) {
- throw new IllegalArgumentException("Invalid null package source URL.");
- }
- Bundle bundle = OSGiBundleActivator.findBundle(packageSourceURL);
+ public List<String> scan(Contribution contribution) throws ContributionReadException {
+ Bundle bundle = OSGiBundleActivator.findBundle(contribution.getLocation());
if (bundle == null) {
- throw new IllegalArgumentException("Could not find OSGi bundle " + packageSourceURL);
+ throw new IllegalArgumentException("Could not find OSGi bundle " + contribution.getLocation());
}
List<String> artifacts = new ArrayList<String>();
@@ -145,7 +143,39 @@ public class OSGiBundleContributionScanner implements ContributionScanner { } catch (Exception e) {
throw new RuntimeException(e);
}
-
+ contribution.getExtensions().add(bundle);
+ contribution.getTypes().add(getContributionType());
+ contribution.setClassLoader(new BundleClassLoader(bundle));
return artifacts;
}
+
+ private static class BundleClassLoader extends ClassLoader {
+ private Bundle bundle;
+ public BundleClassLoader(Bundle bundle) {
+ super(null);
+ this.bundle = bundle;
+ }
+
+ @Override
+ protected Class<?> findClass(String name) throws ClassNotFoundException {
+ return bundle.loadClass(name);
+ }
+
+ @Override
+ protected URL findResource(String name) {
+ return bundle.getResource(name);
+ }
+
+ @Override
+ protected Enumeration<URL> findResources(String name) throws IOException {
+ Enumeration<URL> urls = bundle.getResources(name);
+ if (urls == null) {
+ List<URL> list = Collections.emptyList();
+ return Collections.enumeration(list);
+ } else {
+ return urls;
+ }
+ }
+ }
+
}
diff --git a/java/sca/modules/contribution-osgi/src/main/java/org/apache/tuscany/sca/contribution/osgi/impl/OSGiClassReferenceModelResolver.java b/java/sca/modules/contribution-osgi/src/main/java/org/apache/tuscany/sca/contribution/osgi/impl/OSGiClassReferenceModelResolver.java index 15b77490b8..4862050084 100644 --- a/java/sca/modules/contribution-osgi/src/main/java/org/apache/tuscany/sca/contribution/osgi/impl/OSGiClassReferenceModelResolver.java +++ b/java/sca/modules/contribution-osgi/src/main/java/org/apache/tuscany/sca/contribution/osgi/impl/OSGiClassReferenceModelResolver.java @@ -6,24 +6,20 @@ * 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. + * under the License. */ package org.apache.tuscany.sca.contribution.osgi.impl; -import java.util.HashMap; -import java.util.Map; - import org.apache.tuscany.sca.contribution.Contribution; -import org.apache.tuscany.sca.contribution.Import; import org.apache.tuscany.sca.contribution.resolver.ClassReference; import org.apache.tuscany.sca.contribution.resolver.ModelResolver; import org.apache.tuscany.sca.core.FactoryExtensionPoint; @@ -35,98 +31,42 @@ import org.osgi.framework.Bundle; * @version $Rev$ $Date$ */ public class OSGiClassReferenceModelResolver implements ModelResolver { - private Contribution contribution; - private Map<String, ClassReference> map = new HashMap<String, ClassReference>(); + // private Contribution contribution; private Bundle bundle; - private boolean initialized; - private boolean useOSGi; public OSGiClassReferenceModelResolver(Contribution contribution, FactoryExtensionPoint modelFactories) { - this.contribution = contribution; + // this.contribution = contribution; + this.bundle = OSGiBundleActivator.findBundle(contribution.getLocation()); } public void addModel(Object resolved) { - ClassReference clazz = (ClassReference)resolved; - map.put(clazz.getClassName(), clazz); } public Object removeModel(Object resolved) { - return map.remove(((ClassReference)resolved).getClassName()); - } - - /** - * Handle artifact resolution when the specific class reference is imported from another contribution - * @param unresolved - * @return - */ - private ClassReference resolveImportedModel(ClassReference unresolved) { - ClassReference resolved = unresolved; - - if (this.contribution != null) { - for (Import import_ : this.contribution.getImports()) { - - if (resolved == unresolved && bundle != null) { - resolved = import_.getModelResolver().resolveModel(ClassReference.class, unresolved); - if (resolved != unresolved) - break; - } - } - - } return resolved; } public <T> T resolveModel(Class<T> modelClass, T unresolved) { - Object resolved = map.get(unresolved); - - if (resolved != null) { - return modelClass.cast(resolved); - } - initialize(); - if (!useOSGi) - return unresolved; - //Load a class on demand - Class clazz = null; + Class<?> clazz = null; if (bundle != null) { try { clazz = bundle.loadClass(((ClassReference)unresolved).getClassName()); } catch (Exception e) { - // we will later try to delegate to imported model resolvers + // Ignore } } if (clazz != null) { - //if we load the class + //if we load the class // Store a new ClassReference wrapping the loaded class ClassReference classReference = new ClassReference(clazz); - map.put(getPackageName(classReference), classReference); // Return the resolved ClassReference return modelClass.cast(classReference); } else { - //delegate resolution of the class - resolved = this.resolveImportedModel((ClassReference)unresolved); - return modelClass.cast(resolved); + return unresolved; } } - - /*************** - * Helper methods - ***************/ - - private String getPackageName(ClassReference clazz) { - int pos = clazz.getClassName().lastIndexOf("."); - return clazz.getClassName().substring(0, pos - 1); - } - - private void initialize() { - try { - bundle = OSGiBundleActivator.findBundle(contribution.getLocation()); - useOSGi = bundle != null; - } catch (Throwable e) { - // Ignore errors, default to ClassReferenceModelResolver - } - } } diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/Contribution.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/Contribution.java index de09725cdb..bdc523c33e 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/Contribution.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/Contribution.java @@ -20,6 +20,7 @@ package org.apache.tuscany.sca.contribution; import java.util.List; +import java.util.Set; import org.apache.tuscany.sca.assembly.Composite; import org.apache.tuscany.sca.assembly.Extensible; @@ -88,11 +89,11 @@ public interface Contribution extends Artifact, Extensible { /** * Returns the list of contributions that this contribution depends on. - * + * * @return */ List<Contribution> getDependencies(); - + /** * Returns the ClassLoader used to load classes and resources from * this contribution @@ -115,4 +116,10 @@ public interface Contribution extends Artifact, Extensible { */ void setClassLoader(ClassLoader classLoader); + /** + * Get a list of mime types that apply to this contribution archive + * @return + */ + Set<String> getTypes(); + }
\ No newline at end of file diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/PackageType.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/PackageType.java index 26833bb4c0..c6c0aff737 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/PackageType.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/PackageType.java @@ -6,49 +6,49 @@ * 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. + * under the License. */ package org.apache.tuscany.sca.contribution; /** * Constants for the main supported contribution package types. - * + * * @version $Rev$ $Date$ */ public interface PackageType { - + /** * Java compressed contribution package */ - String JAR = "application/x-compressed"; + String JAR = "application/java-archive"; /** * Zip archive contribution package */ - String ZIP = "application/x-compressed"; + String ZIP = "application/zip"; /** * Filesystem folder contribution package */ String FOLDER = "application/vnd.tuscany.folder"; - - + + String BUNDLE = "application/osgi.bundle"; - + /** * Java EE Web Application Archive */ String WAR = "application/war"; - + /** * Java EE Enterprise Application Archive */ diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/impl/ContributionImpl.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/impl/ContributionImpl.java index 04a39b061d..7be9472fc9 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/impl/ContributionImpl.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/impl/ContributionImpl.java @@ -20,7 +20,9 @@ package org.apache.tuscany.sca.contribution.impl; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.apache.tuscany.sca.assembly.Composite; import org.apache.tuscany.sca.assembly.impl.ExtensibleImpl; @@ -47,6 +49,7 @@ class ContributionImpl extends ExtensibleImpl implements Contribution { private List<Artifact> artifacts = new ArrayList<Artifact>(); private List<Contribution> dependencies = new ArrayList<Contribution>(); private ModelResolver modelResolver; + private Set<String> types = new HashSet<String>(); // FIXME remove this dependency on Java ClassLoaders private ClassLoader classLoader; @@ -157,4 +160,8 @@ class ContributionImpl extends ExtensibleImpl implements Contribution { "from: " + location; } + public Set<String> getTypes() { + return types; + } + } diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassLoaderModelResolver.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassLoaderModelResolver.java index 516b0d6342..d54eac2228 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassLoaderModelResolver.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/java/impl/ClassLoaderModelResolver.java @@ -6,15 +6,15 @@ * 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. + * under the License. */ package org.apache.tuscany.sca.contribution.java.impl; @@ -46,8 +46,11 @@ import org.apache.tuscany.sca.extensibility.ServiceDiscovery; public class ClassLoaderModelResolver extends URLClassLoader implements ModelResolver { private Contribution contribution; private Map<String, ModelResolver> importResolvers = new HashMap<String, ModelResolver>(); - - private static ClassLoader parentClassLoader() { + + private static ClassLoader parentClassLoader(Contribution contribution) { + if (contribution.getClassLoader() != null) { + return contribution.getClassLoader(); + } ClassLoader parentClassLoader = null; // FIXME: Need a better way to not use the ThreadContextClassLoader when running in Equinox @@ -64,6 +67,10 @@ public class ClassLoaderModelResolver extends URLClassLoader implements ModelRes } private static URL[] getContributionURLs(final Contribution contribution) throws IOException { + if (contribution.getClassLoader() != null) { + // Do not include the contribution url + return new URL[0]; + } List<URL> urls = new ArrayList<URL>(); urls.add(new URL(contribution.getLocation())); urls.addAll(ContributionHelper.getNestedJarUrls(contribution)); @@ -71,9 +78,9 @@ public class ClassLoaderModelResolver extends URLClassLoader implements ModelRes } public ClassLoaderModelResolver(final Contribution contribution, FactoryExtensionPoint modelFactories) throws IOException { - super(getContributionURLs(contribution), parentClassLoader()); + super(getContributionURLs(contribution), parentClassLoader(contribution)); this.contribution = contribution; - + // Index Java import resolvers by package name Map<String, List<ModelResolver>> resolverMap = new HashMap<String, List<ModelResolver>>(); for (Import import_: this.contribution.getImports()) { @@ -87,7 +94,7 @@ public class ClassLoaderModelResolver extends URLClassLoader implements ModelRes resolvers.add(javaImport.getModelResolver()); } } - + // Create a delegating model resolver for each imported package for (Map.Entry<String, List<ModelResolver>> entry: resolverMap.entrySet()) { importResolvers.put(entry.getKey(), new DefaultDelegatingModelResolver(entry.getValue())); @@ -106,38 +113,38 @@ public class ClassLoaderModelResolver extends URLClassLoader implements ModelRes if (!(unresolved instanceof ClassReference)) { return unresolved; } - + try { - + // Load the class and return a class reference for it String className = ((ClassReference)unresolved).getClassName(); Class<?> clazz = Class.forName(className, true, this); return modelClass.cast(new ClassReference(clazz)); - + } catch (ClassNotFoundException e) { return unresolved; } catch (NoClassDefFoundError e) { return unresolved; } } - + @Override public URL findResource(String name) { - + //TODO delegate to the Java import resolvers - + URL url = super.findResource(name); return url; } @Override public Enumeration<URL> findResources(String name) throws IOException { - + //TODO delegate to the Java import resolvers //Enumeration<URL> importedResources; - + Enumeration<URL> resources = super.findResources(name); - List<URL> allResources = new ArrayList<URL>(); + List<URL> allResources = new ArrayList<URL>(); //for (; importedResources.hasMoreElements(); ) { // allResources.add(importedResources.nextElement()); //} @@ -146,10 +153,10 @@ public class ClassLoaderModelResolver extends URLClassLoader implements ModelRes } return Collections.enumeration(allResources); } - + @Override protected Class<?> findClass(String name) throws ClassNotFoundException { - + // Extract the package name int d = name.lastIndexOf('.'); String packageName; @@ -158,7 +165,7 @@ public class ClassLoaderModelResolver extends URLClassLoader implements ModelRes } else { packageName = null; } - + // First try to load the class using the Java import resolvers ModelResolver importResolver = importResolvers.get(packageName); if (importResolver != null) { @@ -172,5 +179,5 @@ public class ClassLoaderModelResolver extends URLClassLoader implements ModelRes Class<?> clazz = super.findClass(name); return clazz; } - + } diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/ContributionContentProcessor.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/ContributionContentProcessor.java index cf62a047f4..5d8b950c45 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/ContributionContentProcessor.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/ContributionContentProcessor.java @@ -129,9 +129,9 @@ public class ContributionContentProcessor implements ExtendedURLArtifactProcesso // Scan the contribution and list the artifacts contained in it List<Artifact> artifacts = contribution.getArtifacts(); boolean contributionMetadata = false; - List<String> artifactURIs = scanner.getArtifacts(contributionURL); + List<String> artifactURIs = scanner.scan(contribution); for (String artifactURI: artifactURIs) { - URL artifactURL = scanner.getArtifactURL(contributionURL, artifactURI); + URL artifactURL = scanner.getArtifactURL(contribution, artifactURI); // Add the deployed artifact model to the contribution Artifact artifact = this.contributionFactory.createArtifact(); @@ -177,7 +177,6 @@ public class ContributionContentProcessor implements ExtendedURLArtifactProcesso DefaultExport defaultExport = contributionFactory.createDefaultExport(); contribution.getExports().add(defaultExport); } - return contribution; } @@ -199,10 +198,10 @@ public class ContributionContentProcessor implements ExtendedURLArtifactProcesso resolveExports(contribution, contributionResolver); // Resolve Imports resolveImports(contribution, contributionResolver); - + preResolved = true; } // end method preResolve - + public void resolve(Contribution contribution, ModelResolver resolver) throws ContributionResolveException { if( !preResolved ) preResolve( contribution, resolver); @@ -230,7 +229,7 @@ public class ContributionContentProcessor implements ExtendedURLArtifactProcesso } } // end for } // end method resolve - + /** * Resolves the Exports of the contribution * @param contribution @@ -247,7 +246,7 @@ public class ContributionContentProcessor implements ExtendedURLArtifactProcesso } // end for } // end method resolveExports - + /** * Resolves the Imports of the contribution * @param contribution @@ -256,7 +255,7 @@ public class ContributionContentProcessor implements ExtendedURLArtifactProcesso private void resolveImports(Contribution contribution, ModelResolver resolver) throws ContributionResolveException { for (Import import_: contribution.getImports()) { extensionProcessor.resolve(import_, resolver); - } // end for + } // end for } // end method resolveImports } // end class ContributionContentProcessor diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/ContributionInfoProcessor.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/ContributionInfoProcessor.java index 5f50b46022..ef435d6aed 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/ContributionInfoProcessor.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/impl/ContributionInfoProcessor.java @@ -123,7 +123,7 @@ public class ContributionInfoProcessor implements URLArtifactProcessor<Contribut for (String path: new String[]{ Contribution.SCA_CONTRIBUTION_GENERATED_META, Contribution.SCA_CONTRIBUTION_META}) { - URL url = scanner.getArtifactURL(contributionURL, path); + URL url = scanner.getArtifactURL(contribution, path); try { // Check if the file actually exists before trying to read it URLConnection connection = url.openConnection(); @@ -151,7 +151,7 @@ public class ContributionInfoProcessor implements URLArtifactProcessor<Contribut if (!contributionMetadata) { List<String> artifactURIs; try { - artifactURIs = scanner.getArtifacts(contributionURL); + artifactURIs = scanner.scan(contribution); } catch (ContributionReadException e) { artifactURIs = null; } @@ -168,7 +168,7 @@ public class ContributionInfoProcessor implements URLArtifactProcessor<Contribut } } if (read) { - URL artifactURL = scanner.getArtifactURL(contributionURL, artifactURI); + URL artifactURL = scanner.getArtifactURL(contribution, artifactURI); // Read each artifact Object model = artifactProcessor.read(contributionURL, URI.create(artifactURI), artifactURL); diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/scanner/ContributionScanner.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/scanner/ContributionScanner.java index f84e336e6d..d2f29a001b 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/scanner/ContributionScanner.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/scanner/ContributionScanner.java @@ -6,15 +6,15 @@ * 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. + * under the License. */ package org.apache.tuscany.sca.contribution.scanner; @@ -22,46 +22,46 @@ import java.io.IOException; import java.net.URL; import java.util.List; +import org.apache.tuscany.sca.contribution.Contribution; import org.apache.tuscany.sca.contribution.processor.ContributionReadException; /** * Interface for contribution package scanners - * + * * Contribution scanners understand the format of the contribution and how to get the * artifacts in the contribution. - * + * * @version $Rev$ $Date$ */ public interface ContributionScanner { - + /** * Returns the type of package supported by this package scanner. - * + * * @return the package type */ String getContributionType(); /** * Returns a list of artifacts in the contribution. - * - * @param contributionURL Contribution URL + * + * @param contribution Contribution URL * @return List of artifact URIs * @throws ContributionReadException * @throws IOException */ - List<String> getArtifacts(URL contributionURL) throws ContributionReadException; + List<String> scan(Contribution contribution) throws ContributionReadException; /** * Return the URL for an artifact in the contribution. - * + * * This is needed for archives such as jar files that have specific URL schemes * for the artifacts they contain. - * - * @param contributionURL Contribution URL + * + * @param contribution Contribution URL * @param artifact The relative URI for the artifact * @throws ContributionReadException * @return The artifact URL */ - URL getArtifactURL(URL contributionURL, String artifact) throws ContributionReadException; - + URL getArtifactURL(Contribution contribution, String artifact) throws ContributionReadException; } diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/scanner/DefaultContributionScannerExtensionPoint.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/scanner/DefaultContributionScannerExtensionPoint.java index 2a288e1708..a1af72da72 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/scanner/DefaultContributionScannerExtensionPoint.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/scanner/DefaultContributionScannerExtensionPoint.java @@ -6,15 +6,15 @@ * 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. + * under the License. */ package org.apache.tuscany.sca.contribution.scanner; @@ -27,13 +27,14 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.apache.tuscany.sca.contribution.Contribution; import org.apache.tuscany.sca.contribution.processor.ContributionReadException; import org.apache.tuscany.sca.extensibility.ServiceDeclaration; import org.apache.tuscany.sca.extensibility.ServiceDiscovery; /** * Default implementation of a contribution scanner extension point. - * + * * @version $Rev$ $Date$ */ public class DefaultContributionScannerExtensionPoint implements ContributionScannerExtensionPoint { @@ -62,24 +63,24 @@ public class DefaultContributionScannerExtensionPoint implements ContributionSca return; // Get the scanner service declarations - Collection<ServiceDeclaration> scannerDeclarations; + Collection<ServiceDeclaration> scannerDeclarations; try { scannerDeclarations = ServiceDiscovery.getInstance().getServiceDeclarations(ContributionScanner.class.getName()); } catch (IOException e) { throw new IllegalStateException(e); } - + for (ServiceDeclaration scannerDeclaration: scannerDeclarations) { Map<String, String> attributes = scannerDeclaration.getAttributes(); - + // Load a URL artifact scanner String contributionType = attributes.get("type"); - + // Create a scanner wrapper and register it ContributionScanner scanner = new LazyContributionScanner(contributionType, scannerDeclaration); addContributionScanner(scanner); } - + loaded = true; } @@ -87,28 +88,28 @@ public class DefaultContributionScannerExtensionPoint implements ContributionSca * A facade for contribution scanners. */ private static class LazyContributionScanner implements ContributionScanner { - + private ServiceDeclaration scannerDeclaration; private String contributionType; private ContributionScanner scanner; - + private LazyContributionScanner(String contributionType, ServiceDeclaration scannerDeclaration) { this.scannerDeclaration = scannerDeclaration; this.contributionType = contributionType; } - public URL getArtifactURL(URL contributionSourceURL, String artifact) throws ContributionReadException { + public URL getArtifactURL(Contribution contributionSourceURL, String artifact) throws ContributionReadException { return getScanner().getArtifactURL(contributionSourceURL, artifact); } - public List<String> getArtifacts(URL contributionSourceURL) throws ContributionReadException { - return getScanner().getArtifacts(contributionSourceURL); + public List<String> scan(Contribution contributionSourceURL) throws ContributionReadException { + return getScanner().scan(contributionSourceURL); } public String getContributionType() { return contributionType; } - + private ContributionScanner getScanner() { if (scanner == null) { try { diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/scanner/impl/DirectoryContributionScanner.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/scanner/impl/DirectoryContributionScanner.java index 16838a95a5..95a4ed69e8 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/scanner/impl/DirectoryContributionScanner.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/scanner/impl/DirectoryContributionScanner.java @@ -6,15 +6,15 @@ * 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. + * under the License. */ package org.apache.tuscany.sca.contribution.scanner.impl; @@ -22,17 +22,20 @@ package org.apache.tuscany.sca.contribution.scanner.impl; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; +import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.util.ArrayList; import java.util.List; +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.PackageType; import org.apache.tuscany.sca.contribution.processor.ContributionReadException; import org.apache.tuscany.sca.contribution.scanner.ContributionScanner; /** * Folder contribution processor. - * + * * @version $Rev$ $Date$ */ public class DirectoryContributionScanner implements ContributionScanner { @@ -41,11 +44,11 @@ public class DirectoryContributionScanner implements ContributionScanner { } public String getContributionType() { - return "application/vnd.tuscany.folder"; + return PackageType.FOLDER; } - public URL getArtifactURL(URL contributionURL, String artifact) throws ContributionReadException { - File directory = directory(contributionURL); + public URL getArtifactURL(Contribution contribution, String artifact) throws ContributionReadException { + File directory = directory(contribution); File file = new File(directory, artifact); try { return file.toURI().toURL(); @@ -54,20 +57,21 @@ public class DirectoryContributionScanner implements ContributionScanner { } } - public List<String> getArtifacts(URL contributionURL) throws ContributionReadException { - File directory = directory(contributionURL); + public List<String> scan(Contribution contribution) throws ContributionReadException { + File directory = directory(contribution); List<String> artifacts = new ArrayList<String>(); try { traverse(artifacts, directory, directory); } catch (IOException e) { throw new ContributionReadException(e); } + contribution.getTypes().add(getContributionType()); return artifacts; } /** * Recursively traverse a root directory - * + * * @param fileList * @param file * @param root @@ -82,7 +86,7 @@ public class DirectoryContributionScanner implements ContributionScanner { uri = uri.substring(0, uri.length() - 1); } fileList.add(uri); - + File[] files = file.listFiles(); for (File f: files) { if (!f.getName().startsWith(".")) { @@ -92,16 +96,17 @@ public class DirectoryContributionScanner implements ContributionScanner { } } - private static File directory(URL url) throws ContributionReadException { + private static File directory(Contribution contribution) throws ContributionReadException { File file; try { - file = new File(url.toURI()); + file = new File(new URI(contribution.getLocation())); } catch (URISyntaxException e) { throw new ContributionReadException(e); } if (!file.exists() || !file.isDirectory()) { - throw new ContributionReadException(url.toString()); + throw new ContributionReadException(contribution.getLocation()); } return file; } + } diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/scanner/impl/JarContributionScanner.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/scanner/impl/JarContributionScanner.java index efb3e02b5a..f1fb6f2a70 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/scanner/impl/JarContributionScanner.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/scanner/impl/JarContributionScanner.java @@ -6,15 +6,15 @@ * 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. + * under the License. */ package org.apache.tuscany.sca.contribution.scanner.impl; @@ -30,12 +30,14 @@ import java.util.Set; import java.util.jar.JarEntry; import java.util.jar.JarInputStream; +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.PackageType; import org.apache.tuscany.sca.contribution.processor.ContributionReadException; import org.apache.tuscany.sca.contribution.scanner.ContributionScanner; /** * JAR Contribution processor. - * + * * @version $Rev$ $Date$ */ public class JarContributionScanner implements ContributionScanner { @@ -44,16 +46,16 @@ public class JarContributionScanner implements ContributionScanner { } public String getContributionType() { - return "application/x-compressed"; + return PackageType.JAR; } - public URL getArtifactURL(URL contributionURL, String artifact) throws ContributionReadException { + public URL getArtifactURL(Contribution contribution, String artifact) throws ContributionReadException { try { URL url; - if (contributionURL.toString().startsWith("jar:")) { - url = new URL(contributionURL, artifact.toString()); + if (contribution.toString().startsWith("jar:")) { + url = new URL(new URL(contribution.getLocation()), artifact.toString()); } else { - url = new URL("jar:" + contributionURL.toExternalForm() + "!/" + artifact); + url = new URL("jar:" + contribution.getLocation() + "!/" + artifact); } return url; } catch (MalformedURLException e) { @@ -61,11 +63,12 @@ public class JarContributionScanner implements ContributionScanner { } } - public List<String> getArtifacts(URL contributionURL) throws ContributionReadException { + public List<String> scan(Contribution contribution) throws ContributionReadException { // Assume the URL references a JAR file try { - URLConnection connection = contributionURL.openConnection(); + URL url = new URL(contribution.getLocation()); + URLConnection connection = url.openConnection(); connection.setUseCaches(false); JarInputStream jar = new JarInputStream(connection.getInputStream()); try { @@ -77,9 +80,9 @@ public class JarContributionScanner implements ContributionScanner { break; } - String name = entry.getName(); + String name = entry.getName(); if (name.length() != 0 && !name.startsWith(".")) { - + // Trim trailing / if (name.endsWith("/")) { name = name.substring(0, name.length() - 1); @@ -88,7 +91,7 @@ public class JarContributionScanner implements ContributionScanner { // Add the entry name if (!names.contains(name)) { names.add(name); - + // Add parent folder names to the list too for (;;) { int s = name.lastIndexOf('/'); @@ -106,11 +109,12 @@ public class JarContributionScanner implements ContributionScanner { } } } - + // Return list of URIs List<String> artifacts = new ArrayList<String>(names); + contribution.getTypes().add(getContributionType()); return artifacts; - + } finally { jar.close(); } @@ -118,4 +122,7 @@ public class JarContributionScanner implements ContributionScanner { throw new ContributionReadException(e); } } + + public void postProcess(Contribution contribution) { + } } diff --git a/java/sca/modules/contribution/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.ContributionPostProcessorExtensionPoint b/java/sca/modules/contribution/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.ContributionPostProcessorExtensionPoint deleted file mode 100644 index 9e6433ba6c..0000000000 --- a/java/sca/modules/contribution/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.ContributionPostProcessorExtensionPoint +++ /dev/null @@ -1,18 +0,0 @@ -# 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.
-
-org.apache.tuscany.sca.contribution.processor.DefaultContributionPostProcessorExtensionPoint
diff --git a/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/Contributions.java b/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/Contributions.java index 71688d7073..7dabb471be 100644 --- a/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/Contributions.java +++ b/java/sca/modules/node-impl/src/main/java/org/apache/tuscany/sca/node/impl/Contributions.java @@ -6,21 +6,23 @@ * 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.
+ * under the License.
*/
package org.apache.tuscany.sca.node.impl;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
+import java.util.Set;
import org.apache.tuscany.sca.assembly.Composite;
import org.apache.tuscany.sca.assembly.Extension;
@@ -32,7 +34,7 @@ import org.apache.tuscany.sca.contribution.resolver.ModelResolver; /**
* A Contribution impl wrapping multiple other contributions
- * Currently the sole reason for this is so
+ * Currently the sole reason for this is so
*/
public class Contributions implements Contribution {
@@ -42,13 +44,13 @@ public class Contributions implements Contribution { private Object model;
private byte[] contents;
private boolean unresolved;
- private ModelResolver modelResolver;
+ private ModelResolver modelResolver;
private List<Contribution> dependencies = new ArrayList<Contribution>();
-
+
public Contributions(List<Contribution> contributions) {
this.contributions = contributions;
}
-
+
public String getLocation() {
return location;
}
@@ -72,11 +74,11 @@ public class Contributions implements Contribution { public byte[] getContents() {
return contents;
}
-
+
public void setContents(byte[] contents) {
this.contents = contents;
}
-
+
public void setURI(String uri) {
this.uri = uri;
}
@@ -92,11 +94,11 @@ public class Contributions implements Contribution { public List<Contribution> getContributions() {
return contributions;
}
-
+
public List<Artifact> getArtifacts() {
return (List<Artifact>)(Object)contributions;
}
-
+
public List<Contribution> getDependencies() {
return dependencies;
}
@@ -105,11 +107,11 @@ public class Contributions implements Contribution { //FIXME Remove later
return null;
}
-
+
public void setClassLoader(ClassLoader classLoader) {
//FIXME Remove later
}
-
+
public List<Composite> getDeployables() {
List<Composite> deployables = new ArrayList<Composite>();
for (Contribution contribution: contributions) {
@@ -117,7 +119,7 @@ public class Contributions implements Contribution { }
return deployables;
}
-
+
public List<Export> getExports() {
List<Export> exports = new ArrayList<Export>();
for (Contribution contribution: contributions) {
@@ -125,7 +127,7 @@ public class Contributions implements Contribution { }
return exports;
}
-
+
public List<Import> getImports() {
List<Import> imports = new ArrayList<Import>();
for (Contribution contribution: contributions) {
@@ -133,11 +135,11 @@ public class Contributions implements Contribution { }
return imports;
}
-
+
public ModelResolver getModelResolver() {
return modelResolver;
}
-
+
public void setModelResolver(ModelResolver modelResolver) {
this.modelResolver = modelResolver;
}
@@ -151,4 +153,8 @@ public class Contributions implements Contribution { // TODO Auto-generated method stub
return null;
}
+
+ public Set<String> getTypes() {
+ return Collections.emptySet();
+ }
}
diff --git a/java/sca/modules/workspace/src/main/java/org/apache/tuscany/sca/workspace/impl/WorkspaceImpl.java b/java/sca/modules/workspace/src/main/java/org/apache/tuscany/sca/workspace/impl/WorkspaceImpl.java index 0e2b673063..7674c13627 100644 --- a/java/sca/modules/workspace/src/main/java/org/apache/tuscany/sca/workspace/impl/WorkspaceImpl.java +++ b/java/sca/modules/workspace/src/main/java/org/apache/tuscany/sca/workspace/impl/WorkspaceImpl.java @@ -20,7 +20,9 @@ package org.apache.tuscany.sca.workspace.impl; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.apache.tuscany.sca.assembly.Composite; import org.apache.tuscany.sca.assembly.impl.ExtensibleImpl; @@ -46,11 +48,12 @@ class WorkspaceImpl extends ExtensibleImpl implements Workspace { private boolean unresolved; private ModelResolver modelResolver; private List<Contribution> dependencies = new ArrayList<Contribution>(); - + private Set<String> types = new HashSet<String>(); /** * Constructs a new workspace. */ WorkspaceImpl() { + types.add("application/vnd.tuscany.workspace"); } public String getLocation() { @@ -145,4 +148,8 @@ class WorkspaceImpl extends ExtensibleImpl implements Workspace { public void setModelResolver(ModelResolver modelResolver) { this.modelResolver = modelResolver; } + + public Set<String> getTypes() { + return types; + } } |