KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > runtime > 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.runtime;
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
21     // command line options
22
public static final String JavaDoc PROP_DEV = "osgi.dev"; //$NON-NLS-1$
23

24     static protected boolean inDevelopmentMode = false;
25     static protected String JavaDoc[] devDefaultClasspath;
26     static protected Properties devProperties = null;
27
28     static {
29         // Check the osgi.dev property to see if dev classpath entries have been defined.
30
String JavaDoc osgiDev = Activator.getContext() == null ? System.getProperty(PROP_DEV) : Activator.getContext().getProperty(PROP_DEV);
31         if (osgiDev != null) {
32             try {
33                 inDevelopmentMode = true;
34                 URL JavaDoc location = new URL JavaDoc(osgiDev);
35                 devProperties = load(location);
36                 if (devProperties != null)
37                     devDefaultClasspath = getArrayFromList(devProperties.getProperty("*")); //$NON-NLS-1$
38
} catch (MalformedURLException JavaDoc e) {
39                 devDefaultClasspath = getArrayFromList(osgiDev);
40             }
41         }
42     }
43
44     public static String JavaDoc[] getDevClassPath(String JavaDoc id) {
45         String JavaDoc[] result = null;
46         if (id != null && devProperties != null) {
47             String JavaDoc entry = devProperties.getProperty(id);
48             if (entry != null)
49                 result = getArrayFromList(entry);
50         }
51         if (result == null)
52             result = devDefaultClasspath;
53         return result;
54     }
55
56     /**
57      * Returns the result of converting a list of comma-separated tokens into an array
58      *
59      * @return the array of string tokens
60      * @param prop the initial comma-separated string
61      */

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

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