1
2
3
4
5
6 package gov.bnl.gums.command;
7
8 import java.net.MalformedURLException;
9 import java.net.URL;
10 import java.util.MissingResourceException;
11
12 import java.util.PropertyResourceBundle;
13 import java.util.ResourceBundle;
14 import gov.bnl.gums.admin.*;
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17
18
19 /***
20 * @author carcassi
21 */
22 public class Configuration {
23 private static Log log = LogFactory.getLog(Configuration.class);
24 private static Configuration conf = new Configuration();
25 private URL locationURL;
26 private URL authZLocationURL;
27 private boolean direct;
28 private boolean loaded;
29
30 /***
31 * TODO: write doc
32 *
33 * @return TODO: write doc
34 */
35 public static Configuration getInstance() {
36 return conf;
37 }
38
39 private void loadConf() {
40 PropertyResourceBundle prop = (PropertyResourceBundle) ResourceBundle.getBundle(
41 "gums-client");
42
43 if (prop == null) {
44 throw new RuntimeException(
45 "Couldn't find gums-client.properties configuration file.");
46 }
47
48 String location = prop.getString("gums.location");
49
50 if (location == null) {
51 throw new RuntimeException(
52 "Couldn't find gums.location URL within gums-client.properties.");
53 }
54
55 if (location.equals("direct")) {
56 direct = true;
57 loaded = true;
58 return;
59 }
60
61 String authZLocation = null;
62 try {
63 authZLocation = prop.getString("gums.authz");
64 } catch (MissingResourceException e) {
65
66 log.warn("gums.authz not found within gums-client.properties");
67 }
68
69 try {
70 locationURL = new URL(location);
71 loaded = true;
72 } catch (MalformedURLException e) {
73 throw new RuntimeException("The value in gums.location '" +
74 location + "' is not a valid url: " + e.getMessage(), e);
75 }
76
77 try {
78 if (authZLocation != null) {
79 authZLocationURL = new URL(authZLocation);
80 }
81 } catch (MalformedURLException e) {
82 throw new RuntimeException("The value in gums.authz '" +
83 location + "' is not a valid url: " + e.getMessage(), e);
84 }
85 }
86
87 public boolean isDirect() {
88 if (!loaded) {
89 loadConf();
90 }
91
92 return direct;
93 }
94
95 /***
96 * TODO: write doc
97 *
98 * @return TODO: write doc
99 */
100 public URL getGUMSLocation() {
101 if (!loaded) {
102 loadConf();
103 }
104
105 return locationURL;
106 }
107
108 public URL getGUMSAuthZLocation() {
109 if (!loaded) {
110 loadConf();
111 }
112
113 if (authZLocationURL == null) {
114 throw new RuntimeException(
115 "Couldn't find gums.authz URL within gums-client.properties.");
116 }
117
118 return authZLocationURL;
119 }
120 }