source: ccr/trunk/nhin-vista/projects/NHINC/Current/Product/Production/Common/PropAccessorEJB/src/java/gov/hhs/fha/nhinc/common/propaccessor/PropertyAccessHelper.java@ 507

Last change on this file since 507 was 507, checked in by George Lilly, 15 years ago

NHIN gateway and adaptor for use on linux with VistA EHR and RPMS

File size: 17.9 KB
Line 
1package gov.hhs.fha.nhinc.common.propaccessor;
2
3import java.util.Set;
4import java.util.Iterator;
5import java.util.Properties;
6
7import gov.hhs.fha.nhinc.common.propertyaccess.GetPropertyResponseType;
8import gov.hhs.fha.nhinc.common.propertyaccess.GetPropertyRequestType;
9import gov.hhs.fha.nhinc.common.propertyaccess.GetPropertyBooleanResponseType;
10import gov.hhs.fha.nhinc.common.propertyaccess.GetPropertyBooleanRequestType;
11import gov.hhs.fha.nhinc.common.propertyaccess.GetPropertyNamesResponseType;
12import gov.hhs.fha.nhinc.common.propertyaccess.GetPropertyNamesRequestType;
13import gov.hhs.fha.nhinc.common.propertyaccess.GetPropertiesResponseType;
14import gov.hhs.fha.nhinc.common.propertyaccess.GetPropertiesRequestType;
15import gov.hhs.fha.nhinc.common.propertyaccess.GetRefreshDurationResponseType;
16import gov.hhs.fha.nhinc.common.propertyaccess.GetRefreshDurationRequestType;
17import gov.hhs.fha.nhinc.common.propertyaccess.GetDurationBeforeNextRefreshResponseType;
18import gov.hhs.fha.nhinc.common.propertyaccess.GetDurationBeforeNextRefreshRequestType;
19import gov.hhs.fha.nhinc.common.propertyaccess.ForceRefreshResponseType;
20import gov.hhs.fha.nhinc.common.propertyaccess.ForceRefreshRequestType;
21import gov.hhs.fha.nhinc.common.propertyaccess.GetPropertyFileLocationResponseType;
22import gov.hhs.fha.nhinc.common.propertyaccess.GetPropertyFileLocationRequestType;
23import gov.hhs.fha.nhinc.common.propertyaccess.DumpPropsToLogResponseType;
24import gov.hhs.fha.nhinc.common.propertyaccess.DumpPropsToLogRequestType;
25import gov.hhs.fha.nhinc.common.propertyaccess.PropertiesType;
26import gov.hhs.fha.nhinc.common.propertyaccess.PropertyType;
27
28import gov.hhs.fha.nhinc.common.propertyaccess.DeletePropertyFileRequestType;
29import gov.hhs.fha.nhinc.common.propertyaccess.DeletePropertyFileResponseType;
30import gov.hhs.fha.nhinc.common.propertyaccess.WritePropertyFileRequestType;
31import gov.hhs.fha.nhinc.common.propertyaccess.WritePropertyFileResponseType;
32
33import gov.hhs.fha.nhinc.properties.PropertyAccessor;
34import gov.hhs.fha.nhinc.properties.PropertyAccessException;
35import gov.hhs.fha.nhinc.properties.PropertyFileManager;
36
37/**
38 * This class is used by the PropertyAccessService to do the actual work
39 * of this web service. It keeps the web service class as a simple wrapper.
40 *
41 * @author Les Westberg
42 */
43public class PropertyAccessHelper
44{
45 /**
46 * This method returns the value of the given property that is located within the
47 * given property file. If the properties have been cached and the cache is
48 * still fresh, then it will return the value from the cache. If the properties
49 * are cached, but the cache is not fresh, then the cache will be updated with the
50 * current values in the properties file and then the property will be returned.
51 * If the properties for that file are not cached at all, the property will be
52 * retrieved from the properties file and returned.
53 *
54 * @param input The input parameters - Property File and Property Name.
55 * @return The value for the property.
56 */
57 public static GetPropertyResponseType getProperty(GetPropertyRequestType input)
58 throws PropertyAccessException
59 {
60 GetPropertyResponseType oOutput = new GetPropertyResponseType();
61
62 if ((input != null) &&
63 (input.getPropertyFile() != null) &&
64 (input.getPropertyFile().length() > 0) &&
65 (input.getPropertyName() != null) &&
66 (input.getPropertyName().length() > 0))
67 {
68 String sPropertyFile = input.getPropertyFile();
69 String sPropertyName = input.getPropertyName();
70
71 String sValue = PropertyAccessor.getProperty(sPropertyFile, sPropertyName);
72 if (sValue != null)
73 {
74 oOutput.setPropertyValue(sValue);
75 }
76 }
77
78 return oOutput;
79 }
80
81 /**
82 * This will return true if the property value is: T, t, or any case combination
83 * of "TRUE" and it will return false for all other values.
84 *
85 * @param input The property file and property name.
86 * @return TRUE if the property is true and false if it is not.
87 */
88 public static GetPropertyBooleanResponseType getPropertyBoolean(GetPropertyBooleanRequestType input)
89 throws PropertyAccessException
90 {
91 GetPropertyBooleanResponseType oOutput = new GetPropertyBooleanResponseType();
92
93 if ((input != null) &&
94 (input.getPropertyFile() != null) &&
95 (input.getPropertyFile().length() > 0) &&
96 (input.getPropertyName() != null) &&
97 (input.getPropertyName().length() > 0))
98 {
99 String sPropertyFile = input.getPropertyFile();
100 String sPropertyName = input.getPropertyName();
101
102 boolean bValue = PropertyAccessor.getPropertyBoolean(sPropertyFile, sPropertyName);
103 oOutput.setPropertyValue(bValue);
104 }
105
106 return oOutput;
107 }
108
109 /**
110 * This method returns the set of keys in a property file.
111 *
112 * @param input The name of the property file.
113 * @return The list of property names in the property file.
114 */
115 public static GetPropertyNamesResponseType getPropertyNames(GetPropertyNamesRequestType input)
116 throws PropertyAccessException
117 {
118 GetPropertyNamesResponseType oOutput = new GetPropertyNamesResponseType();
119
120 if ((input != null) &&
121 (input.getPropertyFile() != null) &&
122 (input.getPropertyFile().length() > 0))
123 {
124 String sPropertyFile = input.getPropertyFile();
125
126 Set<String> setKeys = PropertyAccessor.getPropertyNames(sPropertyFile);
127 if (setKeys != null)
128 {
129 Iterator<String> iterKeys = setKeys.iterator();
130 while (iterKeys.hasNext())
131 {
132 String sKey = iterKeys.next();
133 oOutput.getPropertyName().add(sKey);
134 }
135 }
136 }
137
138 return oOutput;
139 }
140
141 /**
142 * This method returns the properties that are located within the given property
143 * file. If the properties have been cached and the cache is still fresh, then
144 * it will return the values from the cache. If the properties are cached, but
145 * the cache is not fresh, then the cache will be updated with the current values
146 * in the properties file and then the property values will be returned. If the
147 * properties for that file are not cached at all, the property will be retrieved
148 * from the properties file and returned.
149 *
150 * NOTE: THIS IS AN EXPENSIVE OPERATION. IT WILL CREATE A DEEP COPY OF THE
151 * PROPERTIES AND RETURN IT. THAT MEANS IT WILL CREATE AN EXACT REPLICA
152 * WITH ALL DATA. THIS IS A PROTECTION TO MAKE SURE THAT A PROPERTY
153 * IS NOT INADVERTANTLY CHANGED OUTSIDE OF THIS CLASS.
154 *
155 * @param input Name of the property file.
156 * @return Returns all of the properties and values in the property file.
157 */
158 public static GetPropertiesResponseType getProperties(GetPropertiesRequestType input)
159 throws PropertyAccessException
160 {
161 GetPropertiesResponseType oOutput = new GetPropertiesResponseType();
162 PropertiesType oProperties = new PropertiesType();
163 boolean bHasProps = false;
164
165 if ((input != null) &&
166 (input.getPropertyFile() != null) &&
167 (input.getPropertyFile().length() > 0))
168 {
169 String sPropertyFile = input.getPropertyFile();
170
171 Properties oProps = PropertyAccessor.getProperties(sPropertyFile);
172 if (oProps != null)
173 {
174 Set<String> setKeys = oProps.stringPropertyNames();
175 if (setKeys != null)
176 {
177 Iterator<String> iterKeys = setKeys.iterator();
178 while (iterKeys.hasNext())
179 {
180 String sKey = iterKeys.next();
181 String sValue = oProps.getProperty(sKey);
182
183 PropertyType oProperty = new PropertyType();
184 oProperty.setPropertyName(sKey);
185 oProperty.setPropertyValue(sValue);
186 oProperties.getProperty().add(oProperty);
187 bHasProps = true;
188 }
189 }
190 }
191 }
192
193 if (bHasProps)
194 {
195 oOutput.setProperties(oProperties);
196 }
197
198 return oOutput;
199 }
200
201 /**
202 * This will return the in milliseconds the refresh duration on the property file.
203 * A setting of -1 means it never refreshes.
204 *
205 * @param input The name of the property file.
206 * @return the refresh duration for the property file.
207 */
208 public static GetRefreshDurationResponseType getRefreshDuration(GetRefreshDurationRequestType input)
209 throws PropertyAccessException
210 {
211 GetRefreshDurationResponseType oOutput = new GetRefreshDurationResponseType();
212
213 if ((input != null) &&
214 (input.getPropertyFile() != null) &&
215 (input.getPropertyFile().length() > 0))
216 {
217 String sPropertyFile = input.getPropertyFile();
218
219 int iValue = PropertyAccessor.getRefreshDuration(sPropertyFile);
220 oOutput.setDurationMillis(iValue);
221 }
222
223 return oOutput;
224 }
225
226 /**
227 * This will return the duration in milliseconds before the next refresh of the
228 * properties file. A value of -1 indicates that no refresh will occur.
229 *
230 * @param input The name of the property file.
231 * @return The number of milliseconds before the next refresh will occur.
232 */
233 public static GetDurationBeforeNextRefreshResponseType getDurationBeforeNextRefresh(GetDurationBeforeNextRefreshRequestType input)
234 throws PropertyAccessException
235 {
236 GetDurationBeforeNextRefreshResponseType oOutput = new GetDurationBeforeNextRefreshResponseType();
237
238 if ((input != null) &&
239 (input.getPropertyFile() != null) &&
240 (input.getPropertyFile().length() > 0))
241 {
242 String sPropertyFile = input.getPropertyFile();
243
244 int iValue = PropertyAccessor.getDurationBeforeNextRefresh(sPropertyFile);
245 oOutput.setDurationMillis(iValue);
246 }
247
248 return oOutput;
249 }
250
251 /**
252 * If a property file has been cached, this will force a refresh of the property
253 * file. If a property file is not cached, then this operation will do nothing.
254 *
255 * @param input The name of the property file.
256 * @return true if the property file was refreshed.
257 */
258 public static ForceRefreshResponseType forceRefresh(ForceRefreshRequestType input)
259 throws PropertyAccessException
260 {
261 ForceRefreshResponseType oOutput = new ForceRefreshResponseType();
262
263 if ((input != null) &&
264 (input.getPropertyFile() != null) &&
265 (input.getPropertyFile().length() > 0))
266 {
267 String sPropertyFile = input.getPropertyFile();
268
269 PropertyAccessor.forceRefresh(sPropertyFile);
270
271 // If we got here without an exception, then we refreshed.
272 //----------------------------------------------------------
273 oOutput.setRefreshed(true);
274 }
275
276 return oOutput;
277 }
278
279 /**
280 * This method will return the location of the property files. Essentially it
281 * will return the value in the NHINC_PROPERTIES_DIR environment variable.
282 *
283 * @param input Nothing important - just need this unique for document literal binding.
284 * @return The path and location of the property files.
285 */
286 public static GetPropertyFileLocationResponseType getPropertyFileLocation(GetPropertyFileLocationRequestType input)
287 throws PropertyAccessException
288 {
289 GetPropertyFileLocationResponseType oOutput = new GetPropertyFileLocationResponseType();
290
291 String sLocation = PropertyAccessor.getPropertyFileLocation();
292 oOutput.setLocation(sLocation);
293
294 return oOutput;
295 }
296
297 /**
298 * This method dumps the properties and associated values for a properties file to
299 * the log file.
300 *
301 * @param input the name of the property file.
302 * @return Nothing - it simply always returns true.
303 * @throws PropertyAccessException
304 */
305 public static DumpPropsToLogResponseType dumpPropsToLog(DumpPropsToLogRequestType input)
306 throws PropertyAccessException
307 {
308 DumpPropsToLogResponseType oOutput = new DumpPropsToLogResponseType();
309
310 if ((input != null) &&
311 (input.getPropertyFile() != null) &&
312 (input.getPropertyFile().length() > 0))
313 {
314 String sPropertyFile = input.getPropertyFile();
315
316 PropertyAccessor.dumpPropsToLog(sPropertyFile);
317
318 // If we got here without an exception, then we refreshed.
319 //----------------------------------------------------------
320 oOutput.setCompleted(true);
321 }
322
323 return oOutput;
324 }
325
326 /**
327 * This method writes out the given properties as the specified properties file.
328 * Note: It does not merge information. It will completely overwrite the current
329 * file with the new properties information. If the file does not exist, it will
330 * create it. This writes the property file to the NHINC properties directory.
331 *
332 * WARNING: If a property file is currently cached in memory - the file will not be
333 * re-read until the next time the cache refreshes its property from the file based on
334 * the criteria that was put in place when the properties were last loaded from file.
335 * This is based on setting of the "CacheRefreshDuration" property in the property file.
336 *
337 * NOTE THAT THERE MUST BE AT LEAST ONE PROPERTY TO WRITE.
338 *
339 * @param part1 The name of the property file and the properties to write.
340 * @return True if this succeeds.
341 * @throws PropertyAccessException
342 */
343 public static WritePropertyFileResponseType writePropertyFile(WritePropertyFileRequestType part1)
344 throws PropertyAccessException
345 {
346 WritePropertyFileResponseType oOutput = new WritePropertyFileResponseType();
347
348 if ((part1 != null) &&
349 (part1.getPropertyFile() != null) &&
350 (part1.getPropertyFile().length() > 0) &&
351 (part1.getProperties() != null) &&
352 (part1.getProperties().getProperty() != null) &&
353 (part1.getProperties().getProperty().size() > 0))
354 {
355 String sPropertyFile = part1.getPropertyFile();
356
357 java.util.Properties oPropsToStore = new java.util.Properties();
358
359 for (PropertyType oInputProp : part1.getProperties().getProperty())
360 {
361 if ((oInputProp.getPropertyName() == null) ||
362 (oInputProp.getPropertyName().length() <= 0))
363 {
364 throw new PropertyAccessException("Found a property without a name. All properties must have a name.");
365 }
366
367 if (oInputProp.getPropertyValue() == null)
368 {
369 throw new PropertyAccessException("The property value cannot be null. Property: " +
370 oInputProp.getPropertyName());
371 }
372
373 String sPropName = oInputProp.getPropertyName();
374 String sPropValue = oInputProp.getPropertyValue();
375
376 oPropsToStore.setProperty(sPropName, sPropValue);
377 }
378
379 PropertyFileManager.writePropertyFile(sPropertyFile, oPropsToStore);
380 }
381 else
382 {
383 int iCount = 0;
384 if ((part1 != null) &&
385 (part1.getProperties() != null) &&
386 (part1.getProperties().getProperty() != null))
387 {
388 iCount = part1.getProperties().getProperty().size();
389 }
390 String sErrorMessage = "Failed to write property file. There must be both a valid " +
391 "file name without the '.properties' extension and at least " +
392 "one property to write. PropertyFile:" + part1.getPropertyFile() +
393 ", PropertiesCount = " + iCount;
394 throw new PropertyAccessException(sErrorMessage);
395 }
396
397 oOutput.setSuccess(true); // If we got here we were successful
398 return oOutput;
399 }
400
401 /**
402 * This method deletes the specified properties file.
403 * Note: It will completely delete the file from the NHINC properties directory.
404 *
405 * WARNING: If a property file is currently cached in memory - the file will not be
406 * removed from memory until the next time the cache refreshes its property from the file based on
407 * the criteria that was put in place when the properties were last loaded from file.
408 * This is based on setting of the "CacheRefreshDuration" property in the property file.
409 *
410 * @param part1 The name of the property file to be deleted without the ".properties" extension.
411 * @return True if this succeeds.
412 * @throws PropertyAccessException
413 */
414 public static DeletePropertyFileResponseType deletePropertyFile(DeletePropertyFileRequestType part1)
415 throws PropertyAccessException
416 {
417 DeletePropertyFileResponseType oOutput = new DeletePropertyFileResponseType();
418
419 if ((part1 != null) &&
420 (part1.getPropertyFile() != null) &&
421 (part1.getPropertyFile().length() > 0))
422 {
423 String sPropertyFile = part1.getPropertyFile();
424 PropertyFileManager.deletePropertyFile(sPropertyFile);
425 }
426
427 oOutput.setSuccess(true); // If we got here we were successful
428 return oOutput;
429 }
430}
Note: See TracBrowser for help on using the repository browser.