KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > server > core > jmx > auth > ASJMXAuthenticator


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.admin.server.core.jmx.auth;
24
25 import java.util.logging.Logger JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import javax.management.remote.JMXAuthenticator JavaDoc;
28 import javax.security.auth.Subject JavaDoc;
29
30 import com.sun.enterprise.admin.common.constant.AdminConstants;
31 import com.sun.enterprise.util.i18n.StringManager;
32
33 public class ASJMXAuthenticator implements JMXAuthenticator JavaDoc {
34
35     private static final boolean _debug = false;
36     
37     private static Logger JavaDoc _logger =
38             Logger.getLogger(AdminConstants.kLoggerName);
39
40     private static StringManager _strings =
41             StringManager.getManager(ASLoginDriverImpl.class);
42
43     private String JavaDoc realmName;
44     private LoginDriver loginDriver;
45
46     public ASJMXAuthenticator() {
47     }
48
49     public ASJMXAuthenticator(String JavaDoc realmName) {
50         setRealmName(realmName);
51     }
52
53     public void setRealmName(String JavaDoc realm) {
54         // TODO: Add permission check.
55
realmName = realm;
56     }
57
58     public String JavaDoc getRealmName() {
59         return realmName;
60     }
61
62     public LoginDriver getLoginDriver() {
63         return loginDriver;
64     }
65
66     public void setLoginDriver(LoginDriver driver) {
67         // TODO: Add permission check.
68
loginDriver = driver;
69     }
70
71     public Subject JavaDoc authenticate(Object JavaDoc credentials) {
72         if (credentials == null) {
73             if (_debug) {
74                 System.out.println("JMXAuthenticator: Null credentials sent from the client");
75             }
76             throwInvalidCredentialsException();
77         }
78         if (!(credentials instanceof String JavaDoc[])) {
79             if (_debug) {
80                 System.out.println("JMXAuthenticator: Invalid credentials sent from the client " + credentials.getClass().getName());
81             }
82             throwInvalidCredentialsException();
83         }
84         String JavaDoc[] userpass = (String JavaDoc[])credentials;
85         if (userpass.length != 2) {
86             if (_debug) {
87                 System.out.println("JMXAuthenticator: Invalid credentials sent from client, string array of length " + userpass.length);
88             }
89             throwInvalidCredentialsException();
90         }
91         if (_debug) {
92             System.out.println("JMX authentication request for user "
93                 + userpass[0] + " and password " + userpass[1]);
94             System.out.println("Authentication realm is " + realmName);
95         }
96
97         Subject JavaDoc subject = null;
98         if (loginDriver != null) {
99             subject = loginDriver.login(userpass[0], userpass[1], realmName);
100         } else {
101             // TODO: WARNING message, JMX connector not protected
102
}
103         return subject;
104     }
105
106     private void throwInvalidCredentialsException() {
107         throw new SecurityException JavaDoc(
108                 _strings.getString("admin.auth.invalid.credentials"));
109     }
110 }
111
Popular Tags