01: /*
02:
03: This software is OSI Certified Open Source Software.
04: OSI Certified is a certification mark of the Open Source Initiative.
05:
06: The license (Mozilla version 1.0) can be read at the MMBase site.
07: See http://www.MMBase.org/license
08:
09: */
10: package org.mmbase.bridge;
11:
12: /**
13: * Main class to aquire CloudContexts
14: * @author Kees Jongenburger
15: * @version $Id: ContextProvider.java,v 1.10 2005/11/04 23:20:25 michiel Exp $
16: * @since MMBase-1.5
17: */
18: public class ContextProvider {
19: /**
20: * When no system property mmbase.defaultcloudcontext is set
21: * the default cloud context is the context returned when
22: * DEFAULT_CLOUD_CONTEXT_NAME is fed to getCloudContext(String)<BR>
23: * DEFAULT_CLOUD_CONTEXT_NAME="local"
24: **/
25:
26: public final static String DEFAULT_CLOUD_CONTEXT_NAME = "local";
27: private static String defaultCloudContextName;
28:
29: /**
30: * Factory method to get an instance of a CloudContext. Depending
31: * on the uri parameter given the CloudContext might be a local context
32: * or a remote context (rmi)
33: * @param uri an identifier for the context<br />
34: * possible values:
35: * <ul>
36: * <li>local : will return a local context</li>
37: * <li>rmi://hostname:port/contextname : will return a remote context</li>
38: * <li>a null parameter: will return the default context </li>
39: * </ul>
40: * @return a cloud context
41: * @throws BridgeException if the cloudcontext was not found
42: */
43: public static CloudContext getCloudContext(String uri) {
44: if (uri == null || (uri != null && uri.trim().length() == 0)) {
45: uri = getDefaultCloudContextName();
46: }
47:
48: if (uri.startsWith("rmi")) {
49: return RemoteContext.getCloudContext(uri);
50: } else if (uri.startsWith("local")) {
51: return LocalContext.getCloudContext();
52: }
53: throw new BridgeException("cloudcontext with name {" + uri
54: + "} is not known to MMBase");
55: }
56:
57: /**
58: * @since MMBase-1.7
59: * @return the name of the cloud context to be used as default
60: **/
61: public static String getDefaultCloudContextName() {
62: //first choice.. set the cloud context using system properties
63: if (defaultCloudContextName == null) {
64: defaultCloudContextName = System
65: .getProperty("mmbase.defaultcloudcontext");
66: }
67: if (defaultCloudContextName == null) {
68: defaultCloudContextName = DEFAULT_CLOUD_CONTEXT_NAME;
69: }
70: return defaultCloudContextName;
71: }
72:
73: /**
74: * @since MMBase-1.7
75: * @return the default cloud context This is the local cloud if mmbase is running or could be started (with mmbase.config system property),
76: * Otherwise a default rmmci cloud, or specified with mmbase.defaultcloudtext.property
77: **/
78: public static CloudContext getDefaultCloudContext() {
79: String uri = System.getProperty("mmbase.defaultcloudcontext");
80: if (uri != null) {
81: return getCloudContext(uri);
82: }
83:
84: try {
85: return getCloudContext(getDefaultCloudContextName());
86: } catch (NotFoundException nfe) {
87: return getCloudContext("rmi://127.0.0.1:1111/remotecontext");
88: } catch (BridgeException be) {
89: throw new BridgeException(
90: be.getMessage()
91: + " You may want to specify -Dmmbase.defaultcloudcontext=<URI>",
92: be);
93: }
94: }
95:
96: }
|