1 | package gov.hhs.fha.nhinc.properties;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.util.Properties;
|
---|
5 | import java.io.FileWriter;
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * This class is used to manage a property file programmtically.
|
---|
9 | *
|
---|
10 | * @author Les Westberg
|
---|
11 | */
|
---|
12 | public class PropertyFileManager
|
---|
13 | {
|
---|
14 | /**
|
---|
15 | * This saves out the properties to the specified property file.
|
---|
16 | * If the file already exists, it is replaced by the specified one.
|
---|
17 | * If it does not exist, it is created.
|
---|
18 | *
|
---|
19 | * @param sPropertyFile The name of the property file without the ".properties" extension.
|
---|
20 | * @param oProps The contents to write out as the property file.
|
---|
21 | * @throws PropertyAccessException This exception is thrown if there are any errors.
|
---|
22 | */
|
---|
23 | public static void writePropertyFile (String sPropertyFile, Properties oProps)
|
---|
24 | throws PropertyAccessException
|
---|
25 | {
|
---|
26 | if ((sPropertyFile == null) ||
|
---|
27 | (sPropertyFile.length() <= 0))
|
---|
28 | {
|
---|
29 | throw new PropertyAccessException("writePropertyFile called with no property file name.");
|
---|
30 | }
|
---|
31 |
|
---|
32 | if (oProps == null)
|
---|
33 | {
|
---|
34 | throw new PropertyAccessException("writePropertyFile called with no property file.");
|
---|
35 | }
|
---|
36 |
|
---|
37 | String sPropFile = PropertyAccessor.getPropertyFileLocation() + sPropertyFile + ".properties";
|
---|
38 | FileWriter fwPropFile = null;
|
---|
39 | Exception eError = null;
|
---|
40 | String sErrorMessage = "";
|
---|
41 |
|
---|
42 | try
|
---|
43 | {
|
---|
44 | fwPropFile = new FileWriter(sPropFile);
|
---|
45 | oProps.store(fwPropFile, "");
|
---|
46 | }
|
---|
47 | catch (Exception e)
|
---|
48 | {
|
---|
49 | sErrorMessage = "Failed to store property file: " +
|
---|
50 | sPropFile + ". Error: " + e.getMessage();
|
---|
51 | eError = e;
|
---|
52 | }
|
---|
53 | finally
|
---|
54 | {
|
---|
55 | if (fwPropFile != null)
|
---|
56 | {
|
---|
57 | try
|
---|
58 | {
|
---|
59 | fwPropFile.close();
|
---|
60 | }
|
---|
61 | catch (Exception e)
|
---|
62 | {
|
---|
63 | sErrorMessage = "Failed to close property file: " +
|
---|
64 | sPropFile + ". Error: " + e.getMessage();
|
---|
65 | eError = e;
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | if (eError != null)
|
---|
71 | {
|
---|
72 | throw new PropertyAccessException(sErrorMessage, eError);
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Delete the specified property file.
|
---|
78 | *
|
---|
79 | * @param sPropertyFile The file to be deleted. This is the name of the property file
|
---|
80 | * without the ".properties" extension. It must be located in the
|
---|
81 | * configured properties directory.
|
---|
82 | * @throws gov.hhs.fha.nhinc.properties.PropertyAccessException This exception is thrown if
|
---|
83 | * there is an error.
|
---|
84 | */
|
---|
85 | public static void deletePropertyFile(String sPropertyFile)
|
---|
86 | throws PropertyAccessException
|
---|
87 | {
|
---|
88 | String sPropFile = PropertyAccessor.getPropertyFileLocation() + sPropertyFile + ".properties";
|
---|
89 | File fPropFile = new File(sPropFile);
|
---|
90 |
|
---|
91 | try
|
---|
92 | {
|
---|
93 | if (fPropFile.exists())
|
---|
94 | {
|
---|
95 | fPropFile.delete();
|
---|
96 | }
|
---|
97 | }
|
---|
98 | catch (Exception e)
|
---|
99 | {
|
---|
100 | throw new PropertyAccessException("Failed to delete file: " + sPropFile +
|
---|
101 | ". Error: " + e.getMessage(), e);
|
---|
102 | }
|
---|
103 |
|
---|
104 | }
|
---|
105 | }
|
---|