KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > plugins > DevClassPathHelper


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

11 package org.eclipse.core.internal.plugins;
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                 devDefaultClasspath = getArrayFromList(devProperties.getProperty("*")); //$NON-NLS-1$
33
} catch (MalformedURLException JavaDoc e) {
34                 devDefaultClasspath = getArrayFromList(osgiDev);
35             }
36         }
37     }
38
39     public static String JavaDoc[] getDevClassPath(String JavaDoc id) {
40         String JavaDoc[] result = null;
41         if (id != null && devProperties != null) {
42             String JavaDoc entry = devProperties.getProperty(id);
43             if (entry != null)
44                 result = getArrayFromList(entry);
45         }
46         if (result == null)
47             result = devDefaultClasspath;
48         return result;
49     }
50
51     /**
52      * Returns the result of converting a list of comma-separated tokens into an array
53      *
54      * @return the array of string tokens
55      * @param prop the initial comma-separated string
56      */

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

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