📄 userbean.java
字号:
/* * UserEJB - CyberDemia's User management library implemented using EJBs. * Copyright (C) 2003 CyberDemia Research and Services * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. * * See the COPYING file located in the top-level-directory of * the archive of this library for complete text of license. */package com.cyberdemia.user.impl;import javax.ejb.*;import java.util.*;import java.util.logging.*;import java.security.*;import com.cyberdemia.user.*;import com.cyberdemia.util.UniqueIdGenerator;import com.cyberdemia.util.HexString;/*** UserBean is the implementation of a User class that* stores the primary data about a user such as email, password and name.* These primary data may be accessed or set via the UserData data transfer object.* Additional data or resources may be associated with a User using the* "resource reference" mechanism.** @author Alexander Yap*/public abstract class UserBean extends AbstractEntityBean{ /** * Sets the email. * @param email The email address. */ public abstract void setEmail(String email); /** * Gets the email. * @return The email address. */ public abstract String getEmail(); /** * Sets the hashed password encoded in hexadecimal format. * @param password The hashed password. */ public abstract void setHashedPassword(String password); /** * Gets the hashed password encoded in hexadecimal format. * @return The hashed password. */ public abstract String getHashedPassword(); public abstract void setFirstName(String firstName); public abstract String getFirstName(); public abstract void setLastName(String lastName); public abstract String getLastName(); public abstract void setTitle(String title); public abstract String getTitle(); public abstract void setGroups(String groups); public abstract String getGroups(); public abstract void setCreatedMillis(long created); public abstract long getCreatedMillis(); public abstract void setLastModifiedMillis(long lastModified); public abstract long getLastModifiedMillis(); public abstract void setPasswordExpiryMillis(long passwordExpiry); public abstract long getPasswordExpiryMillis(); /** * Sets LocalResourceReferences associated with this user. */ public abstract void setResourceReferences(Set resRefs); /** * Gets LocalResourceReferences associated with this user. */ public abstract Set getResourceReferences(); /** * Gets the data in this User. * This is a convenience method that should be more efficient * than calling the individual getter methods when all the data need * to be updated. * @return Data of the User. */ public UserData getUserData() { UserData data = new UserData(); data.id = getId(); data.email = getEmail(); data.title = getTitle(); data.firstName = getFirstName(); data.lastName = getLastName(); data.groups = getGroups(); data.createdMillis = getCreatedMillis(); data.lastModifiedMillis = getLastModifiedMillis(); data.passwordExpiryMillis = getPasswordExpiryMillis(); return data; } /** * Sets the data in this User. * This is a convenience method that should be more efficient * than calling the individual setter methods when all the data need * to be updated. * Note that the "id" field in data is ignored because it cannot be modified. * @param data Data of the User. */ public void setUserData( UserData data) { setEmail( data.email ); if ( (data.password!=null) && (data.password.length()>0)) { try { byte[] hashed = MessageDigest.getInstance("MD5").digest( data.password.getBytes() ); setHashedPassword( HexString.toString(hashed) ); } catch (NoSuchAlgorithmException ex) { s_logger.log(Level.SEVERE, "Cannot create MD5 MessageDigest",ex); throw new EJBException("Cannot create MD5 MessageDigest",ex); } } setTitle( data.title ); setFirstName( data.firstName ); // Last name is always saved in all upper case to simplify searching setLastName( data.lastName.toUpperCase() ); setGroups( data.groups ); setLastModifiedMillis( System.currentTimeMillis() ); } /** * Gets resources within the specified category that are * associated with this user. * @param catKey Key to identify the category of resource to extract. * @return Array of LocalResourceReference objects. */ public LocalResourceReference[] getResources( String catKey ) { Iterator resIter = getResourceReferences().iterator(); List foundList = new ArrayList(); while (resIter.hasNext()) { LocalResourceReference res = (LocalResourceReference)resIter.next(); if (catKey!=null) { if ( res.getCategoryKey().equals( catKey )) { foundList.add( res ); } } else { foundList.add(res); } } return (LocalResourceReference[])foundList.toArray( new LocalResourceReference[0] ); } /** * Associates a resource with this user. * @param res The LocalResourceReference to add. * @return true if resource is added successfully, otherwise false (it already exists). */ public boolean addResource( LocalResourceReference res ) { return getResourceReferences().add(res); } /** * Removes a resource from this user. * @param res The LocalResourceReference to remove. * @return true if resource is removed successfully, otherwise false (not found). */ public boolean removeResource( LocalResourceReference res ) { return getResourceReferences().remove(res); } /** * Removes all resources from this user. */ public void removeAllResources() { getResourceReferences().clear(); } //----------------------------- // Create methods. //----------------------------- public String ejbCreate(String password, String email, String title, String firstName, String lastName, String groups ) throws CreateException { setId( UniqueIdGenerator.getInstance().createId(email) ); try { byte[] hashed = MessageDigest.getInstance("MD5").digest( password.getBytes() ); setHashedPassword( HexString.toString(hashed) ); } catch (NoSuchAlgorithmException ex) { s_logger.log(Level.SEVERE, "Cannot create MD5 MessageDigest",ex); throw new CreateException("Cannot create MD5 MessageDigest"); } setEmail(email); setTitle(title); setFirstName(firstName); setLastName(lastName); setGroups(groups); long nowMillis = System.currentTimeMillis(); setCreatedMillis( nowMillis ); setLastModifiedMillis(nowMillis); setPasswordExpiryMillis(0); return null; } public void ejbPostCreate( String password, String email, String title, String firstName, String lastName, String groups) { } private static Logger s_logger = Logger.getLogger( UserBean.class.getName());}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -