View Javadoc

1   package fr.in2p3.jsaga;
2   
3   import fr.in2p3.jsaga.engine.config.ConfigurationException;
4   
5   import java.io.*;
6   import java.net.MalformedURLException;
7   import java.net.URL;
8   import java.util.Properties;
9   
10  /* ***************************************************
11  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
12  * ***             http://cc.in2p3.fr/             ***
13  * ***************************************************
14  * File:   EngineProperties
15  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
16  * Date:   11 mars 2008
17  * ***************************************************
18  * Description:                                      */
19  /**
20   *
21   */
22  public class EngineProperties {
23      public static final String JSAGA_DEFAULT_CONTEXTS = "jsaga.default.contexts";
24      public static final String JSAGA_TIMEOUT = "jsaga.timeout";
25      public static final String LOG4J_CONFIGURATION = "log4j.configuration";
26      public static final String JSAGA_DEFAULT_CONTEXTS_CHECK_CONFLICTS = "jsaga.default.contexts.check.conflicts";
27      public static final String DATA_IMPLICIT_CLOSE_TIMEOUT = "data.implicit.close.timeout";
28      public static final String DATA_COPY_BUFFER_SIZE = "data.copy.buffer.size";
29      public static final String DATA_ATTRIBUTES_CACHE_LIFETIME = "data.attributes.cache.lifetime";
30      public static final String JOB_MONITOR_POLL_PERIOD = "job.monitor.poll.period";
31      public static final String JOB_MONITOR_ERROR_THRESHOLD = "job.monitor.error.threshold";
32      public static final String JOB_CONTROL_CHECK_AVAILABILITY = "job.control.check.availability";
33      public static final String JOB_CONTROL_CHECK_MATCH = "job.control.check.match";
34      public static final String JOB_CANCEL_CHECK_STATUS = "job.cancel.check.status";
35      public static final String JAVAX_NET_SSL_KEYSTORE = "javax.net.ssl.keyStore";
36      public static final String JAVAX_NET_SSL_KEYSTOREPASSWORD = "javax.net.ssl.keyStorePassword";
37      public static final String JAVAX_NET_SSL_TRUSTSTORE = "javax.net.ssl.trustStore";
38      public static final String JAVAX_NET_SSL_TRUSTSTOREPASSWORD = "javax.net.ssl.trustStorePassword";
39      
40      private static Exception s_exception;
41      private static Properties s_prop;
42  
43      public static Properties getProperties() {
44          if (s_prop == null) {
45              // set default properties
46              s_prop = new Properties();
47              //s_prop.setProperty(JSAGA_DEFAULT_CONTEXTS, "etc/jsaga-default-contexts.xml");
48              //s_prop.setProperty(JSAGA_TIMEOUT, "etc/jsaga-timeout.properties");
49              //s_prop.setProperty(LOG4J_CONFIGURATION, "etc/log4j.properties");
50              s_prop.setProperty(JSAGA_DEFAULT_CONTEXTS_CHECK_CONFLICTS, "true");
51              //s_prop.setProperty(DATA_IMPLICIT_CLOSE_TIMEOUT, "-1");
52              s_prop.setProperty(DATA_COPY_BUFFER_SIZE, "1048576");
53              s_prop.setProperty(DATA_ATTRIBUTES_CACHE_LIFETIME, "60000");
54              s_prop.setProperty(JOB_MONITOR_POLL_PERIOD, "60000");
55              s_prop.setProperty(JOB_MONITOR_ERROR_THRESHOLD, "3");
56              s_prop.setProperty(JOB_CONTROL_CHECK_AVAILABILITY, "true");
57              s_prop.setProperty(JOB_CONTROL_CHECK_MATCH, "false");
58              s_prop.setProperty(JOB_CANCEL_CHECK_STATUS, "true");
59  
60              // load properties
61              File file = new File(Base.JSAGA_HOME, "etc/jsaga-config.properties");
62              try {
63                  InputStream in = new FileInputStream(file);
64                  s_prop.load(in);
65                  in.close();
66              } catch (IOException e) {
67                  s_exception = e;
68              }
69          }
70          return s_prop;
71      }
72  
73      public static Exception getException() {
74          return s_exception;
75      }
76  
77      public static void setProperty(String name, String value) {
78          getProperties().setProperty(name, value);
79      }
80  
81      private static String getEngineProperty(String name) {
82          String value = getProperties().getProperty(name);
83          if (value != null) {
84              return value;
85          } else {
86              return null;
87          }
88      }
89  
90      public static String getProperty(String name) {
91          // try with system property
92          String value = System.getProperty(name);
93          if (value != null) {
94              return value;
95          } else {
96              // try with engine property
97              return EngineProperties.getEngineProperty(name);
98          }
99      }
100 
101     /**
102      * Get the URL corresponding to property <code>name</code>.
103      * The URL can be either a JAR resource (System properties only)
104      * or a local file path (Engine properties only).
105      * @param name the name of the property
106      * @return the URL
107      */
108     public static URL getURL(String name) throws ConfigurationException {
109         // try with system property
110         String value = System.getProperty(name);
111         if (value != null) {
112             try {
113                 return new URL(value);
114             } catch (MalformedURLException e) {
115                 throw new ConfigurationException("Malformed URL: "+value, e);
116             }
117         } else {
118             // try with engine property
119             String path = getProperties().getProperty(name);
120             if (path != null) {
121                 File file;
122                 if (new File(path).isAbsolute()) {
123                     file = new File(path);
124                 } else {
125                     file = new File(Base.JSAGA_HOME, path);
126                 }
127                 if (! file.exists()) {
128                     throw new ConfigurationException("File not found: "+file);
129                 }
130                 try {
131                     return file.toURL();
132                 } catch (MalformedURLException e) {
133                     throw new ConfigurationException("Malformed URL: "+file, e);
134                 }
135             } else {
136                 return null;
137             }
138         }
139     }
140 
141     public static Integer getInteger(String name) throws NumberFormatException {
142         String value = getProperty(name);
143         if (value != null) {
144             return Integer.parseInt(value);
145         } else {
146             return null;
147         }
148     }
149 
150     public static Boolean getBoolean(String name) {
151         String value = getProperty(name);
152         if (value != null) {
153             return "true".equalsIgnoreCase(value);
154         } else {
155             return null;
156         }
157     }
158 }