KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > internal > defaultadaptor > DevClassPathHelper


1 /*******************************************************************************
2  * Copyright (c) 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.osgi.framework.internal.defaultadaptor;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.net.MalformedURLException JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.util.*;
18
19 public class DevClassPathHelper {
20     static protected boolean inDevelopmentMode = false;
21     static protected String JavaDoc[] devDefaultClasspath;
22     static protected Properties devProperties = null;
23
24     static {
25         // Check the osgi.dev property to see if dev classpath entries have been defined.
26
String JavaDoc osgiDev = System.getProperty("osgi.dev"); //$NON-NLS-1$
27
if (osgiDev != null) {
28             try {
29                 inDevelopmentMode = true;
30                 URL JavaDoc location = new URL JavaDoc(osgiDev);
31                 devProperties = load(location);
32                 if (devProperties != null)
33                     devDefaultClasspath = getArrayFromList(devProperties.getProperty("*")); //$NON-NLS-1$
34
} catch (MalformedURLException JavaDoc e) {
35                 devDefaultClasspath = getArrayFromList(osgiDev);
36             }
37         }
38     }
39
40     public static String JavaDoc[] getDevClassPath(String JavaDoc id) {
41         String JavaDoc[] result = null;
42         if (id != null && devProperties != null) {
43             String JavaDoc entry = devProperties.getProperty(id);
44             if (entry != null)
45                 result = getArrayFromList(entry);
46         }
47         if (result == null)
48             result = devDefaultClasspath;
49         return result;
50     }
51
52     /**
53      * Returns the result of converting a list of comma-separated tokens into an array
54      *
55      * @return the array of string tokens
56      * @param prop the initial comma-separated string
57      */

58     public static String JavaDoc[] getArrayFromList(String JavaDoc prop) {
59         if (prop == null || prop.trim().equals("")) //$NON-NLS-1$
60
return new String JavaDoc[0];
61         Vector list = new Vector();
62         StringTokenizer tokens = new StringTokenizer(prop, ","); //$NON-NLS-1$
63
while (tokens.hasMoreTokens()) {
64             String JavaDoc token = tokens.nextToken().trim();
65             if (!token.equals("")) //$NON-NLS-1$
66
list.addElement(token);
67         }
68         return list.isEmpty() ? new String JavaDoc[0] : (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]);
69     }
70
71     public static boolean inDevelopmentMode() {
72         return inDevelopmentMode;
73     }
74
75     /*
76      * Load the given properties file
77      */

78     private static Properties load(URL JavaDoc url) {
79         Properties props = new Properties();
80         try {
81             InputStream JavaDoc is = null;
82             try {
83                 is = url.openStream();
84                 props.load(is);
85             } finally {
86                 is.close();
87             }
88         } catch (IOException JavaDoc e) {
89             // TODO consider logging here
90
}
91         return props;
92     }
93 }
Popular Tags