001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.system.main;
018    
019    import java.io.IOException;
020    import java.net.JarURLConnection;
021    import java.net.URI;
022    import java.net.URISyntaxException;
023    import java.net.URL;
024    import java.util.ArrayList;
025    import java.util.Collections;
026    import java.util.List;
027    import java.util.StringTokenizer;
028    import java.util.jar.Attributes;
029    import java.util.jar.Manifest;
030    
031    import org.apache.geronimo.gbean.AbstractNameQuery;
032    import org.apache.geronimo.kernel.repository.Artifact;
033    
034    /**
035     * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
036     */
037    public class CommandLineManifest {
038        public static final Attributes.Name MAIN_GBEAN = new Attributes.Name("Main-GBean");
039        public static final Attributes.Name MAIN_METHOD = new Attributes.Name("Main-Method");
040        public static final Attributes.Name CONFIGURATIONS = new Attributes.Name("Configurations");
041        public static final Attributes.Name ENDORSED_DIRS = new Attributes.Name("Endorsed-Dirs");
042        public static final Attributes.Name EXTENSION_DIRS = new Attributes.Name("Extension-Dirs");
043    
044        public static CommandLineManifest getManifestEntries() {
045            // find the startup jar
046            ClassLoader classLoader = CommandLine.class.getClassLoader();
047            URL url = classLoader.getResource("META-INF/startup-jar");
048            if (url == null) {
049                throw new IllegalArgumentException("Unable to determine location of startup jar");
050            }
051    
052            // extract the manifest
053            Manifest manifest;
054            try {
055                JarURLConnection jarConnection = (JarURLConnection) url.openConnection();
056                manifest = jarConnection.getManifest();
057            } catch (IOException e) {
058                System.err.println("Startup jar does not contain a manifest: " + url);
059                System.exit(1);
060                throw new AssertionError();
061            }
062            Attributes mainAttributes = manifest.getMainAttributes();
063    
064            // get the main gbean class
065            String mainGBeanString = mainAttributes.getValue(MAIN_GBEAN);
066    
067            AbstractNameQuery mainGBean = null;
068            if (mainGBeanString != null) {
069                try {
070                    mainGBean = new AbstractNameQuery(new URI(mainGBeanString));
071                } catch (URISyntaxException e) {
072                    System.err.println("Invalid Main-GBean name: " + mainGBeanString);
073                    System.exit(1);
074                    throw new AssertionError();
075                }
076            }
077    
078            // get the main method
079            String mainMethod = mainAttributes.getValue(MAIN_METHOD);
080    
081            // get the list of extra configurations to load
082            List configurations = new ArrayList();
083            String configurationsString = mainAttributes.getValue(CONFIGURATIONS);
084            if (configurationsString != null) {
085                for (StringTokenizer tokenizer = new StringTokenizer(configurationsString, " "); tokenizer.hasMoreTokens();)
086                {
087                    String configuration = tokenizer.nextToken();
088                    configurations.add(Artifact.create(configuration));
089                }
090            }
091    
092            // get the list of endorsed directories
093            List endorsedDirs = new ArrayList();
094            String endorsedDirsString = mainAttributes.getValue(ENDORSED_DIRS);
095            if (endorsedDirsString != null) {
096                for (StringTokenizer tokenizer = new StringTokenizer(endorsedDirsString, " "); tokenizer.hasMoreTokens();) {
097                    String directory = tokenizer.nextToken();
098                    endorsedDirs.add(directory);
099                }
100            }
101    
102            // get the list of extension directories
103            List extensionDirs = new ArrayList();
104            String extensionDirsString = mainAttributes.getValue(EXTENSION_DIRS);
105            if (extensionDirsString != null) {
106                for (StringTokenizer tokenizer = new StringTokenizer(extensionDirsString, " "); tokenizer.hasMoreTokens();)
107                {
108                    String directory = tokenizer.nextToken();
109                    extensionDirs.add(directory);
110                }
111            }
112    
113            CommandLineManifest commandLineManifest = new CommandLineManifest(mainGBean, mainMethod, configurations, endorsedDirs, extensionDirs);
114            return commandLineManifest;
115        }
116    
117        private final AbstractNameQuery mainGBean;
118        private final String mainMethod;
119        private final List configurations;
120        private final List endorsedDirs;
121        private final List extensionDirs;
122    
123        public CommandLineManifest(AbstractNameQuery mainGBean, String mainMethod, List configurations, List endorsedDirs, List extensionDirs) {
124            this.mainGBean = mainGBean;
125            this.mainMethod = mainMethod;
126            this.configurations = Collections.unmodifiableList(configurations);
127            this.endorsedDirs = endorsedDirs;
128            this.extensionDirs = extensionDirs;
129        }
130    
131        public AbstractNameQuery getMainGBeanQuery() {
132            return mainGBean;
133        }
134    
135        public String getMainMethod() {
136            return mainMethod;
137        }
138    
139        public List getConfigurations() {
140            return configurations;
141        }
142    
143        public List getEndorsedDirs() {
144            return endorsedDirs;
145        }
146    
147        public List getExtensionDirs() {
148            return extensionDirs;
149        }
150    }