001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (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 */
019package org.nuxeo.runtime.tomcat.dev;
020
021import java.io.BufferedReader;
022import java.io.File;
023import java.io.IOException;
024import java.io.InputStream;
025import java.io.InputStreamReader;
026import java.io.Serializable;
027import java.net.MalformedURLException;
028import java.net.URL;
029import java.util.ArrayList;
030
031/**
032 * Bundle descriptor for hot deployment
033 *
034 * @since 5.5
035 */
036public class DevBundle implements Serializable {
037
038    private static final long serialVersionUID = 1L;
039
040    protected String name; // the bundle symbolic name if not a lib
041
042    protected final DevBundleType devBundleType;
043
044    protected final String path;
045
046    public static DevBundle[] parseDevBundleLines(InputStream is) throws IOException {
047        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
048        try {
049            ArrayList<DevBundle> bundles = new ArrayList<DevBundle>();
050            String line = reader.readLine();
051            while (line != null) {
052                line = line.trim();
053                if (line.length() > 0 && !line.startsWith("#")) {
054                    bundles.add(parseDevBundleLine(line));
055                }
056                line = reader.readLine();
057            }
058            return bundles.toArray(new DevBundle[bundles.size()]);
059        } finally {
060            reader.close();
061        }
062    }
063
064    public static DevBundle parseDevBundleLine(String line) throws MalformedURLException {
065        int index = line.indexOf(':');
066        String typename = line.substring(0, index);
067        typename = typename.substring(0, 1).toUpperCase() + typename.substring(1);
068        String path = line.substring(index + 1);
069        return new DevBundle(path, DevBundleType.valueOf(typename));
070    }
071
072    public DevBundle(String path, DevBundleType devBundleType) {
073        this.path = path;
074        this.devBundleType = devBundleType;
075    }
076
077    public URL url() throws IOException {
078        return new File(path).toURI().toURL();
079    }
080
081    public File file() {
082        return new File(path);
083    }
084
085    public String getName() {
086        return name;
087    }
088
089    public String getPath() {
090        return file().getAbsolutePath();
091    }
092}