⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 accountmanagebd.java

📁 weblogic+j2ee构建音乐网站(原代码+数据库) src.zip: Workshop 8.1 工程 db.zip: 数据库备份
💻 JAVA
字号:
package music.web;

import java.rmi.*;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;

import music.ejb.*;
import music.shared.*;

/**
 * AccountManageBD作为Session.setAttribute("ACCOUNT_BD", AccountManageBD)
 * 作为用户登陆与否的标志
 */
public class AccountManageBD implements java.io.Serializable {

    public static final String ACCOUNT = "ACCOUNT_BD";

    private static final String JNDI_ACCOUNT_MANAGE = "ejb/AccountManage";
    private static AccountManageRemoteHome home = null;
    private static Object syn = new Object();

    // 用户标记:
    private AccountVO account = null;

    public boolean isAdmin() {
        if(this.account==null) return false;
        return "admin".equals(account.getRole());
    }

    public String getName() {
        if(this.account==null)
            return null;
        return this.account.getUsername();
    }

    // 获得home接口:
    private static AccountManageRemoteHome getHome() {
        if(home==null) {
            synchronized(syn) {
                if(home==null) {
                    Hashtable ht = new Hashtable();
                    ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
                    ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
                    Context ctx = null;
                    try {
                        ctx = new InitialContext(ht);
                        Object obj = ctx.lookup(JNDI_ACCOUNT_MANAGE);
                        home = (AccountManageRemoteHome)javax.rmi.PortableRemoteObject.narrow(obj, AccountManageRemoteHome.class);
                    }
                    catch(NamingException ne) {
                        // Error log:
                        System.out.println("-- Exception in lookup ejb: ejb/AccountManage --");
                        ne.printStackTrace();
                    }
                    finally {
                        try { ctx.close(); } catch(Exception e) {}
                    }
                }
            }
        }
        return home;
    }

    /**
     * 用户登陆
     */
    public void login(String username, String password)
        throws UnauthorizedException, ApplicationException
    {
        AccountManageRemote accountManage = null;
        try {
            accountManage = getHome().create();
            this.account = accountManage.login(username, password);
        }
        catch(CreateException ce) {
            throw new ApplicationException(ce);
        }
        catch(RemoteException re) {
            throw new ApplicationException(re);
        }
        finally {
            if(accountManage!=null) {
                try {
                    accountManage.remove();
                }
                catch(Exception e) {}
            }
        }
    }

    /**
     * 用户注销
     */
    public void logout() {
        this.account = null;
    }

    /**
     * 判断用户是否登陆
     */
    public boolean isLogin() {
        return (this.account != null);
    }

    /**
     * 注册一个新用户(注意注册后并未登陆)
     */
    public void register(AccountVO accountVO)
        throws UserExistException, FormatException, ApplicationException
    {
        AccountManageRemote accountManage = null;
        try {
            accountManage = getHome().create();
            accountManage.register(accountVO);
            // if registered successfully, marked as Logged In:
            // this.username = accountVO.getUsername().toLowerCase();
        }
        catch(RemoteException re) {
            throw new ApplicationException(re);
        }
        catch(CreateException ce) {
            throw new ApplicationException(ce);
        }
        finally {
            try { accountManage.remove(); } catch(Exception e) {}
        }
    }

    /**
     * 更改口令
     */
    public void  changePassword(String oldPassword, String newPassword)
        throws UnauthorizedException, FormatException, ApplicationException
    {
        if(!isLogin()) throw new UnauthorizedException("Login first.");

        try {
            getHome().create().changePassword(this.account.getUsername(), oldPassword, newPassword);
        }
        catch(CreateException ce) {
            throw new ApplicationException("System internal error.");
        }
        catch(RemoteException ce) {
            throw new ApplicationException("System internal error.");
        }
    }

} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -