001/*
002 * (C) Copyright 2006-2017 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     slacoin
018 *     Kevin Leturc <kleturc@nuxeo.com>
019 */
020package org.nuxeo.runtime.tomcat.dev;
021
022import java.io.BufferedReader;
023import java.io.File;
024import java.io.IOException;
025import java.io.InputStream;
026import java.io.InputStreamReader;
027import java.io.Serializable;
028import java.net.URL;
029import java.util.ArrayList;
030import java.util.List;
031
032/**
033 * Bundle descriptor for hot deployment.
034 *
035 * @since 5.5
036 */
037public class DevBundle implements Serializable {
038
039    private static final long serialVersionUID = 1L;
040
041    protected String name; // the bundle symbolic name if not a lib
042
043    protected final DevBundleType devBundleType;
044
045    protected final String path;
046
047    public static DevBundle[] parseDevBundleLines(InputStream is) throws IOException {
048        try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
049            List<DevBundle> bundles = new ArrayList<>();
050            String line;
051            while ((line = reader.readLine()) != null) {
052                line = line.trim();
053                if (line.length() > 0 && !line.startsWith("#")) {
054                    bundles.add(parseDevBundleLine(line));
055                }
056            }
057            return bundles.toArray(new DevBundle[bundles.size()]);
058        }
059    }
060
061    public static DevBundle parseDevBundleLine(String line) {
062        int index = line.indexOf(':');
063        String typename = line.substring(0, index);
064        typename = typename.substring(0, 1).toUpperCase() + typename.substring(1);
065        String path = line.substring(index + 1);
066        return new DevBundle(path, DevBundleType.valueOf(typename));
067    }
068
069    public DevBundle(String path, DevBundleType devBundleType) {
070        this.path = path;
071        this.devBundleType = devBundleType;
072    }
073
074    public URL url() throws IOException {
075        return file().toURI().toURL();
076    }
077
078    public File file() {
079        return new File(path);
080    }
081
082    public String getName() {
083        return name;
084    }
085
086    public String getPath() {
087        return file().getAbsolutePath();
088    }
089
090    /**
091     * @since 9.3
092     */
093    public DevBundleType getDevBundleType() {
094        return devBundleType;
095    }
096
097    /**
098     * Converts the {@link DevBundle} to String with the same format as the one expected in
099     * {@link DevBundle#parseDevBundleLine(String) parseDevBundleLine}.
100     *
101     * @since 9.3
102     */
103    public String toString() {
104        return devBundleType.toString().toLowerCase() + ':' + path;
105    }
106
107}