org.fabric3.maven.contribution.ModuleContributionProcessor Maven / Gradle / Ivy
/*
* Fabric3
* Copyright (c) 2009 Metaform Systems
*
* Fabric3 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version, with the
* following exception:
*
* Linking this software statically or dynamically with other
* modules is making a combined work based on this software.
* Thus, the terms and conditions of the GNU General Public
* License cover the whole combination.
*
* As a special exception, the copyright holders of this software
* give you permission to link this software with independent
* modules to produce an executable, regardless of the license
* terms of these independent modules, and to copy and distribute
* the resulting executable under terms of your choice, provided
* that you also meet, for each linked independent module, the
* terms and conditions of the license of that module. An
* independent module is a module which is not derived from or
* based on this software. If you modify this software, you may
* extend this exception to your version of the software, but
* you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version.
*
* Fabric3 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the
* GNU General Public License along with Fabric3.
* If not, see .
*/
package org.fabric3.maven.contribution;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.osoa.sca.annotations.EagerInit;
import org.osoa.sca.annotations.Init;
import org.osoa.sca.annotations.Reference;
import org.fabric3.host.contribution.InstallException;
import org.fabric3.spi.contribution.Contribution;
import org.fabric3.spi.contribution.ContributionManifest;
import org.fabric3.spi.contribution.ContributionProcessor;
import org.fabric3.spi.contribution.ProcessorRegistry;
import org.fabric3.spi.contribution.Resource;
import org.fabric3.spi.contribution.archive.Action;
import org.fabric3.spi.introspection.DefaultIntrospectionContext;
import org.fabric3.spi.introspection.IntrospectionContext;
import org.fabric3.spi.introspection.xml.Loader;
import org.fabric3.spi.introspection.xml.LoaderException;
import org.fabric3.spi.contenttype.ContentTypeResolutionException;
import org.fabric3.spi.contenttype.ContentTypeResolver;
import org.fabric3.util.io.FileHelper;
/**
* Processes a Maven module directory.
*
* @version $Rev: 7441 $ $Date: 2009-07-26 14:38:08 +0200 (Sun, 26 Jul 2009) $
*/
@EagerInit
public class ModuleContributionProcessor implements ContributionProcessor {
public static final List CONTENT_TYPES = initializeContentTypes();
private ProcessorRegistry registry;
private ContentTypeResolver contentTypeResolver;
private Loader loader;
public ModuleContributionProcessor(@Reference ProcessorRegistry registry,
@Reference ContentTypeResolver contentTypeResolver,
@Reference Loader loader) {
this.registry = registry;
this.contentTypeResolver = contentTypeResolver;
this.loader = loader;
}
public List getContentTypes() {
return CONTENT_TYPES;
}
@Init
public void init() {
registry.register(this);
}
public void process(Contribution contribution, IntrospectionContext context, ClassLoader loader) throws InstallException {
ClassLoader oldClassloader = Thread.currentThread().getContextClassLoader();
URI contributionUri = contribution.getUri();
try {
Thread.currentThread().setContextClassLoader(loader);
for (Resource resource : contribution.getResources()) {
if (!resource.isProcessed()) {
registry.processResource(contributionUri, resource, context, loader);
}
}
} finally {
Thread.currentThread().setContextClassLoader(oldClassloader);
}
}
public void processManifest(Contribution contribution, final IntrospectionContext context) throws InstallException {
ContributionManifest manifest;
try {
URL sourceUrl = contribution.getLocation();
URL manifestURL = new URL(sourceUrl.toExternalForm() + "/classes/META-INF/sca-contribution.xml");
ClassLoader cl = getClass().getClassLoader();
URI uri = contribution.getUri();
IntrospectionContext childContext = new DefaultIntrospectionContext(cl, uri, null);
manifest = loader.load(manifestURL, ContributionManifest.class, childContext);
if (childContext.hasErrors()) {
context.addErrors(childContext.getErrors());
}
if (childContext.hasWarnings()) {
context.addWarnings(childContext.getWarnings());
}
contribution.setManifest(manifest);
} catch (LoaderException e) {
if (e.getCause() instanceof FileNotFoundException) {
// ignore no manifest found
} else {
throw new InstallException(e);
}
} catch (MalformedURLException e) {
// ignore no manifest found
}
}
public void index(Contribution contribution, final IntrospectionContext context) throws InstallException {
iterateArtifacts(contribution, context, new Action() {
public void process(Contribution contribution, String contentType, URL url)
throws InstallException {
registry.indexResource(contribution, contentType, url, context);
}
});
}
private void iterateArtifacts(Contribution contribution, final IntrospectionContext context, Action action) throws InstallException {
File root = FileHelper.toFile(contribution.getLocation());
assert root.isDirectory();
iterateArtifactsResursive(contribution, context, action, root);
}
private void iterateArtifactsResursive(Contribution contribution, final IntrospectionContext context, Action action, File dir)
throws InstallException {
File[] files = dir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
iterateArtifactsResursive(contribution, context, action, file);
} else {
try {
URL entryUrl = file.toURI().toURL();
String contentType = contentTypeResolver.getContentType(entryUrl);
action.process(contribution, contentType, entryUrl);
} catch (MalformedURLException e) {
context.addWarning(new ContributionIndexingFailure(file, e));
} catch (IOException e) {
context.addWarning(new ContributionIndexingFailure(file, e));
} catch (ContentTypeResolutionException e) {
context.addWarning(new ContributionIndexingFailure(file, e));
}
}
}
}
private static List initializeContentTypes() {
List list = new ArrayList(1);
list.add("application/vnd.fabric3.maven-project");
return list;
}
} © 2015 - 2025 Weber Informatics LLC | Privacy Policy