Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements properties methods for metacat
4
 *  Copyright: 2008 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Michael Daigle
7
 *
8
 *   '$Author: daigle $'
9
 *     '$Date: 2009-08-14 17:38:05 -0700 (Fri, 14 Aug 2009) $'
10
 * '$Revision: 5028 $'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26

    
27
package edu.ucsb.nceas.metacat.properties;
28

    
29
import java.io.IOException;
30
import java.util.Map;
31
import java.util.Vector;
32

    
33
import javax.servlet.ServletContext;
34
import javax.servlet.http.HttpServletRequest;
35

    
36
import org.apache.log4j.Logger;
37

    
38
import edu.ucsb.nceas.metacat.shared.BaseService;
39
import edu.ucsb.nceas.metacat.shared.ServiceException;
40
import edu.ucsb.nceas.utilities.GeneralPropertyException;
41
import edu.ucsb.nceas.utilities.PropertiesMetaData;
42
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
43
import edu.ucsb.nceas.utilities.SortedProperties;
44

    
45
/**
46
 * A suite of utility classes for the metadata configuration utility
47
 */
48
public class SimpleProperties extends BaseService implements PropertiesInterface {
49
	
50
	private static SortedProperties sortedProperties = null;
51
	
52
	private static Logger logMetacat = Logger.getLogger(SimpleProperties.class);
53

    
54
	/**
55
	 * private constructor since this is a singleton
56
	 * 
57
	 * @param servletContext the context we will use to get relative paths
58
	 */
59
	protected SimpleProperties() throws ServiceException {		
60
		_serviceName = "SimpleProperties";
61
				
62
		initialize();		
63
	}
64
	
65
	public boolean refreshable() {
66
		return true;
67
	}
68
	
69
	public void doRefresh() throws ServiceException {
70
		initialize();
71
	}
72
	
73
	public void stop() throws ServiceException {
74
		return;
75
	}
76
	
77
	/**
78
	 * Initialize the singleton.
79
	 * 
80
	 * @param servletContext the context we will use to get relative paths
81
	 */
82
	private void initialize() throws ServiceException {
83
		
84
		logMetacat.debug("Initializing SimpleProperties");
85
		
86
		String mainConfigFilePath = 
87
			PropertyService.CONFIG_FILE_PATH;
88
		sortedProperties = new SortedProperties(mainConfigFilePath);
89
		
90
		try {
91
			sortedProperties.load();
92
		} catch (IOException ioe) {
93
			throw new ServiceException("I/O problem while loading properties: "
94
					+ ioe.getMessage());
95
		} 
96
	}
97

    
98
	/**
99
	 * Utility method to get a property value from the properties file
100
	 * 
101
	 * @param propertyName
102
	 *            the name of the property requested
103
	 * @return the String value for the property
104
	 */
105
	public String getProperty(String propertyName)
106
			throws PropertyNotFoundException {
107
		return sortedProperties.getProperty(propertyName);
108
	}
109
	
110
	/**
111
     * Get a set of all property names.
112
     * 
113
     * @return Set of property names  
114
     */
115
    public Vector<String> getPropertyNames() {   	
116
    	return sortedProperties.getPropertyNames();
117
    }
118
    
119

    
120
	/**
121
	 * Get a Set of all property names that start with the groupName prefix.
122
	 * 
123
	 * @param groupName
124
	 *            the prefix of the keys to search for.
125
	 * @return enumeration of property names
126
	 */
127
    public Vector<String> getPropertyNamesByGroup(String groupName) {   	
128
    	return sortedProperties.getPropertyNamesByGroup(groupName);
129
    }
130
    
131
	/**
132
	 * Get a Map of all properties that start with the groupName prefix.
133
	 * 
134
	 * @param groupName
135
	 *            the prefix of the keys to search for.
136
	 * @return Map of property names
137
	 */
138
    public Map<String, String> getPropertiesByGroup(String groupName) throws PropertyNotFoundException {   	
139
    	return sortedProperties.getPropertiesByGroup(groupName);
140
    }
141

    
142
	/**
143
	 * Utility method to set a property value both in memory and to the
144
	 * properties file
145
	 * 
146
	 * @param propertyName
147
	 *            the name of the property requested
148
	 * @param newValue
149
	 *            the new value for the property
150
	 */
151
	public void setProperty(String propertyName, String newValue) throws GeneralPropertyException {
152
		sortedProperties.setProperty(propertyName, newValue);
153
		sortedProperties.store();
154
	}
155

    
156
	/**
157
	 * Utility method to set a property value in memory. This will NOT cause the
158
	 * property to be written to disk. Use this method to set multiple
159
	 * properties in a row without causing excessive I/O. You must call
160
	 * persistProperties() once you're done setting properties to have them
161
	 * written to disk.
162
	 * 
163
	 * @param propertyName
164
	 *            the name of the property requested
165
	 * @param newValue
166
	 *            the new value for the property
167
	 */
168
	public void setPropertyNoPersist(String propertyName, String newValue) throws GeneralPropertyException {
169
		sortedProperties.setPropertyNoPersist(propertyName, newValue);
170
	}
171

    
172
	/**
173
	 * Save the properties to a properties file. Note, the 
174
	 * order and comments will be preserved.
175
	 */
176
	public void persistProperties() throws GeneralPropertyException {
177
		sortedProperties.store();
178
	}
179
	
180
	/**
181
	 * Take input from the user in an HTTP request about an property to be changed
182
	 * and update the metacat property file with that new value if it has
183
	 * changed from the value that was originally set.
184
	 * 
185
	 * @param request
186
	 *            that was generated by the user
187
	 * @param response
188
	 *            to send output back to the user
189
	 * @param propertyName
190
	 *            the name of the property to be checked and set
191
	 */
192
	public void checkAndSetProperty(HttpServletRequest request, String propertyName) 
193
			throws GeneralPropertyException {
194
		String value = getProperty(propertyName);
195
		String newValue = request.getParameter(propertyName);
196
		if (newValue != null && !newValue.trim().equals(value)) {
197
			setPropertyNoPersist(propertyName, newValue.trim());
198
		}
199
	}
200
	
201
	public SortedProperties getMainBackupProperties() {
202
		return sortedProperties;
203
	}
204
	
205
	public  SortedProperties getAuthBackupProperties() throws GeneralPropertyException {
206
		throw new GeneralPropertyException("SimpleProperties.getAuthBackupProperties - " +
207
				"SimpleProperties does not support backup properties");
208
	}
209
	
210
	public PropertiesMetaData getMainMetaData() throws GeneralPropertyException {
211
		throw new GeneralPropertyException("SimpleProperties.getMainMetaData - " +
212
				"SimpleProperties does not support metadata");
213
	}
214
	
215
	public PropertiesMetaData getAuthMetaData() throws GeneralPropertyException {
216
		throw new GeneralPropertyException("SimpleProperties.getAuthMetaData - " +
217
				"SimpleProperties does not support auth metadata");
218
	}
219
	
220
	public void persistMainBackupProperties() throws GeneralPropertyException {
221
		throw new GeneralPropertyException("SimpleProperties.persistMainBackupProperties - " +
222
			"SimpleProperties does not support backup properties");
223
	}
224
	
225
	public void persistAuthBackupProperties(ServletContext servletContext) throws GeneralPropertyException {
226
		throw new GeneralPropertyException("SimpleProperties.persistAuthBackupProperties - " +
227
				"SimpleProperties does not support backup properties");
228
	}
229
	
230
	public boolean arePropertiesConfigured()  throws GeneralPropertyException {
231
		return true;
232
	}
233
	
234
	public boolean doBypass() throws GeneralPropertyException {
235
		throw new GeneralPropertyException("SimpleProperties.doBypass - " +
236
			"SimpleProperties does not support doBypass method.");
237
	}
238
	
239
	public void bypassConfiguration()  throws GeneralPropertyException {
240
		throw new GeneralPropertyException("SimpleProperties.doBypass - " +
241
			"SimpleProperties does not support bypassConfiguration method.");
242
	}
243

    
244
}
(4-4/5)