KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/main/CmsSystemInfo.java,v $
3  * Date : $Date: 2006/04/28 15:20:52 $
4  * Version: $Revision: 1.49 $
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.i18n.CmsEncoder;
35 import org.opencms.mail.CmsMailSettings;
36 import org.opencms.util.CmsFileUtil;
37
38 import java.io.File JavaDoc;
39 import java.util.Properties JavaDoc;
40
41 /**
42  * Provides access to system wide "read only" information.<p>
43  *
44  * Regarding the naming conventions used, this comes straight from the Servlet Sepc v2.4:<p>
45  *
46  * <i>SRV.3.1 Introduction to the ServletContext Interface<br>
47  * [...] A ServletContext is rooted at a known path within a web server. For example
48  * a servlet context could be located at http://www.mycorp.com/catalog. All
49  * requests that begin with the /catalog request path, known as the <b>context path</b>, are
50  * routed to the <b>web application</b> associated with the ServletContext.</i><p>
51  *
52  * @author Alexander Kandzior
53  *
54  * @version $Revision: 1.49 $
55  *
56  * @since 6.0.0
57  */

58 public class CmsSystemInfo {
59
60     /** The name of the opencms.properties file. */
61     public static final String JavaDoc FILE_PROPERTIES = "opencms.properties";
62
63     /** Path to the "config" folder relative to the "WEB-INF" directory of the application. */
64     public static final String JavaDoc FOLDER_CONFIG = "config" + File.separatorChar;
65
66     /** The name of the module folder in the package path. */
67     public static final String JavaDoc FOLDER_MODULES = "modules" + File.separatorChar;
68
69     /** Path to the "packages" folder relative to the "WEB-INF" directory of the application. */
70     public static final String JavaDoc FOLDER_PACKAGES = "packages" + File.separatorChar;
71
72     /** Default encoding. */
73     private static final String JavaDoc DEFAULT_ENCODING = CmsEncoder.ENCODING_UTF_8;
74
75     /** Static version number to use if version.properties can not be read. */
76     private static final String JavaDoc DEFAULT_VERSION_NUMBER = "6.2.x";
77
78     /** The abolute path to the "opencms.properties" configuration file (in the "real" file system). */
79     private String JavaDoc m_configurationFileRfsPath;
80
81     /** The web application context path. */
82     private String JavaDoc m_contextPath;
83
84     /** Default encoding, can be overwritten in "opencms.properties". */
85     private String JavaDoc m_defaultEncoding;
86
87     /** The default web application (usually "ROOT"). */
88     private String JavaDoc m_defaultWebApplicationName;
89
90     /** The HTTP basic authentication settings. */
91     private CmsHttpAuthenticationSettings m_httpAuthenticationSettings;
92
93     /** The settings for the internal OpenCms email service. */
94     private CmsMailSettings m_mailSettings;
95
96     /** The project in which timestamps for the content notification are read. */
97     private String JavaDoc m_notificationProject;
98
99     /** The duration after which responsibles will be notified about out-dated content (in days). */
100     private int m_notificationTime;
101
102     /** The OpenCms context and servlet path, e.g. <code>/opencms/opencms</code>. */
103     private String JavaDoc m_openCmsContext;
104
105     /** The abolute path to the "packages" folder (in the "real" file system). */
106     private String JavaDoc m_packagesRfsPath;
107
108     /** The name of the OpenCms server. */
109     private String JavaDoc m_serverName;
110
111     /** The servlet path for the OpenCms servlet. */
112     private String JavaDoc m_servletPath;
113
114     /** The startup time of this OpenCms instance. */
115     private long m_startupTime;
116
117     /** The version identifier of this OpenCms installation, contains "OpenCms/" and the version number. */
118     private String JavaDoc m_version;
119
120     /** Indicates if the version history is enabled. */
121     private boolean m_versionHistoryEnabled;
122
123     /** The maximum number of entries in the version history (per resource). */
124     private int m_versionHistoryMaxCount;
125
126     /** The version number of this OpenCms installation. */
127     private String JavaDoc m_versionNumber;
128
129     /** The web application name. */
130     private String JavaDoc m_webApplicationName;
131
132     /** The OpenCms web application servlet container folder path (in the "real" file system). */
133     private String JavaDoc m_webApplicationRfsPath;
134
135     /** The OpenCms web application "WEB-INF" path (in the "real" file system). */
136     private String JavaDoc m_webInfRfsPath;
137
138     /**
139      * Creates a new system info container.<p>
140      */

141     public CmsSystemInfo() {
142
143         // set startup time
144
m_startupTime = System.currentTimeMillis();
145         // init version onformation
146
initVersion();
147         // set default encoding (will be changed again later when properties have been read)
148
m_defaultEncoding = DEFAULT_ENCODING.intern();
149     }
150
151     /**
152      * Returns an absolute path (to a directory or a file in the "real" file system) from a path relative to
153      * the web application folder of OpenCms.<p>
154      *
155      * If the provided path is already absolute, then it is returned unchanged.
156      * If the provided path is a folder, the result will always end with a folder separator.<p>
157      *
158      * @param path the path (relative) to generate an absolute path from
159      * @return an absolute path (to a directory or a file) from a path relative to the web application folder of OpenCms
160      */

161     public String JavaDoc getAbsoluteRfsPathRelativeToWebApplication(String JavaDoc path) {
162
163         if ((path == null) || (getWebApplicationRfsPath() == null)) {
164             return null;
165         }
166         // check for absolute path is system depended, let's just use the standard check
167
File JavaDoc f = new File JavaDoc(path);
168         if (f.isAbsolute()) {
169             // apparently this is an absolute path already
170
path = f.getAbsolutePath();
171             if (f.isDirectory() && !path.endsWith(File.separator)) {
172                 // make sure all folder paths end with a separator
173
path = path.concat(File.separator);
174             }
175             return path;
176         }
177         return CmsFileUtil.normalizePath(getWebApplicationRfsPath() + path);
178     }
179
180     /**
181      * Returns an absolute path (to a directory or a file in the "real" file system) from a path relative to
182      * the "WEB-INF" folder of the OpenCms web application.<p>
183      *
184      * If the provided path is already absolute, then it is returned unchanged.<p>
185      *
186      * @param path the path (relative) to generate an absolute path from
187      * @return an absolute path (to a directory or a file) from a path relative to the "WEB-INF" folder
188      */

189     public String JavaDoc getAbsoluteRfsPathRelativeToWebInf(String JavaDoc path) {
190
191         if (path == null) {
192             return null;
193         }
194         // check for absolute path is system depended, let's just use the standard check
195
File JavaDoc f = new File JavaDoc(path);
196         if (f.isAbsolute()) {
197             // apparently this is an absolute path already
198
return f.getAbsolutePath();
199         }
200         return CmsFileUtil.normalizePath(getWebInfRfsPath() + path);
201     }
202
203     /**
204      * Returns the abolute path to the "opencms.properties" configuration file (in the "real" file system).<p>
205      *
206      * @return the abolute path to the "opencms.properties" configuration file
207      */

208     public String JavaDoc getConfigurationFileRfsPath() {
209
210         if (m_configurationFileRfsPath == null) {
211             m_configurationFileRfsPath = getAbsoluteRfsPathRelativeToWebInf(FOLDER_CONFIG + FILE_PROPERTIES);
212         }
213         return m_configurationFileRfsPath;
214     }
215
216     /**
217      * Returns the web application context path, e.g. "" (empty String) if the web application
218      * is the default web application (usually "ROOT"), or "/opencms" if the web application
219      * is called "opencms".<p>
220      *
221      * <i>From the Java Servlet Sepcecification v2.4:</i><br>
222      * <b>Context Path:</b> The path prefix associated with the ServletContext that this
223      * servlet is a part of. If this context is the "default" context rooted at the base of
224      * the web server's URL namespace, this path will be an empty string. Otherwise,
225      * if the context is not rooted at the root of the server's namespace, the path starts
226      * with a "/" character but does not end with a "/" character.<p>
227      *
228      * @return the web application context path
229      * @see #getWebApplicationName()
230      * @see #getServletPath()
231      * @see #getOpenCmsContext()
232      */

233     public String JavaDoc getContextPath() {
234
235         return m_contextPath;
236     }
237
238     /**
239      * Return the OpenCms default character encoding.<p>
240      *
241      * The default is set in the "opencms.properties" file.
242      * If this is not set in "opencms.properties" the default
243      * is "UTF-8".<p>
244      *
245      * @return the default encoding, e.g. "UTF-8" or "ISO-8859-1"
246      */

247     public String JavaDoc getDefaultEncoding() {
248
249         return m_defaultEncoding;
250     }
251
252     /**
253      * Returns the default web application name (usually "ROOT").<p>
254      *
255      * @return the default web application name
256      */

257     public String JavaDoc getDefaultWebApplicationName() {
258
259         return m_defaultWebApplicationName;
260     }
261
262     /**
263      * Returns the HTTP authentication settings.<p>
264      *
265      * @return the HTTP authentication settings
266      */

267     public CmsHttpAuthenticationSettings getHttpAuthenticationSettings() {
268
269         return m_httpAuthenticationSettings;
270     }
271
272     /**
273      * Returns the filename of the logfile (in the "real" file system).<p>
274      *
275      * If the method returns <code>null</code>, this means that the log
276      * file is not managed by OpenCms.<p>
277      *
278      * @return the filename of the logfile (in the "real" file system)
279      */

280     public String JavaDoc getLogFileRfsPath() {
281
282         return CmsLog.getLogFileRfsPath();
283     }
284
285     /**
286      * Returns the settings for the internal OpenCms email service.<p>
287      *
288      * @return the settings for the internal OpenCms email service
289      */

290     public CmsMailSettings getMailSettings() {
291
292         return m_mailSettings;
293     }
294
295     /**
296      * Returns the project in which timestamps for the content notification are read.<p>
297      *
298      * @return the project in which timestamps for the content notification are read
299      */

300     public String JavaDoc getNotificationProject() {
301
302         return m_notificationProject;
303     }
304
305     /**
306      * Returns the duration after which responsibles will be notified about out-dated content (in days).<p>
307      *
308      * @return the duration after which responsibles will be notified about out-dated content
309      */

310     public int getNotificationTime() {
311
312         return m_notificationTime;
313     }
314
315     /**
316      * Returns the OpenCms request context, e.g. "/opencms/opencms".<p>
317      *
318      * The OpenCms context will always start with a "/" and never have a trailing "/".
319      * The OpenCms context is identical to <code>getContexPath() + getServletPath()</code>.<p>
320      *
321      * @return the OpenCms request context, e.g. "/opencms/opencms"
322      * @see #getContextPath()
323      * @see #getServletPath()
324      */

325     public String JavaDoc getOpenCmsContext() {
326
327         return m_openCmsContext;
328     }
329
330     /**
331      * Returns the abolute path to the "packages" folder (in the "real" file system).<p>
332      *
333      * @return the abolute path to the "packages" folder
334      */

335     public String JavaDoc getPackagesRfsPath() {
336
337         if (m_packagesRfsPath == null) {
338             m_packagesRfsPath = getAbsoluteRfsPathRelativeToWebInf(CmsSystemInfo.FOLDER_PACKAGES);
339         }
340         return m_packagesRfsPath;
341     }
342
343     /**
344      * Returns the time this OpenCms instance is running in miliseconds.<p>
345      *
346      * @return the time this OpenCms instance is running in miliseconds
347      */

348     public long getRuntime() {
349
350         return System.currentTimeMillis() - m_startupTime;
351     }
352
353     /**
354      * Returns the OpenCms server name, e.g. "OpenCmsServer".<p>
355      *
356      * The server name is set in <code>opencms.properties</code>.
357      * It is not related to any DNS name the server might also have.
358      * The server name is usefull e.g. in a cluster to distinguish different servers,
359      * or if you compare logfiles from multiple servers.<p>
360      *
361      * @return the OpenCms server name
362      */

363     public String JavaDoc getServerName() {
364
365         return m_serverName;
366     }
367
368     /**
369      * Returns the OpenCms servlet path, e.g. "/opencms".<p>
370      *
371      * <i>From the Java Servlet Sepcecification v2.4:</i><br>
372      * <b>Servlet Path:</b> The path section that directly corresponds to the mapping
373      * which activated this request. This path starts with a?/? character except in the
374      * case where the request is matched with the ?/*? pattern, in which case it is the
375      * empty string.<p>
376      *
377      * @return the OpenCms servlet path
378      * @see #getContextPath()
379      * @see #getWebApplicationName()
380      * @see #getOpenCmsContext()
381      */

382     public String JavaDoc getServletPath() {
383
384         return m_servletPath;
385     }
386
387     /**
388      * Returns the time this OpenCms instance was started in miliseconds.<p>
389      *
390      * @return the time this OpenCms instance was started in miliseconds
391      */

392     public long getStartupTime() {
393
394         return m_startupTime;
395     }
396
397     /**
398      * Returns the identifier "OpenCms/" plus the OpenCms version number.<p>
399      *
400      * This information is used for example to identify OpenCms in http response headers.<p>
401      *
402      * @return the identifier "OpenCms/" plus the OpenCms version number
403      */

404     public String JavaDoc getVersion() {
405
406         return m_version;
407     }
408
409     /**
410      * Returns the maximum number of versions that are kept per file in the VFS version history.<p>
411      *
412      * If the version history is disabled, this setting has no effect.<p>
413      *
414      * @return the maximum number of versions that are kept per file
415      * @see #isVersionHistoryEnabled()
416      */

417     public int getVersionHistoryMaxCount() {
418
419         return m_versionHistoryMaxCount;
420     }
421
422     /**
423      * Returns the version name (that is the version number) of this OpenCms system.<p>
424      *
425      * @return the version name (that is the version number) of this OpenCms system
426      */

427     public String JavaDoc getVersionName() {
428
429         return m_versionNumber;
430     }
431
432     /**
433      * Returns the OpenCms web application name, e.g. "opencms" or "ROOT" (no leading or trainling "/").<p>
434      *
435      * The web application name is stored for informational purposes only.
436      * If you want to construct an URI, use either {@link #getContextPath()} and
437      * {@link #getServletPath()}, or for links to the OpenCms VFS use {@link #getOpenCmsContext()}.<p>
438      *
439      * @return the OpenCms web application name
440      * @see #getContextPath()
441      * @see #getServletPath()
442      * @see #getOpenCmsContext()
443      */

444     public String JavaDoc getWebApplicationName() {
445
446         return m_webApplicationName;
447     }
448
449     /**
450      * Returns the OpenCms web application folder in the servlet container.<p>
451      *
452      * @return the OpenCms web application folder in the servlet container
453      */

454     public String JavaDoc getWebApplicationRfsPath() {
455
456         return m_webApplicationRfsPath;
457     }
458
459     /**
460      * Returns the OpenCms web application "WEB-INF" directory path.<p>
461      *
462      * @return the OpenCms web application "WEB-INF" directory path
463      */

464     public String JavaDoc getWebInfRfsPath() {
465
466         return m_webInfRfsPath;
467     }
468
469     /**
470      * Returns if the VFS version history is enabled.<p>
471      *
472      * @return if the VFS version history is enabled
473      */

474     public boolean isVersionHistoryEnabled() {
475
476         return m_versionHistoryEnabled;
477     }
478
479     /**
480      * Returns if versions in the VFS version history should be kept
481      * after a resource is deleted.<p>
482      *
483      * @return if versions in the VFS version history should be kept
484      */

485     public boolean keepVersionHistory() {
486
487         // TODO: make configurable
488
return true;
489     }
490
491     /**
492      * Sets the project in which timestamps for the content notification are read.<p>
493      *
494      * @param notificationProject the project in which timestamps for the content notification are read
495      */

496     public void setNotificationProject(String JavaDoc notificationProject) {
497
498         m_notificationProject = notificationProject;
499     }
500
501     /**
502      * Sets the duration after which responsibles will be notified about out-dated content (in days).<p>
503      *
504      * @param notificationTime the duration after which responsibles will be notified about out-dated content
505      */

506     public void setNotificationTime(int notificationTime) {
507
508         m_notificationTime = notificationTime;
509     }
510
511     /**
512      * VFS version history settings are set here.<p>
513      *
514      * @param historyEnabled if true the history is enabled
515      * @param historyMaxCount the maximum number of versions that are kept per VFS resource
516      */

517     public void setVersionHistorySettings(boolean historyEnabled, int historyMaxCount) {
518
519         m_versionHistoryEnabled = historyEnabled;
520         m_versionHistoryMaxCount = historyMaxCount;
521     }
522
523     /**
524      * Sets the OpenCms web application "WEB-INF" directory path (in the "real" file system).<p>
525      *
526      * @param webInfRfsPath the OpenCms web application "WEB-INF" path in the "real" file system) to set
527      * @param servletMapping the OpenCms servlet mapping (e.g. "/opencms/*")
528      * @param webApplicationContext the name/path of the OpenCms web application context (optional, will be calculated form the path if null)
529      * @param defaultWebApplication the default web application name (usually "ROOT")
530      */

531     protected void init(
532         String JavaDoc webInfRfsPath,
533         String JavaDoc servletMapping,
534         String JavaDoc webApplicationContext,
535         String JavaDoc defaultWebApplication) {
536
537         // init base path
538
webInfRfsPath = webInfRfsPath.replace('\\', '/');
539         if (!webInfRfsPath.endsWith("/")) {
540             webInfRfsPath = webInfRfsPath + "/";
541         }
542         m_webInfRfsPath = CmsFileUtil.normalizePath(webInfRfsPath);
543
544         // set the servlet paths
545
if (!servletMapping.startsWith("/")) {
546             servletMapping = "/" + servletMapping;
547         }
548         if (servletMapping.endsWith("/*")) {
549             // usually a mapping must be in the form "/opencms/*", cut off all slashes
550
servletMapping = servletMapping.substring(0, servletMapping.length() - 2);
551         }
552         m_servletPath = servletMapping;
553
554         // set the default web application name
555
if (defaultWebApplication.endsWith("/")) {
556             defaultWebApplication = defaultWebApplication.substring(0, defaultWebApplication.length() - 1);
557         }
558         if (defaultWebApplication.startsWith("/")) {
559             defaultWebApplication = defaultWebApplication.substring(1);
560         }
561         m_defaultWebApplicationName = defaultWebApplication;
562
563         // set the web application name
564
File JavaDoc path = new File JavaDoc(m_webInfRfsPath);
565         m_webApplicationName = path.getParentFile().getName();
566
567         String JavaDoc contextPath;
568         if (webApplicationContext == null) {
569             // default: use web application context calculated form RFS path (fine with Tomcat)
570
contextPath = m_webApplicationName;
571         } else {
572             // optional: web application context was set in web.xml, required for certain
573
// runtime environments (e.g. Jboss) that do not use the same RFS and context path
574
contextPath = webApplicationContext;
575         }
576
577         // set the context path
578
if (contextPath.equals(getDefaultWebApplicationName())) {
579             m_contextPath = "";
580         } else {
581             m_contextPath = "/" + contextPath;
582         }
583
584         // this fixes an issue with context names in Jboss
585
if (m_contextPath.endsWith(".war")) {
586             m_contextPath = m_contextPath.substring(0, m_contextPath.length() - 4);
587         }
588
589         // set the OpenCms context
590
m_openCmsContext = m_contextPath + m_servletPath;
591
592         // set the web application path
593
m_webApplicationRfsPath = path.getParentFile().getAbsolutePath();
594         if (!m_webApplicationRfsPath.endsWith(File.separator)) {
595             m_webApplicationRfsPath += File.separator;
596         }
597     }
598
599     /**
600      * Sets the default encoding, called after the properties have been read.<p>
601      *
602      * @param encoding the default encoding to set
603      */

604     protected void setDefaultEncoding(String JavaDoc encoding) {
605
606         m_defaultEncoding = encoding.intern();
607         if (CmsLog.INIT.isInfoEnabled()) {
608             CmsLog.INIT.info(Messages.get().getBundle().key(Messages.LOG_SET_DEFAULT_ENCODING_1, m_defaultEncoding));
609         }
610     }
611
612     /**
613      * Sets the HTTP authentication settings.<p>
614      *
615      * @param httpAuthenticationSettings the HTTP authentication settings to set
616      */

617     protected void setHttpAuthenticationSettings(CmsHttpAuthenticationSettings httpAuthenticationSettings) {
618
619         m_httpAuthenticationSettings = httpAuthenticationSettings;
620     }
621
622     /**
623      * Sets the settings for the internal OpenCms email service.<p>
624      *
625      * @param mailSettings the settings for the internal OpenCms email service to set
626      */

627     protected void setMailSettings(CmsMailSettings mailSettings) {
628
629         m_mailSettings = mailSettings;
630     }
631
632     /**
633      * Sets the server name.<p>
634      *
635      * The server name is set in <code>opencms.properties</code>.
636      * It is not related to any DNS name the server might also have.
637      * The server name is usefull e.g. in a cluster to distinguish different servers,
638      * or if you compare logfiles from multiple servers.<p>
639      *
640      * @param serverName the server name to set
641      */

642     protected void setServerName(String JavaDoc serverName) {
643
644         m_serverName = serverName;
645         if (CmsLog.INIT.isInfoEnabled()) {
646             CmsLog.INIT.info(Messages.get().getBundle().key(Messages.LOG_SET_SERVERNAME_1, m_serverName));
647         }
648     }
649
650     /**
651      * Initializes the version for this OpenCms, will be called by
652      * CmsHttpServlet or CmsShell upon system startup.<p>
653      */

654     private void initVersion() {
655
656         // init version information with static defaults
657
m_versionNumber = DEFAULT_VERSION_NUMBER;
658         // set OpenCms version identifier with default values
659
m_version = "OpenCms/" + m_versionNumber;
660         // read the version-informations from properties
661
Properties JavaDoc props = new Properties JavaDoc();
662         try {
663             props.load(this.getClass().getClassLoader().getResourceAsStream("org/opencms/main/version.properties"));
664         } catch (Throwable JavaDoc t) {
665             // ignore this exception - no properties found
666
return;
667         }
668         m_versionNumber = props.getProperty("version.number", DEFAULT_VERSION_NUMBER);
669         // set OpenCms version identifier with propery values
670
m_version = "OpenCms/" + m_versionNumber;
671     }
672 }
Popular Tags