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

📄 ecspec.java

📁 一个开源的rfid middleware 资料
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }

    /**
     * @hibernate.set id="logicalReaders" table="ECSpec_IIds"
     * @hibernate.collection-key column="specName"
     * @hibernate.collection-element column="deviceManagerId" type="string"
     *                               lazy="false"
     */
    public Set<String> getLogicalReaders() {
        return logicalReaders;
    }

    public void setLogicalReaders(Set<String> x) {
        logicalReaders = x;
    }

    /**
     * Places string in Set of logicalReaders Calls method that instantiates IO
     * threads to reader (if hasn't already been done so by another ECSpec
     * thread
     * 
     */
    public void addInterrogator(String deviceManagerId) throws Exception {
        log.debug("addInterogator deviceManagerId = " + deviceManagerId);
        logicalReaders.add(deviceManagerId);
    }

    /**
     * 
     * @param deviceManagerId
     */
    public void removeInterrogator(String deviceManagerId) {
        getLogicalReaders().remove(deviceManagerId);
    }

    /* NOTE: XDoclet doesn't seem to have a way to indicate --> lazy = "false" */
    /**
     * @hibernate.many-to-one id="boundaries"
     *                        class="org.firstopen.singularity.ale.ECBoundarySpec"
     *                        unique="true" cascade="all" lazy="false"
     */
    public ECBoundarySpec getBoundaries() {
        return boundaries;
    }

    public void setBoundaries(ECBoundarySpec x) {
        boundaries = x;
    }

    /**
     * @hibernate.set id="subscribers" table="ECSpec_Subscribers"
     * @hibernate.collection-key column="specName"
     * @hibernate.collection-element column="subscribers" type="string"
     *                               lazy="false"
     */
    public Set getSubscribers() {
        return subscribers;
    }

    public void setSubscribers(Set<String> x) {
        subscribers = x;
    }

    /**
     * @hibernate.property
     */
    public boolean getIncludeSpecInReports() {
        return includeSpecInReports;
    }

    public void setIncludeSpecInReports(boolean x) {
        this.includeSpecInReports = x;
    }

    /*
     * From 8.2 of ALE spec : If the reportSpecs parameter is .......contains
     * two ECReportSpec instances with the same reportName, then the define and
     * immediate methods SHALL raise an ECSpecValidation.
     */
    public void addReportSpec(ECReportSpec x) throws ECSpecValidationException {
        if (reportSpecs.get(x.getReportName()) != null)
            throw new ECSpecValidationException(getSpecName()
                    + "already has a reportSpec with id " + x.getReportName());
        reportSpecs.put(x.getReportName(), x);
    }

    public void deleteReportSpec(ECReportSpec x) {
        reportSpecs.remove(x.getReportName());
    }

    public void startDocument() throws SAXException {
        log.debug("Document Start");
    }

    public void startPrefixMapping(java.lang.String prefix, String uri) {
        log.debug("startPrefixMapping");
    }

    public void skippedEntity(java.lang.String name) {
        log.debug("skippedEntity");
    }

    public void setDocumentLocator(org.xml.sax.Locator locator) {
        log.debug("setDocumentLocator");
    }

    public void endPrefixMapping(java.lang.String prefix) {
        log.debug("endPrefixMapping");
    }

    public void processingInstruction(java.lang.String target,
            java.lang.String data) {
        log.debug("processingInstruction");
    }

    public void ignorableWhitespace(char buf[], int offset, int len) {
        log.debug("ignorableWhitespace");
    }

    public void startElement(String uri, String localName, String qName,
            Attributes attrs) throws SAXException {
        log.debug("start element" + localName);
        if (qName.equals(DeviceProtocol.READER_EVENT)) {
            try {
                Sensor sensor = new Sensor(attrs.getValue(DeviceProtocol.SENSORID));
                readerEvent = new ReaderEvent(sensor);
                readerEvent.setTimestamp(Long.parseLong(attrs
                        .getValue(DeviceProtocol.TIMESTAMP)));
                readerEvent.setReaderName(attrs.getValue(DeviceProtocol.READERID));
                tagList = new ArrayList<Tag>();

            } catch (Exception e) {
                // TODO Auto-generated catch block
                log.error("exception to do: ", e);
            }

        } else if (qName.equals(DeviceProtocol.TAG)) {
            currentTag = new Tag();
            currentTag.setCount(Integer.parseInt(attrs.getValue(DeviceProtocol.COUNT)));
            currentTag.setBinvalue(attrs.getValue(DeviceProtocol.BINVALUE).getBytes());
            currentTag.setValue(attrs.getValue(DeviceProtocol.VALUE));
        }
    }

    public void characters(char buf[], int offset, int len) throws SAXException {
        log.debug("characters");
        /*
         * While parsing a java.io.Reader (as opposed to a java.io.InputStream),
         * SAX parser calls this method when it encounters the '\n' character
         * ... no good for me 'cause in doing so, my charArray StringBuffer will
         * be over-written and populated with a '\n'.
         */
        if (buf[offset] == '\n') return;

        charArray.append(buf, offset, len);
    }

    public void endElement(String nameSpaceURI, String localName, String qName)
            throws SAXException {
        log.debug("QName=" + qName + " uri=" + nameSpaceURI);
        if (qName.equals(DeviceProtocol.TAG)) {
            tagList.add(currentTag);
        }
        charArray.delete(0, charArray.length());

        if (qName.equals(DeviceProtocol.READER_EVENT)) {
            readerEvent.setTagIds(tagList);
            events.add(readerEvent);
            tagList = null;
            readerEvent = null;
        }
    }

    public void endDocument() {
    }

    /**
     * 
     * @param dataQueue
     */
    public void registerSpecWithDeviceManagers(Queue dataQueue) {

        boolean success = true;

        DeviceECSpec deviceECSpec = getDeviceECSpec(dataQueue);

        List<LogicalDevice> logicalDevices = this.getLogicalDevices();

        Set<String> deviceManagerSet = new HashSet<String>();

        long timeout = 10000;
        for (LogicalDevice logicalDevice : logicalDevices) {

            Set<Reader> readers = logicalDevice.getReaderSet();

            for (Reader reader : readers) {

                deviceManagerSet.add(reader.getDeviceManagerId());

                deviceECSpec.addReader(reader.getName());
            }

        }

        for (String deviceManagerId : deviceManagerSet) {

            DeviceManager dManager;
            try {
                dManager = (DeviceManager) ServiceLocator.getService(
                        DeviceManager.class, deviceManagerId, timeout);

                dManager.registerDeviceECSpec(deviceECSpec);

                log.debug(getSpecName() + " is registered with DM");

            } catch (Exception e) {
                success = false;
                log.error("unable to register " + deviceManagerId, e);
            }
        }// end for all device managers

        if (!success)
            throw new InfrastructureException(
                    "not able to register with all device managers");
    }

    public boolean unRegisterSpecWithDeviceManagers()
            throws InfrastructureException {

        List<LogicalDevice> logicalDevices = this.getLogicalDevices();

        Set<String> deviceManagerSet = new HashSet<String>();

        long timeout = 10000;
        for (LogicalDevice logicalDevice : logicalDevices) {

            Set<Reader> readers = logicalDevice.getReaderSet();

            for (Reader reader : readers) {

                deviceManagerSet.add(reader.getDeviceManagerId());

            }

        }

        try {
            for (String deviceManagerId : deviceManagerSet) {

                log.debug("looking up :" + deviceManagerId);
                DeviceManager dManager;

                dManager = (DeviceManager) ServiceLocator.getService(
                        DeviceManager.class, deviceManagerId, timeout);

                log.debug("Located device manager = " + dManager
                        + " for deviceManagerId = " + deviceManagerId);
                dManager.unRegisterDeviceECSpec(getSpecName());
                log.debug(getSpecName() + " is unregistered with DM");
            }

        } catch (Exception e) {
            log.error("cannot unregister with Device Managers");
            throw new InfrastructureException(e);
        }
        return true;
    }

    public void sendReports() throws ImplementationException {
        try {

            ECReportsDocument reports = generateReports();

            for (Iterator iter = subscribers.iterator(); iter.hasNext();) {
                String subscriber = (String) iter.next();

                log.debug("subscriber=" + subscriber);

                log.debug("send reports: " + xml);

                /*
                 * Document doc = XMLUtil.generateDocFromECReports(reports);
                 * String xml = XMLUtil.generateXMLFromDoc(doc); NameValuePair
                 * nvSpecName = new NameValuePair("specName",specName);
                 * NameValuePair nvPair = new NameValuePair(specName,xml);
                 * nvpairs[0] = nvPair; nvpairs[1] = nvSpecName; HTTPConnector
                 * httpConnector = new HTTPConnector(subscriber,nvpairs);
                 * httpConnector.post();
                 */
                URI uri = new URI(subscriber);
                log.debug(uri.getHost() + ":" + uri.getPath());

                JMSUtil.deliverMessageToQueue(uri.getHost(), uri.getPath(),
                        reports.xmlText());

            }// end for
            /*
             * persists ReaderEvents
             * 
             * 
             * ReaderEventDAO readerEventDAO = ReaderEventDAOFactory.create();
             * for (Iterator iter = events.iterator(); iter.hasNext();) {
             * ReaderEvent element = (ReaderEvent) iter.next(); try {
             * readerEventDAO.updateReaderEvent(element); } catch
             * (InfrastructureException e) { // TODO Auto-generated catch block
             * e.printStackTrace(); } }// end for
             * 
             */
        } catch (URISyntaxException e) {
            log.error("sendReports failed: ", e);
            throw new ImplementationException(e);
        } catch (XmlException e) {
            log.error("sendReports failed: ", e);
            throw new ImplementationException(e);
        }

    }

    /**
     * 
     * @hibernate.property type="text" lazy="false"
     * 
     * @return Returns the xml.
     */
    public String getXml() {
        return xml;
    }

    /**
     * @param xml
     *            The xml to set.
     */
    public void setXml(String xml) {
        this.xml = xml;
    }

    /**
     * 
     * 
     * @return Returns the reportSpecs.
     */
    public HashMap getReportSpecs() {
        return reportSpecs;
    }

    /**
     * @param reportSpecs
     *            The reportSpecs to set.
     */
    public void setReportSpecs(HashMap<String, ECReportSpec> reportSpecs) {
        this.reportSpecs = reportSpecs;
    }

    /**
     * @hibernate.property
     * 
     * @return Returns the currentState.
     */
    public int getCurrentState() {
        return currentState;
    }

    /**
     * @param currentState
     *            The currentState to set.
     */
    public void setCurrentState(int currentState) {
        this.currentState = currentState;
    }

    private List<LogicalDevice> getLogicalDevices() {

        LogicalDeviceDAO logicalDeviceDAO = LogicalDeviceDAOFactory.create();

        Set<String> logicalDeviceNameList = this.getLogicalReaders();
        if (logicalDeviceNameList == null)
            throw new InfrastructureException("logicalDeviceNameList is null");

        if (logicalDeviceNameList.size() <= 0)
            throw new InfrastructureException("logicalDeviceNameList is empty");

        List<LogicalDevice> logicalDeviceList = new ArrayList<LogicalDevice>();

        LogicalDevice logicalDeviceExample = null;
        try {
            for (String logicalDeviceName : logicalDeviceNameList) {
                logicalDeviceExample = new LogicalDevice();
                logicalDeviceExample.clear();
                logicalDeviceExample.setName(logicalDeviceName);
                LogicalDevice ld = logicalDeviceDAO.get(logicalDeviceExample);
                if (ld == null)
                    throw new InfrastructureException(
                            "cannot find logical device: " + logicalDeviceName);
                logicalDeviceList.add(ld);
            }// end for
        } catch (InfrastructureException e) {
            throw e;
        }

        return logicalDeviceList;
    }

    private DeviceECSpec getDeviceECSpec(Queue dataQueue) {

        DeviceECSpec deviceECSpec = new DeviceECSpec();

        try {
            ECTime time = this.getBoundaries().getDuration();

            deviceECSpec.setSpecName(getSpecName());

            deviceECSpec.setDuration(time.getDuration());

            deviceECSpec.setQueueName(dataQueue.getQueueName());

            deviceECSpec.setHost(hostname);
            /*
             * create a new set, otherwise hibernate will have lazyloading that
             * will cause RMI martialing to fail, since ECSpec is not
             * serializable.
             */
            Set<String> logicalReaders = new HashSet<String>();
            logicalReaders.addAll(logicalReaders);
            deviceECSpec.setLogicalReaders(logicalReaders);

        } catch (JMSException e) {
            log.error("unable to get JMS Queue Name to create DeviceECSpec");
            throw new InfrastructureException(e);
        }

        return deviceECSpec;

    }
}

⌨️ 快捷键说明

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