KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > ContextProvider


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

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 JavaDoc DEFAULT_CLOUD_CONTEXT_NAME = "local";
27     private static String JavaDoc 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 JavaDoc 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 + "} is not known to MMBase");
54     }
55
56     /**
57      * @since MMBase-1.7
58      * @return the name of the cloud context to be used as default
59      **/

60      public static String JavaDoc getDefaultCloudContextName() {
61          //first choice.. set the cloud context using system properties
62
if (defaultCloudContextName == null) {
63              defaultCloudContextName = System.getProperty("mmbase.defaultcloudcontext");
64          }
65          if (defaultCloudContextName == null) {
66              defaultCloudContextName = DEFAULT_CLOUD_CONTEXT_NAME;
67          }
68          return defaultCloudContextName;
69      }
70     
71     /**
72      * @since MMBase-1.7
73      * @return the default cloud context This is the local cloud if mmbase is running or could be started (with mmbase.config system property),
74      * Otherwise a default rmmci cloud, or specified with mmbase.defaultcloudtext.property
75      **/

76     public static CloudContext getDefaultCloudContext() {
77         String JavaDoc uri = System.getProperty("mmbase.defaultcloudcontext");
78         if (uri != null) {
79             return getCloudContext(uri);
80         }
81
82         try {
83             return getCloudContext(getDefaultCloudContextName());
84         } catch (NotFoundException nfe) {
85             return getCloudContext("rmi://127.0.0.1:1111/remotecontext");
86         } catch (BridgeException be) {
87             throw new BridgeException(be.getMessage() + " You may want to specify -Dmmbase.defaultcloudcontext=<URI>", be);
88         }
89     }
90
91 }
92
Popular Tags