KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > main > CommandLineManifest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
20 import java.net.JarURLConnection JavaDoc;
21 import java.net.URI JavaDoc;
22 import java.net.URISyntaxException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28 import java.util.jar.Attributes JavaDoc;
29 import java.util.jar.Manifest JavaDoc;
30
31 import org.apache.geronimo.gbean.AbstractNameQuery;
32 import org.apache.geronimo.kernel.repository.Artifact;
33
34 /**
35  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
36  */

37 public class CommandLineManifest {
38     public static final Attributes.Name JavaDoc MAIN_GBEAN = new Attributes.Name JavaDoc("Main-GBean");
39     public static final Attributes.Name JavaDoc MAIN_METHOD = new Attributes.Name JavaDoc("Main-Method");
40     public static final Attributes.Name JavaDoc CONFIGURATIONS = new Attributes.Name JavaDoc("Configurations");
41     public static final Attributes.Name JavaDoc ENDORSED_DIRS = new Attributes.Name JavaDoc("Endorsed-Dirs");
42     public static final Attributes.Name JavaDoc EXTENSION_DIRS = new Attributes.Name JavaDoc("Extension-Dirs");
43
44     public static CommandLineManifest getManifestEntries() {
45         // find the startup jar
46
ClassLoader JavaDoc classLoader = CommandLine.class.getClassLoader();
47         URL JavaDoc url = classLoader.getResource("META-INF/startup-jar");
48         if (url == null) {
49             throw new IllegalArgumentException JavaDoc("Unable to determine location of startup jar");
50         }
51
52         // extract the manifest
53
Manifest JavaDoc manifest;
54         try {
55             JarURLConnection JavaDoc jarConnection = (JarURLConnection JavaDoc) url.openConnection();
56             manifest = jarConnection.getManifest();
57         } catch (IOException JavaDoc e) {
58             System.err.println("Startup jar does not contain a manifest: " + url);
59             System.exit(1);
60             throw new AssertionError JavaDoc();
61         }
62         Attributes JavaDoc mainAttributes = manifest.getMainAttributes();
63
64         // get the main gbean class
65
String JavaDoc mainGBeanString = mainAttributes.getValue(MAIN_GBEAN);
66
67         AbstractNameQuery mainGBean = null;
68         if (mainGBeanString != null) {
69             try {
70                 mainGBean = new AbstractNameQuery(new URI JavaDoc(mainGBeanString));
71             } catch (URISyntaxException JavaDoc e) {
72                 System.err.println("Invalid Main-GBean name: " + mainGBeanString);
73                 System.exit(1);
74                 throw new AssertionError JavaDoc();
75             }
76         }
77
78         // get the main method
79
String JavaDoc mainMethod = mainAttributes.getValue(MAIN_METHOD);
80
81         // get the list of extra configurations to load
82
List JavaDoc configurations = new ArrayList JavaDoc();
83         String JavaDoc configurationsString = mainAttributes.getValue(CONFIGURATIONS);
84         if (configurationsString != null) {
85             for (StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(configurationsString, " "); tokenizer.hasMoreTokens();)
86             {
87                 String JavaDoc configuration = tokenizer.nextToken();
88                 configurations.add(Artifact.create(configuration));
89             }
90         }
91
92         // get the list of endorsed directories
93
List JavaDoc endorsedDirs = new ArrayList JavaDoc();
94         String JavaDoc endorsedDirsString = mainAttributes.getValue(ENDORSED_DIRS);
95         if (endorsedDirsString != null) {
96             for (StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(endorsedDirsString, " "); tokenizer.hasMoreTokens();) {
97                 String JavaDoc directory = tokenizer.nextToken();
98                 endorsedDirs.add(directory);
99             }
100         }
101
102         // get the list of extension directories
103
List JavaDoc extensionDirs = new ArrayList JavaDoc();
104         String JavaDoc extensionDirsString = mainAttributes.getValue(EXTENSION_DIRS);
105         if (extensionDirsString != null) {
106             for (StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(extensionDirsString, " "); tokenizer.hasMoreTokens();)
107             {
108                 String JavaDoc 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 JavaDoc mainMethod;
119     private final List JavaDoc configurations;
120     private final List JavaDoc endorsedDirs;
121     private final List JavaDoc extensionDirs;
122
123     public CommandLineManifest(AbstractNameQuery mainGBean, String JavaDoc mainMethod, List JavaDoc configurations, List JavaDoc endorsedDirs, List JavaDoc 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 JavaDoc getMainMethod() {
136         return mainMethod;
137     }
138
139     public List JavaDoc getConfigurations() {
140         return configurations;
141     }
142
143     public List JavaDoc getEndorsedDirs() {
144         return endorsedDirs;
145     }
146
147     public List JavaDoc getExtensionDirs() {
148         return extensionDirs;
149     }
150 }
151
Popular Tags