KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > main > OpenCms


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/main/OpenCms.java,v $
3  * Date : $Date: 2006/05/12 16:05:48 $
4  * Version: $Revision: 1.60 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.main;
33
34 import org.opencms.db.CmsDefaultUsers;
35 import org.opencms.db.CmsLoginManager;
36 import org.opencms.db.CmsSqlManager;
37 import org.opencms.file.CmsObject;
38 import org.opencms.file.CmsResource;
39 import org.opencms.i18n.CmsLocaleManager;
40 import org.opencms.importexport.CmsImportExportManager;
41 import org.opencms.loader.CmsResourceManager;
42 import org.opencms.lock.CmsLockManager;
43 import org.opencms.module.CmsModuleManager;
44 import org.opencms.monitor.CmsMemoryMonitor;
45 import org.opencms.scheduler.CmsScheduleManager;
46 import org.opencms.search.CmsSearchManager;
47 import org.opencms.security.CmsRole;
48 import org.opencms.security.I_CmsPasswordHandler;
49 import org.opencms.security.I_CmsValidationHandler;
50 import org.opencms.site.CmsSiteManager;
51 import org.opencms.staticexport.CmsLinkManager;
52 import org.opencms.staticexport.CmsStaticExportManager;
53 import org.opencms.workplace.CmsWorkplaceManager;
54 import org.opencms.xml.CmsXmlContentTypeManager;
55
56 import java.util.List JavaDoc;
57 import java.util.Map JavaDoc;
58 import java.util.Set JavaDoc;
59
60 import javax.servlet.http.HttpServletRequest JavaDoc;
61 import javax.servlet.http.HttpServletResponse JavaDoc;
62
63 import org.apache.commons.logging.Log;
64
65 /**
66  * The OpenCms "operating system" that provides
67  * public static methods which can be used by other classes to access
68  * basic system features of OpenCms like logging etc.<p>
69  *
70  * This Object provides singleton access to the initialized OpenCms runtime system.
71  * Some methods are for internal or advanced use only, but others are of also of interest
72  * for general OpenCms development.<p>
73  *
74  * For example, to generate a new instance of <code>{@link org.opencms.file.CmsObject}</code> class in your application,
75  * use <code>{@link org.opencms.main.OpenCms#initCmsObject(String)}</code>. The argument String should be
76  * the name of the guest user, usually "Guest" and more formally obtained by <code>{@link org.opencms.db.CmsDefaultUsers#getUserGuest()}</code>.
77  * This will give you an initialized context with guest user permissions.
78  * Then use <code>{@link CmsObject#loginUser(String, String)}</code> to log in the user you want.
79  * Obviously you need the password for the new user.<p>
80  *
81  * Using <code>{@link #getSiteManager()}</code> you can obtain the initialized <code>{@link org.opencms.site.CmsSiteManager}</code>
82  * which provides information about the sites configured in the running OpenCms instance.<p>
83  *
84  * The <code>{@link org.opencms.db.CmsDefaultUsers}</code> instance returned by <code>{@link #getDefaultUsers()}</code>
85  * provides information about the names of the OpenCms default users.<p>
86  *
87  * Other objects of note that can be obtained by this class include the <code>{@link org.opencms.module.CmsModuleManager}</code>
88  * or the <code>{@link org.opencms.scheduler.CmsScheduleManager}</code>.<p>
89  *
90  * When using the instances returned by this object, keep in mind that applying changes to these may alter the basic OpenCms
91  * system configuration, which in turn may affect the systems performance or stability.<p>
92  *
93  * @author Alexander Kandzior
94  *
95  * @version $Revision: 1.60 $
96  *
97  * @since 6.0.0
98  */

99 public final class OpenCms {
100
101     /** Runlevel 0: System is offline. */
102     public static final int RUNLEVEL_0_OFFLINE = 0;
103
104     /** Runlevel 1: Core object created, no database (some test cases run in this level). */
105     public static final int RUNLEVEL_1_CORE_OBJECT = 1;
106
107     /** Runlevel 2: Initializing the system, required since this may take some seconds because of database connections. */
108     public static final int RUNLEVEL_2_INITIALIZING = 2;
109
110     /** Runlevel 3: Shell access to the database possible, but no servlet context available. */
111     public static final int RUNLEVEL_3_SHELL_ACCESS = 3;
112
113     /** Runlevel 4: Final runlevel where database and servlet are initialized. */
114     public static final int RUNLEVEL_4_SERVLET_ACCESS = 4;
115
116     /**
117      * The public contructor is hidden to prevent generation of instances of this class.<p>
118      */

119     private OpenCms() {
120
121         // empty
122
}
123
124     /**
125      * Add a cms event listener that listens to all events.<p>
126      *
127      * @param listener the listener to add
128      */

129     public static void addCmsEventListener(I_CmsEventListener listener) {
130
131         OpenCmsCore.getInstance().getEventManager().addCmsEventListener(listener);
132     }
133
134     /**
135      * Add a cms event listener that listens only to particular events.<p>
136      *
137      * @param listener the listener to add
138      * @param eventTypes the events to listen for
139      */

140     public static void addCmsEventListener(I_CmsEventListener listener, int[] eventTypes) {
141
142         OpenCmsCore.getInstance().getEventManager().addCmsEventListener(listener, eventTypes);
143     }
144
145     /**
146      * Notify all event listeners that a particular event has occurred.<p>
147      *
148      * @param event a CmsEvent
149      */

150     public static void fireCmsEvent(CmsEvent event) {
151
152         OpenCmsCore.getInstance().getEventManager().fireEvent(event);
153     }
154
155     /**
156      * Notify all event listeners that a particular event has occurred.<p>
157      *
158      * The event will be given to all registered <code>{@link I_CmsEventListener}</code> objects.<p>
159      *
160      * @param type event type
161      * @param data event data
162      */

163     public static void fireCmsEvent(int type, Map JavaDoc data) {
164
165         OpenCmsCore.getInstance().getEventManager().fireEvent(type, data);
166     }
167
168     /**
169      * Returns the configured list of default directory file names (instances of <code>{@link String}</code>).<p>
170      *
171      * Caution: This list can not be modified.<p>
172      *
173      * @return the configured list of default directory file names
174      */

175     public static List JavaDoc getDefaultFiles() {
176
177         return OpenCmsCore.getInstance().getDefaultFiles();
178     }
179
180     /**
181      * Returns the default user and group name configuration.<p>
182      *
183      * @return the default user and group name configuration
184      */

185     public static CmsDefaultUsers getDefaultUsers() {
186
187         return OpenCmsCore.getInstance().getDefaultUsers();
188     }
189
190     /**
191      * Returns the event manger that handles all OpenCms events.<p>
192      *
193      * @return the event manger that handles all OpenCms events
194      */

195     public static CmsEventManager getEventManager() {
196
197         return OpenCmsCore.getInstance().getEventManager();
198     }
199
200     /**
201      * Returns the configured export points,
202      * the returned set being an unmodifiable set.<p>
203      *
204      * @return an unmodifiable set of the configured export points
205      */

206     public static Set JavaDoc getExportPoints() {
207
208         return OpenCmsCore.getInstance().getExportPoints();
209     }
210
211     /**
212      * Returns the initialized import/export manager,
213      * which contains information about how to handle imported resources.<p>
214      *
215      * @return the initialized import/export manager
216      */

217     public static CmsImportExportManager getImportExportManager() {
218
219         return OpenCmsCore.getInstance().getImportExportManager();
220     }
221
222     /**
223      * Returns the link manager to resolve links in &lt;link&gt; tags.<p>
224      *
225      * @return the link manager to resolve links in &lt;link&gt; tags
226      */

227     public static CmsLinkManager getLinkManager() {
228
229         return OpenCmsCore.getInstance().getLinkManager();
230     }
231
232     /**
233      * Returns the locale manager used for obtaining the current locale.<p>
234      *
235      * @return the locale manager
236      */

237     public static CmsLocaleManager getLocaleManager() {
238
239         return OpenCmsCore.getInstance().getLocaleManager();
240     }
241
242     /**
243      * Returns the lock manager used for the locking mechanism.<p>
244      *
245      * @return the lock manager used for the locking mechanism
246      */

247     public static CmsLockManager getLockManager() {
248
249         return OpenCmsCore.getInstance().getLockManager();
250     }
251
252     /**
253      * Returns the log for the selected object.<p>
254      *
255      * If the provided object is a String, this String will
256      * be used as channel name. Otherwise the objects
257      * class name will be used as channel name.<p>
258      *
259      * @param obj the object channel to use
260      * @return the log for the selected object channel
261      */

262     public static Log getLog(Object JavaDoc obj) {
263
264         return CmsLog.getLog(obj);
265     }
266
267     /**
268      * Returns the login manager used to check if a login is possible.<p>
269      *
270      * @return the login manager
271      */

272     public static CmsLoginManager getLoginManager() {
273
274         return OpenCmsCore.getInstance().getLoginManager();
275     }
276
277     /**
278      * Returns the memory monitor.<p>
279      *
280      * @return the memory monitor
281      */

282     public static CmsMemoryMonitor getMemoryMonitor() {
283
284         return OpenCmsCore.getInstance().getMemoryMonitor();
285     }
286
287     /**
288      * Returns the module manager.<p>
289      *
290      * @return the module manager
291      */

292     public static CmsModuleManager getModuleManager() {
293
294         return OpenCmsCore.getInstance().getModuleManager();
295     }
296
297     /**
298      * Returns the password handler.<p>
299      *
300      * @return the password handler
301      */

302     public static I_CmsPasswordHandler getPasswordHandler() {
303
304         return OpenCmsCore.getInstance().getPasswordHandler();
305     }
306
307     /**
308      * Returns the resource manager.<p>
309      *
310      * @return the resource manager
311      */

312     public static CmsResourceManager getResourceManager() {
313
314         return OpenCmsCore.getInstance().getResourceManager();
315     }
316
317     /**
318      * Returns the current OpenCms run level.<p>
319      *
320      * The following runlevels are defined:
321      * <dl>
322      * <dt>Runlevel {@link OpenCms#RUNLEVEL_0_OFFLINE}:</dt><dd>
323      * OpenCms is in the process of being shut down, the system is offline.</dd>
324      *
325      * <dt>Runlevel {@link OpenCms#RUNLEVEL_1_CORE_OBJECT}:</dt><dd>
326      * OpenCms instance available, but configuration has not been processed.
327      * No database or VFS available.</dd>
328      *
329      * <dt>Runlevel {@link OpenCms#RUNLEVEL_2_INITIALIZING}:</dt><dd>
330      * OpenCms is initializing, but the process is not finished.
331      * The database with the VFS is currently being connected but can't be accessed.</dd>
332      *
333      * <dt>Runlevel {@link OpenCms#RUNLEVEL_3_SHELL_ACCESS}:</dt><dd>
334      * OpenCms database and VFS available, but http processing (i.e. servlet) not initialized.
335      * This is the runlevel the OpenCms shell operates in.</dd>
336      *
337      * <dt>Runlevel {@link OpenCms#RUNLEVEL_4_SERVLET_ACCESS}:</dt><dd>
338      * OpenCms fully initialized, servlet and database available.
339      * This is the "default" when OpenCms is in normal operation.</dd>
340      * </dl>
341      *
342      * @return the OpenCms run level
343      */

344     public static int getRunLevel() {
345
346         return OpenCmsCore.getInstance().getRunLevel();
347     }
348
349     /**
350      * Looks up a value in the runtime property Map.<p>
351      *
352      * @param key the key to look up in the runtime properties
353      * @return the value for the key, or null if the key was not found
354      */

355     public static Object JavaDoc getRuntimeProperty(Object JavaDoc key) {
356
357         return OpenCmsCore.getInstance().getRuntimeProperty(key);
358     }
359
360     /**
361      * Returns the configured schedule manager.<p>
362      *
363      * @return the configured schedule manager
364      */

365     public static CmsScheduleManager getScheduleManager() {
366
367         return OpenCmsCore.getInstance().getScheduleManager();
368     }
369
370     /**
371      * Returns the initialized search manager,
372      * which provides indexing and searching operations.<p>
373      *
374      * @return the initialized search manager
375      */

376     public static CmsSearchManager getSearchManager() {
377
378         return OpenCmsCore.getInstance().getSearchManager();
379     }
380
381     /**
382      * Returns the session manager that keeps track of the active users.<p>
383      *
384      * @return the session manager that keeps track of the active users
385      */

386     public static CmsSessionManager getSessionManager() {
387
388         return OpenCmsCore.getInstance().getSessionManager();
389     }
390
391     /**
392      * Returns the initialized site manager,
393      * which contains information about all configured sites.<p>
394      *
395      * @return the initialized site manager
396      */

397     public static CmsSiteManager getSiteManager() {
398
399         return OpenCmsCore.getInstance().getSiteManager();
400     }
401
402     /**
403      * Returns an instance of the common sql manager.<p>
404      *
405      * @return an instance of the common sql manager
406      */

407     public static CmsSqlManager getSqlManager() {
408
409         return OpenCmsCore.getInstance().getSqlManager();
410     }
411
412     /**
413      * Returns the properties for the static export.<p>
414      *
415      * @return the properties for the static export
416      */

417     public static CmsStaticExportManager getStaticExportManager() {
418
419         return OpenCmsCore.getInstance().getStaticExportManager();
420     }
421
422     /**
423      * Returns the system information storage.<p>
424      *
425      * @return the system information storage
426      */

427     public static CmsSystemInfo getSystemInfo() {
428
429         return OpenCmsCore.getInstance().getSystemInfo();
430     }
431
432     /**
433      * Returns the list of system defined roles (instances of <code>{@link CmsRole}</code>).<p>
434      *
435      * Caution: This list can not be modified.<p>
436      *
437      * @return the list of system defined roles
438      */

439     public static List JavaDoc getSystemRoles() {
440
441         return CmsRole.getSystemRoles();
442     }
443
444     /**
445      * Returns the OpenCms Thread store.<p>
446      *
447      * @return the OpenCms Thread store
448      */

449     public static CmsThreadStore getThreadStore() {
450
451         return OpenCmsCore.getInstance().getThreadStore();
452     }
453
454     /**
455      * Returns the runtime validation handler.<p>
456      *
457      * @return the validation handler
458      */

459     public static I_CmsValidationHandler getValidationHandler() {
460
461         return OpenCmsCore.getInstance().getValidationHandler();
462     }
463
464     /**
465      * Returns the initialized workplace manager,
466      * which contains information about the global workplace settings.<p>
467      *
468      * @return the initialized workplace manager
469      */

470     public static CmsWorkplaceManager getWorkplaceManager() {
471
472         return OpenCmsCore.getInstance().getWorkplaceManager();
473     }
474
475     /**
476      * Returns the XML content type manager.<p>
477      *
478      * @return the XML content type manager
479      */

480     public static CmsXmlContentTypeManager getXmlContentTypeManager() {
481
482         return OpenCmsCore.getInstance().getXmlContentTypeManager();
483     }
484
485     /**
486      * Returns an independent copy of the provided CmsObject.<p>
487      *
488      * This can be useful in case a permanent reference to a CmsObject is stored.
489      * Changing the request context values (for example project, siteroot) in the new CmsObject
490      * will have no side effects to the CmsObject it was copied form.<p>
491      *
492      * @param cms the CmsObject to create a copy of
493      *
494      * @return an independent copy of the provided CmsObject
495      *
496      * @throws CmsException in case the intialization failed
497      *
498      * @see OpenCms#initCmsObject(CmsObject)
499      * @see OpenCms#initCmsObject(CmsObject, CmsContextInfo)
500      * @see OpenCms#initCmsObject(String)
501      */

502     public static CmsObject initCmsObject(CmsObject cms) throws CmsException {
503
504         return OpenCmsCore.getInstance().initCmsObject(cms);
505     }
506
507     /**
508      * Returns an initialized CmsObject with the user and context initialized as provided.<p>
509      *
510      * Note: Only if the provided <code>adminCms</code> CmsObject has admin permissions,
511      * this method allows the creation a CmsObject for any existing user. Otherwise
512      * only the default users 'Guest' and 'Export' can initialized with
513      * this method, all other user names will throw an Exception.<p>
514      *
515      * @param adminCms must either be initialized with "Admin" permissions, or null
516      * @param contextInfo the context info to create a CmsObject for
517      *
518      * @return an initialized CmsObject with the given users permissions
519      *
520      * @throws CmsException if an invalid user name was provided, or if something else goes wrong
521      *
522      * @see org.opencms.db.CmsDefaultUsers#getUserGuest()
523      * @see org.opencms.db.CmsDefaultUsers#getUserExport()
524      * @see OpenCms#initCmsObject(CmsObject)
525      * @see OpenCms#initCmsObject(CmsObject, CmsContextInfo)
526      * @see OpenCms#initCmsObject(String)
527      */

528     public static CmsObject initCmsObject(CmsObject adminCms, CmsContextInfo contextInfo) throws CmsException {
529
530         return OpenCmsCore.getInstance().initCmsObject(adminCms, contextInfo);
531     }
532
533     /**
534      * Returns an initialized CmsObject (OpenCms user context) with the user initialized as provided,
535      * with the "Online" project selected and "/" set as the current site root.<p>
536      *
537      * Note: Only the default users 'Guest' and 'Export' can initialized with
538      * this method, all other user names will throw an Exception.<p>
539      *
540      * In order to initialize another user (for example, the {@link CmsDefaultUsers#getUserAdmin()}),
541      * you need to get the 'Guest' user context first, then login the target user with
542      * his user name and password, using {@link CmsObject#loginUser(String, String)}.
543      * There is no way to obtain a user context other then the 'Guest' or 'Export' user
544      * without the users password. This is a security feature.<p>
545      *
546      * @param user the user name to initialize, can only be
547      * {@link org.opencms.db.CmsDefaultUsers#getUserGuest()} or
548      * {@link org.opencms.db.CmsDefaultUsers#getUserExport()}
549      *
550      * @return an initialized CmsObject with the given users permissions
551      *
552      * @throws CmsException if an invalid user name was provided, or if something else goes wrong
553      *
554      * @see org.opencms.db.CmsDefaultUsers#getUserGuest()
555      * @see org.opencms.db.CmsDefaultUsers#getUserExport()
556      * @see OpenCms#initCmsObject(CmsObject)
557      * @see OpenCms#initCmsObject(CmsObject, CmsContextInfo)
558      * @see OpenCms#initCmsObject(String)
559      */

560     public static CmsObject initCmsObject(String JavaDoc user) throws CmsException {
561
562         return OpenCmsCore.getInstance().initCmsObject(user);
563     }
564
565     /**
566      * Reads the requested resource from the OpenCms VFS,
567      * and in case a directory name is requested, the default files of the
568      * directory will be looked up and the first match is returned.<p>
569      *
570      * The resource that is returned is always a <code>{@link org.opencms.file.CmsFile}</code>,
571      * even though the content will usually not be loaded in the result. Folders are never returned since
572      * the point of this method is really to load the default file if just a folder name is requested.<p>
573      *
574      * The URI stored in the given OpenCms user context will be changed to the URI of the resource
575      * that was found and returned.<p>
576      *
577      * Implementing and configuring an <code>{@link I_CmsResourceInit}</code> handler
578      * allows to customize the process of default resouce selection.<p>
579      *
580      * @param cms the current users OpenCms context
581      * @param resourceName the path of the requested resource in the OpenCms VFS
582      * @param req the current http request
583      * @param res the current http response
584      * @return the requested resource read from the VFS
585      *
586      * @throws CmsException in case the requested file does not exist or the user has insufficient access permissions
587      */

588     public static CmsResource initResource(
589         CmsObject cms,
590         String JavaDoc resourceName,
591         HttpServletRequest JavaDoc req,
592         HttpServletResponse JavaDoc res) throws CmsException {
593
594         return OpenCmsCore.getInstance().initResource(cms, resourceName, req, res);
595     }
596
597     /**
598      * Removes a cms event listener.<p>
599      *
600      * @param listener the listener to remove
601      */

602     public static void removeCmsEventListener(I_CmsEventListener listener) {
603
604         OpenCmsCore.getInstance().getEventManager().removeCmsEventListener(listener);
605     }
606
607     /**
608      * This method adds an Object to the OpenCms runtime properties.
609      * The runtime properties can be used to store Objects that are shared
610      * in the whole system.<p>
611      *
612      * @param key the key to add the Object with
613      * @param value the value of the Object to add
614      */

615     public static void setRuntimeProperty(Object JavaDoc key, Object JavaDoc value) {
616
617         OpenCmsCore.getInstance().setRuntimeProperty(key, value);
618     }
619
620     /**
621      * Writes the XML configuration for the provided configuration class.<p>
622      *
623      * @param clazz the configuration class to write the XML for
624      */

625     public static void writeConfiguration(Class JavaDoc clazz) {
626
627         OpenCmsCore.getInstance().writeConfiguration(clazz);
628     }
629 }
Popular Tags