View Javadoc

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;
19  import java.io.InputStream;
20  import java.util.HashMap;
21  import java.util.Map;
22  import java.util.Properties;
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 CLASS_NAME = "PlutoAdminContext";
41  	/*** Home for the container (servlet engine) that Pluto sits inside of. In Tomcat, this is
42  	 *  */
43  	private static String _containerHome;
44  	private static String _plutoHome;
45  	private static String _plutoContext;
46  	private static String _deploymentPath;
47  	private static Map _cache = new HashMap();
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 getPageRegistryPath() {
68  		String 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 getPortletEntityRegistryPath() {
80  		String 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 getPlutoHome(){
92  //	    final String 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 NullPointerException If the InputStream accessing the properties
131 	 * file is null.
132 	 */
133 	public static Properties getProperties(String propFileName){
134 		final String METHOD_NAME = "getProperties(propFileName)";
135 		Properties props = null;
136 		//retreive from cache if available
137 		props = (Properties)_cache.get(propFileName);
138 		if ( props == null) {
139 	    //get the properties from prop file
140 	    InputStream stream = PlutoAdminContext.class.getClassLoader().getResourceAsStream(propFileName);
141 	    if (stream == null) {
142 	    	String logMsg = "Null InputStream." +
143 	    	" Please make sure the properties file exists and is in the classpath.";
144 				NullPointerException e = new NullPointerException(logMsg);
145 	    		PlutoAdminLogger.logError(CLASS_NAME, METHOD_NAME, e);
146 				throw e;
147 			}
148 	        props = new Properties();
149 			try {
150 				props.load(stream);
151 			} catch (IOException e) {
152 				PlutoAdminLogger.logError(CLASS_NAME, METHOD_NAME, e);
153 				throw new PlutoAdminException(e);
154 			}
155 			//add props to the cache
156 			_cache.put(propFileName, props);
157 		}
158 		return props;
159 	}
160 
161 	public static Properties getProperties(){
162 		return getProperties(PlutoAdminConstants.PROP_FILENAME);
163 	}
164 
165 	private String getRelDataDir(){
166 		String dir = getProperties().getProperty("data-dir-relative-path");
167 		return dir;
168 	}
169 
170 	/***
171 	 * Accessor for the full path to the portletcontexts.txt file
172 	 */
173 	public String getPortletContextsPath() {
174 		String path = getPlutoHome() + PlutoAdminConstants.FS + getRelDataDir() + PlutoAdminConstants.FS + getProperties().getProperty("portletcontexts-file");
175 		return path;
176 	}
177 
178 	/***
179 	 * Finds home directory of the container that holds Pluto (usually Tomcat)
180 	 * 
181 	 */
182 	public static String getContainerHome(){
183 //			final String METHOD_NAME = "getContainerHome()";
184 			return _containerHome;
185 	}
186 
187 	/*** 
188 	 * Parses out paths from the Pluto Home directory sent in from
189 	 * PortletContext.getRealPath("") call in ControllerPortlet.init()
190 	 * 
191      * @param plutoHome The _plutoHome to set.
192      */
193     public static void parseDeploymentPaths(String plutoHome) {
194   			final String METHOD_NAME = "parseDeploymentPaths(plutoHome)";
195   			//TODO: test for null and use of alternate path in pluto-admin.properties
196   			int lastSlash = 0;
197   			if (plutoHome == null) {
198   			    _plutoHome = getProperties().getProperty("pluto-home");
199   			    if (_plutoHome == null || _plutoHome.equals("")) {
200   			        throw new PlutoAdminException("pluto-home needs to be set in pluto-admin.properties.");
201   			    }
202 		    //get rid of last slash if it is the last character
203   			} else if (plutoHome.lastIndexOf(PlutoAdminConstants.FS) == plutoHome.length()-1) {
204   			    lastSlash = plutoHome.lastIndexOf(PlutoAdminConstants.FS);
205   			    _plutoHome = plutoHome.substring(0, lastSlash);
206   			} else {
207   			    _plutoHome = plutoHome;  			    
208   			}
209     		PlutoAdminLogger.logDebug(CLASS_NAME, METHOD_NAME, "Pluto home: " + _plutoHome);
210     		//Parse out context (default=pluto)
211   			lastSlash = _plutoHome.lastIndexOf(PlutoAdminConstants.FS);
212 		    _plutoContext = _plutoHome.substring(lastSlash + 1);
213   			PlutoAdminLogger.logDebug(CLASS_NAME, METHOD_NAME, "Pluto web context: " + _plutoContext);
214     		//Parse out path to deployment dir
215   			_deploymentPath = _plutoHome.substring(0, lastSlash);
216     		PlutoAdminLogger.logDebug(CLASS_NAME, METHOD_NAME, "Portlet deployment path: " + _deploymentPath);
217     		//Parse out container path (CATALINA_HOME if using Tomcat)
218     		lastSlash = _deploymentPath.lastIndexOf(PlutoAdminConstants.FS);
219   			_containerHome = _deploymentPath.substring(0, lastSlash);
220     		PlutoAdminLogger.logDebug(CLASS_NAME, METHOD_NAME, "Container (Tomcat) home: " + _containerHome);
221     }
222     
223     /***
224      * Accessor for the path to the portlet deployment directory (webapps in Tomcat container) 
225      */
226     public static String getDeploymentPath(){
227         return _deploymentPath;        
228     }
229     
230     /***
231      * Accessor for the web context for Pluto (default=pluto)
232      */
233     public static String getPlutoWebContext(){
234         return _plutoContext;        
235     }
236 }