View Javadoc

1   /*
2    * BNLPersistenceFactory.java
3    *
4    * Created on February 3, 2005, 9:52 AM
5    */
6   
7   package gov.bnl.gums.persistence;
8   
9   import gov.bnl.gums.configuration.Configuration;
10  import gov.bnl.gums.db.AccountPoolMapperDB;
11  import gov.bnl.gums.db.ManualAccountMapperDB;
12  import gov.bnl.gums.db.ManualUserGroupDB;
13  import gov.bnl.gums.db.UserGroupDB;
14  
15  import java.util.ArrayList;
16  import java.util.Date;
17  import java.util.Enumeration;
18  import java.util.Iterator;
19  import java.util.List;
20  import java.util.Properties;
21  import java.util.StringTokenizer;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  /**
27   * Persistence factory instantiation that combines a hibernate persistence factory
28   * with a small part of the ldap persistence factory (see addToSecondaryGroup).  When a pool 
29   * account is assigned a group, this is set in ldap.
30   *
31   * @author Gabriele Carcassi, Jay Packard
32   */
33  public class LocalPersistenceFactory extends PersistenceFactory {
34      /**
35       * Implements AccountPoolMapperDB for the local persistence factory
36       * 
37       * @author Gabriele Carcassi, Jay Packard
38       */
39      private class LocalAccountPoolMapperDB implements AccountPoolMapperDB {
40          private AccountPoolMapperDB db;
41          private String group;
42          private List secondaryGroups;
43          
44          public LocalAccountPoolMapperDB(String poolname, String group, List secondaryGroups) {
45              this.group = group;
46              this.secondaryGroups = secondaryGroups;
47              db = persFactory.retrieveAccountPoolMapperDB(poolname);
48          }
49          
50          public void addAccount(String account) {
51              db.addAccount(account);
52          }
53  
54          public String assignAccount(String userDN) {
55              String account = db.assignAccount(userDN);
56              if (account == null) return null;
57              log.trace("Assigned '" + account + "' to '" + userDN);
58              try {
59                  assignGroups(account);
60                  return account;
61              } catch (RuntimeException e) {
62                  log.trace("Group assignment failed: unassign user", e);
63                  unassignUser(userDN);
64                  log.trace("User unassigned");
65                  throw e;
66              }
67          }
68          
69          public boolean needsCacheRefresh() {
70          	return true;
71          }
72          
73          public boolean removeAccount(String account) {
74          	return db.removeAccount(account);
75          }
76  
77          public String retrieveAccount(String userDN) {
78              String account = db.retrieveAccount(userDN);
79              if (account!=null && synchGroups) {
80                  assignGroups(account);
81              }
82              return account;
83          }
84  
85          public java.util.Map retrieveAccountMap() {
86              return db.retrieveAccountMap();
87          }
88          
89          public java.util.Map retrieveReverseAccountMap() {
90              return db.retrieveReverseAccountMap();
91          }
92  
93          public List retrieveUsersNotUsedSince(Date date) {
94              return db.retrieveUsersNotUsedSince(date);
95          }
96          
97          synchronized public void setNeedsCacheRefresh(boolean value) {
98          }
99  
100         public void unassignAccount(String account) {
101         	db.unassignAccount(account);
102         }
103         
104         public void unassignUser(String user) {
105             db.unassignUser(user);
106         }
107         
108         private void assignGroups(String account) {
109             ldap.changeGroupID(account, group);
110             log.trace("Assigned '" + group + "' to '" + account);
111             Iterator iter = secondaryGroups.iterator();
112             while (iter.hasNext()) {
113                 String group = (String) iter.next();
114                 ldap.addToSecondaryGroup(account, group);
115                 log.trace("Assigned secondary group '" + group + "' to '" + account);
116             }
117         }
118         
119     }
120     
121     static public String getTypeStatic() {
122 		return "local";
123 	}
124     private Log log = LogFactory.getLog(LocalPersistenceFactory.class);    
125     private HibernatePersistenceFactory persFactory;
126 	private LDAPPersistenceFactory ldap;
127     
128 	private boolean synchGroups = false;
129 	
130     /**
131      * Create a new local persistence factory.  This empty constructor is needed by the XML Digester.
132      */
133     public LocalPersistenceFactory() {
134     	super();
135         persFactory = new HibernatePersistenceFactory();
136         ldap = new LDAPPersistenceFactory();   	
137     	log.trace("LocalPersistenceFactory instanciated");
138     }
139     
140     /**
141      * Create a new local persistence factory with a configuration.
142      * 
143      * @param configuration
144      */
145     public LocalPersistenceFactory(Configuration configuration) {
146     	super(configuration);
147         persFactory = new HibernatePersistenceFactory(configuration);
148         ldap = new LDAPPersistenceFactory(configuration);   	
149     	log.trace("LocalPersistenceFactory instanciated");
150     }
151     
152     /**
153      * Create a new local persistence factory with a configuration and a name.
154      * 
155      * @param configuration
156      * @param name
157      */
158     public LocalPersistenceFactory(Configuration configuration, String name) {
159     	super(configuration, name);
160         persFactory = new HibernatePersistenceFactory(configuration, name);
161         ldap = new LDAPPersistenceFactory(configuration, name);  
162     	log.trace("LocalPersistenceFactory instanciated");
163     }
164  
165     public PersistenceFactory clone(Configuration configuration) {
166     	LocalPersistenceFactory persistenceFactory = new LocalPersistenceFactory(configuration, new String(getName()));
167     	persistenceFactory.setDescription(new String(getDescription()));
168 //   	persistenceFactory.setCaCertFile(getCaCertFile());
169 //    	persistenceFactory.setTrustStorePassword(getTrustStorePassword());
170     	persistenceFactory.setGroupIdField(new String(getGroupIdField()));
171     	persistenceFactory.setAccountField(new String(getAccountField()));
172     	persistenceFactory.setMemberAccountField(new String(getMemberAccountField()));
173     	persistenceFactory.setProperties((Properties)getProperties().clone());
174     	persistenceFactory.setSynchGroups(persistenceFactory.isSynchGroups());
175     	return persistenceFactory;
176     }
177     
178     public String getCaCertFile() {
179     	return ldap.getCaCertFile();
180     }
181 
182     public String getAccountField() {
183     	return ldap.getAccountField();
184     }    
185     
186     public String getGroupIdField() {
187     	return ldap.getGroupIdField();
188     }
189     
190     public String getMemberAccountField() {
191     	return ldap.getMemberAccountField();
192     }
193     
194     public Properties getLDAPProperties() {
195         return filterProperties("ldap.");
196     }
197 
198     public Properties getMySQLProperties() {
199         return filterProperties("mysql.");
200     }
201     
202     public String getTrustStorePassword() {
203     	return ldap.getTrustStorePassword();
204     }
205     
206     public String getType() {
207 		return "local";
208 	}
209     
210     /**
211      * Getter for property synchGroups.
212      * @return Value of property synchGroups.
213      */
214     public boolean isSynchGroups() {
215         return this.synchGroups;
216     }
217 
218     public AccountPoolMapperDB retrieveAccountPoolMapperDB(String name) {
219         StringTokenizer tokens = new StringTokenizer(name, ".");
220         if (!tokens.hasMoreTokens()) {
221             return persFactory.retrieveAccountPoolMapperDB(name);
222         }
223         String pool = tokens.nextToken();
224         if (!tokens.hasMoreTokens()) {
225             return persFactory.retrieveAccountPoolMapperDB(name);
226         }
227         String group = tokens.nextToken();
228         List secondaryGroups = new ArrayList();
229         while (tokens.hasMoreTokens()) {
230             secondaryGroups.add(tokens.nextToken());
231         }
232         return new LocalAccountPoolMapperDB(pool, group, secondaryGroups);
233     }
234     
235     public ManualAccountMapperDB retrieveManualAccountMapperDB(String name) {
236         return persFactory.retrieveManualAccountMapperDB(name);
237     }
238     
239     public ManualUserGroupDB retrieveManualUserGroupDB(String name) {
240         return persFactory.retrieveManualUserGroupDB(name);
241     }
242     
243     public UserGroupDB retrieveUserGroupDB(String name) {
244         return persFactory.retrieveUserGroupDB(name);
245     }
246     
247     public void setCaCertFile(String caCertFile) {
248         ldap.setCaCertFile( caCertFile );
249     }
250     
251     public void setConfiguration(Configuration configuration) {
252     	super.setConfiguration(configuration);
253     }
254         
255     public void setAccountField(String accountField) {
256     	ldap.setAccountField(accountField);
257     }
258     
259     public void setMemberAccountField(String memberAccountField) {
260     	ldap.setMemberAccountField(memberAccountField);
261     }
262     
263     public void setGroupIdField(String groupIdField) {
264     	ldap.setGroupIdField(groupIdField);
265     }
266     
267     public void setName(String name) {
268     	super.setName(name);
269     }
270 
271     public void setProperties(Properties properties) {
272         super.setProperties(properties);
273         persFactory.setProperties(getMySQLProperties());
274         ldap.setProperties(getLDAPProperties());
275     }
276     
277     public void setSynchGroups(boolean synchGroups) {
278     	this.synchGroups = synchGroups;
279     }
280 
281     public void setTrustStorePassword(String trustStorePassword) {
282         ldap.setTrustStorePassword(trustStorePassword);
283     }    
284     
285 	public String toXML() {
286     	String retStr = "\t\t<localPersistenceFactory\n"+
287     		"\t\t\tname='"+getName()+"'\n"+
288     		"\t\t\tdescription='"+getDescription()+"'\n"+
289     		"\t\t\tsynchGroups='"+synchGroups+"'\n"+
290 //    		"\t\t\tcaCertFile='"+getCaCertFile()+"'\n"+
291 //    		"\t\t\ttrustStorePassword='"+getTrustStorePassword()+"'\n"+
292     		"\t\t\tgroupIdField='"+getGroupIdField()+"'\n"+
293     		"\t\t\taccountField='"+getAccountField()+"'\n"+
294 			"\t\t\tmemberAccountField='"+getMemberAccountField()+"'\n";
295     	
296     	Iterator keyIt = getProperties().keySet().iterator();
297     	while(keyIt.hasNext()) {
298     		String key = (String)keyIt.next();
299     		retStr += "\t\t\t"+key+"='"+getProperties().getProperty(key)+"'\n";
300     	}
301 
302     	if (retStr.charAt(retStr.length()-1)=='\n')
303     		retStr = retStr.substring(0, retStr.length()-1);    	
304     	
305     	retStr += "/>\n\n";
306     	
307     	return retStr;
308 	}
309     
310     private Properties filterProperties(String prefix) {
311         Properties filtered = new Properties();
312         Enumeration keys = getProperties().propertyNames();
313         while (keys.hasMoreElements()) {
314             String name = (String) keys.nextElement();
315             if (name.startsWith(prefix)) {
316                 filtered.setProperty(name.substring(prefix.length()), getProperties().getProperty(name));
317             }
318         }
319         return filtered;
320     }
321    
322 }