1 //
2 // ========================================================================
3 // Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18
19
20 package org.eclipse.jetty.osgi.boot.utils;
21
22 import java.net.MalformedURLException;
23 import java.net.URL;
24 import java.util.ArrayList;
25 import java.util.Dictionary;
26 import java.util.List;
27 import java.util.StringTokenizer;
28
29 /**
30 * Util
31 *
32 * Various useful functions used widely.
33 */
34 public class Util
35 {
36 public static final String DEFAULT_DELIMS = ",;";
37
38 /* ------------------------------------------------------------ */
39 /**
40 * Treating the string as a separated list of filenames,
41 * convert and return the list of urls.
42 *
43 * @param val the separated list
44 * @param delims the separators (default is ,;)
45 * @return
46 * @throws MalformedURLException
47 */
48 public static List<URL> fileNamesAsURLs(String val, String delims)
49 throws Exception
50 {
51 String separators = DEFAULT_DELIMS;
52 if (delims == null)
53 delims = separators;
54
55 StringTokenizer tokenizer = new StringTokenizer(val, delims, false);
56 List<URL> urls = new ArrayList<URL>();
57 while (tokenizer.hasMoreTokens())
58 {
59 urls.add(BundleFileLocatorHelperFactory.getFactory().getHelper().getLocalURL(new URL(tokenizer.nextToken())));
60 }
61 return urls;
62 }
63
64
65 /* ------------------------------------------------------------ */
66 public static void setProperty(Dictionary<String,String> properties, String key, String value)
67 {
68 if (value != null)
69 {
70 properties.put(key, value);
71 }
72 }
73
74
75 /* ------------------------------------------------------------ */
76 /**
77 * recursively substitute the ${sysprop} by their actual system property.
78 * ${sysprop,defaultvalue} will use 'defaultvalue' as the value if no
79 * sysprop is defined. Not the most efficient code but we are shooting for
80 * simplicity and speed of development here.
81 *
82 * @param value
83 * @return
84 */
85 public static String resolvePropertyValue(String value)
86 {
87 int ind = value.indexOf("${");
88 if (ind == -1) { return value; }
89 int ind2 = value.indexOf('}', ind);
90 if (ind2 == -1) { return value; }
91 String sysprop = value.substring(ind + 2, ind2);
92 String defaultValue = null;
93 int comma = sysprop.indexOf(',');
94 if (comma != -1 && comma + 1 != sysprop.length())
95 {
96 defaultValue = sysprop.substring(comma + 1);
97 defaultValue = resolvePropertyValue(defaultValue);
98 sysprop = sysprop.substring(0, comma);
99 }
100 else
101 {
102 defaultValue = "${" + sysprop + "}";
103 }
104
105 String v = System.getProperty(sysprop);
106
107 String reminder = value.length() > ind2 + 1 ? value.substring(ind2 + 1) : "";
108 reminder = resolvePropertyValue(reminder);
109 if (v != null)
110 {
111 return value.substring(0, ind) + v + reminder;
112 }
113 else
114 {
115 return value.substring(0, ind) + defaultValue + reminder;
116 }
117 }
118 }