| 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 | 5 | public class AbstractJndiParser { |
| 16 | |
|
| 17 | 5 | 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 | 10 | super(); |
| 25 | 10 | this.jndiName = jndiName; |
| 26 | 10 | this.initialContextFactory = initialContextFactory; |
| 27 | 10 | this.nameValuePairs = nameValuePairs; |
| 28 | 10 | } |
| 29 | |
|
| 30 | |
protected InitialContext getJndiContext(final String key, final Properties props, InitialContext context) |
| 31 | |
throws ConfigurationManagerException { |
| 32 | |
try { |
| 33 | 10 | context = new InitialContext(props); |
| 34 | 0 | } catch (NamingException e) { |
| 35 | 0 | log.error("Could not find jndi context with initial context for jndi name: " + jndiName + " and other parameters from configuration " + key, e); |
| 36 | 0 | throw new ConfigurationManagerException("Could not find jndi context with initial context for jndi name: " + jndiName + " and other parameters from configuration " + key, e); |
| 37 | |
} |
| 38 | 10 | return context; |
| 39 | |
} |
| 40 | |
|
| 41 | |
protected void prepareJndiProperties(final Properties props) { |
| 42 | 10 | if (initialContextFactory != null) { props.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory); } |
| 43 | 10 | if (nameValuePairs != null) { |
| 44 | 0 | final Iterator<NameValuePairImpl> it = nameValuePairs.iterator(); |
| 45 | 0 | while(it.hasNext()) { |
| 46 | 0 | NameValuePairImpl nvp = it.next(); |
| 47 | 0 | props.put(nvp.getName(), nvp.getValue()); |
| 48 | |
} |
| 49 | |
} |
| 50 | 10 | } |
| 51 | |
|
| 52 | |
protected void validateParameters(final String key) |
| 53 | |
throws ConfigurationManagerException { |
| 54 | 10 | if (key == null) { |
| 55 | 0 | throw new ConfigurationManagerException("key is null, please provide the key in you master configuraton file for all the jndi cofigurations"); |
| 56 | |
} |
| 57 | 10 | if (jndiName == null) { |
| 58 | 0 | throw new ConfigurationManagerException("jndiName is null, please provide jndi Name in you master configuraton file for external cofiguraton with key =" + key); |
| 59 | |
} |
| 60 | 10 | } |
| 61 | |
|
| 62 | |
} |