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.GridUser;
10  import gov.bnl.gums.configuration.Configuration;
11  import gov.bnl.gums.db.AccountPoolMapperDB;
12  import gov.bnl.gums.db.LDAPAccountMapperDB;
13  import gov.bnl.gums.db.ManualAccountMapperDB;
14  import gov.bnl.gums.db.ManualUserGroupDB;
15  import gov.bnl.gums.db.UserGroupDB;
16  import gov.bnl.gums.db.ConfigurationDB;
17  
18  import java.lang.ref.SoftReference;
19  import java.util.ArrayList;
20  import java.util.Date;
21  import java.util.Enumeration;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Properties;
25  import java.util.StringTokenizer;
26  
27  import org.apache.log4j.Logger;
28  import org.hibernate.SessionFactory;
29  
30  /**
31   * Persistence factory instantiation that combines a hibernate persistence factory
32   * with a small part of the ldap persistence factory (see addToSecondaryGroup).  When a pool 
33   * account is assigned a group, this is set in ldap.
34   *
35   * @author Gabriele Carcassi, Jay Packard
36   */
37  public class LocalPersistenceFactory extends PersistenceFactory {
38  	/**
39  	 * Implements AccountPoolMapperDB for the local persistence factory
40  	 * 
41  	 * @author Gabriele Carcassi, Jay Packard
42  	 */
43  	private class LocalAccountPoolMapperDB implements AccountPoolMapperDB {
44  		private AccountPoolMapperDB db;
45  		private String group;
46  		private List secondaryGroups;
47  
48  		public LocalAccountPoolMapperDB(String poolname, String group, List secondaryGroups) {
49  			this.group = group;
50  			this.secondaryGroups = secondaryGroups;
51  			db = persFactory.retrieveAccountPoolMapperDB(poolname);
52  		}
53  
54  		public void addAccount(String account) {
55  			db.addAccount(account);
56  		}
57  
58  		public String assignAccount(GridUser user) {
59  			String account = db.assignAccount(user);
60  			if (account == null) return null;
61  			log.trace("Assigned '" + account + "' to '" + user.getCertificateDN());
62  			try {
63  				assignGroups(account);
64  				assignEmail(account, user.getEmail());
65  				return account;
66  			} catch (RuntimeException e) {
67  				log.trace("Group assignment failed: unassign user", e);
68  				unassignUser(user.getCertificateDN());
69  				log.trace("User unassigned");
70  				throw e;
71  			}
72  		}
73  		
74  	    public String getMap() {
75  	    	return db.getMap();
76  	    }
77  
78  		public boolean needsCacheRefresh() {
79  			return db.needsCacheRefresh();
80  		}
81  
82  		public boolean removeAccount(String account) {
83  			return db.removeAccount(account);
84  		}
85  
86  		public String retrieveAccount(GridUser user) {
87  			String account = db.retrieveAccount(user);
88  			if (account!=null && synch) {
89  				assignGroups(account);
90  				assignEmail(account, user.getEmail());
91  			}
92  			return account;
93  		}
94  
95  		public java.util.Map retrieveAccountMap() {
96  			return db.retrieveAccountMap();
97  		}
98  
99  		public java.util.Map retrieveReverseAccountMap() {
100 			return db.retrieveReverseAccountMap();
101 		}
102 
103 		public List retrieveUsersNotUsedSince(Date date) {
104 			return db.retrieveUsersNotUsedSince(date);
105 		}
106 
107 		synchronized public void setCacheRefreshed() {
108 			db.setCacheRefreshed();
109 		}
110 
111 		public void unassignAccount(String account) {
112 			db.unassignAccount(account);
113 		}
114 
115 		public void unassignUser(String user) {
116 			db.unassignUser(user);
117 		}
118 
119 		private void assignEmail(String account, String email) {
120 			ldap.changeEmail(account, email);
121 			log.trace("Assigned email '" + email + "' to '" + account);
122 		}
123 		
124 		private void assignGroups(String account) {
125 			ldap.changeGroupID(account, group);
126 			log.trace("Assigned '" + group + "' to '" + account);
127 			Iterator iter = secondaryGroups.iterator();
128 			while (iter.hasNext()) {
129 				String group = (String) iter.next();
130 				ldap.addToSecondaryGroup(account, group);
131 				log.trace("Assigned secondary group '" + group + "' to '" + account);
132 			}
133 		}
134 
135 	}
136 
137 	static public String getTypeStatic() {
138 		return "local";
139 	}
140 	private Logger log = Logger.getLogger(LocalPersistenceFactory.class);    
141 	private HibernatePersistenceFactory persFactory;
142 	private LDAPPersistenceFactory ldap;
143 
144 	private boolean synch = false;
145 
146 	/**
147 	 * Create a new local persistence factory.  This empty constructor is needed by the XML Digester.
148 	 */
149 	public LocalPersistenceFactory() {
150 		super();
151 		persFactory = new HibernatePersistenceFactory();
152 		ldap = new LDAPPersistenceFactory();   	
153 		log.trace("LocalPersistenceFactory instanciated");
154 	}
155 
156 	/**
157 	 * Create a new local persistence factory with a configuration.
158 	 * 
159 	 * @param configuration
160 	 */
161 	public LocalPersistenceFactory(Configuration configuration) {
162 		super(configuration);
163 		persFactory = new HibernatePersistenceFactory(configuration);
164 		ldap = new LDAPPersistenceFactory(configuration);   	
165 		log.trace("LocalPersistenceFactory instanciated");
166 	}
167 
168 	/**
169 	 * Create a new local persistence factory with a configuration and a name.
170 	 * 
171 	 * @param configuration
172 	 * @param name
173 	 */
174 	public LocalPersistenceFactory(Configuration configuration, String name) {
175 		super(configuration, name);
176 		persFactory = new HibernatePersistenceFactory(configuration, name);
177 		ldap = new LDAPPersistenceFactory(configuration, name);  
178 		log.trace("LocalPersistenceFactory instanciated");
179 	}
180 	
181 	public PersistenceFactory clone(Configuration configuration) {
182 		LocalPersistenceFactory persistenceFactory = new LocalPersistenceFactory(configuration, new String(getName()));
183 		persistenceFactory.setDescription(new String(getDescription()));
184 		persistenceFactory.setStoreConfig(getStoreConfig());
185 //		persistenceFactory.setCaCertFile(getCaCertFile());
186 //		persistenceFactory.setTrustStorePassword(getTrustStorePassword());
187 		persistenceFactory.setPeopleTree(new String(getPeopleTree()));
188 		persistenceFactory.setGroupTree(new String(getGroupTree()));
189 		persistenceFactory.setGumsTree(new String(getGumsTree()));
190 		persistenceFactory.setGroupCnField(new String(getGroupCnField()));
191 		persistenceFactory.setUidField(new String(getUidField()));
192 		persistenceFactory.setGidNumberField(new String(getGidNumberField()));
193 		persistenceFactory.setMemberUidField(new String(getMemberUidField()));
194 		persistenceFactory.setProperties((Properties)getProperties().clone());
195 		persistenceFactory.setSynch(persistenceFactory.isSynch());
196 		persistenceFactory.setEmailField(new String(persistenceFactory.getEmailField()));
197 		return persistenceFactory;
198 	}
199 
200 	public String getCaCertFile() {
201 		return ldap.getCaCertFile();
202 	}
203 	
204 	public String getEmailField() {
205 		return ldap.getEmailField();
206 	}
207 
208 	public String getGidNumberField() {
209 		return ldap.getGidNumberField();
210 	}
211 
212 	public String getGroupCnField() {
213 		return ldap.getGroupCnField();
214 	}
215 
216 	public String getGroupTree() {
217 		return ldap.getGroupTree();
218 	}
219 
220 	public String getGumsObject() {
221 		return ldap.getGumsObject();
222 	}
223 	
224 	public String getGumsTree() {
225 		return ldap.getGumsTree();
226 	}
227 	
228 	public Properties getLDAPProperties() {
229 		return filterProperties("ldap.");
230 	}
231 
232 	public String getMemberUidField() {
233 		return ldap.getMemberUidField();
234 	}
235 
236 	public Properties getMySQLProperties() {
237 		return filterProperties("mysql.");
238 	}
239 
240 	public String getPeopleTree() {
241 		return ldap.getPeopleTree();
242 	}
243 
244 	public String getTrustStorePassword() {
245 		return ldap.getTrustStorePassword();
246 	}
247 
248 	public String getType() {
249 		return "local";
250 	}
251 
252 	public String getUidField() {
253 		return ldap.getUidField();
254 	}
255 
256 	/**
257 	 * Getter for property synchGroups.
258 	 * @return Value of property synchGroups.
259 	 */
260 	public boolean isSynch() {
261 		return this.synch;
262 	}
263 	/**
264 	 * @depricated
265 	 */
266 	public boolean isSynchGroups() {
267 		return this.synch;
268 	}
269 
270 	public AccountPoolMapperDB retrieveAccountPoolMapperDB(String name) {
271 		StringTokenizer tokens = new StringTokenizer(name, ".");
272 		if (!tokens.hasMoreTokens()) {
273 			return persFactory.retrieveAccountPoolMapperDB(name);
274 		}
275 		String pool = tokens.nextToken();
276 		if (!tokens.hasMoreTokens()) {
277 			return persFactory.retrieveAccountPoolMapperDB(name);
278 		}
279 		String group = tokens.nextToken();
280 		List secondaryGroups = new ArrayList();
281 		while (tokens.hasMoreTokens()) {
282 			secondaryGroups.add(tokens.nextToken());
283 		}
284 		return new LocalAccountPoolMapperDB(pool, group, secondaryGroups);
285 	}
286 
287 	public ConfigurationDB retrieveConfigurationDB() {
288 		return persFactory.retrieveConfigurationDB();
289 	}
290 	
291 	public ManualAccountMapperDB retrieveManualAccountMapperDB(String name) {
292 		return persFactory.retrieveManualAccountMapperDB(name);
293 	}
294 
295 	public ManualUserGroupDB retrieveManualUserGroupDB(String name) {
296 		return persFactory.retrieveManualUserGroupDB(name);
297 	}
298 
299 	public UserGroupDB retrieveUserGroupDB(String name) {
300 		return persFactory.retrieveUserGroupDB(name);
301 	}
302 
303 	// @depricated
304 	public void setAccountField(String accountField) {
305 		ldap.setAccountField(accountField);
306 	}
307 
308 	public void setCaCertFile(String caCertFile) {
309 		ldap.setCaCertFile( caCertFile );
310 	}
311 
312 	public void setConfiguration(Configuration configuration) {
313 		super.setConfiguration(configuration);
314 		ldap.setConfiguration(configuration);
315 		persFactory.setConfiguration(configuration);
316 	}
317 	
318 	public void setEmailField(String emailField) {
319 		ldap.setEmailField(emailField);
320 	}
321 
322 	public void setGidNumberField(String gidNumberField) {
323 		ldap.setGidNumberField(gidNumberField);
324 	}
325 	
326 	public void setGroupCnField(String groupCnField) {
327 		ldap.setGroupCnField(groupCnField);
328 	}
329 	// @depricated
330 	public void setGroupField(String groupField) {
331 		ldap.setGroupCnField(groupField);
332 	}
333 
334 	// @depricated
335 	public void setGroupIdField(String groupIdField) {
336 		ldap.setGroupIdField(groupIdField);
337 	}
338 	public void setGroupTree(String groupTree) {
339 		ldap.setGroupTree(groupTree);
340 	}
341 
342 	public void setGumsTree(String gumsTree) {
343 		ldap.setGumsTree(gumsTree);
344 	}
345 	// @depricated 
346 	public void setMemberAccountField(String memberAccountField) {
347 		ldap.setMemberAccountField(memberAccountField);
348 	}
349 
350 	public void setMemberUidField(String memberUidField) {
351 		ldap.setMemberUidField(memberUidField);
352 	}
353 	
354 	public void setName(String name) {
355 		super.setName(name);
356 		ldap.setName(name);
357 		persFactory.setName(name);
358 	}
359 
360 	public void setPeopleTree(String peopleTree) {
361 		ldap.setPeopleTree(peopleTree);
362 	}
363 
364 	public void setProperties(Properties properties) {
365 		super.setProperties(properties);
366 		persFactory.setProperties(getMySQLProperties());
367 		ldap.setProperties(getLDAPProperties());
368 	}
369 
370 	public void setSynch(boolean synch) {
371 		this.synch = synch;
372 	}
373 
374 	/**
375 	 * @depricated
376 	 */
377 	public void setSynchGroups(boolean synchGroups) {
378 		this.synch = synchGroups;
379 	}
380 	public void setTrustStorePassword(String trustStorePassword) {
381 		ldap.setTrustStorePassword(trustStorePassword);
382 	}
383 
384 	public void setUidField(String uidField) {
385 		ldap.setUidField(uidField);
386 	}    
387 
388 	public String toXML() {
389 		String retStr = "\t\t<localPersistenceFactory\n"+
390 		"\t\t\tname='"+getName()+"'\n"+
391 		"\t\t\tdescription='"+getDescription()+"'\n"+
392 		"\t\t\tstoreConfig='"+(getStoreConfig()?"true":"false")+"'\n"+
393 		"\t\t\tsynch='"+synch+"'\n"+
394 //		"\t\t\tcaCertFile='"+getCaCertFile()+"'\n"+
395 //		"\t\t\ttrustStorePassword='"+getTrustStorePassword()+"'\n"+
396 		"\t\t\tgidNumberField='"+getGidNumberField()+"'\n"+
397 		"\t\t\tgroupCnField='"+getGroupCnField()+"'\n"+
398 		"\t\t\tuidField='"+getUidField()+"'\n"+
399 		"\t\t\tmemberUidField='"+getMemberUidField()+"'\n"+
400 		"\t\t\temailField='"+getEmailField()+"'\n"+
401 		"\t\t\tgroupTree='"+getGroupTree()+"'\n"+
402 		"\t\t\tgumsTree='"+getGroupTree()+"'\n"+
403 		"\t\t\tpeopleTree='"+getPeopleTree()+"'\n";
404 		
405 		Iterator keyIt = getProperties().keySet().iterator();
406 		while(keyIt.hasNext()) {
407 			String key = (String)keyIt.next();
408 			retStr += "\t\t\t"+key+"='"+getProperties().getProperty(key)+"'\n";
409 		}
410 
411 		if (retStr.charAt(retStr.length()-1)=='\n')
412 			retStr = retStr.substring(0, retStr.length()-1);    	
413 
414 		retStr += "/>\n\n";
415 
416 		return retStr;
417 	}
418 	
419 	private Properties filterProperties(String prefix) {
420 		Properties filtered = new Properties();
421 		Enumeration keys = getProperties().propertyNames();
422 		while (keys.hasMoreElements()) {
423 			String name = (String) keys.nextElement();
424 			if (name.startsWith(prefix)) {
425 				filtered.setProperty(name.substring(prefix.length()), getProperties().getProperty(name));
426 			}
427 		}
428 		return filtered;
429 	}
430 
431 }