001: /**
002: * $Id: LoginBean.java,v 1.7 2006/06/15 22:34:45 cathywu Exp $
003: * Copyright 2005 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.admin.console.common;
014:
015: import java.util.logging.Level;
016: import java.util.Properties;
017: import java.io.InputStream;
018:
019: import javax.servlet.http.HttpSession;
020:
021: import javax.management.remote.JMXConnector;
022:
023: import javax.faces.context.FacesContext;
024: import javax.faces.application.FacesMessage;
025: import javax.faces.el.VariableResolver;
026: import javax.faces.validator.ValidatorException;
027: import javax.faces.component.UIComponent;
028: import javax.faces.component.UIInput;
029:
030: import com.sun.portal.admin.common.util.AdminClientUtil;
031: import com.sun.portal.admin.console.common.LoginAlertBean;
032: import com.sun.portal.admin.console.util.JMXHelpers;
033:
034: public class LoginBean {
035:
036: // Holds value of property userName.
037: private String userName = null;
038:
039: // Holds value of property password.
040: private String password = null;
041:
042: // Holds value of property usernameField .
043: private UIInput usernameField;
044:
045: // Holds value of property passwordField.
046: private UIInput passwordField;
047:
048: // Name of the connection property file
049: public static final String PAS_SETTINGS_FILE = "pasconnect.properties";
050:
051: // The host name where the mbean server is running
052: private static String pasHost = null;
053:
054: // The portal domain for which the console is connecting
055: private static String pasDomain = null;
056:
057: static {
058:
059: try {
060: ClassLoader lbsCloader = LoginBean.class.getClassLoader();
061: InputStream is = lbsCloader
062: .getResourceAsStream(PAS_SETTINGS_FILE);
063: Properties pasSettings = new Properties();
064: if (is != null) {
065: pasSettings.load(is);
066: pasHost = pasSettings.getProperty("pas.host", "")
067: .trim();
068: pasDomain = pasSettings.getProperty("pas.domain", "")
069: .trim();
070: }
071: } catch (Exception e) {
072: // Cannot load the connection settings file
073: // Ignore and proceed. console will connect to local mbean server
074: }
075: }
076:
077: public LoginBean() {
078: }
079:
080: public String getUserName() {
081: return userName;
082: }
083:
084: public void setUserName(String name) {
085: userName = name;
086: }
087:
088: public String getPassword() {
089: return password;
090: }
091:
092: public void setPassword(String pwd) {
093: password = pwd;
094: }
095:
096: /**
097: * Getter for property usernameField.
098: *
099: * @return Value of property usernameField.
100: */
101: public UIInput getUsernameField() {
102: return this .usernameField;
103: }
104:
105: /**
106: * Getter for pasHost.
107: */
108: public String getPasHost() {
109: return pasHost;
110: }
111:
112: /**
113: * Getter for pasDomain.
114: */
115: public String getPasDomain() {
116: return pasDomain;
117: }
118:
119: /**
120: * Setter for property usernameField.
121: *
122: * @param usernameField Value of property usernameField.
123: */
124: public void setUsernameField(UIInput usernameField) {
125: this .usernameField = usernameField;
126: }
127:
128: /**
129: * Getter for property passwordField.
130: *
131: * @return Value of property passwordField.
132: */
133: public UIInput getPasswordField() {
134: return this .passwordField;
135: }
136:
137: /**
138: * Setter for property passwordField.
139: *
140: * @param passwordField Value of property passwordFiled.
141: */
142: public void setPasswordField(UIInput passwordField) {
143: this .passwordField = passwordField;
144: }
145:
146: private void setupLoginAlert(FacesContext context, String summary,
147: String detail, String type) {
148: VariableResolver vr = context.getApplication()
149: .getVariableResolver();
150: Object obj = vr.resolveVariable(context, "LoginAlert");
151: if ((obj != null) && (obj instanceof LoginAlertBean)) {
152: LoginAlertBean lab = (LoginAlertBean) obj;
153: lab.setDisplayError(Boolean.TRUE);
154: lab.setDisplaySpacer(Boolean.TRUE);
155: lab.setAlertSummary(summary);
156: lab.setAlertDetail(detail);
157: lab.setAlertType(type);
158: }
159: }
160:
161: /**
162: * Validator method to be called durng the Process Validation phase.
163: * It validates that the username and password fields contain non-null
164: * values.
165: *
166: * @param context The faces context for this request.
167: * @param component The component to be validated.
168: * @param value The value to be validated.
169: */
170: public void validateData(FacesContext context,
171: UIComponent component, Object value) {
172: boolean pswdInvalid = false;
173: boolean unameInvalid = false;
174:
175: String username = "";
176: String password = "";
177: Object obj = usernameField.getLocalValue();
178: if (obj != null) {
179: username = obj.toString();
180: }
181:
182: obj = passwordField.getLocalValue();
183: if (obj != null) {
184: password = obj.toString();
185: }
186:
187: FacesMessage message = new FacesMessage();
188:
189: // validate the local value of the password field component.
190: if (!isValidData(password)) {
191: pswdInvalid = true;
192: }
193:
194: // validate the local value of the username field component.
195: if (!isValidData(username)) {
196: unameInvalid = true;
197: }
198:
199: // Set appropriate error messages in LoginError object.
200: String[] args = { "" };
201: if ((pswdInvalid) && (unameInvalid)) {
202: setupLoginAlert(context, "login.validation.aleret.summary",
203: "login.uidpwd.validation.alert.detail", "error");
204: } else if (pswdInvalid) {
205: setupLoginAlert(context, "login.validation.aleret.summary",
206: "login.password.validation.alert.detail", "error");
207: } else if (unameInvalid) {
208: setupLoginAlert(context, "login.validation.aleret.summary",
209: "login.username.validation.alert.detail", "error");
210: }
211:
212: // If at least one field is invalid, throw the validator exception
213: // to cause the same page to be re-rendered.
214: if ((pswdInvalid) || (unameInvalid)) {
215: throw new ValidatorException(message);
216: }
217: }
218:
219: // Helper method to validate data.
220: private boolean isValidData(String data) {
221: if ((data == null) || (data.trim().length() == 0)) {
222: return false;
223: }
224: return true;
225: }
226:
227: public String login() {
228: try {
229: authenticate(userName, password);
230: } catch (SecurityException se) {
231: FacesContext context = FacesContext.getCurrentInstance();
232: setupLoginAlert(context,
233: "login.authentication.alert.summary",
234: "login.authentication.alert.detail", "error");
235: PSBaseBean
236: .log(
237: Level.SEVERE,
238: "Failed to authenticate with JMX Server: LoginBean.login()",
239: se);
240: return "failure";
241: } catch (Exception e) {
242: FacesContext context = FacesContext.getCurrentInstance();
243: setupLoginAlert(context,
244: "login.authentication.alert.summary",
245: "login.authentication.alert.detail", "error");
246: PSBaseBean
247: .log(
248: Level.SEVERE,
249: "Failed to authenticate with JMX Server: LoginBean.login()",
250: e);
251: return "failure";
252: }
253: return "success";
254: }
255:
256: public String getAppContext() {
257: return FacesContext.getCurrentInstance().getExternalContext()
258: .getRequestContextPath();
259: }
260:
261: private void authenticate(String uid, String pwd)
262: throws SecurityException, Exception {
263: JMXConnector connector = JMXConnect(uid, pwd);
264: if (connector != null) {
265: AuthCredentialBean abean = AuthCredentialBean
266: .getCurrentInstance();
267: if (abean != null) {
268: abean.setAuthCredential(connector, uid);
269: FacesContext context = FacesContext
270: .getCurrentInstance();
271: context.getApplication().createValueBinding(
272: "#{AuthCredentialBean}").setValue(context,
273: abean);
274: }
275: }
276: }
277:
278: private JMXConnector JMXConnect(String uid, String pwd)
279: throws SecurityException, Exception {
280:
281: JMXConnector jmxconn = null;
282:
283: try {
284:
285: if (pasHost != null && pasHost.length() > 0) {
286: if (pasDomain != null && pasDomain.length() > 0) {
287: // Connect to the mbean server and domain defined in the
288: // connection property file
289: jmxconn = AdminClientUtil.getJMXConnector(pasHost,
290: pasDomain, uid, pwd);
291: } else {
292: // Connect to defaultdomain
293: jmxconn = AdminClientUtil.getJMXConnector(pasHost,
294: AdminClientUtil.DEFAULT_DOMAIN, uid, pwd);
295: }
296: } else {
297: // Connect to the mbean server on local host
298: jmxconn = AdminClientUtil.getJMXConnector(uid, pwd);
299: }
300:
301: } catch (SecurityException se) {
302: // must clean up so that we don't fool ourselves to believe there's
303: // a valid session
304: AuthCredentialBean abean = AuthCredentialBean
305: .getCurrentInstance();
306: if (abean != null) {
307: abean.resetAuthCredential();
308: }
309: throw se;
310: } catch (Exception e) {
311: // must clean up so that we don't fool ourselves to believe there's
312: // a valid session
313: AuthCredentialBean abean = AuthCredentialBean
314: .getCurrentInstance();
315: if (abean != null) {
316: abean.resetAuthCredential();
317: }
318: throw e;
319: }
320:
321: return jmxconn;
322: }
323: }
|