1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package org.astrogrid.community.common.config ;
32
33 import org.apache.commons.logging.Log ;
34 import org.apache.commons.logging.LogFactory ;
35
36 import java.io.File ;
37 import java.net.InetAddress ;
38
39 import javax.naming.Context;
40 import javax.naming.InitialContext;
41
42 import org.jconfig.Configuration ;
43 import org.jconfig.ConfigurationManager ;
44 import org.jconfig.handler.URLHandler ;
45 import org.jconfig.handler.XMLFileHandler ;
46
47 /***
48 * Class to encapsulate the Community configuration.
49 * @todo deprecate the whole class.
50 *
51 */
52 public class CommunityConfig
53 {
54 /***
55 * Our debug logger.
56 *
57 */
58 private static Log log = LogFactory.getLog(CommunityConfig.class);
59
60 /***
61 * The configuration name to use within jConfig.
62 *
63 */
64 public static final String CONFIG_NAME = "org.astrogrid.community";
65
66 /***
67 * The default category to use in the config file.
68 * Corresponds to the name of the category element in the config file.
69 * <pre>
70 * <properties>
71 * <category name="org.astrogrid.community">
72 * ....
73 * </properties>
74 * </pre>
75 *
76 */
77 public static final String DEFAULT_CATEGORY = "org.astrogrid.community";
78
79 /***
80 * The default JNDI name.
81 * Corresponds to the name in the env-entry-name entry in your web.xml.
82 * <pre>
83 * <env-entry>
84 * <env-entry-name>org.astrogrid.community.config</env-entry-name>
85 * <env-entry-value>....</env-entry-value>
86 * <env-entry-type>java.lang.String</env-entry-type>
87 * </env-entry>
88 * </pre>
89 *
90 */
91 public static final String DEFAULT_JNDI_NAME = "org.astrogrid.community.config";
92
93 /***
94 * The default system property name.
95 *
96 */
97 public static final String DEFAULT_PROPERTY_NAME = "org.astrogrid.community.config";
98
99 /***
100 * The name of the config property for our local host name.
101 *
102 */
103 public static final String LOCALHOST_PROPERTY_NAME = "community.host";
104
105 /***
106 * The name of the config property for our local community name.
107 *
108 */
109 public static final String COMMUNITY_PROPERTY_NAME = "community.name";
110
111 /***
112 * The name of the property for our policy manager URL.
113 *
114 */
115 public static final String POLICY_MANAGER_PROPERTY_NAME = "policy.manager.url";
116
117 /***
118 * The name of the property for our service URL.
119 *
120 */
121 public static final String POLICY_SERVICE_PROPERTY_NAME = "policy.service.url";
122
123 /***
124 * The name of the property for our authentication URL.
125 *
126 */
127 public static final String AUTHENTICATOR_PROPERTY_NAME = "authentication.service.url";
128
129 /***
130 * Our JConfig Configuration instance.
131 *
132 */
133 private static Configuration config = null ;
134
135 /***
136 * Static method to read a JNDI property.
137 *
138 */
139 public static String getJndiProperty(String name)
140 {
141 log.debug("") ;
142 log.debug("----\"----") ;
143 log.debug("CommunityConfig.getJndiProperty()") ;
144 String value = null ;
145 try {
146 log.debug(" JNDI name : " + DEFAULT_JNDI_NAME) ;
147 Context initCtx = new InitialContext();
148 Context envCtx = (Context) initCtx.lookup("java:comp/env");
149 value = (String) envCtx.lookup(DEFAULT_JNDI_NAME);
150 log.debug(" JNDI value : " + value) ;
151 }
152 catch(Exception ouch)
153 {
154 log.debug("Exception while trying JNDI lookup") ;
155 value = null;
156 }
157 log.debug("----\"----") ;
158 log.debug("") ;
159 return value ;
160 }
161
162 /***
163 * Static method to load our configuration.
164 * Tries using the the default JNDI name, and then reverts to the default system property.
165 * The JNDI name corresponds to the name in the env-entry-name entry in your web.xml.
166 * <pre>
167 * <env-entry>
168 * <env-entry-name>org.astrogrid.community.config</env-entry-name>
169 * <env-entry-value>....</env-entry-value>
170 * <env-entry-type>java.lang.String</env-entry-type>
171 * </env-entry>
172 * </pre>
173 *
174 */
175 public static Configuration loadConfig()
176 {
177 log.debug("") ;
178 log.debug("----\"----") ;
179 log.debug("CommunityConfig.loadConfig()") ;
180
181
182 if (null == config)
183 {
184 String path = null ;
185
186
187 if (null == path)
188 {
189 log.debug("Trying JNDI property") ;
190 path = getJndiProperty(DEFAULT_JNDI_NAME) ;
191 if (null != path)
192 {
193 log.debug("PASS : Got JNDI property") ;
194 }
195 }
196
197
198 if (null == path)
199 {
200 log.debug("Trying system property") ;
201 path = System.getProperty(DEFAULT_PROPERTY_NAME) ;
202 if (null != path)
203 {
204 log.debug("PASS : Got system property") ;
205 }
206 }
207
208
209 if (null == path)
210 {
211 log.debug("FAIL : Unable to get config file path") ;
212 }
213
214
215 if (null != path)
216 {
217 loadConfig(path) ;
218 }
219 }
220 log.debug("----\"----") ;
221 log.debug("") ;
222 return config ;
223 }
224
225 /***
226 * Static method to load our configuration, given the config file location.
227 *
228 */
229 public static Configuration loadConfig(String path)
230 {
231 log.debug("") ;
232 log.debug("----\"----") ;
233 log.debug("CommunityConfig.loadConfig()") ;
234 log.debug(" Path : " + path) ;
235
236
237 if (null == config)
238 {
239
240
241 if (null != path)
242 {
243 ConfigurationManager manager = ConfigurationManager.getInstance();
244 try {
245 if (null != path)
246 {
247 if (path.startsWith("http"))
248 {
249 URLHandler handler = new URLHandler();
250 handler.setURL(path);
251 manager.load(handler, CONFIG_NAME);
252 }
253 else {
254 File file = new File(path);
255 XMLFileHandler handler = new XMLFileHandler();
256 handler.setFile(file);
257 handler.load(file);
258 manager.load(handler, CONFIG_NAME);
259 }
260 config = ConfigurationManager.getConfiguration(CONFIG_NAME);
261 }
262 }
263 catch(Exception e)
264 {
265 e.printStackTrace();
266 config = null;
267 }
268 if (null != config)
269 {
270
271
272 config.setProperty("config.location", path) ;
273 log.debug(" ----") ;
274 String[] names = config.getPropertyNames(DEFAULT_CATEGORY) ;
275 for (int i = 0 ; i < names.length ; i++)
276 {
277 String name = names[i] ;
278 String value = config.getProperty(name, "", DEFAULT_CATEGORY) ;
279 log.debug(" '" + name + "' : '" + value + "'") ;
280 }
281 log.debug(" ----") ;
282 }
283 }
284 }
285 log.debug("----\"----") ;
286 log.debug("") ;
287 return config ;
288 }
289
290 /***
291 * Static method to get a config property.
292 *
293 */
294 public static String getProperty(String propName)
295 {
296 return getProperty(propName, null, DEFAULT_CATEGORY);
297 }
298
299 /***
300 * Static method to get a config property.
301 *
302 */
303 public static String getProperty(String propName, String defaultVal)
304 {
305 return getProperty(propName, defaultVal, DEFAULT_CATEGORY);
306 }
307
308 /***
309 * Static method to get a config property.
310 *
311 */
312 public static String getProperty(String propName, String defaultVal, String category)
313 {
314 if (null == config)
315 {
316 config = ConfigurationManager.getConfiguration(CONFIG_NAME);
317 }
318 return (null != config) ? config.getProperty(propName, defaultVal, category) : null ;
319 }
320
321 /***
322 * Our local host name.
323 *
324 */
325 private static String localhost = null ;
326
327 /***
328 * Static method to get our localhost name.
329 * If not specified in the config file or a system property, defaults to our local host name.
330 *
331 */
332 public static String getHostName()
333 {
334
335
336 if ((null == localhost) || (localhost.length() <= 0))
337 {
338 log.debug("getHostName()") ;
339 log.debug(" Trying config property '" + LOCALHOST_PROPERTY_NAME + "'") ;
340 localhost = getProperty(LOCALHOST_PROPERTY_NAME) ;
341 log.debug(" Config property result : " + localhost) ;
342 }
343
344
345 if ((null == localhost) || (localhost.length() <= 0))
346 {
347 String name = CONFIG_NAME + "." + LOCALHOST_PROPERTY_NAME ;
348 log.debug("getHostName()") ;
349 log.debug(" Trying system property '" + name + "'") ;
350 localhost = System.getProperty(name) ;
351 log.debug(" System property result : " + localhost) ;
352 }
353
354
355 if ((null == localhost) || (localhost.length() <= 0))
356 {
357 log.debug("getHostName()") ;
358 log.debug(" Trying localhost ....") ;
359
360
361 try {
362 localhost = InetAddress.getLocalHost().getHostName() ;
363 }
364 catch (Exception ouch)
365 {
366 localhost = "localhost" ;
367 }
368 log.debug(" Localhost result : " + localhost) ;
369 }
370 return localhost ;
371 }
372
373 /***
374 * Our local community name.
375 *
376 */
377 private static String community = null ;
378
379 /***
380 * Static method to get our community name.
381 * If not specified in the config file or a system property, defaults to our local host name.
382 *
383 */
384 public static String getCommunityName()
385 {
386
387
388 if ((null == community) || (community.length() <= 0))
389 {
390 log.debug("getCommunityName()") ;
391 log.debug(" Trying config property '" + COMMUNITY_PROPERTY_NAME + "'") ;
392 community = getProperty(COMMUNITY_PROPERTY_NAME) ;
393 log.debug(" Config property result : " + community) ;
394 }
395
396
397 if ((null == community) || (community.length() <= 0))
398 {
399 String name = CONFIG_NAME + "." + COMMUNITY_PROPERTY_NAME ;
400 log.debug("getCommunityName()") ;
401 log.debug(" Trying system property '" + name + "'") ;
402 community = System.getProperty(name) ;
403 log.debug(" System property result : " + community) ;
404 }
405
406
407 if ((null == community) || (community.length() <= 0))
408 {
409 log.debug("getCommunityName()") ;
410 log.debug(" Trying localhost ....") ;
411
412
413 community = getHostName() ;
414 log.debug(" Localhost result : " + community) ;
415 }
416 return community ;
417 }
418
419 /***
420 * Our local manager URL.
421 *
422 */
423 private static String manager = null ;
424
425 /***
426 * Static method to get our local manager URL.
427 *
428 */
429 public static String getManagerUrl()
430 {
431 log.debug("getManagerUrl()") ;
432
433
434 if ((null == manager) || (manager.length() <= 0))
435 {
436 log.debug("getManagerUrl()") ;
437 log.debug(" Trying config property '" + POLICY_MANAGER_PROPERTY_NAME + "'") ;
438 manager = getProperty(POLICY_MANAGER_PROPERTY_NAME) ;
439 if (null != manager) manager = manager.trim() ;
440 log.debug(" Config property result : " + manager) ;
441 }
442
443
444 if ((null == manager) || (manager.length() <= 0))
445 {
446 log.debug("getManagerUrl()") ;
447 log.debug(" Trying localhost ....") ;
448 manager = "http://" + getHostName() + ":8080/axis/services/PolicyManager" ;
449 log.debug(" Localhost result : " + manager) ;
450 }
451 log.debug(" Manager URL : " + manager) ;
452 return manager ;
453 }
454
455 /***
456 * Our local service URL.
457 *
458 */
459 private static String service = null ;
460
461 /***
462 * Static method to get our local service URL.
463 *
464 */
465 public static String getServiceUrl()
466 {
467 log.debug("getServiceUrl()") ;
468
469
470 if ((null == service) || (service.length() <= 0))
471 {
472 log.debug("getServiceUrl()") ;
473 log.debug(" Trying config property '" + POLICY_SERVICE_PROPERTY_NAME + "'") ;
474 service = getProperty(POLICY_SERVICE_PROPERTY_NAME) ;
475 if (null != service) service = service.trim() ;
476 log.debug(" Config property result : " + service) ;
477 }
478
479
480 if ((null == service) || (service.length() <= 0))
481 {
482 log.debug("getServiceUrl()") ;
483 log.debug(" Trying localhost ....") ;
484 service = "http://" + getHostName() + ":8080/axis/services/PolicyService" ;
485 log.debug(" Localhost result : " + service) ;
486 }
487 log.debug(" Service URL : " + service) ;
488 return service ;
489 }
490
491
492 /***
493 * Our local authentication service URL.
494 *
495 */
496 private static String authenticator = null ;
497
498 /***
499 * Static method to get our local authentication service URL.
500 *
501 */
502 public static String getAuthenticationServiceUrl()
503 {
504 log.debug("getAuthenticationServiceUrl()") ;
505
506
507 if ((null == authenticator) || (authenticator.length() <= 0))
508 {
509 log.debug("getAuthenticationServiceUrl()") ;
510 log.debug(" Trying config property '" + AUTHENTICATOR_PROPERTY_NAME + "'") ;
511 authenticator = getProperty(AUTHENTICATOR_PROPERTY_NAME) ;
512 if (null != authenticator) authenticator = authenticator.trim() ;
513 log.debug(" Config property result : " + authenticator) ;
514 }
515
516
517 if ((null == authenticator) || (authenticator.length() <= 0))
518 {
519 log.debug("getAuthenticationServiceUrl()") ;
520 log.debug(" Trying localhost ....") ;
521 authenticator = "http://" + getHostName() + ":8080/axis/services/AuthenticationService" ;
522 log.debug(" Localhost result : " + authenticator) ;
523 }
524 log.debug(" Authenticator URL : " + authenticator) ;
525 return authenticator ;
526 }
527
528 /***
529 * Our Administrator for a portal or the community
530 *
531 */
532 private static String admin = null ;
533
534 /***
535 * The name of the property for admin name
536 *
537 */
538 public static final String ASTROGRID_ADMIN_PROPERTY_NAME = "astrogrid.admin";
539
540 /***
541 * Static method to get our local service URL.
542 *
543 */
544 public static String getAdministrator()
545 {
546
547
548 if (null == admin)
549 {
550 admin = getProperty(ASTROGRID_ADMIN_PROPERTY_NAME) ;
551 }
552 return admin;
553 }
554
555 /***
556 * Our Administrator for a portal or the community
557 *
558 */
559 private static String adminEmail = null ;
560
561 /***
562 * The name of the property for admin name
563 *
564 */
565 public static final String ASTROGRID_ADMIN_EMAIL_PROPERTY_NAME = "astrogrid.adminEmail";
566
567 /***
568 * Static method to get our local service URL.
569 *
570 */
571 public static String getAdministratorEmail()
572 {
573
574
575 if (null == adminEmail)
576 {
577 adminEmail = getProperty(ASTROGRID_ADMIN_EMAIL_PROPERTY_NAME) ;
578 }
579 return adminEmail;
580 }
581 }