1
2
3
4
5
6
7 package gov.bnl.gums;
8
9 import java.io.File;
10 import java.net.URI;
11 import java.net.URL;
12 import java.util.Date;
13 import org.apache.commons.logging.*;
14
15 /*** Implements the logic to retrieve the configuration from the gums.config file
16 * taken from the classpath. The file will be reloaded as soon as if it changes,
17 * on demand (no polling).
18 *
19 * @author Gabriele Carcassi
20 */
21 public class FileConfigurationStore implements ConfigurationStore {
22 private Log log = LogFactory.getLog(FileConfigurationStore.class);
23 private Log gumsSiteAdminLog = LogFactory.getLog(GUMS.siteAdminLog);
24 private Log gumsResourceAdminLog = LogFactory.getLog(GUMS.resourceAdminLog);
25 private Exception configError;
26
27 private Configuration conf;
28 private Date lastRetrival;
29 private String filename;
30
31 public FileConfigurationStore() {
32 }
33
34 /*** Allows to specify the absolute name of the configuration file
35 */
36 public FileConfigurationStore(String filename) {
37 this.filename = filename;
38 }
39
40 public boolean isActive() {
41 log.debug("Checking whether gums.config is present");
42 return ((filename != null) || (getConfURL() != null));
43 }
44
45 public boolean isReadOnly() {
46 return true;
47 }
48
49 public URL getConfURL() {
50 return getClass().getClassLoader().getResource("gums.config");
51 }
52
53 public synchronized Configuration retrieveConfiguration() {
54 if ((lastRetrival == null) || (lastRetrival.before(lastModification())))
55 reloadConfiguration();
56 if (configError != null) {
57 throw new RuntimeException("GUMS is misconfigured: please check the resource admin log for errors, and the gums.config file.");
58 }
59 return conf;
60 }
61
62 private Date lastModification() {
63 try {
64 if (filename != null) {
65
66 File file = new File(filename);
67 return new Date(file.lastModified());
68 } else {
69
70
71 URL confURL = getConfURL();
72 URI uri = new URI(confURL.toString());
73 File file = new File(uri);
74 return new Date(file.lastModified());
75 }
76 } catch (Exception e) {
77 gumsResourceAdminLog.fatal("The configuration wasn't read properly. GUMS is not operational.", e);
78 return null;
79 }
80 }
81
82 private void reloadConfiguration() {
83 conf = null;
84 configError = null;
85 try {
86 log.debug("Attempting to load configuration from gums.config");
87 if (filename != null) {
88 conf = ConfigurationToolkit.loadConfiguration(filename);
89 log.trace("Configuration reloaded from '" + filename + "'");
90 gumsResourceAdminLog.info("Configuration reloaded from '" + filename + "'");
91 gumsSiteAdminLog.info("Configuration reloaded from '" + filename + "'");
92 } else {
93 URL confURL = getConfURL();
94 conf = ConfigurationToolkit.loadConfiguration(confURL);
95 log.trace("Configuration reloaded from classpath '" + confURL + "'");
96 gumsResourceAdminLog.info("Configuration reloaded '" + confURL + "'");
97 gumsSiteAdminLog.info("Configuration reloaded '" + confURL + "'");
98 }
99 lastRetrival = new Date();
100 } catch (Exception e) {
101 configError = e;
102 gumsResourceAdminLog.fatal("The configuration wasn't read properly. GUMS is not operational: " + e.getMessage());
103 log.info("Configuration wasn't read correctly.", e);
104 }
105 }
106
107 public void storeConfiguration(Configuration conf) {
108 log.debug("Attempting to store configuration");
109 throw new UnsupportedOperationException("The FileConfiurationStore doesn't handle save operation");
110 }
111
112 }