KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > core > system > SolutionContextListener


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Apr 12, 2005
14  * @author James Dixon
15  *
16  */

17
18 package org.pentaho.core.system;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.Properties JavaDoc;
25 import javax.servlet.ServletContext JavaDoc;
26 import javax.servlet.ServletContextEvent JavaDoc;
27 import javax.servlet.ServletContextListener JavaDoc;
28
29 import org.pentaho.messages.Messages;
30 import org.pentaho.messages.util.LocaleHelper;
31 import org.pentaho.util.VersionHelper;
32 import org.pentaho.util.logging.Logger;
33
34 public class SolutionContextListener implements ServletContextListener JavaDoc {
35
36     // list of the default locations to search for the solution repository
37
private static final String JavaDoc defaultLocations[] = { "/eclipse/workspace/pentaho-solutions", //$NON-NLS-1$ Solution samples from CVS in the default eclipse workspace
38
"/eclipse/workspace/pentaho-samples/solutions/test-solution" //$NON-NLS-1$ All Samples from CVS in the default eclipse workspace
39
};
40
41     protected static String JavaDoc solutionPath;
42
43     protected static String JavaDoc contextPath;
44
45     public void contextInitialized(ServletContextEvent JavaDoc event) {
46
47         ServletContext JavaDoc context = event.getServletContext();
48
49         String JavaDoc encoding = context.getInitParameter("encoding"); //$NON-NLS-1$
50
if (encoding != null) {
51             LocaleHelper.setSystemEncoding(encoding);
52         }
53
54         String JavaDoc textDirection = context.getInitParameter("text-direction"); //$NON-NLS-1$
55
if (textDirection != null) {
56             LocaleHelper.setTextDirection(textDirection);
57         }
58
59         String JavaDoc localeLanguage = context.getInitParameter("locale-language"); //$NON-NLS-1$
60
String JavaDoc localeCountry = context.getInitParameter("locale-country"); //$NON-NLS-1$
61
boolean localeSet = false;
62         if (localeLanguage != null && !"".equals(localeLanguage) && localeCountry != null && !"".equals(localeCountry)) { //$NON-NLS-1$ //$NON-NLS-2$
63
Locale JavaDoc locales[] = Locale.getAvailableLocales();
64             if (locales != null) {
65                 for (int i = 0; i < locales.length; i++) {
66                     if (locales[i].getLanguage().equals(localeLanguage) && locales[i].getCountry().equals(localeCountry)) {
67                         LocaleHelper.setLocale(locales[i]);
68                         localeSet = true;
69                         break;
70                     }
71                 }
72             }
73         }
74         if (!localeSet) {
75             // do this thread in the default locale
76
LocaleHelper.setLocale(Locale.getDefault());
77         }
78         LocaleHelper.setDefaultLocale(LocaleHelper.getLocale());
79         // log everything that goes on here
80
Logger.info(SolutionContextListener.class.getName(), Messages.getString("SolutionContextListener.INFO_INITIALIZING")); //$NON-NLS-1$
81
Logger.info(SolutionContextListener.class.getName(), Messages.getString("SolutionContextListener.INFO_SERVLET_CONTEXT") + context); //$NON-NLS-1$
82
contextPath = context.getRealPath(""); //$NON-NLS-1$
83
Logger.info(SolutionContextListener.class.getName(), Messages.getString("SolutionContextListener.INFO_CONTEXT_PATH") + contextPath); //$NON-NLS-1$
84

85         solutionPath = getSolutionPath(context);
86         if ((solutionPath == null) || (solutionPath.length() == 0)) {
87             Logger.error(getClass().getName(), Messages.getErrorString("SolutionContextListener.ERROR_0001_NO_ROOT_PATH")); //$NON-NLS-1$
88
}
89
90         Logger.info(getClass().getName(), Messages.getString("SolutionContextListener.INFO_ROOT_PATH") + solutionPath); //$NON-NLS-1$
91

92         // TODO: derive the base URL from somewhere
93
String JavaDoc baseUrl = context.getInitParameter("base-url"); //$NON-NLS-1$
94
if (baseUrl == null) {
95             // assume this is a demo installation
96
// TODO: Create a servlet that's loaded on startup to set this value
97
baseUrl = "http://localhost:8080/pentaho/"; //$NON-NLS-1$
98
}
99         IApplicationContext applicationContext = new WebApplicationContext(solutionPath, baseUrl, context.getRealPath("")); //$NON-NLS-1$
100

101         /*
102          * Copy out all the initParameter values from the servlet context and
103          * put them in the application context.
104          */

105         Properties JavaDoc props = new Properties JavaDoc();
106         Enumeration JavaDoc initParmNames = context.getInitParameterNames();
107         String JavaDoc initParmName;
108         while (initParmNames.hasMoreElements()) {
109             initParmName = (String JavaDoc) initParmNames.nextElement();
110             props.setProperty(initParmName, context.getInitParameter(initParmName));
111         }
112         ((WebApplicationContext) applicationContext).setProperties(props);
113
114         boolean initOk = PentahoSystem.init(applicationContext);
115         this.showInitializationMessage(initOk);
116     }
117
118     public void showInitializationMessage(boolean initOk) {
119       if (initOk) {
120         System.out.println(Messages.getString("SolutionContextListener.INFO_SYSTEM_READY") + " (" + VersionHelper.getVersion() + "-" + VersionHelper.getBuild() + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
121
} else {
122         System.out.println(Messages.getString("SolutionContextListener.INFO_SYSTEM_NOT_READY") + " (" + VersionHelper.getVersion() + "-" + VersionHelper.getBuild() + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
123
}
124     }
125     
126     protected String JavaDoc getSolutionPath(ServletContext JavaDoc context) {
127         File JavaDoc pentahoSolutions;
128
129         // first try the web.xml setting
130
String JavaDoc rootPath = context.getInitParameter("solution-path"); //$NON-NLS-1$
131
if (rootPath != null && !"".equals(rootPath)) { //$NON-NLS-1$
132
pentahoSolutions = new File JavaDoc(rootPath);
133             if (pentahoSolutions.exists() && pentahoSolutions.isDirectory()) {
134                 return rootPath;
135             }
136         }
137
138         // next try the default locations
139
for (int i = 0; i < defaultLocations.length; ++i) {
140             pentahoSolutions = new File JavaDoc(defaultLocations[i]);
141             if (pentahoSolutions.exists() && pentahoSolutions.isDirectory()) {
142                 try {
143                     return pentahoSolutions.getCanonicalPath();
144                 } catch (IOException JavaDoc e) {
145                     return pentahoSolutions.getAbsolutePath();
146                 }
147             }
148         }
149
150         // now try the path to the WEB-INF to see if we find
151
File JavaDoc file = new File JavaDoc(context.getRealPath("")); //$NON-NLS-1$
152
while (file != null) {
153             if (file.exists() && file.isDirectory()) {
154                 pentahoSolutions = new File JavaDoc(file.getAbsolutePath() + File.separator + "pentaho-solutions"); //$NON-NLS-1$
155
if (pentahoSolutions.exists() && pentahoSolutions.isDirectory()) {
156                     try {
157                         return pentahoSolutions.getCanonicalPath();
158                     } catch (IOException JavaDoc e) {
159                         return pentahoSolutions.getAbsolutePath();
160                     }
161                 }
162             }
163             file = file.getParentFile();
164         }
165         return null;
166     }
167
168     protected String JavaDoc getContextPath() {
169         return contextPath;
170     }
171
172     protected String JavaDoc getRootPath() {
173         return solutionPath;
174     }
175
176     public void contextDestroyed(ServletContextEvent JavaDoc event) {
177
178         PentahoSystem.shutdown();
179         if (LocaleHelper.getLocale() == null) {
180             LocaleHelper.setLocale(Locale.getDefault());
181         }
182         // log everything that goes on here
183
Logger.info(SolutionContextListener.class.getName(), Messages.getString("SolutionContextListener.INFO_SYSTEM_EXITING")); //$NON-NLS-1$
184
}
185 }
186
Popular Tags