View Javadoc

1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.ldap.server.authn;
18  
19  
20  import javax.naming.NamingException;
21  import javax.naming.spi.InitialContextFactory;
22  
23  import org.apache.ldap.common.name.LdapName;
24  import org.apache.ldap.server.configuration.AuthenticatorConfiguration;
25  import org.apache.ldap.server.jndi.ContextFactoryConfiguration;
26  import org.apache.ldap.server.jndi.ServerContext;
27  
28  
29  /***
30   * Base class for all Authenticators.
31   *
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   * @version $Rev: 264732 $, $Date: 2005-08-30 04:04:51 -0400 (Tue, 30 Aug 2005) $
34   */
35  public abstract class AbstractAuthenticator implements Authenticator
36  {
37      private ContextFactoryConfiguration factoryCfg;
38      private AuthenticatorConfiguration cfg;
39  
40      /*** authenticator type */
41      private String authenticatorType;
42  
43  
44      /***
45       * Creates a new instance.
46       *
47       * @param type the type of this authenticator (e.g. <tt>'simple'</tt>, <tt>'none'</tt>...)
48       */
49      protected AbstractAuthenticator( String type )
50      {
51          this.authenticatorType = type;
52      }
53  
54  
55      /***
56       * Returns {@link ContextFactoryConfiguration} of {@link InitialContextFactory}
57       * which initialized this authenticator.
58       */
59      public ContextFactoryConfiguration getFactoryConfiguration()
60      {
61          return factoryCfg;
62      }
63      
64      /***
65       * Returns the configuration of this authenticator.
66       */
67      public AuthenticatorConfiguration getConfiguration()
68      {
69          return cfg;
70      }
71  
72      public String getAuthenticatorType()
73      {
74          return authenticatorType;
75      }
76  
77  
78      /***
79       * Initializes default properties (<tt>factoryConfiguration</tt> and
80       * <tt>configuration</tt>, and calls {@link #doInit()} method.
81       * Please put your initialization code into {@link #doInit()}.
82       */
83      public final void init( ContextFactoryConfiguration factoryCfg, AuthenticatorConfiguration cfg ) throws NamingException
84      {
85          this.factoryCfg = factoryCfg;
86          this.cfg = cfg;
87          doInit();
88      }
89  
90  
91      /***
92       * Implement your initialization code here.
93       */
94      protected void doInit() throws NamingException
95      {
96      }
97  
98      /***
99       * Calls {@link #doDestroy()} method, and clears default properties
100      * (<tt>factoryConfiguration</tt> and <tt>configuration</tt>).
101      * Please put your deinitialization code into {@link #doDestroy()}. 
102      */
103     public final void destroy()
104     {
105         try
106         {
107             doDestroy();
108         }
109         finally
110         {
111             this.factoryCfg = null;
112             this.cfg = null;
113         }
114     }
115     
116     /***
117      * Implement your deinitialization code here.
118      */
119     protected void doDestroy()
120     {
121     }
122 
123     public abstract LdapPrincipal authenticate( ServerContext ctx ) throws NamingException;
124 
125 
126     /***
127      * Returns a new {@link LdapPrincipal} instance whose value is the specified
128      * <tt>name</tt>.
129      *
130      * @param name the distinguished name of the X.500 principal
131      * @return the principal for the <tt>name</tt>
132      * @throws NamingException if there is a problem parsing <tt>name</tt>
133      */
134     protected static LdapPrincipal createLdapPrincipal( String name ) throws NamingException
135     {
136         LdapName principalDn = new LdapName( name );
137         return new LdapPrincipal( principalDn );
138     }
139 }