KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > security > AbstractPasswordCredentialLoginModule


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software 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 GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.resource.security;
23
24 import java.util.Map JavaDoc;
25 import javax.management.MBeanServer JavaDoc;
26 import javax.management.MalformedObjectNameException JavaDoc;
27 import javax.management.ObjectName JavaDoc;
28 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
29 import javax.security.auth.Subject JavaDoc;
30 import javax.security.auth.callback.CallbackHandler JavaDoc;
31 import javax.security.auth.login.LoginException JavaDoc;
32
33 import org.jboss.logging.Logger;
34 import org.jboss.mx.util.MBeanServerLocator;
35 import org.jboss.security.auth.spi.AbstractServerLoginModule;
36
37
38 /** A base login module that provides access to the ManagedConnectionFactory
39  * needed by the PasswordCredential.
40  *
41  * @see javax.resource.spi.security.PasswordCredential
42  *
43  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
44  * @author Scott.Stark@jboss.org
45  * @version $Revision: 37459 $
46  */

47 public abstract class AbstractPasswordCredentialLoginModule
48    extends AbstractServerLoginModule
49 {
50    private static final Logger log = Logger.getLogger(AbstractPasswordCredentialLoginModule.class);
51    private MBeanServer JavaDoc server;
52    private ObjectName JavaDoc managedConnectionFactoryName;
53    private ManagedConnectionFactory JavaDoc mcf;
54    /** A flag that allows a missing MCF service to be ignored */
55    private Boolean JavaDoc ignoreMissigingMCF;
56
57    public AbstractPasswordCredentialLoginModule()
58    {
59       
60    }
61
62    public void initialize(Subject JavaDoc subject, CallbackHandler JavaDoc handler, Map JavaDoc sharedState, Map JavaDoc options)
63    {
64       super.initialize(subject, handler, sharedState, options);
65       String JavaDoc name = (String JavaDoc) options.get("managedConnectionFactoryName");
66       try
67       {
68          managedConnectionFactoryName = new ObjectName JavaDoc(name);
69       }
70       catch (MalformedObjectNameException JavaDoc mone)
71       {
72          throw new IllegalArgumentException JavaDoc("Malformed ObjectName: " + name);
73       }
74
75       if (managedConnectionFactoryName == null)
76       {
77          throw new IllegalArgumentException JavaDoc("Must supply a managedConnectionFactoryName!");
78       }
79       Object JavaDoc flag = options.get("ignoreMissigingMCF");
80       if( flag instanceof Boolean JavaDoc )
81          ignoreMissigingMCF = (Boolean JavaDoc) flag;
82       else if( flag != null )
83          ignoreMissigingMCF = Boolean.valueOf(flag.toString());
84       server = MBeanServerLocator.locateJBoss();
85       getMcf();
86    }
87
88    /** Return false if there is no mcf, else return super.login(). Override
89     * to provide custom authentication.
90     *
91     * @return false if there is no mcf, else return super.login().
92     * @exception LoginException if an error occurs
93     */

94    public boolean login() throws LoginException JavaDoc
95    {
96       if (mcf == null)
97       {
98          return false;
99       }
100       return super.login();
101    }
102
103    public boolean logout() throws LoginException JavaDoc
104    {
105       removeCredentials();
106       return super.logout();
107    }
108
109    protected ManagedConnectionFactory JavaDoc getMcf()
110    {
111       if (mcf == null)
112       {
113          try
114          {
115             mcf = (ManagedConnectionFactory JavaDoc) server.getAttribute(
116                managedConnectionFactoryName,
117                "ManagedConnectionFactory");
118          }
119          catch (Exception JavaDoc e)
120          {
121             log.error("The ConnectionManager mbean: " + managedConnectionFactoryName
122                + " specified in a ConfiguredIdentityLoginModule could not be found."
123                + " ConnectionFactory will be unusable!");
124             if( Boolean.TRUE != ignoreMissigingMCF )
125             {
126                throw new IllegalArgumentException JavaDoc("Managed Connection Factory not found: "
127                   + managedConnectionFactoryName);
128             }
129          } // end of try-catch
130
if (log.isTraceEnabled())
131          {
132             log.trace("mcfname: " + managedConnectionFactoryName);
133          }
134       } // end of if ()
135

136       return mcf;
137    }
138
139    protected MBeanServer JavaDoc getServer()
140    {
141       return server;
142    }
143
144    /** This removes the javax.security.auth.login.name and
145     * javax.security.auth.login.password settings from the sharteState map
146     * along with any PasswordCredential found in the PrivateCredentials set
147     */

148    protected void removeCredentials()
149    {
150       sharedState.remove("javax.security.auth.login.name");
151       sharedState.remove("javax.security.auth.login.password");
152       SubjectActions.removeCredentials(subject, mcf);
153    }
154
155 }
156
157
Popular Tags