KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jmxremoting > Authenticator


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.jmxremoting;
18
19 import java.util.Map JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import javax.management.remote.JMXAuthenticator JavaDoc;
23 import javax.management.remote.JMXConnectionNotification JavaDoc;
24 import javax.management.NotificationListener JavaDoc;
25 import javax.management.Notification JavaDoc;
26 import javax.security.auth.Subject JavaDoc;
27 import javax.security.auth.login.LoginContext JavaDoc;
28 import javax.security.auth.login.LoginException JavaDoc;
29
30 /**
31  * JMX Authenticator that checks the Credentials by logging in via JAAS.
32  *
33  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
34  */

35 public class Authenticator implements JMXAuthenticator JavaDoc, NotificationListener JavaDoc {
36     private final String JavaDoc configName;
37     private final ClassLoader JavaDoc cl;
38     private ThreadLocal JavaDoc threadContext = new ThreadLocal JavaDoc();
39     private Map JavaDoc contextMap = Collections.synchronizedMap(new HashMap JavaDoc());
40
41     /**
42      * Constructor indicating which JAAS Application Configuration Entry to use.
43      * @param configName the JAAS config name
44      */

45     public Authenticator(String JavaDoc configName, ClassLoader JavaDoc cl) {
46         this.configName = configName;
47         this.cl = cl;
48     }
49
50     public Subject JavaDoc authenticate(Object JavaDoc o) throws SecurityException JavaDoc {
51         if (o instanceof String JavaDoc[] == false) {
52             throw new IllegalArgumentException JavaDoc("Expected String[2], got " + o == null ? null : o.getClass().getName());
53         }
54         String JavaDoc[] params = (String JavaDoc[]) o;
55         if (params.length != 2) {
56             throw new IllegalArgumentException JavaDoc("Expected String[2] but length was " + params.length);
57         }
58
59         Thread JavaDoc thread = Thread.currentThread();
60         ClassLoader JavaDoc oldCL = thread.getContextClassLoader();
61         Credentials credentials = new Credentials(params[0], params[1]);
62         try {
63             thread.setContextClassLoader(cl);
64             LoginContext JavaDoc context = new LoginContext JavaDoc(configName, credentials);
65             context.login();
66             threadContext.set(context);
67             return context.getSubject();
68         } catch (LoginException JavaDoc e) {
69             // do not propogate cause - we don't know what information is may contain
70
throw new SecurityException JavaDoc("Invalid login");
71         } finally {
72             credentials.clear();
73             thread.setContextClassLoader(oldCL);
74         }
75     }
76
77     public void handleNotification(Notification JavaDoc notification, Object JavaDoc o) {
78         if (notification instanceof JMXConnectionNotification JavaDoc) {
79             JMXConnectionNotification JavaDoc cxNotification = (JMXConnectionNotification JavaDoc) notification;
80             String JavaDoc type = cxNotification.getType();
81             String JavaDoc connectionId = cxNotification.getConnectionId();
82             if (JMXConnectionNotification.OPENED.equals(type)) {
83                 LoginContext JavaDoc context = (LoginContext JavaDoc) threadContext.get();
84                 threadContext.set(null);
85                 contextMap.put(connectionId, context);
86             } else {
87                 LoginContext JavaDoc context = (LoginContext JavaDoc) contextMap.remove(connectionId);
88                 if (context != null) {
89                     try {
90                         context.logout();
91                     } catch (LoginException JavaDoc e) {
92                         //nothing we can do here...
93
}
94                 }
95             }
96         }
97     }
98 }
99
Popular Tags