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

Quick Search    Search Deep

Source code: info/crossbar/state/AppConfig.java


1   /*
2    *  @(#)AppConfig.java $Revision: 1.3 $ $Date: 2003/06/04 04:55:32 $
3    *
4    *  Copyright 2002 by Daniel Kehoe <kehoe@fortuity.com>
5    *  All Rights Reserved
6    *
7    *  Redistribution and use in source and binary forms, with or without
8    *  modification, are permitted provided that the following conditions
9    *  are met:
10   *  1. Redistributions of source code must retain the above copyright
11   *  notice, this list of conditions and the following disclaimer.
12   *  2. Redistributions in binary form must reproduce the above copyright
13   *  notice, this list of conditions and the following disclaimer in the
14   *  documentation and/or other materials provided with the distribution.
15   *
16   *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17   *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18   *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19   *  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20   *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21   *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22   *  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23   *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24   *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25   *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26   *  SUCH DAMAGE.
27   */
28  package info.crossbar.state;
29  
30  import java.util.logging.Logger;
31  
32  import java.util.prefs.*;
33  
34  import java.io.File;
35  
36  import javax.servlet.ServletException;
37  
38  import info.crossbar.state.App;
39  
40  import info.crossbar.filtersAndListeners.ContextListener;
41  
42  /**
43   * AppConfig class for use by <a href="http://www.crossbar.info/">Crossbar</a>
44   *
45   * @author     Daniel Kehoe, <a href="http://www.fortuity.com/">Fortuity Consulting</a>
46   * @version    <a href="http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/crossbar/crossbar-sitemap/src/java/info/crossbar/state/AppConfig.java">View source, revision history</a>
47   * $Revision: 1.3 $ $Date: 2003/06/04 04:55:32 $
48   * <p>
49   * DESCRIPTION:
50   * The AppConfig class encapsulates a Java Preferences object.
51   */
52  
53  public class AppConfig {
54  
55    /**
56     * No logging in this class because it is initialized before logging is initialized.
57     */
58    // private static Logger log = Logger.getLogger(AppConfig.class.getName());
59  
60    /**
61     * Application preferences object.
62     */
63    private Preferences prefs;
64    
65    /**
66     * Constructor. Obtains the application preferences object.
67     *
68     * @exception  ServletException  thrown by any error
69     */
70    public AppConfig()
71      throws ServletException {
72      // Obtain the menu preferences object 
73      // (it needs to be qualified with the application name):
74      this.prefs = (Preferences.systemNodeForPackage(
75        AppConfig.class)).node(App.context.getServletContextName());
76    }
77    
78    public void setDebugLevel(String debuglevel) {
79          this.prefs.put("info.crossbar.debuglevel", debuglevel);
80      try {
81        ContextListener.setLogging(App.context);
82      } catch (ServletException e) {
83        App.context.log(e.getMessage());
84      }
85      }
86      public String getDebugLevel() {
87      String value = this.prefs.get("info.crossbar.debuglevel", App.context.getInitParameter("info.crossbar.debuglevel"));
88      if(!"true".equals(App.context.getInitParameter("info.crossbar.ignoreWEBXML"))) 
89        value = App.context.getInitParameter("info.crossbar.debuglevel");
90          return value;
91      }
92    
93    public void setLogFilePath(String logFilePath) {
94          this.prefs.put("info.crossbar.logfilepath", logFilePath);
95      try {
96        ContextListener.setLogging(App.context);
97      } catch (ServletException e) {
98        App.context.log(e.getMessage());
99      }
100     }
101     public String getLogFilePath() {
102     String value = this.prefs.get("info.crossbar.logfilepath", App.context.getInitParameter("info.crossbar.logfilepath"));
103     if(!"true".equals(App.context.getInitParameter("info.crossbar.ignoreWEBXML"))) 
104       value = App.context.getInitParameter("info.crossbar.logfilepath");
105     if ("DEFAULT".equals(value.toUpperCase())) {
106       String logFileName = App.context.getServletContextName().toLowerCase() + ".log";
107       String slash = System.getProperty("file.separator");
108       // try getting the tempdir value, it looks like 
109       // "C:\java\tomcat4\work\Standalone\localhost\crossbar",
110       // it works better than context.getRealPath("") because 
111       // context.getRealPath("") doesn't work well when the 
112       // app is deployed from an unexploded war file
113       String lengthyPath = ((File) App.context.getAttribute("javax.servlet.context.tempdir")).getParent();
114       if (lengthyPath == null) {
115         String msg = "FATAL: cannot obtain file system reference, cannot set log file";
116         System.out.println(msg);
117         App.context.log(msg);
118       }
119       lengthyPath = lengthyPath.substring(0, lengthyPath.lastIndexOf(slash));
120       lengthyPath = lengthyPath.substring(0, lengthyPath.lastIndexOf(slash));
121       value = lengthyPath.substring(0, lengthyPath.lastIndexOf(slash)) 
122         + slash + "logs" + slash + logFileName;
123       // when we're done, the log filepath should look something like: 
124       // "D:\Program Files\tomcat\logs\crossbar.log"
125     }
126         return value;
127     }
128 
129 }// end of class AppConfig
130