org.fabric3.contribution.war.WarContributionHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fabric3-contribution-war Show documentation
Show all versions of fabric3-contribution-war Show documentation
Support for processing and loading WAR contributions
/*
* 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.fabric3.contribution.war;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.osoa.sca.annotations.Destroy;
import org.osoa.sca.annotations.EagerInit;
import org.osoa.sca.annotations.Init;
import org.osoa.sca.annotations.Reference;
import org.fabric3.api.annotation.Monitor;
import org.fabric3.host.contribution.Constants;
import org.fabric3.host.contribution.ContributionException;
import org.fabric3.introspection.DefaultIntrospectionContext;
import org.fabric3.introspection.IntrospectionContext;
import org.fabric3.introspection.xml.Loader;
import org.fabric3.introspection.xml.LoaderException;
import org.fabric3.scdl.ValidationContext;
import org.fabric3.spi.services.contenttype.ContentTypeResolutionException;
import org.fabric3.spi.services.contenttype.ContentTypeResolver;
import org.fabric3.spi.services.contribution.Action;
import org.fabric3.spi.services.contribution.ArchiveContributionHandler;
import org.fabric3.spi.services.contribution.Contribution;
import org.fabric3.spi.services.contribution.ContributionManifest;
import org.fabric3.spi.services.contribution.ProcessorRegistry;
/**
* Introspects a WAR contribution, delegating to ResourceProcessors for handling leaf-level children.
*/
@EagerInit
public class WarContributionHandler implements ArchiveContributionHandler {
private final Loader loader;
private final ContentTypeResolver contentTypeResolver;
private WarContributionMonitor monitor;
private ProcessorRegistry registry;
public WarContributionHandler(@Reference ProcessorRegistry processorRegistry,
@Reference Loader loader,
@Reference ContentTypeResolver contentTypeResolver,
@Monitor WarContributionMonitor monitor) {
this.registry = processorRegistry;
this.loader = loader;
this.contentTypeResolver = contentTypeResolver;
this.monitor = monitor;
}
@Init
public void init() {
monitor.extensionStarted();
}
@Destroy
public void destroy() {
monitor.extensionStopped();
}
public String getContentType() {
return Constants.ZIP_CONTENT_TYPE;
}
public boolean canProcess(Contribution contribution) {
String sourceUrl = contribution.getLocation().toString();
return sourceUrl.endsWith(".war");
}
public void processManifest(Contribution contribution, final ValidationContext context) throws ContributionException {
ContributionManifest manifest;
try {
URL sourceUrl = contribution.getLocation();
URL manifestURL = new URL("jar:" + sourceUrl.toExternalForm() + "!/WEB-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());
}
} catch (LoaderException e) {
if (e.getCause() instanceof FileNotFoundException) {
manifest = new ContributionManifest();
} else {
throw new ContributionException(e);
}
} catch (MalformedURLException e) {
manifest = new ContributionManifest();
}
contribution.setManifest(manifest);
iterateArtifacts(contribution, new Action() {
public void process(Contribution contribution, String contentType, URL url) throws ContributionException {
InputStream stream = null;
try {
stream = url.openStream();
registry.processManifestArtifact(contribution.getManifest(), contentType, stream, context);
} catch (IOException e) {
throw new ContributionException(e);
} finally {
try {
if (stream != null) {
stream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
public void iterateArtifacts(Contribution contribution, Action action) throws ContributionException {
URL location = contribution.getLocation();
ZipInputStream zipStream = null;
try {
zipStream = new ZipInputStream(location.openStream());
while (true) {
ZipEntry entry = zipStream.getNextEntry();
if (entry == null) {
// EOF
break;
}
if (entry.isDirectory()) {
continue;
}
URL entryUrl = new URL("jar:" + location.toExternalForm() + "!/" + entry.getName());
// hack to return the correct content type
String contentType = contentTypeResolver.getContentType(new URL(location, entry.getName()));
// String contentType = contentTypeResolver.getContentType(entryUrl);
// skip entry if we don't recognize the content type
if (contentType == null) {
continue;
}
action.process(contribution, contentType, entryUrl);
}
} catch (ContentTypeResolutionException e) {
throw new ContributionException(e);
} catch (MalformedURLException e) {
throw new ContributionException(e);
} catch (IOException e) {
throw new ContributionException(e);
} finally {
try {
if (zipStream != null) {
zipStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy