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

📄 jobmatch.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 JobMatch implements MessageDrivenBean, MessageListener
{
    private ApplicantLocalHome applicantHome;
    private JobLocalHome jobHome;
    private MatchedLocalHome matchedHome;

    public void onMessage(Message message) {
        String xmlText = null;
        String location = null;
        if (!(message instanceof TextMessage)) {
            System.out.println("JobMatch: 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);
            }
            String ref = getAttribute (document, "job", "reference");
            String customer = getAttribute(document, "job", "customer");
            String newJob = getAttribute (document, "job", "new");
            if (newJob.equals("false")) {
                matchedHome.deleteByJob(ref, customer);
            }
            location = getTagContents (document, "location");
            Collection jobSkills = getMultipleContents (document, "skill");
            Collection col = applicantHome.findByLocation(location);
            Iterator appIter = col.iterator();
            while (appIter.hasNext()) {
                ApplicantLocal app = (ApplicantLocal)appIter.next();
                Collection appSkills = app.getSkills();
                int skillMatch = 0;
                Iterator appSkillIter = appSkills.iterator();
                while (appSkillIter.hasNext()) {
                    SkillLocal appSkill = (SkillLocal)appSkillIter.next();
                    if (jobSkills.contains(appSkill.getName()))
                       skillMatch++;
                }
                if (jobSkills.size() > 0 && skillMatch == 0)
                    continue;
                boolean exact = skillMatch == jobSkills.size();
                MatchedPK key = new MatchedPK(app.getLogin(),ref ,customer);
                try {
                    matchedHome.create(key.getApplicant(),key.getJob(),key.getCustomer(), exact);
                }
                catch (CreateException ex) {
                    System.out.println("JobMatch: failed to create matched entry: "+key);
                }
            }
        }
        catch (FinderException ex) {
            System.out.println("JobMatch: failed to find location data: "+location);
        }
        catch (RuntimeException ex) {
            System.out.println("JobMatch: "+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 = "JobMatch: "+msg + "\n" + ex;
        System.out.println(s);
        throw new EJBException(s,ex);
    }
}


⌨️ 快捷键说明

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