All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.joshworks.snappy.maven.tools.Layouts Maven / Gradle / Ivy

There is a newer version: 0.6.0
Show newest version
/*
 * Copyright 2012-2013 the original author or authors.
 *
 * 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 io.joshworks.snappy.maven.tools;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Common {@link Layout}s.
 *
 * @author Phillip Webb
 * @author Dave Syer
 * @author Andy Wilkinson
 */
public final class Layouts {

    private Layouts() {
    }

    /**
     * Return a layout for the given source file.
     *
     * @param file the source file
     * @return a {@link Layout}
     */
    public static Layout forFile(File file) {
        if (file == null) {
            throw new IllegalArgumentException("File must not be null");
        }
        if (file.getName().toLowerCase().endsWith(".jar")) {
            return new Jar();
        }
        if (file.getName().toLowerCase().endsWith(".war")) {
            return new War();
        }
        if (file.isDirectory() || file.getName().toLowerCase().endsWith(".zip")) {
            return new Expanded();
        }
        throw new IllegalStateException("Unable to deduce layout for '" + file + "'");
    }

    /**
     * Executable JAR layout.
     */
    public static class Jar implements Layout {

        @Override
        public String getLauncherClassName() {
            return "io.joshworks.snappy.loader.JarLauncher";
        }

        @Override
        public String getLibraryDestination(String libraryName, LibraryScope scope) {
            return "lib/";
        }

        @Override
        public String getClassesLocation() {
            return "";
        }

        @Override
        public boolean isExecutable() {
            return true;
        }

    }

    /**
     * Executable expanded archive layout.
     */
    public static class Expanded extends Jar {

        @Override
        public String getLauncherClassName() {
            return "io.joshworks.snappy.loader.PropertiesLauncher";
        }

    }

    /**
     * No layout.
     */
    public static class None extends Jar {

        @Override
        public String getLauncherClassName() {
            return null;
        }

        @Override
        public boolean isExecutable() {
            return false;
        }

    }

    /**
     * Executable WAR layout.
     */
    public static class War implements Layout {

        private static final Map SCOPE_DESTINATIONS;

        static {
            Map map = new HashMap();
            map.put(LibraryScope.COMPILE, "WEB-INF/lib/");
            map.put(LibraryScope.CUSTOM, "WEB-INF/lib/");
            map.put(LibraryScope.RUNTIME, "WEB-INF/lib/");
            map.put(LibraryScope.PROVIDED, "WEB-INF/lib-provided/");
            SCOPE_DESTINATIONS = Collections.unmodifiableMap(map);
        }

        @Override
        public String getLauncherClassName() {
            return "io.joshworks.snappy.loader.WarLauncher";
        }

        @Override
        public String getLibraryDestination(String libraryName, LibraryScope scope) {
            return SCOPE_DESTINATIONS.get(scope);
        }

        @Override
        public String getClassesLocation() {
            return "WEB-INF/classes/";
        }

        @Override
        public boolean isExecutable() {
            return true;
        }

    }

    /**
     * Module layout (designed to be used as a "plug-in").
     */
    public static class Module implements Layout {

        private static final Set LIB_DESTINATION_SCOPES = new HashSet(
                Arrays.asList(LibraryScope.COMPILE, LibraryScope.RUNTIME,
                        LibraryScope.CUSTOM));

        @Override
        public String getLauncherClassName() {
            return null;
        }

        @Override
        public String getLibraryDestination(String libraryName, LibraryScope scope) {
            if (LIB_DESTINATION_SCOPES.contains(scope)) {
                return "lib/";
            }
            return null;
        }

        @Override
        public String getClassesLocation() {
            return "";
        }

        @Override
        public boolean isExecutable() {
            return false;
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy