View Javadoc

1   /**
2    *
3    * Copyright 2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.geronimo.system.main;
18  
19  import java.io.IOException;
20  import java.net.JarURLConnection;
21  import java.net.URI;
22  import java.net.URISyntaxException;
23  import java.net.URL;
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.List;
27  import java.util.StringTokenizer;
28  import java.util.jar.Attributes;
29  import java.util.jar.Manifest;
30  
31  import org.apache.geronimo.gbean.AbstractNameQuery;
32  import org.apache.geronimo.kernel.repository.Artifact;
33  
34  /**
35   * @version $Rev: 406440 $ $Date: 2006-05-14 14:45:03 -0700 (Sun, 14 May 2006) $
36   */
37  public class CommandLineManifest {
38      public static final Attributes.Name MAIN_GBEAN = new Attributes.Name("Main-GBean");
39      public static final Attributes.Name MAIN_METHOD = new Attributes.Name("Main-Method");
40      public static final Attributes.Name CONFIGURATIONS = new Attributes.Name("Configurations");
41      public static final Attributes.Name ENDORSED_DIRS = new Attributes.Name("Endorsed-Dirs");
42      public static final Attributes.Name EXTENSION_DIRS = new Attributes.Name("Extension-Dirs");
43  
44      public static CommandLineManifest getManifestEntries() {
45          // find the startup jar
46          ClassLoader classLoader = CommandLine.class.getClassLoader();
47          URL url = classLoader.getResource("META-INF/startup-jar");
48          if (url == null) {
49              throw new IllegalArgumentException("Unable to determine location of startup jar");
50          }
51  
52          // extract the manifest
53          Manifest manifest;
54          try {
55              JarURLConnection jarConnection = (JarURLConnection) url.openConnection();
56              manifest = jarConnection.getManifest();
57          } catch (IOException e) {
58              System.err.println("Startup jar does not contain a manifest: " + url);
59              System.exit(1);
60              throw new AssertionError();
61          }
62          Attributes mainAttributes = manifest.getMainAttributes();
63  
64          // get the main gbean class
65          String mainGBeanString = mainAttributes.getValue(MAIN_GBEAN);
66  
67          AbstractNameQuery mainGBean = null;
68          if (mainGBeanString != null) {
69              try {
70                  mainGBean = new AbstractNameQuery(new URI(mainGBeanString));
71              } catch (URISyntaxException e) {
72                  System.err.println("Invalid Main-GBean name: " + mainGBeanString);
73                  System.exit(1);
74                  throw new AssertionError();
75              }
76          }
77  
78          // get the main method
79          String mainMethod = mainAttributes.getValue(MAIN_METHOD);
80  
81          // get the list of extra configurations to load
82          List configurations = new ArrayList();
83          String configurationsString = mainAttributes.getValue(CONFIGURATIONS);
84          if (configurationsString != null) {
85              for (StringTokenizer tokenizer = new StringTokenizer(configurationsString, " "); tokenizer.hasMoreTokens();)
86              {
87                  String configuration = tokenizer.nextToken();
88                  configurations.add(Artifact.create(configuration));
89              }
90          }
91  
92          // get the list of endorsed directories
93          List endorsedDirs = new ArrayList();
94          String endorsedDirsString = mainAttributes.getValue(ENDORSED_DIRS);
95          if (endorsedDirsString != null) {
96              for (StringTokenizer tokenizer = new StringTokenizer(endorsedDirsString, " "); tokenizer.hasMoreTokens();) {
97                  String directory = tokenizer.nextToken();
98                  endorsedDirs.add(directory);
99              }
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 }