1
2
3
4
5
6
7 package gov.bnl.gums;
8
9 import gov.bnl.gums.configuration.Configuration;
10 import gov.bnl.gums.configuration.ConfigurationStore;
11 import gov.bnl.gums.configuration.FileConfigurationStore;
12
13 import java.util.Collection;
14 import java.util.Timer;
15 import java.util.TimerTask;
16 import javax.naming.Context;
17 import javax.naming.InitialContext;
18 import javax.naming.NamingException;
19
20 import org.apache.commons.logging.*;
21
22
23
24
25
26
27
28
29 public class GUMS {
30 static final public String siteAdminLog = "gums.siteAdmin";
31 static final public String resourceAdminLog = "gums.resourceAdmin";
32 static private Log log = LogFactory.getLog(GUMS.class);
33 static private Log gumsResourceAdminLog = LogFactory.getLog(GUMS.resourceAdminLog);
34 static private Timer timer;
35
36
37
38
39
40
41 static private synchronized void startUpdateThread(final GUMS gums) {
42
43 if (timer == null) {
44 try {
45 Context env = (Context) new InitialContext().lookup("java:comp/env");
46 Integer minutes = (Integer) env.lookup("updateGroupsMinutes");
47 if (minutes != null) {
48 timer = new Timer();
49 TimerTask updateTask = new TimerTask() {
50 public void run() {
51 try {
52 gumsResourceAdminLog.info("Starting automatic updateGroups");
53 gums.getResourceManager().updateGroups();
54 gumsResourceAdminLog.info("Automatic updateGroups ended");
55 } catch (Exception e) {
56 gumsResourceAdminLog.error("Automatic updateGroups failed - " + e.getMessage());
57 log.info("Automatic updateGroups failed", e);
58 }
59 }
60 };
61 timer.scheduleAtFixedRate(updateTask, 0, minutes.intValue()*60*1000);
62 gumsResourceAdminLog.info("Automatic updateGroups set: will refresh every " + minutes.intValue() + " minutes starting now.");
63 } else {
64 gumsResourceAdminLog.warn("Didn't start the automatic updateGroups: 'updateGroupsMinutes' was set to null.");
65 }
66 } catch (NamingException e) {
67 gumsResourceAdminLog.warn("Didn't start the automatic updateGroups: " + e.getMessage());
68 log.warn("Couldn't read JNDI property: " + e.getMessage(), e);
69 }
70 }
71 }
72
73 private Configuration conf;
74 private ResourceManager resMan = new ResourceManager(this);
75 protected ConfigurationStore confStore;
76
77
78
79
80 public GUMS(Configuration conf) {
81 this.conf = conf;
82 }
83
84
85
86
87 public GUMS() {
88 confStore = new FileConfigurationStore();
89 if (!confStore.isActive()) {
90 gumsResourceAdminLog.fatal("Couldn't read GUMS policy file (gums.config)");
91 }
92
93 startUpdateThread(this);
94 }
95
96
97
98
99
100
101 public GUMS(ConfigurationStore confStore) {
102 this.confStore = confStore;
103 if (!confStore.isActive()) {
104 gumsResourceAdminLog.fatal("Couldn't read GUMS policy file (gums.config)");
105 }
106
107 startUpdateThread(this);
108 }
109
110
111
112
113
114
115 public void deleteBackupConfiguration(String dateStr) {
116 if (confStore != null) confStore.deleteBackupConfiguration(dateStr);
117 }
118
119
120
121
122
123
124 public Collection getBackupConfigDates() {
125 if (confStore != null) return confStore.getBackupConfigDates();
126 return null;
127 }
128
129
130
131
132
133
134
135
136
137 public Configuration getConfiguration() {
138 if (confStore != null) return confStore.retrieveConfiguration();
139 return conf;
140 }
141
142
143
144
145
146
147 public ResourceManager getResourceManager() {
148 return resMan;
149 }
150
151
152
153
154
155
156 public void restoreConfiguration(String dateStr) {
157 try {
158 this.conf = conf;
159 if (!confStore.isReadOnly()) {
160 confStore.restoreConfiguration(dateStr);
161 }
162 else
163 throw new RuntimeException("cannot write configuration because it is read-only");
164 } catch(Exception e) {
165 throw new RuntimeException("cannot write configuration: " + e.getMessage());
166 }
167 }
168
169
170
171
172
173
174 public void setConfiguration(Configuration conf, boolean backup) {
175 try {
176 this.conf = conf;
177 if (!confStore.isReadOnly()) {
178 confStore.setConfiguration(conf, backup);
179 }
180 else
181 throw new RuntimeException("cannot write configuration because it is read-only");
182 } catch(Exception e) {
183 throw new RuntimeException("cannot write configuration: " + e.getMessage());
184 }
185 }
186
187 }