001package org.nuxeo.runtime.tomcat.dev;
002
003import java.io.BufferedReader;
004import java.io.File;
005import java.io.IOException;
006import java.io.InputStream;
007import java.io.InputStreamReader;
008import java.io.Serializable;
009import java.net.MalformedURLException;
010import java.net.URL;
011import java.util.ArrayList;
012
013/*
014 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
015 *
016 * All rights reserved. This program and the accompanying materials
017 * are made available under the terms of the GNU Lesser General Public License
018 * (LGPL) version 2.1 which accompanies this distribution, and is available at
019 * http://www.gnu.org/licenses/lgpl.html
020 *
021 * This library is distributed in the hope that it will be useful,
022 * but WITHOUT ANY WARRANTY; without even the implied warranty of
023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
024 * Lesser General Public License for more details.
025 *
026 * Contributors:
027 *     slacoin
028 */
029
030/**
031 * Bundle descriptor for hot deployment
032 * 
033 * @since 5.5
034 */
035public class DevBundle implements Serializable {
036
037    private static final long serialVersionUID = 1L;
038
039    protected String name; // the bundle symbolic name if not a lib
040
041    protected final DevBundleType devBundleType;
042
043    protected final String path;
044
045    public static DevBundle[] parseDevBundleLines(InputStream is) throws IOException {
046        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
047        try {
048            ArrayList<DevBundle> bundles = new ArrayList<DevBundle>();
049            String line = reader.readLine();
050            while (line != null) {
051                line = line.trim();
052                if (line.length() > 0 && !line.startsWith("#")) {
053                    bundles.add(parseDevBundleLine(line));
054                }
055                line = reader.readLine();
056            }
057            return bundles.toArray(new DevBundle[bundles.size()]);
058        } finally {
059            reader.close();
060        }
061    }
062
063    public static DevBundle parseDevBundleLine(String line) throws MalformedURLException {
064        int index = line.indexOf(':');
065        String typename = line.substring(0, index);
066        typename = typename.substring(0, 1).toUpperCase() + typename.substring(1);
067        String path = line.substring(index + 1);
068        return new DevBundle(path, DevBundleType.valueOf(typename));
069    }
070
071    public DevBundle(String path, DevBundleType devBundleType) {
072        this.path = path;
073        this.devBundleType = devBundleType;
074    }
075
076    public URL url() throws IOException {
077        return new File(path).toURI().toURL();
078    }
079
080    public File file() {
081        return new File(path);
082    }
083
084    public String getName() {
085        return name;
086    }
087
088    public String getPath() {
089        return file().getAbsolutePath();
090    }
091}