KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > implementation > BasicCloudContext


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
11 package org.mmbase.bridge.implementation;
12 import org.mmbase.bridge.*;
13 import org.mmbase.security.*;
14 import org.mmbase.module.core.*;
15 import java.util.*;
16 import org.mmbase.util.logging.*;
17
18 /**
19  * @javadoc
20  *
21  * @author Rob Vermeulen
22  * @author Pierre van Rooden
23  * @version $Id: BasicCloudContext.java,v 1.51.2.1 2006/12/05 19:24:51 michiel Exp $
24  */

25 public class BasicCloudContext implements CloudContext {
26     private static final Logger log = Logging.getLoggerInstance(BasicCloudContext.class);
27
28     /**
29     * Link to the mmbase root
30     */

31     static MMBase mmb = null;
32
33     /**
34     * Temporary Node Manager for storing node during edits
35     */

36     static TemporaryNodeManager tmpObjectManager = null;
37
38     /**
39     * Transaction Manager to keep track of transactions
40     */

41     static TransactionManager transactionManager = null;
42
43     // map of clouds by name
44
private static final Set localClouds = new HashSet();
45
46     // map of modules by name
47
private static Map localModules = new HashMap();
48
49     /**
50      * constructor to call from the MMBase class
51      * (protected, so cannot be reached from a script)
52      */

53     protected BasicCloudContext() {
54     }
55
56     /**
57      * @throws NotFoundException If mmbase not running and cannot be started because mmbase.config missing
58      * @throws BridgeException If mmbase not running and cannot be started (but mmbase.config was specified)
59      */

60     protected boolean check() {
61         if(mmb == null) {
62             synchronized(this) {
63                 // obtained lock
64
if (mmb == null) { // if run in the mean time by other thread, then skip
65
Iterator i = org.mmbase.module.Module.getModules();
66                     // check if MMBase is already running
67
if (i == null) {
68                         // build the error message, since it has very litle overhead (only entered once incase of startup)
69
// MMBase may only be started from the bridge when the property mmbase.config was provided
70
if (java.lang.System.getProperty("mmbase.config") == null) {
71                             // when mmbase.config is empty fill it with current working dir + /config
72
// this way there is no need to provide the info on the commandline
73
// java.lang.System.setProperty("mmbase.config", java.lang.System.getProperty("user.dir") + java.io.File.separatorChar + "config");
74
throw new NotFoundException("MMBase has not been started, and cannot be started by this Class. (" + getClass().getName() + " : no property mmbase.config found)");
75                         }
76                         // when MMBase is not running, try to start it!
77
try {
78                             // init the MMBaseContext,...
79
org.mmbase.module.core.MMBaseContext.init();
80                             // try to start MMBase now,...
81
org.mmbase.module.core.MMBase.getMMBase();
82                             // now re-assign the values agina
83
i = org.mmbase.module.Module.getModules();
84                         }
85                         catch(java.lang.Exception JavaDoc ex) {
86                             log.error("Error while trying to start MMBase from the bridge:" + Logging.stackTrace(ex));
87                         }
88                         // if still null,.. give error!
89
if(i == null) {
90                             return false;
91                         }
92                     }
93                     // get the core module!
94
MMBase m = org.mmbase.module.core.MMBase.getMMBase();
95                     // create transaction manager and temporary node manager
96
tmpObjectManager = new TemporaryNodeManager(m);
97                     transactionManager = new TransactionManager(m, tmpObjectManager);
98                     // create module list
99
while(i.hasNext()) {
100                         Module mod = ModuleHandler.getModule((org.mmbase.module.Module)i.next(),this);
101                         localModules.put(mod.getName(), mod);
102                     }
103                     // set all the names of all accessable clouds..
104
localClouds.add("mmbase");
105                     mmb = m;
106                 }
107             }
108         }
109         return true;
110     }
111
112     public ModuleList getModules() {
113         if (!check()) throw new BridgeException("MMBase has not been started, and cannot be started by this Class. (" + getClass().getName() + ")");
114         ModuleList ml = new BasicModuleList(localModules.values());
115         return ml;
116     }
117
118     public Module getModule(String JavaDoc moduleName) throws NotFoundException {
119         if (!check()) throw new BridgeException("MMBase has not been started, and cannot be started by this Class. (" + getClass().getName() + ")");
120         Module mod = (Module)localModules.get(moduleName);
121         if (mod == null) {
122             throw new NotFoundException("Module '" + moduleName + "' does not exist.");
123         }
124         return mod;
125     }
126
127     public boolean hasModule(String JavaDoc moduleName) {
128         if (!check()) throw new BridgeException("MMBase has not been started, and cannot be started by this Class. (" + getClass().getName() + ")");
129         return localModules.get(moduleName) != null;
130     }
131
132
133     protected void checkExists(String JavaDoc cloudName) throws NotFoundException {
134         if (!check()) throw new BridgeException("MMBase has not been started, and cannot be started by this Class. (" + getClass().getName() + ")");
135         if ( !localClouds.contains(cloudName) ) {
136             throw new NotFoundException("Cloud '" + cloudName + "' does not exist.");
137         }
138         if (mmb == null || ! mmb.getState()) {
139             throw new NotFoundException("MMBase is not yet initialized");
140         }
141     }
142     public Cloud getCloud(String JavaDoc cloudName) {
143         checkExists(cloudName);
144         return getCloud(cloudName, "anonymous", null);
145     }
146
147     public Cloud getCloud(String JavaDoc cloudName, String JavaDoc authenticationType, Map loginInfo) throws NotFoundException {
148         checkExists(cloudName);
149         return new BasicCloud(cloudName, authenticationType, loginInfo, this);
150     }
151
152     public Cloud getCloud(String JavaDoc cloudName, UserContext user) throws NotFoundException {
153         checkExists(cloudName);
154        return new BasicCloud(cloudName, user, this);
155     }
156
157     public StringList getCloudNames() {
158         if (!check()) throw new BridgeException("MMBase has not been started, and cannot be started by this Class. (" + getClass().getName() + ")");
159         return new BasicStringList(localClouds);
160     }
161
162     /**
163      * @return String describing the encoding.
164      * @since MMBase-1.6
165      */

166     public String JavaDoc getDefaultCharacterEncoding() {
167         if (!check()) throw new BridgeException("MMBase has not been started, and cannot be started by this Class. (" + getClass().getName() + ")");
168         return mmb.getEncoding();
169     }
170
171     public java.util.Locale JavaDoc getDefaultLocale() {
172         if (!check()) throw new BridgeException("MMBase has not been started, and cannot be started by this Class. (" + getClass().getName() + ")");
173         return mmb.getLocale();
174     }
175
176     public java.util.TimeZone JavaDoc getDefaultTimeZone() {
177         if (!check()) throw new BridgeException("MMBase has not been started, and cannot be started by this Class. (" + getClass().getName() + ")");
178         return mmb.getTimeZone();
179     }
180
181     public FieldList createFieldList() {
182         return new BasicFieldList();
183     }
184
185     public NodeList createNodeList() {
186         return new BasicNodeList();
187     }
188
189     public RelationList createRelationList() {
190         return new BasicRelationList();
191     }
192
193     public NodeManagerList createNodeManagerList() {
194         return new BasicNodeManagerList();
195     }
196
197     public RelationManagerList createRelationManagerList() {
198         return new BasicRelationManagerList();
199     }
200
201     public ModuleList createModuleList() {
202         return new BasicModuleList();
203     }
204
205     public StringList createStringList() {
206         return new BasicStringList();
207     }
208
209     public AuthenticationData getAuthentication() throws NotFoundException {
210         if (!check()) throw new BridgeException("MMBase has not been started, and cannot be started by this Class. (" + getClass().getName() + ")");
211         // checkExists(cloudName);
212
MMBaseCop cop = mmb.getMMBaseCop();
213         if (cop == null) {
214             throw new NotFoundException("MMBase not yet initialized");
215         } else {
216             return cop.getAuthentication();
217         }
218     }
219
220     public boolean isUp() {
221         return mmb != null && mmb.getState() && check();
222     }
223
224     public void assertUp() {
225         // TODO implement with some nice notify-mechanism.
226
CloudContext ctx = LocalContext.getCloudContext();
227         while (!MMBaseContext.isInitialized() || ! isUp()) {
228             try {
229                 check();
230                 Thread.currentThread().sleep(10000);
231                 log.debug("Sleeping another 10 secs");
232             } catch (Exception JavaDoc e) {
233                 // I hate java.
234
}
235         }
236     }
237
238 }
239
Popular Tags