001    /*
002     *  Licensed under the Apache License, Version 2.0 (the "License");
003     *  you may not use this file except in compliance with the License.
004     *  You may obtain a copy of the License at
005     *
006     *      http://www.apache.org/licenses/LICENSE-2.0
007     *
008     *  Unless required by applicable law or agreed to in writing, software
009     *  distributed under the License is distributed on an "AS IS" BASIS,
010     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011     *  See the License for the specific language governing permissions and
012     *  limitations under the License.
013     */
014    /*
015     * This file has been modified by Chris Pheby in accordance with Section 4.2
016     * of the Apache Software License
017     */
018    package org.jadira.dependencynavigator;
019    
020    import java.util.Properties;
021    
022    import javax.swing.UIManager;
023    
024    import org.codehaus.classworlds.ClassWorld;
025    import org.codehaus.plexus.embed.Embedder;
026    import org.jadira.dependencynavigator.config.Config;
027    import org.jadira.dependencynavigator.gui.DependencyBrowserGui;
028    import org.jadira.dependencynavigator.implementations.local.LocalDiskRepository;
029    import org.jadira.dependencynavigator.implementations.local.LocalDiskWorkspace;
030    import org.jadira.dependencynavigator.implementations.zip.ZipFileRepository;
031    import org.jadira.dependencynavigator.implementations.zip.ZipFileWorkspace;
032    
033    public class DependencyNavigator {
034    
035        private static final String KEY_TYPE = "type";
036        public static final String TYPE_LOCAL = "local";
037        private static final String TYPE_ZIP = "zip";
038    
039        /**
040         * @deprecated - use gui config org.jadira.dependencynavigator.config.Config instead
041         */
042        private static final String ARG_REPOSITORY = "--repository=";
043        /**
044         * @deprecated - use gui config org.jadira.dependencynavigator.config.Config instead
045         */
046        private static final String ARG_WORKSPACE = "--workspace=";
047        private static final String ARG_SNAPSHOT = "--snapshot=";
048    
049        public static void main(String[] args) throws Exception {
050            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
051    
052            // try to configure from the command line
053            Properties properties = configureFromCommandLine(args);
054            if (properties.get(KEY_TYPE) == null) {
055                // if no command line config present then use the configuration gui
056                Config configDialog = new Config();
057                properties = configDialog.configure();
058                properties.setProperty(KEY_TYPE, TYPE_LOCAL);
059            }
060    
061            Embedder embedder = new Embedder();
062            ClassWorld classWorld = new ClassWorld();
063            classWorld.newRealm("plexus.core", Thread.currentThread().getContextClassLoader());
064            embedder.setProperties(properties);
065            embedder.start(classWorld);
066            DependencyBrowserGui gui = (DependencyBrowserGui) embedder.lookup(DependencyBrowserGui.ROLE);
067            gui.open();
068            embedder.stop();
069        }
070    
071        private static Properties configureFromCommandLine(String[] args) {
072            Properties properties = new Properties();
073            for (String arg : args) {
074                if (arg.startsWith(ARG_REPOSITORY)) {
075                    setRepositoryType(properties, TYPE_LOCAL);
076                    properties.setProperty(LocalDiskRepository.KEY_LOCAL_REPO_PATH, arg.substring(ARG_REPOSITORY.length()));
077                }
078                if (arg.startsWith(ARG_WORKSPACE)) {
079                    setRepositoryType(properties, TYPE_LOCAL);
080                    properties.setProperty(LocalDiskWorkspace.KEY_WORKSPACE_PATH, arg.substring(ARG_WORKSPACE.length()));
081                }
082                if (arg.startsWith(ARG_SNAPSHOT)) {
083                    setRepositoryType(properties, TYPE_ZIP);
084                    properties.setProperty(ZipFileRepository.KEY_ZIPFILE_PATH, arg.substring(ARG_SNAPSHOT.length()));
085                    properties.setProperty(ZipFileWorkspace.KEY_ZIPFILE_PATH, arg.substring(ARG_SNAPSHOT.length()));
086                }
087            }
088            return properties;
089        }
090    
091        private static void setRepositoryType(Properties properties, String type) {
092            if (properties.get(KEY_TYPE) == null || properties.get(KEY_TYPE).equals(type)) {
093                properties.setProperty(KEY_TYPE, type);
094            } else {
095                throw new IllegalArgumentException("Cannot overwrite configuration type " + properties.get(KEY_TYPE) + " with type " + type);
096            }
097        }
098    }