Java tutorial
/* * Copyright 2016 Johannes Donath <johannesd@torchmind.com> * and other copyright owners as documented in the project's IP log. * * Licensed 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 com.torchmind.upm.bundle; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import org.apache.commons.compress.archivers.ArchiveInputStream; import org.apache.commons.compress.archivers.jar.JarArchiveInputStream; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; import org.apache.commons.compress.compressors.xz.XZCompressorInputStream; import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.io.IOException; import java.io.InputStream; import java.net.URI; /** * <strong>Basic Resource</strong> * * Provides a simple implementation of Resource which can be de-serialized using Jackson. * * @author <a href="mailto:johannesd@torchmind.com">Johannes Donath</a> */ public class BasicResource implements Resource { private final String name; private final String outputDirectory; private final ResourceType type; private final URI uri; @JsonCreator public BasicResource(@Nonnull @JsonProperty(value = "name", required = true) String name, @Nullable @JsonProperty("outputDirectory") String outputDirectory, @Nonnull @JsonProperty(value = "type", required = true) ResourceType type, @Nonnull @JsonProperty(value = "uri", required = true) URI uri) { this.name = name; this.outputDirectory = outputDirectory; this.type = type; this.uri = uri; } /** * {@inheritDoc} */ @Nonnull @Override public ArchiveInputStream createArchiveInputStream() throws IllegalStateException, IOException { switch (this.type) { case RAW: throw new IllegalStateException("Cannot convert RAW resource into archive"); case JAR: return new JarArchiveInputStream(this.createInputStream()); case TAR_ARCHIVE: return new TarArchiveInputStream(this.createInputStream()); case TAR_GZ_ARCHIVE: return new TarArchiveInputStream(new GzipCompressorInputStream(this.createInputStream())); case TAR_XZ_ARCHIVE: return new TarArchiveInputStream(new XZCompressorInputStream(this.createInputStream())); case ZIP_ARCHIVE: return new ZipArchiveInputStream(this.createInputStream()); } throw new UnsupportedOperationException("No such resource type: " + this.type); } /** * {@inheritDoc} */ @Nonnull @Override public InputStream createInputStream() { throw new UnsupportedOperationException("Cannot resolve resource"); } /** * {@inheritDoc} */ @Nonnull @Override public String getName() { return this.name; } /** * {@inheritDoc} */ @Nonnull @Override public String getOutputDirectory() { return this.outputDirectory; } /** * {@inheritDoc} */ @Nonnull @Override public ResourceType getType() { return this.type; } /** * {@inheritDoc} */ @Nonnull @Override public URI getURI() { return this.uri; } }