001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010:
011: package org.mmbase.bridge.implementation;
012:
013: import org.mmbase.bridge.*;
014: import org.mmbase.security.*;
015: import org.mmbase.module.core.*;
016: import java.util.*;
017: import org.mmbase.util.logging.*;
018:
019: /**
020: * @javadoc
021: *
022: * @author Rob Vermeulen
023: * @author Pierre van Rooden
024: * @version $Id: BasicCloudContext.java,v 1.58 2007/11/16 16:07:42 michiel Exp $
025: */
026: public class BasicCloudContext implements CloudContext {
027: private static final Logger log = Logging
028: .getLoggerInstance(BasicCloudContext.class);
029:
030: /**
031: * Link to the mmbase root
032: */
033: static MMBase mmb = null;
034:
035: /**
036: * Transaction Manager to keep track of transactions
037: */
038: static TransactionManager transactionManager = null;
039:
040: /**
041: * @javadoc
042: * Temporary Node Manager for storing node during edits
043: */
044: static TemporaryNodeManager tmpObjectManager = null;
045:
046: // map of clouds by name
047: private static final Set<String> localClouds = new HashSet<String>();
048:
049: // map of modules by name
050: private static final Map<String, Module> localModules = new HashMap<String, Module>();
051:
052: /**
053: * constructor to call from the MMBase class
054: * (protected, so cannot be reached from a script)
055: */
056: protected BasicCloudContext() {
057: }
058:
059: /**
060: * @throws NotFoundException If mmbase not running and cannot be started because mmbase.config missing
061: * @throws BridgeException If mmbase not running and cannot be started (but mmbase.config was specified)
062: */
063: protected boolean check() {
064: if (mmb == null) {
065: synchronized (org.mmbase.module.core.MMBase.class) { // this is the object which MMBase.getMMBase is synchronizing too
066: // obtained lock
067: if (mmb == null) { // if run in the mean time by other thread, then skip
068: Iterator<org.mmbase.module.Module> i = org.mmbase.module.Module
069: .getModules();
070: // check if MMBase is already running
071: if (i == null) {
072: // build the error message, since it has very litle overhead (only entered once incase of startup)
073: // MMBase may only be started from the bridge when the property mmbase.config was provided
074: if (java.lang.System
075: .getProperty("mmbase.config") == null) {
076: // when mmbase.config is empty fill it with current working dir + /config
077: // this way there is no need to provide the info on the commandline
078: // java.lang.System.setProperty("mmbase.config", java.lang.System.getProperty("user.dir") + java.io.File.separatorChar + "config");
079: throw new NotFoundException(
080: "MMBase has not been started, and cannot be started by this Class. ("
081: + getClass().getName()
082: + " : no property mmbase.config found)");
083: }
084: // when MMBase is not running, try to start it!
085: try {
086: // init the MMBaseContext,...
087: org.mmbase.module.core.MMBaseContext.init();
088: // try to start MMBase now,...
089: org.mmbase.module.core.MMBase.getMMBase();
090: // now re-assign the values agina
091: i = org.mmbase.module.Module.getModules();
092: } catch (java.lang.Exception ex) {
093: log.error(
094: "Error while trying to start MMBase from the bridge: "
095: + ex.getMessage(), ex);
096: }
097: // if still null,.. give error!
098: if (i == null) {
099: return false;
100: }
101: }
102: // get the core module!
103: MMBase m = org.mmbase.module.core.MMBase
104: .getMMBase();
105: // create module list
106: while (i.hasNext()) {
107: Module mod = ModuleHandler.getModule(i.next(),
108: this );
109: localModules.put(mod.getName(), mod);
110: }
111:
112: transactionManager = TransactionManager
113: .getInstance();
114: tmpObjectManager = transactionManager
115: .getTemporaryNodeManager();
116:
117: // set all the names of all accessable clouds..
118: localClouds.add("mmbase");
119:
120: mmb = m;
121: }
122: }
123: }
124: return true;
125: }
126:
127: public ModuleList getModules() {
128: if (!check())
129: throw new BridgeException(
130: "MMBase has not been started, and cannot be started by this Class. ("
131: + getClass().getName() + ")");
132: ModuleList ml = new BasicModuleList(localModules.values());
133: return ml;
134: }
135:
136: public Module getModule(String moduleName) throws NotFoundException {
137: if (!check())
138: throw new BridgeException(
139: "MMBase has not been started, and cannot be started by this Class. ("
140: + getClass().getName() + ")");
141: Module mod = localModules.get(moduleName);
142: if (mod == null) {
143: throw new NotFoundException("Module '" + moduleName
144: + "' does not exist.");
145: }
146: return mod;
147: }
148:
149: public boolean hasModule(String moduleName) {
150: if (!check())
151: throw new BridgeException(
152: "MMBase has not been started, and cannot be started by this Class. ("
153: + getClass().getName() + ")");
154: return localModules.get(moduleName) != null;
155: }
156:
157: protected void checkExists(String cloudName)
158: throws NotFoundException {
159: if (!check())
160: throw new BridgeException(
161: "MMBase has not been started, and cannot be started by this Class. ("
162: + getClass().getName() + ")");
163: if (!localClouds.contains(cloudName)) {
164: throw new NotFoundException("Cloud '" + cloudName
165: + "' does not exist.");
166: }
167: if (mmb == null || !mmb.getState()) {
168: throw new NotFoundException("MMBase is not yet initialized");
169: }
170: }
171:
172: public Cloud getCloud(String cloudName) {
173: checkExists(cloudName);
174: return getCloud(cloudName, "anonymous", null);
175: }
176:
177: public Cloud getCloud(String cloudName, String authenticationType,
178: Map loginInfo) throws NotFoundException {
179: checkExists(cloudName);
180: return new BasicCloud(cloudName, authenticationType, loginInfo,
181: this );
182: }
183:
184: public Cloud getCloud(String cloudName, UserContext user)
185: throws NotFoundException {
186: checkExists(cloudName);
187: return new BasicCloud(cloudName, user, this );
188: }
189:
190: public StringList getCloudNames() {
191: if (!check())
192: throw new BridgeException(
193: "MMBase has not been started, and cannot be started by this Class. ("
194: + getClass().getName() + ")");
195: return new BasicStringList(localClouds);
196: }
197:
198: /**
199: * @return String describing the encoding.
200: * @since MMBase-1.6
201: */
202: public String getDefaultCharacterEncoding() {
203: if (!check())
204: throw new BridgeException(
205: "MMBase has not been started, and cannot be started by this Class. ("
206: + getClass().getName() + ")");
207: return mmb.getEncoding();
208: }
209:
210: public java.util.Locale getDefaultLocale() {
211: if (!check())
212: throw new BridgeException(
213: "MMBase has not been started, and cannot be started by this Class. ("
214: + getClass().getName() + ")");
215: return mmb.getLocale();
216: }
217:
218: public java.util.TimeZone getDefaultTimeZone() {
219: if (!check())
220: throw new BridgeException(
221: "MMBase has not been started, and cannot be started by this Class. ("
222: + getClass().getName() + ")");
223: return mmb.getTimeZone();
224: }
225:
226: public FieldList createFieldList() {
227: return new BasicFieldList();
228: }
229:
230: public NodeList createNodeList() {
231: return new BasicNodeList();
232: }
233:
234: public RelationList createRelationList() {
235: return new BasicRelationList();
236: }
237:
238: public NodeManagerList createNodeManagerList() {
239: return new BasicNodeManagerList();
240: }
241:
242: public RelationManagerList createRelationManagerList() {
243: return new BasicRelationManagerList();
244: }
245:
246: public ModuleList createModuleList() {
247: return new BasicModuleList();
248: }
249:
250: public StringList createStringList() {
251: return new BasicStringList();
252: }
253:
254: public AuthenticationData getAuthentication()
255: throws NotFoundException {
256: if (!check())
257: throw new BridgeException(
258: "MMBase has not been started, and cannot be started by this Class. ("
259: + getClass().getName() + ")");
260: // checkExists(cloudName);
261: MMBaseCop cop = mmb.getMMBaseCop();
262: if (cop == null) {
263: throw new NotFoundException("MMBase not yet initialized");
264: } else {
265: return cop.getAuthentication();
266: }
267: }
268:
269: public ActionRepository getActionRepository()
270: throws NotFoundException {
271: if (!check())
272: throw new BridgeException(
273: "MMBase has not been started, and cannot be started by this Class. ("
274: + getClass().getName() + ")");
275: // checkExists(cloudName);
276: MMBaseCop cop = mmb.getMMBaseCop();
277: if (cop == null) {
278: throw new NotFoundException("MMBase not yet initialized");
279: } else {
280: return cop.getActionRepository();
281: }
282: }
283:
284: public boolean isUp() {
285: return mmb != null && mmb.getState() && check();
286: }
287:
288: public void assertUp() {
289: // TODO implement with some nice notify-mechanism.
290: CloudContext ctx = LocalContext.getCloudContext();
291: while (!MMBaseContext.isInitialized() || !isUp()) {
292: try {
293: check();
294: Thread.sleep(10000);
295: log.debug("Sleeping another 10 secs");
296: } catch (Exception e) {
297: // I hate java.
298: }
299: }
300: }
301:
302: }
|