【Java开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了Java开源代码-AuthenticationSessionEJBean.java的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
package ejava.ejbch37.authenticationsession;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import java.rmi.RemoteException;
import javax.ejb.FinderException;
import javax.rmi.PortableRemoteObject;
import ejava.ejbch36.customer.CustomerHome;
import ejava.ejbch36.customer.Customer;
import java.util.Hashtable;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import java.util.Enumeration;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.Context;
import ejava.ejbch36.EJBHelper;
import ejava.servletsch32.Address;
import ejava.ejbch37.authentication.AuthenticationHome;
import ejava.ejbch37.authentication.Authentication;
import ejava.servletsch32.ChargeCard;
public class AuthenticationSessionEJBean implements SessionBean
{
private SessionContext sessionContext;
private CustomerHome customerHome;
private AuthenticationHome
authenticationHome;
private Connection connection;
private final String SMTPHostEnv = "java:comp/env/SMTPHost";
private final String AUTHENTICATION_REFNAME
="java:comp/env/ejb/authentication";
/**
* This method is required by the EJB Specification for
* imeplementing SessionBean
* but is not used by this class.
*/
public void ejbActivate(){}
/**
* This method is required by the EJB Specification for
* imeplementing SessionBean
* but is not used by this class.
*/
public void ejbPassivate(){}
/**
* This method is required by the EJB Specification for
* imeplementing SessionBean
* but is not used by this class.
*/
public void ejbRemove(){}
/**
* This method is required by the EJB Specification for
* imeplementing SessionBean
*/
public void setSessionContext(SessionContext sessionContext)
{
this.sessionContext = sessionContext;
}
/** This method corresponds to the create method in the home interface
* AuthenticationSessionHome.java.
* The parameters of the two methods in both classes are identical.
* When the client calls the container (J2EE or Weblogic)
*/
public void ejbCreate () throws CreateException
{
}
/**
* authenticates the user with email and password
*/
public ejava.servletsch32.Customer login(String email, String password)
throws FailedToLoginException
{
try{
// lookup CustomerHome
lookupCustomerHome();
System.out.println("Customer Home :"+customerHome);
Enumeration customers = customerHome.findByEmail(email);
Customer customerBean = null;
while(customers.hasMoreElements()){
customerBean = (Customer)customers.nextElement();
}
if(customerBean == null){
return null;
}
// get Password and compare with the userentered password
ejava.servletsch32.Customer clientCustomer
= new ejava.servletsch32.Customer();
// set values for the customer object for the Client
clientCustomer.setFirstName(customerBean.getFirstName());
clientCustomer.setLastName(customerBean.getLastName());
clientCustomer.setMiddleName(customerBean.getMiddleName());
clientCustomer.setPhone(customerBean.getPhone());
clientCustomer.setEmail(customerBean.getEmail());
clientCustomer.setCustomerID(""+customerBean.getID().intValue());
ejava.servletsch32.Address address
= new ejava.servletsch32.Address();
address.setAddress_1(customerBean.getAddress_1());
address.setAddress_2(customerBean.getAddress_2());
address.setCity(customerBean.getCity());
address.setState(customerBean.getState());
address.setZipCode(customerBean.getZipCode());
clientCustomer.setAddress(address);
System.out.println("Got Customer"+clientCustomer);
// lookup AuthenticationHome
lookupAuthenticationHome();
Authentication authentication =
authenticationHome.findByPrimaryKey(customerBean.getID());
if(authentication == null){
return null;
}
System.out.println("Authentication :"+authentication);
String passwordInDb = authentication.getPassword();
System.out.println("Customer ID :"+authentication.getCustomerID());
System.out.println("PASS Type :"+authentication.getAuthenticationType());
System.out.println("Authentication :"+authentication.getPassword());
System.out.println("Pass db :"+passwordInDb+"Pass :"+password);
if(!passwordInDb.equalsIgnoreCase(password)){
throw new FailedToLoginException(" Password Does not match ");
}
return clientCustomer;
}
catch(NamingException namingException){
namingException.printStackTrace();
throw new FailedToLoginException(namingException.getMessage());
}
catch(RemoteException remoteException){
remoteException.printStackTrace();
}
catch(FinderException finderException){
throw new FailedToLoginException(finderException.getMessage());
}
return null;
}
/**
* creates new Customer Entity bean with the user entered values
*/
public int register(ejava.servletsch32.Customer customer)
throws RemoteException, FailedToRegisterException
{
System.out.println("Came To Register");
int nextCustomerID =-1;
try{
connection = EJBHelper.getDBConnection();
// get next CustomerID from the DB
nextCustomerID = getNextCustomerID();
String firstName = customer.getFirstName();
String lastName = customer.getLastName();
String middleName = customer.getMiddleName();
ejava.servletsch32.Address address =
customer.getAddress();
String address_1 = address.getAddress_1();
String address_2 = address.getAddress_2();
String city = address.getCity();
String state = address.getState();
String zipCode = address.getZipCode();
String phone = customer.getPhone();
String email = customer.getEmail();
String password = customer.getPassword();
// lookup CustomerHome
lookupCustomerHome();
//create new Customer
Customer customerBean =
customerHome.create(
new Integer(nextCustomerID), firstName, lastName,
middleName, email, address_1, address_2, city,
state, zipCode, phone, password );
System.out.println("ok registered ");
//lookup AuthenticationHome
lookupAuthenticationHome();
String defaultAuthenticationType = "PASSWORD";
Authentication authenticationBean
= authenticationHome.create(new Integer(nextCustomerID),
password, defaultAuthenticationType);
}
catch(NamingException namingException){
namingException.printStackTrace();
}
catch(RemoteException remoteException){
remoteException.printStackTrace();
throw new FailedToRegisterException(remoteException.getMessage
上一篇:
AuthenticationSession.java
下一篇:
不规则零件优化排样的神经网络混合优化算法