KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > launcher > LauncherUtils


1 /* ====================================================================
2  * Loom Software License, version 1.1
3  *
4  * Copyright (c) 2003, Loom Group. All rights reserved.
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. Neither the name of the Loom Group nor the name "Loom" nor
18  * the names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior
20  * written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * ====================================================================
36  *
37  * Loom includes code from the Apache Software Foundation
38  *
39  * ====================================================================
40  * The Apache Software License, Version 1.1
41  *
42  * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
43  * reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  *
49  * 1. Redistributions of source code must retain the above copyright
50  * notice, this list of conditions and the following disclaimer.
51  *
52  * 2. Redistributions in binary form must reproduce the above copyright
53  * notice, this list of conditions and the following disclaimer in
54  * the documentation and/or other materials provided with the
55  * distribution.
56  *
57  * 3. The end-user documentation included with the redistribution,
58  * if any, must include the following acknowledgment:
59  * "This product includes software developed by the
60  * Apache Software Foundation (http://www.apache.org/)."
61  * Alternately, this acknowledgment may appear in the software
62  * itself, if and wherever such third-party acknowledgments
63  * normally appear.
64  *
65  * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
66  * must not be used to endorse or promote products derived from this
67  * software without prior written permission. For written
68  * permission, please contact apache@apache.org.
69  *
70  * 5. Products derived from this software may not be called "Apache",
71  * nor may "Apache" appear in their name, without prior written
72  * permission of the Apache Software Foundation.
73  *
74  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
75  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
76  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
77  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
78  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
79  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
80  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
81  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
82  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
83  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
84  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85  * SUCH DAMAGE.
86  */

87 package org.codehaus.loom.launcher;
88
89 import java.io.File JavaDoc;
90 import java.net.URL JavaDoc;
91 import java.util.ArrayList JavaDoc;
92 import java.util.StringTokenizer JavaDoc;
93
94 /**
95  * A set of utilities that help when writing Launchers.
96  *
97  * @author Peter Donald
98  */

99 public class LauncherUtils
100 {
101     /**
102      * Create a ClassPath for the engine.
103      *
104      * @return the set of URLs that engine uses to load
105      * @throws Exception if unable to aquire classpath
106      */

107     static URL JavaDoc[] generateClassPath( final File JavaDoc homeDir,
108                                     final String JavaDoc libDirectory )
109         throws Exception JavaDoc
110     {
111         final ArrayList JavaDoc urls = new ArrayList JavaDoc();
112
113         final File JavaDoc dir = findLibDir( homeDir, libDirectory );
114         final File JavaDoc[] files = dir.listFiles();
115         for( int i = 0; i < files.length; i++ )
116         {
117             final File JavaDoc file = files[ i ];
118             if( file.getName().endsWith( ".jar" ) )
119             {
120                 urls.add( file.toURL() );
121             }
122         }
123
124         return (URL JavaDoc[])urls.toArray( new URL JavaDoc[ urls.size() ] );
125     }
126
127     /**
128      * Find directory to load engine specific libraries from.
129      *
130      * @return the lib dir
131      * @throws Exception if unable to aquire directory
132      */

133     static File JavaDoc findLibDir( final File JavaDoc homeDir,
134                             final String JavaDoc libDirectory )
135         throws Exception JavaDoc
136     {
137         final String JavaDoc engineLibDir = libDirectory.replace( '/',
138                                                           File.separatorChar );
139         final File JavaDoc dir = new File JavaDoc( homeDir, engineLibDir ).getCanonicalFile();
140         if( !dir.exists() )
141         {
142             throw new Exception JavaDoc(
143                 "Unable to locate library directory at " + engineLibDir );
144         }
145         return dir;
146     }
147
148     /**
149      * Utility method to find the home directory of Loom and make sure system
150      * property is set to it.
151      *
152      * @return the location of loom directory
153      * @throws Exception if unable to locate directory
154      */

155     static File JavaDoc findLoomHome( final String JavaDoc systemProperty,
156                               final String JavaDoc loaderJar )
157         throws Exception JavaDoc
158     {
159         String JavaDoc loomHome = System.getProperty( systemProperty, null );
160         if( null == loomHome )
161         {
162             final File JavaDoc loaderDir =
163                 findLoaderDir( systemProperty, loaderJar );
164             return loaderDir.getCanonicalFile().getParentFile();
165         }
166         else
167         {
168             return new File JavaDoc( loomHome ).getCanonicalFile();
169         }
170     }
171
172     /**
173      * Finds the loaderJar file in the classpath.
174      */

175     private static final File JavaDoc findLoaderDir( final String JavaDoc systemProperty,
176                                              final String JavaDoc loaderJar )
177         throws Exception JavaDoc
178     {
179         final String JavaDoc classpath = System.getProperty( "java.class.path" );
180         final String JavaDoc pathSeparator = System.getProperty( "path.separator" );
181         final StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc( classpath,
182                                                                pathSeparator );
183
184         while( tokenizer.hasMoreTokens() )
185         {
186             final String JavaDoc element = tokenizer.nextToken();
187
188             if( element.endsWith( loaderJar ) )
189             {
190                 File JavaDoc file = ( new File JavaDoc( element ) ).getCanonicalFile();
191                 file = file.getParentFile();
192                 return file;
193             }
194         }
195
196         throw new Exception JavaDoc( "Unable to locate " +
197                              loaderJar +
198                              " in classpath. User must specify " +
199                              systemProperty + " system property." );
200     }
201 }
202
Popular Tags