Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/opencms/core/A_OpenCms.java


1   /*
2   * File   : $Source: /usr/local/cvs/opencms/src/com/opencms/core/Attic/A_OpenCms.java,v $
3   * Date   : $Date: 2003/04/10 15:54:53 $
4   * Version: $Revision: 1.35 $
5   *
6   * This library is part of OpenCms -
7   * the Open Source Content Mananagement System
8   *
9   * Copyright (C) 2001  The OpenCms Group
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 OpenCms, please see the
22  * OpenCms Website: http://www.opencms.org
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27  */
28  
29  package com.opencms.core;
30  
31  import com.opencms.boot.CmsBase;
32  import com.opencms.boot.I_CmsLogChannels;
33  import com.opencms.file.CmsFile;
34  import com.opencms.file.CmsObject;
35  import com.opencms.file.I_CmsRegistry;
36  import com.opencms.file.I_CmsResourceBroker;
37  import com.opencms.flex.CmsEvent;
38  import com.opencms.flex.I_CmsEventListener;
39  
40  import java.util.ArrayList;
41  import java.util.Collections;
42  import java.util.HashMap;
43  import java.util.Hashtable;
44  import java.util.Map;
45  import java.util.Properties;
46  
47  import source.org.apache.java.util.Configurations;
48  
49  /**
50   * Abstract class for the OpenCms "operating system" that provides 
51   * public static methods which can be used by other classes to access 
52   * basic features of OpenCms like logging etc.
53   *
54   * @see OpenCms
55   * 
56   * @author Alexander Lucas
57   * @author Michael Emmerich
58   * @author Alexander Kandzior (a.kandzior@alkacon.com)
59   * 
60   * @version $Revision: 1.35 $ $Date: 2003/04/10 15:54:53 $
61   */
62  public abstract class A_OpenCms implements I_CmsLogChannels {
63  
64      /** The resource-broker to access the database */
65      protected static I_CmsResourceBroker m_resourceBroker = null;
66      
67      /** The filename of the log file */
68      private static String m_logfile;
69  
70      /** List to save the event listeners in */
71      private static java.util.ArrayList m_listeners = new ArrayList();
72              
73      /** A Map for the storage of various runtime properties */
74      private static Map m_runtimeProperties = null;
75  
76      /** The OpenCms configuration read from <code>opencms.properties</code> */
77      private Configurations m_conf = null;
78      
79      /** Default encoding, can be overwritten in "opencms.properties" */
80      private static String m_defaultEncoding = "ISO-8859-1";    
81  
82      /** The version name (including version number) of this OpenCms installation */
83      private static String m_versionName = null;
84      
85      /** The version number of this OpenCms installation */
86      private static String m_versionNumber = null;
87          
88      /** The OpenCms context and servlet path, e.g. <code>/opencms/opencms</code> */   
89    private static String m_openCmsContext = null;
90      
91      /** The default setting for the user language */
92      private static String m_userDefaultLanguage=null;
93  
94      /** The default setting for the user access flags */
95      private static int m_userDefaultaccessFlags = I_CmsConstants.C_ACCESS_DEFAULT_FLAGS;
96         
97     
98      /**
99       * Destructor, should be called when the the class instance is shut down.
100      */
101     abstract void destroy() throws CmsException;
102 
103     /**
104      * Initializes the logging mechanism of OpenCms.
105      * 
106      * @param config The configurations read from <code>opencms.properties</code>
107      */
108     public static void initializeServletLogging(Configurations config) {
109         m_logfile = config.getString("log.file");
110         CmsBase.initializeServletLogging(config);
111     }
112 
113     /**
114      * Returns the filename of the logfile.
115      * 
116      * @return The filename of the logfile.
117      */
118     public static String getLogFileName() {
119         return m_logfile;
120     }
121 
122     /**
123      * Checks if the system logging is active.
124      * 
125      * @return <code>true</code> if the logging is active, <code>false</code> otherwise.
126      */
127     public static boolean isLogging() {
128         return CmsBase.isLogging();    
129     }
130     
131     /**
132      * Checks if the system logging is active for the selected channel.
133      * 
134      * @return <code>true</code> if the logging is active for the channel, <code>false</code> otherwise.
135      */
136     public static boolean isLogging(String channel) {
137         return CmsBase.isLogging(channel);    
138     }    
139     
140     /**
141      * Logs a message into the OpenCms logfile.<p>
142      * 
143      * If the logfile was not initialized (e.g. due tue a missing
144      * ServletConfig while working with the console)
145      * any log output will be written to <code>System.err</code>.
146      * 
147      * @param channel The channel the message is logged into
148      * @param message The message to be logged.
149      */
150     public static void log(String channel, String message) {
151         CmsBase.log(channel, message);
152     }
153 
154     /**
155      * Sets the mimetype of the response.<br>
156      * The mimetype is selected by the file extension of the requested document.
157      * If no available mimetype is found, it is set to the default
158      * "application/octet-stream".
159      *
160      * @param cms The actual OpenCms object.
161      * @param file The requested document.
162      */
163     abstract void setResponse(CmsObject cms, CmsFile file);
164 
165     /**
166      * Selects the appropriate launcher for a given file by analyzing the
167      * file's launcher id and calls the initlaunch() method to initiate the
168      * generating of the output.
169      *
170      * @param cms CmsObject containing all document and user information
171      * @param file CmsFile object representing the selected file.
172      * @throws CmsException In case of problems acessing the resource.
173      */
174     abstract public void showResource(CmsObject cms, CmsFile file) throws CmsException;
175 
176     /**
177      * This method stores sessiondata into the database. It is used
178      * for sessionfailover.
179      *
180      * @param sessionId the id of the session.
181      * @param isNew determines, if the session is new or not.
182      * @return data the sessionData.
183      */
184     abstract void storeSession(String sessionId, Hashtable sessionData) throws CmsException;
185 
186     /**
187      * This method loads old sessiondata from the database. It is used
188      * for sessionfailover.
189      *
190      * @param oldSessionId the id of the old session.
191      * @return The restored sessiondata read from the database.
192      * @throws CmsException In case of problems acessing the database.
193      */
194     abstract Hashtable restoreSession(String oldSessionId) throws CmsException;
195     
196     /**
197      * Reads the current crontable entries from the database and updates the
198      * crontable with them.
199      */
200     abstract void updateCronTable();
201 
202     /**
203      * Starts a schedule job with a correct instantiated CmsObject.
204      * 
205      * @param entry the CmsCronEntry to start.
206      */
207     abstract void startScheduleJob(CmsCronEntry entry);
208     
209     /**       
210      * This method adds an Object to the OpenCms runtime properties.
211      * The runtime properties can be used to store Objects that are shared
212      * in the whole system.<p>
213      *
214      * @since FLEX alpha 1
215      * @param key The key to add the Object with.
216      * @param value The value of the Object to add.
217      */
218     public void setRuntimeProperty(Object key, Object value) {
219         if (m_runtimeProperties == null) {
220             m_runtimeProperties = Collections.synchronizedMap(new HashMap());
221         }
222         m_runtimeProperties.put(key, value);
223     }
224     
225     /** This method looks up a value in the runtime property Map.
226      *
227      * @since FLEX alpha 1
228      * @param key The key to look up in the runtime properties.
229      * @return The value for the key, or null if the key was not found.
230      */
231     public static Object getRuntimeProperty(Object key) {
232         if (m_runtimeProperties == null) return null;
233         return m_runtimeProperties.get(key);
234     }
235     
236     /** This method returns the complete runtime property Map.
237      *
238      * @since FLEX alpha 1
239      * @return The Map of runtime properties.
240      */    
241     public Map getRuntimePropertyMap() {
242         return m_runtimeProperties;
243     }
244 
245     /** This method sets the runtime configuration.
246      *
247      * @since FLEX alpha 1
248      */       
249     public void setConfiguration(Configurations conf) {
250         m_conf = conf;
251     }    
252     
253     /** This method returns the runtime configuration.
254      *
255      * @since FLEX alpha 1
256      * @return The runtime configuration.
257      */       
258     public Configurations getConfiguration() {
259         return m_conf;
260     }
261 
262     /**
263      * Notify all container event listeners that a particular event has
264      * occurred for this Container.  The default implementation performs
265      * this notification synchronously using the calling thread.
266      *
267      * @since FLEX alpha 1
268      * @param cms An initialized CmsObject
269      * @param type Event type
270      * @param data Event data
271      */
272     public static void fireCmsEvent(CmsObject cms, int type, java.util.Map data) {
273        OpenCms.fireCmsEvent( new CmsEvent(cms, type, data) );
274     }    
275     
276     /**
277      * Notify all container event listeners that a particular event has
278      * occurred for this Container.  The default implementation performs
279      * this notification synchronously using the calling thread.
280      *
281      * @since FLEX beta 1
282      * @param cms An initialized CmsObject
283      * @param event A CmsEvent
284      */
285     public static void fireCmsEvent(CmsEvent event) {
286         if (m_listeners.size() < 1)
287             return;
288         I_CmsEventListener list[] = new I_CmsEventListener[0];
289         synchronized (m_listeners) {
290             list = (I_CmsEventListener[]) m_listeners.toArray(list);
291         }
292         for (int i = 0; i < list.length; i++)
293             ((I_CmsEventListener) list[i]).cmsEvent(event);
294     }    
295     
296     /**
297      * Add a cms event listener.
298      *
299      * @since FLEX alpha 1
300      * @param listener The listener to add
301      */
302     public static void addCmsEventListener(I_CmsEventListener listener) {
303         synchronized (m_listeners) {
304             m_listeners.add(listener);
305         }        
306     }    
307     
308     /**
309      * Remove a cms event listener.
310      *
311      * @since FLEX alpha 1
312      * @param listener The listener to add
313      */
314     public static void removeCmsEventListener(I_CmsEventListener listener) {
315         synchronized (m_listeners) {
316             m_listeners.remove(listener);
317         }
318     }
319     
320     /**
321      * Return the OpenCms default character encoding.
322      * The default is set in the "opencms.properties" file.
323      * If this is not set in "opencms.properties" the default 
324      * is "ISO-8859-1". 
325      * 
326      * @return The default encoding (e.g. "UTF-8" or "ISO-8859-1")
327      */
328     public static String getDefaultEncoding() {
329         return m_defaultEncoding;
330     }    
331     
332     /**
333      * Sets the default encoding to the value specified.
334      * 
335      * @param encoding The value to set, e.g. "UTF-8" or "ISO-8859-1".
336      */
337     protected void setDefaultEncoding(String encoding) {
338         m_defaultEncoding = encoding;
339     }
340     
341     
342     /**
343      * Returns a String containing the version information (version name and version number) 
344      * of this OpenCms system.<p>
345      *
346      * @return version a String containnig the version information
347      */
348     public static String getVersionName() {
349         return m_versionName;
350     }        
351     
352     /**
353      * Returns a String containing the version number 
354      * of this OpenCms system.<p>
355      *
356      * @return version a String containnig the version number
357      */    
358     public static String getVersionNumber() {
359         return m_versionNumber;
360     }
361     
362     /**
363      * Initializes the version for this OpenCms, will be called by 
364      * CmsHttpServlet or CmsShell upon system startup.<p>
365      * 
366      * @param o instance of calling object
367      */
368      static void initVersion(Object o) {
369         // read the version-informations from properties, if not done
370         Properties props = new Properties();
371         try {
372             props.load(o.getClass().getClassLoader().getResourceAsStream("com/opencms/core/version.properties"));
373         } catch(java.io.IOException exc) {
374             // ignore this exception - no properties found
375             m_versionName = "unknown";
376             return;
377         }
378         m_versionNumber = props.getProperty("version.number", "5.x");
379         m_versionName = m_versionNumber + " " + props.getProperty("version.name", "??");
380     }
381     
382     /**
383      * Sets the OpenCms request context.<p>
384      * 
385      * @param value the OpenCms request context
386      */
387   protected static void setOpenCmsContext(String value) {
388         if ((value != null) && (value.startsWith("/ROOT"))) {
389             value = value.substring("/ROOT".length());
390         }        
391     m_openCmsContext = value;
392   }   
393     
394     /**
395      * Returns the OpenCms request context.<p>
396      * 
397      * @return String the OpenCms request context
398      */
399     public static String getOpenCmsContext() {
400       if (m_openCmsContext == null) {
401         throw new RuntimeException ("OpenCmsContext not initialised!");
402       }
403       return m_openCmsContext;
404     }
405     
406     /**
407      * Returns the value of the user default language.<p>
408      * 
409      * @return the value of the user default language
410      */
411     public static String getUserDefaultLanguage() {
412         return m_userDefaultLanguage;
413     }
414     
415     /**
416      * Sets the value of the user default language.<p>
417      * 
418      * @param language the new value of the user default language
419      */
420     protected static void setUserDefaultLanguage(String language) {
421         m_userDefaultLanguage = language;
422     }
423     
424     /**
425      * Returns the value for the default user access flags.<p>
426      * 
427      * @return the value for the default user access flags
428      */
429     public static int getUserDefaultAccessFlags() {
430         return m_userDefaultaccessFlags;
431     }
432     
433     /**
434      * Seats the value of the user default access flags.
435      * 
436      * @param flags the new value of the user default access flags
437      */
438     protected static void setUserDefaultAccessFlags(int flags) {
439         m_userDefaultaccessFlags = flags;
440     }
441     
442     /**
443      * Returns the registry to read values from it.<p>
444      * 
445      * You don't have the permissions to write values. 
446      * This is useful for modules to read module-parameters.
447      *
448      * @return The registry to READ values from it.
449      * 
450      * @throws CmsException, if the registry can not be returned.
451      */
452     public static I_CmsRegistry getRegistry() throws CmsException {
453         if (m_resourceBroker == null) return null;
454         return m_resourceBroker.getRegistry(null, null, null);
455     }    
456 }