View Javadoc

1   /*
2    * $Id: LdapContextFactory.java 269 2005-08-10 17:49:22Z josem $
3    *
4    * Tarsis
5    * Copyright (C) 2002 Talika Open Source Group
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   *
21   */
22  
23  package org.talika.tarsis.factory;
24  
25  import java.util.Properties;
26  import javax.naming.Context;
27  import javax.naming.directory.DirContext;
28  import javax.naming.directory.InitialDirContext;
29  import javax.naming.NamingException;
30  
31  /**
32   * JNDI LDAP <code>DirContext</code> factory implementation of <code>Factory</code> interface.<br>
33   * <br>
34   * Creates a <code>DirContext</code> object from given parameters.<br>
35   * <br>
36   * Parameters given to create <code>DirContext</code> are:
37   * <ul>
38   * <li>url - URL for LDAP connections.</li>
39   * <li>user - User name for LDAP connections.</li>
40   * <li>password - User assword for LDAP connections.</li>
41   * </ul>
42   *
43   * @author  Jose M. Palomar
44   * @version $Revision: 269 $
45   * @see javax.naming.directory.DirContext
46   */
47  public final class LdapContextFactory extends FactoryService {
48  
49      // Constants
50  
51      // Fields
52      /**
53       * JNDI context init properties.
54       */
55      private Properties env = new Properties();
56  
57      // Constructors
58      /**
59       * Creates a new <code>LdapContextFactory</code> object.
60       */
61      public LdapContextFactory() {
62          this.env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
63      }
64  
65      // Methods
66      /**
67       * Sets url parameter.
68       *
69       * @param url String URL for LDAP connections.
70       */
71      public void setUrl(String url) {
72          this.env.put(Context.PROVIDER_URL, url);
73      }
74  
75      /**
76       * Sets user parameter.
77       *
78       * @param user String user name for LDAP connections.
79       */
80      public void setUser(String user) {
81          this.env.put(Context.SECURITY_PRINCIPAL, user);
82      }
83  
84      /**
85       * Sets password parameter.
86       *
87       * @param password String user password for LDAP connections.
88       */
89      public void setPassword(String password) {
90          this.env.put(Context.SECURITY_CREDENTIALS, password);
91      }
92  
93      /**
94       * Returns name of service.
95       *
96       * @return String name of service.
97       * @see org.talika.tarsis.service.Service#getName()
98       */
99      public String getName() {
100         return "LdapContextFactory";
101     }
102 
103     /**
104      * Returns a new <code>DirContext</code> instance created by this factory.
105      *
106      * @return Object a new <code>DirContext</code> instance created by this factory.
107      * @see org.talika.tarsis.factory.Factory#getInstance()
108      */
109     public Object getInstance() {
110         return getLdapContext();
111     }
112 
113     /**
114      * Returns a new <code>DirContext</code> instance created by this factory.
115      *
116      * @return DirContext a new <code>DirContext</code> instance created by this factory.
117      */
118     public DirContext getLdapContext() {
119 
120         DirContext ldapContext = null;
121         try {
122             ldapContext = new InitialDirContext(this.env);
123         }
124         catch (NamingException ne) {
125             if (getLogger().isWarningEnabled()) {
126                 getLogger().logWarning("Error creating Ldap Context (" + ne.getMessage() + ")");
127             }
128         }
129 
130         return ldapContext;
131     }
132 
133 }