KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > portlet > admin > util > PlutoAdminContext


1 /*
2  * Copyright 2003,2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.pluto.portlet.admin.util;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import org.apache.pluto.portlet.admin.PlutoAdminConstants;
25 import org.apache.pluto.portlet.admin.PlutoAdminException;
26 import org.apache.pluto.portlet.admin.PlutoAdminLogger;
27
28 /**
29  * A singleton factory that holds methods to get various data on
30  * the Pluto install such as the path to the Pluto home directory
31  * held within properties files. A HashMap cache is used to store
32  * the properties (as a Properties object) when they are first
33  * loaded.
34  *
35  * @author Craig Doremus
36  *
37  */

38 public class PlutoAdminContext {
39
40     private static final String JavaDoc CLASS_NAME = "PlutoAdminContext";
41     /** Home for the container (servlet engine) that Pluto sits inside of. In Tomcat, this is
42      * */

43     private static String JavaDoc _containerHome = null;
44     private static String JavaDoc _plutoHome = null;
45     private static String JavaDoc _plutoContext = null;
46     private static String JavaDoc _deploymentPath = null;
47     private static Map JavaDoc _cache = new HashMap JavaDoc();
48     private static PlutoAdminContext _instance = new PlutoAdminContext();
49
50     /**
51      *
52      */

53     private PlutoAdminContext() {
54     }
55
56     public static PlutoAdminContext getInstance(){
57         return _instance;
58     }
59
60     /**
61      * Accessor for the full path to the pageregistry.xml file using
62      * the getPlutoHome() method.
63      *
64      * @return The absolute path to pageregistry.xml
65      * @see #getPlutoHome()
66      */

67     public String JavaDoc getPageRegistryPath() {
68         String JavaDoc path = getPlutoHome() + PlutoAdminConstants.FS + getRelDataDir() + PlutoAdminConstants.FS + getProperties().getProperty("pageregistry-file");
69         return path;
70     }
71
72     /**
73      * Accessor for the full path to the portletentityregistry.xml file using
74      * the getPlutoHome() method.
75      *
76      * @return The absolute path to portletentityregistry.xml
77      * @see #getPlutoHome()
78      */

79     public String JavaDoc getPortletEntityRegistryPath() {
80         String JavaDoc path = getPlutoHome() + PlutoAdminConstants.FS + getRelDataDir() + PlutoAdminConstants.FS + getProperties().getProperty("portletentityregistry-file");
81         return path;
82     }
83
84     /**
85      * Uses properties in pluto-admin.properties to get the
86      * full path to the installed Pluto home directory, which in
87      * Tomcat is Pluto's webapps directory (usually 'pluto').
88      * @return The absolute path to the directory where the Pluto
89      * container is installed.
90      */

91     public String JavaDoc getPlutoHome(){
92         final String JavaDoc METHOD_NAME = "getPlutoHome()";
93 // String plutoHome = null;
94
// Properties props = getProperties();
95
// String plutoContext = props.getProperty("pluto-web-context");
96
// plutoHome = getWebappsPath() + PlutoAdminConstants.FS + plutoContext;
97
// return plutoHome;
98
// if (_plutoHome == null) {
99
// String msg ="The _plutoHome variable must be set (setPlutoHome()) inside" +
100
// " of the ControllerPortlet.init() method using before this method is called";
101
// IllegalStateException e = new IllegalStateException(msg);
102
// PlutoAdminLogger.logError(CLASS_NAME, METHOD_NAME, msg, e);
103
// throw e;
104
// }
105
return _plutoHome;
106     }
107
108     /**
109      * Uses properties in pluto-admin.properties to get the
110      * full path to the installed Pluto home directory, which in
111      * Tomcat is Pluto's webapps directory (usually 'pluto').
112      * @return The absolute path to the directory where the Pluto
113      * container is installed.
114      */

115 // public String getWebappsPath(){
116
// String path = null;
117
// Properties props = getProperties();
118
// String tomcatHome = getTomcatHome();
119
// path = tomcatHome + "/webapps";
120
// return path;
121
// }
122

123     /**
124      * Returns the Properties object from a properties file that is in the
125      * classpath. After it's first access, the properties are stored
126      * in a cache.
127      *
128      * @param propFileName Name of the properties file.
129      * @return Properties The filled properties object
130      * @throws IOException If there is a problem loading the properties
131      * from the file
132      * @throws NullPointerException If the InputStream accessing the properties
133      * file is null.
134      */

135     public static Properties JavaDoc getProperties(String JavaDoc propFileName){
136         final String JavaDoc METHOD_NAME = "getProperties(propFileName)";
137         Properties JavaDoc props = null;
138         //retreive from cache if available
139
props = (Properties JavaDoc)_cache.get(propFileName);
140         if ( props == null) {
141         //get the properties from prop file
142
InputStream JavaDoc stream = PlutoAdminContext.class.getClassLoader().getResourceAsStream(propFileName);
143         if (stream == null) {
144             String JavaDoc logMsg = "Null InputStream." +
145             " Please make sure the properties file exists and is in the classpath.";
146                 NullPointerException JavaDoc e = new NullPointerException JavaDoc(logMsg);
147                 PlutoAdminLogger.logError(CLASS_NAME, METHOD_NAME, e);
148                 throw e;
149             }
150         props = new Properties JavaDoc();
151             try {
152                 props.load(stream);
153             } catch (IOException JavaDoc e) {
154                 PlutoAdminLogger.logError(CLASS_NAME, METHOD_NAME, e);
155                 throw new PlutoAdminException(e);
156             }
157             //add props to the cache
158
_cache.put(propFileName, props);
159             return props;
160         } else {
161             return props;
162         }
163     }
164
165     public static Properties JavaDoc getProperties(){
166         return getProperties(PlutoAdminConstants.PROP_FILENAME);
167     }
168
169     private String JavaDoc getRelDataDir(){
170         String JavaDoc dir = getProperties().getProperty("data-dir-relative-path");
171         return dir;
172     }
173
174     /**
175      * Accessor for the full path to the portletcontexts.txt file
176      * @return
177      */

178     public String JavaDoc getPortletContextsPath() {
179         String JavaDoc path = getPlutoHome() + PlutoAdminConstants.FS + getRelDataDir() + PlutoAdminConstants.FS + getProperties().getProperty("portletcontexts-file");
180         return path;
181     }
182
183     /**
184      * Finds home directory of the container that holds Pluto (usually Tomcat)
185      *
186      * @return
187      */

188     public static String JavaDoc getContainerHome(){
189             final String JavaDoc METHOD_NAME = "getContainerHome()";
190             return _containerHome;
191         }
192
193     /**
194          * Parses out paths from the Pluto Home directory sent in from
195          * PortletContext.getRealPath("") call in ControllerPortlet.init()
196          *
197      * @param home The _plutoHome to set.
198      */

199     public static void parseDeploymentPaths(String JavaDoc plutoHome) {
200             final String JavaDoc METHOD_NAME = "parseDeploymentPaths(plutoHome)";
201             //TODO: test for null and use of alternate path in pluto-admin.properties
202
int lastSlash = 0;
203             if (plutoHome == null) {
204                 _plutoHome = getProperties().getProperty("pluto-home");
205                 if (_plutoHome == null || _plutoHome.equals("")) {
206                     throw new PlutoAdminException("pluto-home needs to be set in pluto-admin.properties.");
207                 }
208             //get rid of last slash if it is the last character
209
} else if (plutoHome.lastIndexOf(PlutoAdminConstants.FS) == plutoHome.length()-1) {
210                 lastSlash = plutoHome.lastIndexOf(PlutoAdminConstants.FS);
211                 _plutoHome = plutoHome.substring(0, lastSlash);
212             } else {
213                 _plutoHome = plutoHome;
214             }
215             PlutoAdminLogger.logDebug(CLASS_NAME, METHOD_NAME, "Pluto home: " + _plutoHome);
216             //Parse out context (default=pluto)
217
lastSlash = _plutoHome.lastIndexOf(PlutoAdminConstants.FS);
218             _plutoContext = _plutoHome.substring(lastSlash + 1);
219             PlutoAdminLogger.logDebug(CLASS_NAME, METHOD_NAME, "Pluto web context: " + _plutoContext);
220             //Parse out path to deployment dir
221
_deploymentPath = _plutoHome.substring(0, lastSlash);
222             PlutoAdminLogger.logDebug(CLASS_NAME, METHOD_NAME, "Portlet deployment path: " + _deploymentPath);
223             //Parse out container path (CATALINA_HOME if using Tomcat)
224
lastSlash = _deploymentPath.lastIndexOf(PlutoAdminConstants.FS);
225             _containerHome = _deploymentPath.substring(0, lastSlash);
226             PlutoAdminLogger.logDebug(CLASS_NAME, METHOD_NAME, "Container (Tomcat) home: " + _containerHome);
227     }
228     
229     /**
230      * Accessor for the path to the portlet deployment directory (webapps in Tomcat container)
231      * @return
232      */

233     public static String JavaDoc getDeploymentPath(){
234         return _deploymentPath;
235     }
236     
237     /**
238      * Accessor for the web context for Pluto (default=pluto)
239      * @return
240      */

241     public static String JavaDoc getPlutoWebContext(){
242         return _plutoContext;
243     }
244 }
245
Popular Tags