001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/main/OpenCms.java,v $
003: * Date : $Date: 2008-02-27 12:05:39 $
004: * Version: $Revision: 1.66 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.main;
033:
034: import org.opencms.db.CmsDefaultUsers;
035: import org.opencms.db.CmsLoginManager;
036: import org.opencms.db.CmsSqlManager;
037: import org.opencms.file.CmsObject;
038: import org.opencms.file.CmsResource;
039: import org.opencms.i18n.CmsLocaleManager;
040: import org.opencms.importexport.CmsImportExportManager;
041: import org.opencms.loader.CmsResourceManager;
042: import org.opencms.module.CmsModuleManager;
043: import org.opencms.monitor.CmsMemoryMonitor;
044: import org.opencms.publish.CmsPublishManager;
045: import org.opencms.repository.CmsRepositoryManager;
046: import org.opencms.scheduler.CmsScheduleManager;
047: import org.opencms.search.CmsSearchManager;
048: import org.opencms.security.CmsOrgUnitManager;
049: import org.opencms.security.CmsRole;
050: import org.opencms.security.CmsRoleManager;
051: import org.opencms.security.I_CmsAuthorizationHandler;
052: import org.opencms.security.I_CmsPasswordHandler;
053: import org.opencms.security.I_CmsValidationHandler;
054: import org.opencms.site.CmsSiteManagerImpl;
055: import org.opencms.staticexport.CmsLinkManager;
056: import org.opencms.staticexport.CmsStaticExportManager;
057: import org.opencms.workplace.CmsWorkplaceManager;
058: import org.opencms.xml.CmsXmlContentTypeManager;
059:
060: import java.util.List;
061: import java.util.Map;
062: import java.util.Set;
063:
064: import javax.servlet.http.HttpServletRequest;
065: import javax.servlet.http.HttpServletResponse;
066:
067: import org.apache.commons.logging.Log;
068:
069: /**
070: * The OpenCms "operating system" that provides
071: * public static methods which can be used by other classes to access
072: * basic system features of OpenCms like logging etc.<p>
073: *
074: * This Object provides singleton access to the initialized OpenCms runtime system.
075: * Some methods are for internal or advanced use only, but others are of also of interest
076: * for general OpenCms development.<p>
077: *
078: * For example, to generate a new instance of <code>{@link org.opencms.file.CmsObject}</code> class in your application,
079: * use <code>{@link org.opencms.main.OpenCms#initCmsObject(String)}</code>. The argument String should be
080: * the name of the guest user, usually "Guest" and more formally obtained by <code>{@link org.opencms.db.CmsDefaultUsers#getUserGuest()}</code>.
081: * This will give you an initialized context with guest user permissions.
082: * Then use <code>{@link CmsObject#loginUser(String, String)}</code> to log in the user you want.
083: * Obviously you need the password for the new user.<p>
084: *
085: * Using <code>{@link #getSiteManager()}</code> you can obtain the initialized <code>{@link org.opencms.site.CmsSiteManagerImpl}</code>
086: * which provides information about the sites configured in the running OpenCms instance.<p>
087: *
088: * The <code>{@link org.opencms.db.CmsDefaultUsers}</code> instance returned by <code>{@link #getDefaultUsers()}</code>
089: * provides information about the names of the OpenCms default users.<p>
090: *
091: * Other objects of note that can be obtained by this class include the <code>{@link org.opencms.module.CmsModuleManager}</code>
092: * or the <code>{@link org.opencms.scheduler.CmsScheduleManager}</code>.<p>
093: *
094: * When using the instances returned by this object, keep in mind that applying changes to these may alter the basic OpenCms
095: * system configuration, which in turn may affect the systems performance or stability.<p>
096: *
097: * @author Alexander Kandzior
098: *
099: * @version $Revision: 1.66 $
100: *
101: * @since 6.0.0
102: */
103: public final class OpenCms {
104:
105: /** Runlevel 0: System is offline. */
106: public static final int RUNLEVEL_0_OFFLINE = 0;
107:
108: /** Runlevel 1: Core object created, no database (some test cases run in this level). */
109: public static final int RUNLEVEL_1_CORE_OBJECT = 1;
110:
111: /** Runlevel 2: Initializing the system, required since this may take some seconds because of database connections. */
112: public static final int RUNLEVEL_2_INITIALIZING = 2;
113:
114: /** Runlevel 3: Shell access to the database possible, but no servlet context available. */
115: public static final int RUNLEVEL_3_SHELL_ACCESS = 3;
116:
117: /** Runlevel 4: Final runlevel where database and servlet are initialized. */
118: public static final int RUNLEVEL_4_SERVLET_ACCESS = 4;
119:
120: /**
121: * The public contructor is hidden to prevent generation of instances of this class.<p>
122: */
123: private OpenCms() {
124:
125: // empty
126: }
127:
128: /**
129: * Add a cms event listener that listens to all events.<p>
130: *
131: * @param listener the listener to add
132: */
133: public static void addCmsEventListener(I_CmsEventListener listener) {
134:
135: OpenCmsCore.getInstance().getEventManager()
136: .addCmsEventListener(listener);
137: }
138:
139: /**
140: * Add a cms event listener that listens only to particular events.<p>
141: *
142: * @param listener the listener to add
143: * @param eventTypes the events to listen for
144: */
145: public static void addCmsEventListener(I_CmsEventListener listener,
146: int[] eventTypes) {
147:
148: OpenCmsCore.getInstance().getEventManager()
149: .addCmsEventListener(listener, eventTypes);
150: }
151:
152: /**
153: * Notify all event listeners that a particular event has occurred.<p>
154: *
155: * @param event a CmsEvent
156: */
157: public static void fireCmsEvent(CmsEvent event) {
158:
159: OpenCmsCore.getInstance().getEventManager().fireEvent(event);
160: }
161:
162: /**
163: * Notify all event listeners that a particular event has occurred.<p>
164: *
165: * The event will be given to all registered <code>{@link I_CmsEventListener}</code> objects.<p>
166: *
167: * @param type event type
168: * @param data event data
169: */
170: public static void fireCmsEvent(int type, Map data) {
171:
172: OpenCmsCore.getInstance().getEventManager().fireEvent(type,
173: data);
174: }
175:
176: /**
177: * Returns the configured authorization handler.<p>
178: *
179: * @return the configured authorization handler
180: */
181: public static I_CmsAuthorizationHandler getAuthorizationHandler() {
182:
183: return OpenCmsCore.getInstance().getAuthorizationHandler();
184: }
185:
186: /**
187: * Returns the configured list of default directory file names (instances of <code>{@link String}</code>).<p>
188: *
189: * Caution: This list can not be modified.<p>
190: *
191: * @return the configured list of default directory file names
192: */
193: public static List getDefaultFiles() {
194:
195: return OpenCmsCore.getInstance().getDefaultFiles();
196: }
197:
198: /**
199: * Returns the default user and group name configuration.<p>
200: *
201: * @return the default user and group name configuration
202: */
203: public static CmsDefaultUsers getDefaultUsers() {
204:
205: return OpenCmsCore.getInstance().getDefaultUsers();
206: }
207:
208: /**
209: * Returns the event manger that handles all OpenCms events.<p>
210: *
211: * @return the event manger that handles all OpenCms events
212: */
213: public static CmsEventManager getEventManager() {
214:
215: return OpenCmsCore.getInstance().getEventManager();
216: }
217:
218: /**
219: * Returns the configured export points,
220: * the returned set being an unmodifiable set.<p>
221: *
222: * @return an unmodifiable set of the configured export points
223: */
224: public static Set getExportPoints() {
225:
226: return OpenCmsCore.getInstance().getExportPoints();
227: }
228:
229: /**
230: * Returns the initialized import/export manager,
231: * which contains information about how to handle imported resources.<p>
232: *
233: * @return the initialized import/export manager
234: */
235: public static CmsImportExportManager getImportExportManager() {
236:
237: return OpenCmsCore.getInstance().getImportExportManager();
238: }
239:
240: /**
241: * Returns the link manager to resolve links in <link> tags.<p>
242: *
243: * @return the link manager to resolve links in <link> tags
244: */
245: public static CmsLinkManager getLinkManager() {
246:
247: return OpenCmsCore.getInstance().getLinkManager();
248: }
249:
250: /**
251: * Returns the locale manager used for obtaining the current locale.<p>
252: *
253: * @return the locale manager
254: */
255: public static CmsLocaleManager getLocaleManager() {
256:
257: return OpenCmsCore.getInstance().getLocaleManager();
258: }
259:
260: /**
261: * Returns the log for the selected object.<p>
262: *
263: * If the provided object is a String, this String will
264: * be used as channel name. Otherwise the objects
265: * class name will be used as channel name.<p>
266: *
267: * @param obj the object channel to use
268: * @return the log for the selected object channel
269: */
270: public static Log getLog(Object obj) {
271:
272: return CmsLog.getLog(obj);
273: }
274:
275: /**
276: * Returns the login manager used to check if a login is possible.<p>
277: *
278: * @return the login manager
279: */
280: public static CmsLoginManager getLoginManager() {
281:
282: return OpenCmsCore.getInstance().getLoginManager();
283: }
284:
285: /**
286: * Returns the memory monitor.<p>
287: *
288: * @return the memory monitor
289: */
290: public static CmsMemoryMonitor getMemoryMonitor() {
291:
292: return OpenCmsCore.getInstance().getMemoryMonitor();
293: }
294:
295: /**
296: * Returns the module manager.<p>
297: *
298: * @return the module manager
299: */
300: public static CmsModuleManager getModuleManager() {
301:
302: return OpenCmsCore.getInstance().getModuleManager();
303: }
304:
305: /**
306: * Returns the organizational unit manager.<p>
307: *
308: * @return the organizational unit manager
309: */
310: public static CmsOrgUnitManager getOrgUnitManager() {
311:
312: return OpenCmsCore.getInstance().getOrgUnitManager();
313: }
314:
315: /**
316: * Returns the password handler.<p>
317: *
318: * @return the password handler
319: */
320: public static I_CmsPasswordHandler getPasswordHandler() {
321:
322: return OpenCmsCore.getInstance().getPasswordHandler();
323: }
324:
325: /**
326: * Returns the core publish manager class.<p>
327: *
328: * @return the publish manager instance
329: */
330: public static CmsPublishManager getPublishManager() {
331:
332: return OpenCmsCore.getInstance().getPublishManager();
333: }
334:
335: /**
336: * Returns the repository manager.<p>
337: *
338: * @return the repository manager
339: */
340: public static CmsRepositoryManager getRepositoryManager() {
341:
342: return OpenCmsCore.getInstance().getRepositoryManager();
343: }
344:
345: /**
346: * Returns the resource manager.<p>
347: *
348: * @return the resource manager
349: */
350: public static CmsResourceManager getResourceManager() {
351:
352: return OpenCmsCore.getInstance().getResourceManager();
353: }
354:
355: /**
356: * Returns the role manager.<p>
357: *
358: * @return the role manager
359: */
360: public static CmsRoleManager getRoleManager() {
361:
362: return OpenCmsCore.getInstance().getRoleManager();
363: }
364:
365: /**
366: * Returns the current OpenCms run level.<p>
367: *
368: * The following runlevels are defined:
369: * <dl>
370: * <dt>Runlevel {@link OpenCms#RUNLEVEL_0_OFFLINE}:</dt><dd>
371: * OpenCms is in the process of being shut down, the system is offline.</dd>
372: *
373: * <dt>Runlevel {@link OpenCms#RUNLEVEL_1_CORE_OBJECT}:</dt><dd>
374: * OpenCms instance available, but configuration has not been processed.
375: * No database or VFS available.</dd>
376: *
377: * <dt>Runlevel {@link OpenCms#RUNLEVEL_2_INITIALIZING}:</dt><dd>
378: * OpenCms is initializing, but the process is not finished.
379: * The database with the VFS is currently being connected but can't be accessed.</dd>
380: *
381: * <dt>Runlevel {@link OpenCms#RUNLEVEL_3_SHELL_ACCESS}:</dt><dd>
382: * OpenCms database and VFS available, but http processing (i.e. servlet) not initialized.
383: * This is the runlevel the OpenCms shell operates in.</dd>
384: *
385: * <dt>Runlevel {@link OpenCms#RUNLEVEL_4_SERVLET_ACCESS}:</dt><dd>
386: * OpenCms fully initialized, servlet and database available.
387: * This is the "default" when OpenCms is in normal operation.</dd>
388: * </dl>
389: *
390: * @return the OpenCms run level
391: */
392: public static int getRunLevel() {
393:
394: return OpenCmsCore.getInstance().getRunLevel();
395: }
396:
397: /**
398: * Looks up a value in the runtime property Map.<p>
399: *
400: * @param key the key to look up in the runtime properties
401: * @return the value for the key, or null if the key was not found
402: */
403: public static Object getRuntimeProperty(Object key) {
404:
405: return OpenCmsCore.getInstance().getRuntimeProperty(key);
406: }
407:
408: /**
409: * Returns the configured schedule manager.<p>
410: *
411: * @return the configured schedule manager
412: */
413: public static CmsScheduleManager getScheduleManager() {
414:
415: return OpenCmsCore.getInstance().getScheduleManager();
416: }
417:
418: /**
419: * Returns the initialized search manager,
420: * which provides indexing and searching operations.<p>
421: *
422: * @return the initialized search manager
423: */
424: public static CmsSearchManager getSearchManager() {
425:
426: return OpenCmsCore.getInstance().getSearchManager();
427: }
428:
429: /**
430: * Returns the session manager that keeps track of the active users.<p>
431: *
432: * @return the session manager that keeps track of the active users
433: */
434: public static CmsSessionManager getSessionManager() {
435:
436: return OpenCmsCore.getInstance().getSessionManager();
437: }
438:
439: /**
440: * Returns the initialized site manager,
441: * which contains information about all configured sites.<p>
442: *
443: * @return the initialized site manager
444: */
445: public static CmsSiteManagerImpl getSiteManager() {
446:
447: return OpenCmsCore.getInstance().getSiteManager();
448: }
449:
450: /**
451: * Returns an instance of the common sql manager.<p>
452: *
453: * @return an instance of the common sql manager
454: */
455: public static CmsSqlManager getSqlManager() {
456:
457: return OpenCmsCore.getInstance().getSqlManager();
458: }
459:
460: /**
461: * Returns the properties for the static export.<p>
462: *
463: * @return the properties for the static export
464: */
465: public static CmsStaticExportManager getStaticExportManager() {
466:
467: return OpenCmsCore.getInstance().getStaticExportManager();
468: }
469:
470: /**
471: * Returns the system information storage.<p>
472: *
473: * @return the system information storage
474: */
475: public static CmsSystemInfo getSystemInfo() {
476:
477: return OpenCmsCore.getInstance().getSystemInfo();
478: }
479:
480: /**
481: * Returns the list of system defined roles (instances of <code>{@link CmsRole}</code>).<p>
482: *
483: * Caution: This list can not be modified.<p>
484: *
485: * @return the list of system defined roles
486: */
487: public static List getSystemRoles() {
488:
489: return CmsRole.getSystemRoles();
490: }
491:
492: /**
493: * Returns the OpenCms Thread store.<p>
494: *
495: * @return the OpenCms Thread store
496: */
497: public static CmsThreadStore getThreadStore() {
498:
499: return OpenCmsCore.getInstance().getThreadStore();
500: }
501:
502: /**
503: * Returns the runtime validation handler.<p>
504: *
505: * @return the validation handler
506: */
507: public static I_CmsValidationHandler getValidationHandler() {
508:
509: return OpenCmsCore.getInstance().getValidationHandler();
510: }
511:
512: /**
513: * Returns the initialized workplace manager,
514: * which contains information about the global workplace settings.<p>
515: *
516: * @return the initialized workplace manager
517: */
518: public static CmsWorkplaceManager getWorkplaceManager() {
519:
520: return OpenCmsCore.getInstance().getWorkplaceManager();
521: }
522:
523: /**
524: * Returns the XML content type manager.<p>
525: *
526: * @return the XML content type manager
527: */
528: public static CmsXmlContentTypeManager getXmlContentTypeManager() {
529:
530: return OpenCmsCore.getInstance().getXmlContentTypeManager();
531: }
532:
533: /**
534: * Returns an independent copy of the provided CmsObject.<p>
535: *
536: * This can be useful in case a permanent reference to a CmsObject is stored.
537: * Changing the request context values (for example project, siteroot) in the new CmsObject
538: * will have no side effects to the CmsObject it was copied form.<p>
539: *
540: * @param cms the CmsObject to create a copy of
541: *
542: * @return an independent copy of the provided CmsObject
543: *
544: * @throws CmsException in case the intialization failed
545: *
546: * @see OpenCms#initCmsObject(CmsObject)
547: * @see OpenCms#initCmsObject(CmsObject, CmsContextInfo)
548: * @see OpenCms#initCmsObject(String)
549: */
550: public static CmsObject initCmsObject(CmsObject cms)
551: throws CmsException {
552:
553: return OpenCmsCore.getInstance().initCmsObject(cms);
554: }
555:
556: /**
557: * Returns an initialized CmsObject with the user and context initialized as provided.<p>
558: *
559: * Note: Only if the provided <code>adminCms</code> CmsObject has admin permissions,
560: * this method allows the creation a CmsObject for any existing user. Otherwise
561: * only the default users 'Guest' and 'Export' can initialized with
562: * this method, all other user names will throw an Exception.<p>
563: *
564: * @param adminCms must either be initialized with "Admin" permissions, or null
565: * @param contextInfo the context info to create a CmsObject for
566: *
567: * @return an initialized CmsObject with the given users permissions
568: *
569: * @throws CmsException if an invalid user name was provided, or if something else goes wrong
570: *
571: * @see org.opencms.db.CmsDefaultUsers#getUserGuest()
572: * @see org.opencms.db.CmsDefaultUsers#getUserExport()
573: * @see OpenCms#initCmsObject(CmsObject)
574: * @see OpenCms#initCmsObject(CmsObject, CmsContextInfo)
575: * @see OpenCms#initCmsObject(String)
576: */
577: public static CmsObject initCmsObject(CmsObject adminCms,
578: CmsContextInfo contextInfo) throws CmsException {
579:
580: return OpenCmsCore.getInstance().initCmsObject(adminCms,
581: contextInfo);
582: }
583:
584: /**
585: * Returns an initialized CmsObject (OpenCms user context) with the user initialized as provided,
586: * with the "Online" project selected and "/" set as the current site root.<p>
587: *
588: * Note: Only the default users 'Guest' and 'Export' can initialized with
589: * this method, all other user names will throw an Exception.<p>
590: *
591: * In order to initialize another user (for example, the {@link CmsDefaultUsers#getUserAdmin()}),
592: * you need to get the 'Guest' user context first, then login the target user with
593: * his user name and password, using {@link CmsObject#loginUser(String, String)}.
594: * There is no way to obtain a user context other then the 'Guest' or 'Export' user
595: * without the users password. This is a security feature.<p>
596: *
597: * @param user the user name to initialize, can only be
598: * {@link org.opencms.db.CmsDefaultUsers#getUserGuest()} or
599: * {@link org.opencms.db.CmsDefaultUsers#getUserExport()}
600: *
601: * @return an initialized CmsObject with the given users permissions
602: *
603: * @throws CmsException if an invalid user name was provided, or if something else goes wrong
604: *
605: * @see org.opencms.db.CmsDefaultUsers#getUserGuest()
606: * @see org.opencms.db.CmsDefaultUsers#getUserExport()
607: * @see OpenCms#initCmsObject(CmsObject)
608: * @see OpenCms#initCmsObject(CmsObject, CmsContextInfo)
609: * @see OpenCms#initCmsObject(String)
610: */
611: public static CmsObject initCmsObject(String user)
612: throws CmsException {
613:
614: return OpenCmsCore.getInstance().initCmsObject(user);
615: }
616:
617: /**
618: * Reads the requested resource from the OpenCms VFS,
619: * and in case a directory name is requested, the default files of the
620: * directory will be looked up and the first match is returned.<p>
621: *
622: * The resource that is returned is always a <code>{@link org.opencms.file.CmsFile}</code>,
623: * even though the content will usually not be loaded in the result. Folders are never returned since
624: * the point of this method is really to load the default file if just a folder name is requested.<p>
625: *
626: * The URI stored in the given OpenCms user context will be changed to the URI of the resource
627: * that was found and returned.<p>
628: *
629: * Implementing and configuring an <code>{@link I_CmsResourceInit}</code> handler
630: * allows to customize the process of default resource selection.<p>
631: *
632: * @param cms the current users OpenCms context
633: * @param resourceName the path of the requested resource in the OpenCms VFS
634: * @param req the current http request
635: * @param res the current http response
636: * @return the requested resource read from the VFS
637: *
638: * @throws CmsException in case the requested file does not exist or the user has insufficient access permissions
639: */
640: public static CmsResource initResource(CmsObject cms,
641: String resourceName, HttpServletRequest req,
642: HttpServletResponse res) throws CmsException {
643:
644: return OpenCmsCore.getInstance().initResource(cms,
645: resourceName, req, res);
646: }
647:
648: /**
649: * Removes a cms event listener.<p>
650: *
651: * @param listener the listener to remove
652: */
653: public static void removeCmsEventListener(
654: I_CmsEventListener listener) {
655:
656: OpenCmsCore.getInstance().getEventManager()
657: .removeCmsEventListener(listener);
658: }
659:
660: /**
661: * This method adds an Object to the OpenCms runtime properties.
662: * The runtime properties can be used to store Objects that are shared
663: * in the whole system.<p>
664: *
665: * @param key the key to add the Object with
666: * @param value the value of the Object to add
667: */
668: public static void setRuntimeProperty(Object key, Object value) {
669:
670: OpenCmsCore.getInstance().setRuntimeProperty(key, value);
671: }
672:
673: /**
674: * Writes the XML configuration for the provided configuration class.<p>
675: *
676: * @param clazz the configuration class to write the XML for
677: */
678: public static void writeConfiguration(Class clazz) {
679:
680: OpenCmsCore.getInstance().writeConfiguration(clazz);
681: }
682: }
|