KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > adaptor > core > 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 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.osgi.framework.adaptor.core;
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 /**
20  * This class provides helper methods to support developement classpaths.
21  * @since 3.1
22  */

23 public final class DevClassPathHelper {
24     static private boolean inDevelopmentMode = false;
25     static private String JavaDoc[] devDefaultClasspath;
26     static private Dictionary devProperties = null;
27
28     static {
29         // Check the osgi.dev property to see if dev classpath entries have been defined.
30
String JavaDoc osgiDev = System.getProperty("osgi.dev"); //$NON-NLS-1$
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((String JavaDoc) devProperties.get("*")); //$NON-NLS-1$
38
} catch (MalformedURLException JavaDoc e) {
39                 devDefaultClasspath = getArrayFromList(osgiDev);
40             }
41         }
42     }
43
44     private static String JavaDoc[] getDevClassPath(String JavaDoc id, Dictionary properties, String JavaDoc[] defaultClasspath) {
45         String JavaDoc[] result = null;
46         if (id != null && properties != null) {
47             String JavaDoc entry = (String JavaDoc) properties.get(id);
48             if (entry != null)
49                 result = getArrayFromList(entry);
50         }
51         if (result == null)
52             result = defaultClasspath;
53         return result;
54     }
55
56     /**
57      * Returns a list of classpath elements for the specified bundle symbolic name.
58      * @param id a bundle symbolic name to get the development classpath for
59      * @param properties a Dictionary of properties to use or <code>null</code> if
60      * the default develoment classpath properties should be used
61      * @return a list of development classpath elements
62      */

63     public static String JavaDoc[] getDevClassPath(String JavaDoc id, Dictionary properties) {
64         if (properties == null)
65             return getDevClassPath(id, devProperties, devDefaultClasspath);
66         return getDevClassPath(id, properties, getArrayFromList((String JavaDoc) properties.get("*"))); //$NON-NLS-1$
67
}
68
69     /**
70      * Returns a list of classpath elements for the specified bundle symbolic name.
71      * @param id a bundle symbolic name to get the development classpath for
72      * @return a list of development classpath elements
73      */

74     public static String JavaDoc[] getDevClassPath(String JavaDoc id) {
75         return getDevClassPath(id, null);
76     }
77
78     /**
79      * Returns the result of converting a list of comma-separated tokens into an array
80      *
81      * @return the array of string tokens
82      * @param prop the initial comma-separated string
83      */

84     public static String JavaDoc[] getArrayFromList(String JavaDoc prop) {
85         if (prop == null || prop.trim().equals("")) //$NON-NLS-1$
86
return new String JavaDoc[0];
87         Vector list = new Vector();
88         StringTokenizer tokens = new StringTokenizer(prop, ","); //$NON-NLS-1$
89
while (tokens.hasMoreTokens()) {
90             String JavaDoc token = tokens.nextToken().trim();
91             if (!token.equals("")) //$NON-NLS-1$
92
list.addElement(token);
93         }
94         return list.isEmpty() ? new String JavaDoc[0] : (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]);
95     }
96
97     /**
98      * Indicates the development mode.
99      * @return true if in development mode; false otherwise
100      */

101     public static boolean inDevelopmentMode() {
102         return inDevelopmentMode;
103     }
104
105     /*
106      * Load the given properties file
107      */

108     private static Properties load(URL JavaDoc url) {
109         Properties props = new Properties();
110         try {
111             InputStream JavaDoc is = null;
112             try {
113                 is = url.openStream();
114                 props.load(is);
115             } finally {
116                 if (is != null)
117                     is.close();
118             }
119         } catch (IOException JavaDoc e) {
120             // TODO consider logging here
121
}
122         return props;
123     }
124 }
125
Popular Tags