View Javadoc

1   package org.naftulin.configmgr.parsers;
2   
3   import java.util.Iterator;
4   import java.util.List;
5   import java.util.Properties;
6   
7   import javax.naming.Context;
8   import javax.naming.InitialContext;
9   import javax.naming.NamingException;
10  
11  import org.apache.log4j.Logger;
12  import org.naftulin.configmgr.ConfigurationManagerException;
13  import org.naftulin.configmgr.content.NameValuePairImpl;
14  
15  public class AbstractJndiParser {
16  
17  	protected static final Logger log = Logger.getLogger(JndiParserImpl.class);
18  	protected final String jndiName;
19  	protected final String initialContextFactory;
20  	protected final List<NameValuePairImpl> nameValuePairs;
21  
22  	public AbstractJndiParser(final String jndiName, final String initialContextFactory,
23  			final List<NameValuePairImpl> nameValuePairs) {
24  		super();
25  		this.jndiName = jndiName;
26  		this.initialContextFactory = initialContextFactory;
27  		this.nameValuePairs = nameValuePairs;
28  	}
29  
30  	protected InitialContext getJndiContext(final String key, final Properties props, InitialContext context)
31  			throws ConfigurationManagerException {
32  				try {
33  					context = new InitialContext(props);
34  				} catch (NamingException e) {
35  					log.error("Could not find jndi context with initial context for jndi name: " + jndiName + " and other parameters from configuration " + key, e);
36  					throw new ConfigurationManagerException("Could not find jndi context with initial context for jndi name: " + jndiName + " and other parameters from configuration " + key, e);
37  				}
38  				return context;
39  			}
40  
41  	protected void prepareJndiProperties(final Properties props) {
42  		if (initialContextFactory != null) { props.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory); }
43  		if (nameValuePairs != null) {
44  			final Iterator<NameValuePairImpl> it = nameValuePairs.iterator();
45  			while(it.hasNext()) {
46  				NameValuePairImpl nvp = it.next();
47  				props.put(nvp.getName(), nvp.getValue());			
48  			}
49  		}
50  	}
51  
52  	protected void validateParameters(final String key)
53  			throws ConfigurationManagerException {
54  				if (key == null) { 
55  					throw new ConfigurationManagerException("key is null, please provide the key in you master configuraton file for all the jndi cofigurations");
56  				}
57  				if (jndiName == null) {
58  					throw new ConfigurationManagerException("jndiName is null, please provide jndi Name in you master configuraton file for external cofiguraton with key =" + key);
59  				}
60  			}
61  
62  }