View Javadoc

1   package org.lsst.ccs.bootstrap;
2   
3   import java.net.URISyntaxException;
4   import java.net.URL;
5   import java.net.URLClassLoader;
6   import java.nio.file.Paths;
7   import java.util.Properties;
8   import java.util.Set;
9   //import javax.swing.JOptionPane;
10  import org.apache.commons.cli.ParseException;
11  import org.lsst.ccs.bootstrap.resources.BootstrapResourceUtils;
12  
13  /**
14   *
15   * @author turri
16   */
17  class BootstrapEnvironmentUtils {
18  
19      private static final String sysPropFlag = "system.property.";
20      private static final String jvmOptFlag = "system.option.";
21  
22      public static void main(String[] argv) {
23  //      JOptionPane.showMessageDialog(null, "BootstrapEnvironmentUtils called for "+ argv[0]);
24          String arg = argv[0];
25          if (arg.equals("LD_LIBRARY_PATH")) {
26              System.out.println(BootstrapUtils.getBootstrapLibraryPath());
27          } else if (arg.equals("JAVA_OPTS")) {
28              System.out.println(getJavaOpts(argv));
29          } else if (arg.equals("CLASSPATH")) {
30              System.out.println(getClasspath(argv));
31          } else {
32              throw new IllegalArgumentException("Illegal Option " + arg);
33          }
34      }
35  
36      
37      /**
38       * Build the java options string to be passed to the JVM
39       * @param argv the command line arguments
40       * @param fileName the name of the properties to load. 
41       *                 By default system.properties
42       * @return 
43       */
44      protected static String getJavaOpts(String[] argv) {
45          return getJavaOpts(argv,"system");
46      }
47      protected static String getJavaOpts(String[] argv, String fileName) {
48  
49          StringBuilder output = new StringBuilder();
50          Bootstrap b = new Bootstrap(true);
51          try {
52              b.parseCommandLineArguments(argv);
53          } catch (ParseException pe) {
54              throw new RuntimeException(pe);
55          }
56  
57          Properties p = BootstrapResourceUtils.getBootstrapProperties(fileName);
58  
59          Set<Object> keys = BootstrapResourceUtils.getAllKeysInProperties(p);
60          for (Object key : keys) {
61              String keyStr = (String) key;
62              if (keyStr.startsWith(sysPropFlag)) {
63                  String sysPropertyForJava = keyStr.replace(sysPropFlag, "");
64                  String sysPropertyValue = p.getProperty(keyStr);
65                  if (sysPropertyValue != null && !sysPropertyValue.isEmpty()) {
66                      output.append("-D");
67                      output.append(sysPropertyForJava);
68                      output.append("=");
69                      output.append(BootstrapUtils.parseProperty(sysPropertyValue));
70                      output.append(" ");
71                  }
72              } else if (keyStr.startsWith(jvmOptFlag)) {
73                  String sysOptionForJava = keyStr.replace(jvmOptFlag, "");
74                  output.append("-");
75                  output.append(sysOptionForJava);
76                  
77                  String sysOptionValue = p.getProperty(keyStr);
78                  if ( sysOptionValue != null && ! sysOptionValue.isEmpty() ) {                
79                      output.append(":");
80                      output.append(BootstrapUtils.parseProperty(sysOptionValue));                    
81                  }
82                  output.append(" ");
83              }
84          }
85          return output.toString();
86      }
87  
88      protected static String getClasspath(String[] argv) {
89  
90          StringBuilder output = new StringBuilder();
91          Bootstrap b = new Bootstrap(true);
92          try {
93              b.parseCommandLineArguments(argv);
94          } catch (ParseException pe) {
95              throw new RuntimeException(pe);
96          }
97  
98          URLClassLoader loader = Bootstrap.getBootstrapApplicationClassLoader();
99          
100         URL[] urls = loader.getURLs();
101         try {
102         for (URL url : urls) {
103             output.append(Paths.get(url.toURI())).append(BootstrapUtils.PATH_SEPARATOR);
104         }
105         } catch (URISyntaxException x) {
106             throw new RuntimeException(x);
107         }
108         
109         String ret = output.toString();
110         return ret.substring(0,ret.length()-1);
111     }
112     
113     
114 }