/* * #%L * restdoc-plugin * %% * Copyright (C) 2012 IG Group * %% * 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. * #L% */ package com.iggroup.oss.restdoclet.plugin.io; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.jar.JarEntry; import java.util.jar.JarOutputStream; import java.util.jar.Manifest; import org.apache.log4j.Logger; import com.iggroup.oss.restdoclet.plugin.util.MavenUtils; /** * Assembles the web-application generated by documentation. */ public class JarBuilder { private static final Logger LOG = Logger.getLogger(JarBuilder.class); /** * The name of the manifest-file. */ public static final String MANIFEST = "MANIFEST.MF"; /** * The path-separator while building the archive. */ public static final char SEP = '/'; /** * The <code>target</code> directories of the module RestDocumentationMojo * is running on. */ private final transient DirectoryBuilder dirs; /** * The name of the archive the web-application is assembled as. This name * includes the classifier used while running RestDocumentationMojo. */ private transient String finalName; /** * Constructs this builder with the <code>target</code> directories of the * module RestDocumentationMojo is running on and the name of the archive * the web-application has to be assembled as. * * @param dirs the <code>target</code> directories. * @param finalName the name of the archive. */ public JarBuilder(final DirectoryBuilder dirs, final String finalName) { this.dirs = dirs; initFinalName(finalName); } /** * Initialises the name of the archive the web-application has to be * assembled as. * * @param finalName the name of the archive. */ private void initFinalName(final String finalName) { final String suffix = '.' + MavenUtils.JAR_PACKAGING; if (finalName.endsWith(suffix)) { this.finalName = finalName; } else { this.finalName = finalName + suffix; } } /** * Creates the manifest-file. * * @return the manifest-file. * @throws IOException if the manifest-file can't be read. */ private Manifest createManifest() throws IOException { return new Manifest(getClass().getResourceAsStream(MANIFEST)); } /** * Puts an entry in the JAR's output-stream. * * @param jos the JAR's output-stream. * @param name the name of the entry. * @param file the file containing the entry's contents. * @throws IOException if the file can't be copied. */ private void putEntry(final JarOutputStream jos, final String name, final File file) throws IOException { /* add entry */ final JarEntry entry = new JarEntry(name); jos.putNextEntry(entry); /* write contents */ IOUtils.copy(file, jos); } /** * Puts the classpath entries in the JAR's output-stream. * * @param jos the JAR's output-stream. * @throws IOException if an entry can't be copied. */ private void putResourceConfigs(final JarOutputStream jos) throws IOException { for (File file : dirs.getClassesDirectory().listFiles()) { if (file.isFile()) { LOG.debug("Adding " + file); final String name = file.getName(); putEntry(jos, name, file); } } } /** * Assembles the jar . * * @return the assembled jar * @throws IOException if the jar can't be created or a JAR's entry can't be * copied. */ public File build() throws IOException { /* jar output stream */ final File jar = new File(dirs.getTargetDirectory(), finalName); LOG.info("Building " + jar); final JarOutputStream jos = new JarOutputStream(new BufferedOutputStream( new FileOutputStream(jar)), createManifest()); putResourceConfigs(jos); jos.close(); return jar; } }