View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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   * Various useful functions utility methods for OSGi wide use.
31   */
32  public class Util
33  {
34      public static final String DEFAULT_DELIMS = ",;";
35  
36      
37      /**
38       * Get the value of a manifest header.
39       * 
40       * @param name the name of the header
41       * @param altName an alternative name for the header (useful for deprecated names)
42       * @param manifest the dictionary
43       * @return the value from the manifest
44       */
45      public static String getManifestHeaderValue (String name, String altName, Dictionary manifest)
46      {
47          if (manifest == null)
48              return null;
49          if (name == null && altName == null)
50              return null;
51          if (name != null)
52              return (String)manifest.get(name);
53          return (String)manifest.get(altName);
54      }
55      
56    
57      
58      /* ------------------------------------------------------------ */
59      /**
60       * Treating the string as a separated list of filenames,
61       * convert and return the list of urls.
62       * 
63       * @param val the separated list of filenames
64       * @param delims the separators (default is <code>,;</code>)
65       * @return the list of URLs found in the input list
66       * @throws Exception if unable to convert entry to a URL
67       */
68      public static List<URL> fileNamesAsURLs(String val, String delims) 
69      throws Exception
70      {
71          String separators = DEFAULT_DELIMS;
72          if (delims == null)
73              delims = separators;
74  
75          StringTokenizer tokenizer = new StringTokenizer(val, delims, false);
76          List<URL> urls = new ArrayList<URL>();
77          while (tokenizer.hasMoreTokens())
78          {
79              urls.add(BundleFileLocatorHelperFactory.getFactory().getHelper().getLocalURL(new URL(tokenizer.nextToken())));
80          }
81          return urls;
82      }
83      
84      
85      /* ------------------------------------------------------------ */
86      public static void setProperty(Dictionary<String,String> properties, String key, String value)
87      {
88          if (value != null)
89          {
90              properties.put(key, value);
91          }
92      }
93      
94      
95      /* ------------------------------------------------------------ */
96      /**
97       * recursively substitute the <code>${sysprop}</code> by their actual system property.
98       * <code>${sysprop,defaultvalue}</code> will use <code>'defaultvalue'</code> as the value if no
99       * sysprop is defined. Not the most efficient code but we are shooting for
100      * simplicity and speed of development here.
101      * 
102      * @param value the input string
103      * @return the string with replaced properties
104      */
105     public static String resolvePropertyValue(String value)
106     {
107         int ind = value.indexOf("${");
108         if (ind == -1) { return value; }
109         int ind2 = value.indexOf('}', ind);
110         if (ind2 == -1) { return value; }
111         String sysprop = value.substring(ind + 2, ind2);
112         String defaultValue = null;
113         int comma = sysprop.indexOf(',');
114         if (comma != -1 && comma + 1 != sysprop.length())
115         {
116             defaultValue = sysprop.substring(comma + 1);
117             defaultValue = resolvePropertyValue(defaultValue);
118             sysprop = sysprop.substring(0, comma);
119         }
120         else
121         {
122             defaultValue = "${" + sysprop + "}";
123         }
124 
125         String v = System.getProperty(sysprop);
126 
127         String reminder = value.length() > ind2 + 1 ? value.substring(ind2 + 1) : "";
128         reminder = resolvePropertyValue(reminder);
129         if (v != null)
130         {
131             return value.substring(0, ind) + v + reminder;
132         }
133         else
134         {
135             return value.substring(0, ind) + defaultValue + reminder;
136         }
137     }
138 }