View Javadoc

1   /*
2    * GUMS.java
3    *
4    * Created on June 3, 2004, 10:39 AM
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   * Facade for the whole business logic available in GUMS. Using GUMS means
24   * instanciating an object of this class, and use it to reach the rest of the
25   * functionalities.
26   *
27   * @author  Gabriele Carcassi, Jay Packard
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       * Create a thread that updates user group membership every so often
38       * 
39       * @param gums
40       */
41      static private synchronized void startUpdateThread(final GUMS gums) {
42          // If JNDI property is set, run the update every x minutes
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       * Creates and initilializes a new instance of GUMS (should only be used for testing).
79       */
80      public GUMS(Configuration conf) {
81      	this.conf = conf;
82      }
83      
84      /**
85       * Creates and initilializes a new instance of GUMS.
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       * Creates and initilializes a new instance of GUMS with a specified configuration store
98       * 
99       * @param confStore
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      * Delete a backup configuration by date
112      * 
113      * @param dateStr
114      */
115     public void deleteBackupConfiguration(String dateStr) {
116     	if (confStore != null) confStore.deleteBackupConfiguration(dateStr);	
117     }
118     
119     /**
120      * Get a list of dates for which a backup gums.config exists
121      * 
122      * @return Collection of date strings
123      */
124     public Collection getBackupConfigDates() {
125     	if (confStore != null) return confStore.getBackupConfigDates();
126         return null;
127     }
128 
129     /**
130      * Retrieves the configuration being used by GUMS. The configuration might
131      * change from one call to the other. Therefore, the business logic needs
132      * to cache the value returned for the duration of a whole call, and not
133      * further. 
134      * 
135      * @return current configuration or null.
136      */
137     public Configuration getConfiguration() {
138         if (confStore != null) return confStore.retrieveConfiguration();
139         return conf;
140     }
141     
142     /**
143      * Retrieve the ResourceManager to perform actions on the business logic.
144      * 
145      * @return the resource manager.
146      */
147     public ResourceManager getResourceManager() {
148         return resMan;
149     }
150     
151     /**
152      * Restore a configuration from a certain date
153      * 
154      * @param dateStr
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      * Changes the configuration used by GUMS.
171      * 
172      * @param conf the new configuration
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 }