View Javadoc

1   package net.sf.provisioner.config;
2   
3   import java.io.FileNotFoundException;
4   import java.io.InputStream;
5   import java.util.Hashtable;
6   
7   import javax.naming.Context;
8   import javax.naming.NamingException;
9   import javax.naming.directory.DirContext;
10  import javax.naming.directory.InitialDirContext;
11  
12  import org.apache.log4j.Logger;
13  import org.jdom.Element;
14  
15  
16  /**
17   * Esta clase representa a un elemento de red y sus datos
18   * de acceso. Tiene un nombre que puede ser el numero de IP, 
19   * un tipo que sirve para determinar que interfaz se debe 
20   * utilizar para conectarse al mismo, usuario y password
21   * para aquellos elementos de red que requieran iniciar una
22   * sesion, clave de autenticacion para aquellos elementos de 
23   * red que utilicen este mecanismo de seguridad, y patrones
24   * de exito o fracaso para los elementos de red basados en 
25   * protocolo Telnet.
26   * 
27   *             
28   * @version $Revision: 1.1.2.2 $, $Date: 2007/11/13 22:36:06 $
29   * @author Gordon Pearson
30   */
31  public class LDAPNetworkElement extends NetworkElement {
32  		
33  	/** Logger for this class and subclasses */
34  	Logger logger = Logger.getLogger(getClass());
35  	
36  	private String  host;
37  	private String  baseDN;
38  	private String  userDN;
39  	private String  authType;
40  	
41  	public LDAPNetworkElement() {
42  		super();
43  	}
44  	
45      /**
46       * Creates a NetworkElement with its properties populated using 
47       * information in an XML file.
48       * 
49       * @param configFile an XML configuration file (see asterisk.xml and 
50       * intraway.xml for examples of the format.) 
51       * @throws FileNotFoundException 
52       */
53      public LDAPNetworkElement(InputStream xmlConfig) throws FileNotFoundException {
54      	super(xmlConfig);
55      }
56  
57  	/**
58       * Read standard and custom property values from an XML stream.
59       * 
60       * @param current an XML element.
61       */
62  	void getParameters(Element current) {
63  		super.getParameters(current);
64  				
65  		for (Element child : filterChildParameters(current)) {
66  	        String paramName = child.getAttributeValue("name");
67  	        String value     = child.getAttributeValue("value");
68  	        if (paramName.equals("host")) {
69  	        	name = value;
70  	        	host = value;	        	
71  	        } else if (paramName.equals("userDN")) {
72  	        	userDN   = value;
73  	        } else if (paramName.equals("baseDN")) {
74  	        	baseDN   = value;
75  	        } else if (paramName.equals("authType")) {
76  	        	authType = value;
77  	        }
78  	    }   
79  	}
80  
81  	public String getHost() {
82  		return host;
83  	}
84  
85  	public String getBaseDN() {
86  		return baseDN;
87  	}
88  
89  	public String getUserDN() {
90  		return userDN;
91  	}
92  	
93  	public String getAuthType() {
94  		return authType;
95  	}
96  	
97  	public DirContext findRootContext() {
98  		Hashtable env = new Hashtable();
99          env.put(Context.SECURITY_PRINCIPAL     , this.userDN);
100         env.put(Context.SECURITY_CREDENTIALS   , this.password);
101         env.put(Context.SECURITY_AUTHENTICATION, this.authType);
102         env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
103         
104         String url = "";
105         if (!this.host.startsWith("ldap://")) {
106         	url+= "ldap://";
107         }
108         url+= this.host;
109         if (!url.endsWith(("/"))) {
110         	url+= "/";
111         }
112         
113         if (port != null && port.length() > 0) {      
114         	try {
115         		int portNum = Integer.parseInt(port);
116         		url+= ":" + portNum;
117         	} catch (NumberFormatException nfe) {
118         		throw nfe;
119         	}
120         }
121         url+= this.baseDN;
122         
123         logger.trace("finding root context with uri: " + url);
124         env.put(Context.PROVIDER_URL           , url);
125         
126         DirContext ctx;
127         try {
128            ctx = new InitialDirContext(env);
129            return ctx;
130         } catch (NamingException e) {
131         	// TODO: find a more appropriate exception to throw.
132            throw new RuntimeException(e);
133         }
134 	}
135 }