1
2
3
4
5
6
7 package gov.bnl.gums;
8
9 import java.util.Timer;
10 import java.util.TimerTask;
11 import javax.naming.Context;
12 import javax.naming.InitialContext;
13 import javax.naming.NamingException;
14 import org.apache.commons.logging.*;
15
16 /*** Facade for the whole business logic available in GUMS. Using GUMS means
17 * instanciating an object of this class, and use it to reach the rest of the
18 * functionalities.
19 *
20 * @author Gabriele Carcassi
21 */
22 public class GUMS {
23 static final public String siteAdminLog = "gums.siteAdmin";
24 static final public String resourceAdminLog = "gums.resourceAdmin";
25 static private Log log = LogFactory.getLog(GUMS.class);
26 static private Log gumsResourceAdminLog = LogFactory.getLog(GUMS.resourceAdminLog);
27 private static Timer timer;
28
29 private Configuration conf;
30 protected ConfigurationStore confStore;
31 private ResourceManager resMan = new ResourceManager(this);
32
33 /*** Creates and initilializes a new instance of GUMS. */
34 public GUMS() {
35 confStore = new FileConfigurationStore();
36 if (!confStore.isActive()) {
37 gumsResourceAdminLog.fatal("Couldn't read GUMS policy file (gums.config)");
38 }
39
40 startUpdateThread(this);
41 }
42
43 public GUMS(ConfigurationStore confStore) {
44 this.confStore = confStore;
45 if (!confStore.isActive()) {
46 gumsResourceAdminLog.fatal("Couldn't read GUMS policy file (gums.config)");
47 }
48
49 startUpdateThread(this);
50 }
51
52 /*** @todo This constructor should probably go away... */
53 public GUMS(Configuration conf) {
54 this.conf = conf;
55 }
56
57 private static synchronized void startUpdateThread(final GUMS gums) {
58
59 if (timer == null) {
60 try {
61 Context env = (Context) new InitialContext().lookup("java:comp/env");
62 Integer minutes = (Integer) env.lookup("updateGroupsMinutes");
63 if (minutes != null) {
64 timer = new Timer();
65 TimerTask updateTask = new TimerTask() {
66 public void run() {
67 try {
68 gumsResourceAdminLog.info("Starting automatic updateGroups");
69 gums.getResourceManager().updateGroups();
70 gumsResourceAdminLog.info("Automatic updateGroups ended");
71 } catch (RuntimeException e) {
72 gumsResourceAdminLog.error("Automatic updateGroups failed - " + e.getMessage());
73 log.info("Automatic updateGroups failed", e);
74 }
75 }
76 };
77 timer.scheduleAtFixedRate(updateTask, 0, minutes.intValue()*60*1000);
78 gumsResourceAdminLog.info("Automatic updateGroups set: will refresh every " + minutes.intValue() + " minutes starting now.");
79 } else {
80 gumsResourceAdminLog.warn("Didn't start the automatic updateGroups: 'updateGroupsMinutes' was set to null.");
81 }
82 } catch (NamingException e) {
83 gumsResourceAdminLog.warn("Didn't start the automatic updateGroups: " + e.getMessage());
84 log.warn("Couldn't read JNDI property: " + e.getMessage(), e);
85 }
86 }
87 }
88
89 /***
90 * Retrieve the ResourceManager to perform actions on the business logic.
91 * @return the resource manager.
92 */
93 public ResourceManager getResourceManager() {
94 return resMan;
95 }
96
97 /***
98 * Retrieves the configuration being used by GUMS. The configuration might
99 * change from one call to the other. Therefore, the business logic needs
100 * to cache the value returned for the duration of a whole call, and not
101 * further.
102 * @return the current configuration or null.
103 */
104 public Configuration getConfiguration() {
105 if (confStore != null) return confStore.retrieveConfiguration();
106 return conf;
107 }
108
109 /***
110 * Changes the configuration used by GUMS.
111 * <p>
112 * @todo should this method actually save the configuration?
113 * @param conf the new configuration
114 */
115 public void setConfiguration(Configuration conf) {
116 this.conf = conf;
117 if ((confStore != null) && (!confStore.isReadOnly()))
118 confStore.storeConfiguration(conf);
119 }
120 }