KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > security > LoginConfig


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.server.security;
30
31 import com.caucho.config.Config;
32 import com.caucho.config.ConfigException;
33 import com.caucho.config.types.InitProgram;
34 import com.caucho.log.Log;
35 import com.caucho.util.L10N;
36
37 import javax.servlet.ServletException JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39
40 /**
41  * Configuration for the login-config.
42  */

43 public class LoginConfig {
44   static final Logger JavaDoc log = Log.open(LoginConfig.class);
45   static final L10N L = new L10N(LoginConfig.class);
46
47   private String JavaDoc _authMethod = "basic";
48   private String JavaDoc _realmName;
49   private Class JavaDoc _customType;
50   private InitProgram _formLoginConfig;
51   private InitProgram _init;
52
53   private ServletAuthenticator _authenticator;
54
55   /**
56    * Creates the login-config.
57    */

58   public LoginConfig()
59   {
60   }
61
62   /**
63    * Sets the auth-method
64    */

65   public void setAuthMethod(String JavaDoc method)
66   {
67     _authMethod = method;
68   }
69
70   /**
71    * Gets the auth-method
72    */

73   public String JavaDoc getAuthMethod()
74   {
75     return _authMethod;
76   }
77
78   /**
79    * Sets the authenticator.
80    */

81   public void setAuthenticator(ServletAuthenticator auth)
82   {
83     _authenticator = auth;
84   }
85
86   /**
87    * Sets the custom type
88    */

89   public void setType(Class JavaDoc type)
90     throws ConfigException
91   {
92     _customType = type;
93
94     Config.validate(type, AbstractLogin.class);
95   }
96
97   /**
98    * Sets the realm-name
99    */

100   public void setRealmName(String JavaDoc realmName)
101   {
102     _realmName = realmName;
103   }
104
105   /**
106    * Gets the realm-name
107    */

108   public String JavaDoc getRealmName()
109   {
110     return _realmName;
111   }
112   
113   /**
114    * Creates the form-login-config
115    */

116   public InitProgram createFormLoginConfig()
117   {
118     if (_formLoginConfig == null)
119       _formLoginConfig = new InitProgram();
120     
121     return _formLoginConfig;
122   }
123   
124   /**
125    * Creates the init
126    */

127   public InitProgram createInit()
128   {
129     if (_init == null)
130       _init = new InitProgram();
131     
132     return _init;
133   }
134
135   /**
136    * Returns the login.
137    */

138   public AbstractLogin getLogin()
139     throws Throwable JavaDoc
140   {
141     /*
142     if (auth == null)
143       throw new ServletException(L.l("Login needs an authenticator resource with JNDI name java:comp/env/caucho/auth"));
144     */

145
146     AbstractLogin login;
147
148     if (_customType != null) {
149       login = (AbstractLogin) _customType.newInstance();
150       
151       if (_init != null)
152         _init.getBuilderProgram().configure(login);
153     }
154     else if (_authMethod.equalsIgnoreCase("basic")) {
155       BasicLogin basicLogin = new BasicLogin();
156       basicLogin.setRealmName(_realmName);
157       login = basicLogin;
158     }
159     else if (_authMethod.equalsIgnoreCase("digest")) {
160       DigestLogin digestLogin = new DigestLogin();
161       digestLogin.setRealmName(_realmName);
162       login = digestLogin;
163     }
164     else if (_authMethod.equalsIgnoreCase("client-cert")) {
165       ClientCertLogin certLogin = new ClientCertLogin();
166       login = certLogin;
167     }
168     else if (_authMethod.equalsIgnoreCase("form")) {
169       login = new FormLogin();
170       if (_formLoginConfig != null)
171         _formLoginConfig.init(login);
172     }
173     else
174       throw new ServletException JavaDoc(L.l("`{0}' is an unknown auth-type.",
175                                      _authMethod));
176
177     if (_authenticator != null)
178       login.setAuthenticator(_authenticator);
179     
180     login.init();
181
182     return login;
183   }
184 }
185
Popular Tags