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

📄 applicantmatch.java

📁 21天精通Java,这是一本英文书
💻 JAVA
字号:
package data;

import java.util.*;
import javax.ejb.*;
import javax.jms.*;
import javax.naming.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.w3c.dom.*;
import java.io.*;

public class ApplicantMatch implements MessageDrivenBean, MessageListener
{
    private ApplicantLocalHome applicantHome;
    private JobLocalHome jobHome;
    private MatchedLocalHome matchedHome;

    public void onMessage(Message message) {
        String xmlText = null;
        if (!(message instanceof TextMessage)) {
            System.out.println("ApplicantMatch: bad message:" + message.getClass());
            return;
        }
        try {
            xmlText = ((TextMessage)message).getText();
        }
        catch (JMSException ex) {
            error ("Error getting JMS property: NewApplicant",ex);
        }

        Document document = null;
        try {
            // build parse tree
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            try {
                DocumentBuilder builder = factory.newDocumentBuilder();
                InputSource is = new InputSource (new StringReader(xmlText));
                document = builder.parse(is);
                document.getDocumentElement().normalize();
            }
            catch (ParserConfigurationException ex) {
                System.err.println ("Failed to create DOM parser:" + ex);
            }
            catch (SAXException ex) {
                System.err.println ("General SAX exeception:" + ex);
            }
            catch (IOException ex) {
               System.err.println ("IO exeception:" + ex);
            }
            // process XML document
            String login = getAttribute(document, "applicant", "login");
            String newApplicant = getAttribute (document, "applicant", "new");
            if (newApplicant.equals("false")) {
                matchedHome.deleteByApplicant(login);
            }
            String location = getTagContents (document, "location");
            Collection appSkills = getMultipleContents (document, "skill");
            Collection col = jobHome.findByLocation(location);
            Iterator jobsIter = col.iterator();
            while (jobsIter.hasNext()) {
                JobLocal job = (JobLocal)jobsIter.next();
                Collection jobSkills = job.getSkills();
                int skillMatch = 0;
                Iterator jobSkillIter = jobSkills.iterator();
                while (jobSkillIter.hasNext()) {
                    SkillLocal jobSkill = (SkillLocal)jobSkillIter.next();
                    if (appSkills.contains(jobSkill.getName()))
                       skillMatch++;
                }
                if (jobSkills.size() > 0 && skillMatch == 0)
                    continue;
                boolean exact = skillMatch == jobSkills.size();
                MatchedPK key = new MatchedPK(login,job.getRef(),job.getCustomer());
                try {
                    matchedHome.create(key.getApplicant(),key.getJob(),key.getCustomer(), exact);
                }
                catch (CreateException ex) {
                    System.out.println("ApplicantMatch: failed to create matched entry: "+key);
                }
            }
        }
        catch (FinderException ex) {
            System.out.println("ApplicantMatch: failed to find job data");
        }
        catch (RuntimeException ex) {
            System.out.println("ApplicantMatch: "+ex);
            ex.printStackTrace();
            throw ex;
        }
    }

    private String getAttribute (Document document, String tag, String attribute) {
        NodeList nodes = document.getElementsByTagName(tag);
        NamedNodeMap attrs = nodes.item(0).getAttributes();
        Node n = attrs.getNamedItem(attribute);
        if (n == null)
            return null;
        else
            return n.getNodeValue().trim();
    }

    private String getTagContents (Document document, String tag) {
        NodeList nodes = document.getElementsByTagName(tag);
        NodeList contents = nodes.item(0).getChildNodes();
        int len = (contents != null) ? contents.getLength() : 0;
        Node text = contents.item(0);
        if (len != 1 || text.getNodeType() != Node.TEXT_NODE) {
            return null;
        }
        return text.getNodeValue().trim();
    }

    private Collection getMultipleContents (Document document, String tag) {
        Collection col = new ArrayList();
        NodeList nodes = document.getElementsByTagName(tag);
        int len = (nodes != null) ? nodes.getLength() : 0;
        for (int i = 0; i < len; i++) {
            NodeList contents = nodes.item(i).getChildNodes();
            Node text = contents.item(0);
            if (text.getNodeType() == Node.TEXT_NODE) {
                col.add(text.getNodeValue().trim());
            }
        }
        return col;
    }

    // EJB methods start here

    public void setMessageDrivenContext(MessageDrivenContext ctx) {
    InitialContext ic = null;
        try {
            ic = new InitialContext();
            applicantHome = (ApplicantLocalHome)ic.lookup("java:comp/env/ejb/ApplicantLocal");
        }
        catch (NamingException ex) {
            error("Error connecting to java:comp/env/ejb/ApplicantLocal:",ex);
        }
        try {
            jobHome = (JobLocalHome)ic.lookup("java:comp/env/ejb/JobLocal");
        }
        catch (NamingException ex) {
            error("Error connecting to java:comp/env/ejb/JobLocal:",ex);
        }
        try {
            matchedHome = (MatchedLocalHome)ic.lookup("java:comp/env/ejb/MatchedLocal");
        }
        catch (NamingException ex) {
            error("Error connecting to java:comp/env/ejb/MatchedLocal:",ex);
        }
    }

    public void ejbCreate(){
    }

    public void ejbRemove(){
        applicantHome = null;
        jobHome = null;
        matchedHome = null;
    }

    private void error (String msg, Exception ex) {
        String s = "ApplicantMatch: "+msg + "\n" + ex;
        System.out.println(s);
        throw new EJBException(s,ex);
    }
}


⌨️ 快捷键说明

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