📄 ecspec.java
字号:
/*
* Copyright 2005 Jeff Bride
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
*/
package org.firstopen.singularity.ale;
import java.io.StringReader;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.TextMessage;
import org.apache.log4j.Logger;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlOptions;
import org.firstopen.epc.ale.ECReport;
import org.firstopen.epc.ale.ECReportGroup;
import org.firstopen.epc.ale.ECReportGroupList;
import org.firstopen.epc.ale.ECReportGroupListMember;
import org.firstopen.epc.ale.ECReportGroupListMemberExtension;
import org.firstopen.epc.ale.ECReportList;
import org.firstopen.epc.ale.ECReports;
import org.firstopen.epc.ale.ECReportsDocument;
import org.firstopen.epc.ale.ECSpecDocument;
import org.firstopen.epc.ale.ECTerminationCondition;
import org.firstopen.epc.pmlcore.DataType;
import org.firstopen.singularity.ale.exception.ECSpecValidationException;
import org.firstopen.singularity.ale.exception.ImplementationException;
import org.firstopen.singularity.ale.exception.InvalidURIException;
import org.firstopen.singularity.ale.exception.NoSuchSubscriberException;
import org.firstopen.singularity.config.LogicalDevice;
import org.firstopen.singularity.config.dao.LogicalDeviceDAO;
import org.firstopen.singularity.config.dao.LogicalDeviceDAOFactory;
import org.firstopen.singularity.devicemgr.DeviceManager;
import org.firstopen.singularity.devicemgr.common.DeviceECSpec;
import org.firstopen.singularity.devicemgr.common.DeviceProtocol;
import org.firstopen.singularity.devicemgr.common.ServiceLocator;
import org.firstopen.singularity.system.Reader;
import org.firstopen.singularity.system.ReaderEvent;
import org.firstopen.singularity.system.Sensor;
import org.firstopen.singularity.system.Tag;
import org.firstopen.singularity.util.InfrastructureException;
import org.firstopen.singularity.util.JMSUtil;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
/**
*
*
* @author Jeff Bride
* @author Tom Rose
*
* Modified from orginal.
*
* @hibernate.class table="ECSpec" lazy="false"
*/
public class ECSpec implements ContentHandler, MessageListener {
/**
*
*/
private static final long serialVersionUID = 1043637307376972024L;
/**
* public static finals
*/
public final static String SPEC_NAME = "specName";
public final static String READERS = "logicalReaders";
public final static String READER = "logicalReader";
public final static String BOUNDARIES = "boundarySpec";
public final static String REPORT_SPECS = "reportSpecs";
public final static String REPORT_SPEC = "reportSpec";
public final static int ACTIVE_STATE = 1;
public final static int INACTIVE_STATE = 0;
/**
* private
*/
private ECSpecDocument ecSpecDocument = null;
private int currentState = INACTIVE_STATE;
private String specName;
private Set<String> logicalReaders; // list of interrogator names
private ECBoundarySpec boundaries;
private HashMap<String, ECReportSpec> reportSpecs;
private boolean includeSpecInReports = false;
private Set<String> subscribers;
private List<Tag> tagList;
private Logger log = Logger.getLogger(this.getClass());
private StringBuffer charArray = new StringBuffer();
private String xml = null;
private ReaderEvent readerEvent;
private List<ReaderEvent> events = new ArrayList<ReaderEvent>();
private String hostname = "localhost";
private Tag currentTag;
/**
*
*
*/
public ECSpec() {
SecurityManager sm = System.getSecurityManager();
if (sm == null) sm = new SecurityManager();
log.debug("ECSpec.constructor()");
InetAddress inetAddr = null;
try {
inetAddr = InetAddress.getLocalHost();
hostname = inetAddr.getHostName();
} catch (UnknownHostException e) {
log.error("unable to obtain host IP, hostname =" + hostname, e);
}
try {
logicalReaders = new HashSet<String>();
reportSpecs = new HashMap<String, ECReportSpec>();
subscribers = new HashSet<String>();
} catch (Exception x) {
log.error(x);
} finally {
try {
} catch (Exception x) {
log.error("unable to create ECSpec", x);
}
}
}
/*
* Constructor Instantiates ECSpec. Instantiating this class fulfills the
* "defining" action as per the ALE spec. Afterwards, clients can continue
* to "subscribe" to this ECSpec.
*/
public ECSpec(String specName, ECBoundarySpec boundaries) {
this();
log.debug("ECSpec.constructor()");
this.specName = specName;
this.boundaries = boundaries;
}
/*
* poll() As per the ALE spec, when an ECSpec is polled, one event cycle is
* executed
*/
public ECReportsDocument poll() throws ImplementationException {
log.debug("poll() " + getSpecName());
ECReportsDocument reports = null;
try {
reports = generateReports();
} catch (XmlException e) {
log.error(e);
throw new ImplementationException("Unable to poll ", e);
}
return reports;
}
/**
* onMessage An ECSpec object implements javax.jms.MessageListener and is
* listening for incoming raw data files on a queue that was created when
* this object is first instantiated. This method is called as each raw
* datafile , from each interrogator, is sent to queue
*/
public void onMessage(Message message) {
InputSource inSource = null;
StringReader stringReader = null;
try {
log.debug("ECSpec.onMessage()");
if (message instanceof TextMessage) {
String textMessage = ((TextMessage) message).getText();
log.debug("message recieved " + textMessage);
stringReader = new StringReader(textMessage);
inSource = new InputSource(stringReader);
XMLReader xmlReaderObj = XMLReaderFactory
.createXMLReader("org.apache.xerces.parsers.SAXParser");
xmlReaderObj.setContentHandler(this);
xmlReaderObj.setFeature(
"http://xml.org/sax/features/namespaces", false);
xmlReaderObj
.setFeature(
"http://xml.org/sax/features/namespace-prefixes",
false);
// xmlReaderObj.setFeature(
// "http://apache.org/xml/features/validation/schema", true);
xmlReaderObj.parse(inSource);
} else {
message.clearBody();
}
} catch (Exception x) {
log.error(x);
} finally {
try {
if (stringReader != null) stringReader.close();
} catch (Exception x) {
log.error("unable to retieve events from queue", x);
}
}
}
/**
*
* @return
* @throws ImplementationException
* @throws XmlException
*/
public ECReportsDocument generateReports() throws ImplementationException,
XmlException {
ECReportsDocument reportsDocument = null;
ECReports reports = null;
try {
ecSpecDocument = ECSpecDocument.Factory.newInstance();
reportsDocument = ECReportsDocument.Factory.newInstance();
reports = reportsDocument.addNewECReports();
reports.setId(specName);
reports.setALEID(specName);
reports.setSpecName(specName);
reports.setTerminationCondition(ECTerminationCondition.DURATION);
if (getIncludeSpecInReports())
reports.setECSpec(ecSpecDocument.addNewECSpec());
if (boundaries.getDuration() != null)
reports.setTotalMilliseconds((boundaries.getDuration())
.getDuration());
ECReportList reportList = reports.addNewReports();
Iterator rsIterator = reportSpecs.keySet().iterator();
while (rsIterator.hasNext()) {
String reportName = (String) rsIterator.next();
ECReport report = reportList.addNewReport();
report.setReportName(reportName);
ECReportGroup reportGroup = report.addNewGroup();
ECReportGroupList reportGroupList = reportGroup
.addNewGroupList();
for (ReaderEvent element : events) {
Collection<Tag> tags = element.getTagIds();
for (Iterator<Tag> iterator = tags.iterator(); iterator
.hasNext();) {
Tag eventTag = iterator.next();
ECReportGroupListMember member = reportGroupList
.addNewMember();
DataType reportTag = member.addNewTag();
reportTag.setText(eventTag.getValue());
reportTag.setBinary(eventTag.getBinvalue());
ECReportGroupListMemberExtension extension = member
.addNewExtension();
/*
* add extension Tag Count
*/
Node reportTagCount = extension.getDomNode()
.getOwnerDocument().createTextNode(
Integer.toString(eventTag.getCount()));
extension.getDomNode().appendChild(reportTagCount);
/*
* add extension TagReaderID
*/
Node reportTagReaderIDNode = extension.getDomNode()
.getOwnerDocument().createTextNode(
element.getReaderName());
Element readerIDelement = extension.getDomNode()
.getOwnerDocument().createElement(DeviceProtocol.READERID);
readerIDelement.appendChild(reportTagReaderIDNode);
extension.getDomNode().appendChild(readerIDelement);
/*
* add extension geocoords
*/
Node geoCoordNode = extension.getDomNode()
.getOwnerDocument().createTextNode(
element.getGeocoord());
Element geoCoordElement = extension.getDomNode()
.getOwnerDocument().createElement(DeviceProtocol.GEOCOORD);
geoCoordElement.appendChild(geoCoordNode);
extension.getDomNode().appendChild(geoCoordElement);
}// for all tags
}// for all ReaderEvents
}
GregorianCalendar calendar = new GregorianCalendar();
if (events.size() > 0) {
calendar.setTime(events.get(0).getDate());
} else { // report is empty
calendar.setTimeInMillis(System.currentTimeMillis());
}
reports.setDate(calendar);
} catch (Exception x) {
log.error("unable to create ECReports", x);
throw new ImplementationException("Problem attempting to poll "
+ getSpecName());
}
XmlOptions xmlOptions = new XmlOptions();
xmlOptions.setSaveAggressiveNamespaces();
xmlOptions.setValidateOnSet();
log.debug("parsed document" + reports.xmlText());
// Set up the validation error listener.
ArrayList validationErrors = new ArrayList();
XmlOptions validationOptions = new XmlOptions();
validationOptions.setErrorListener(validationErrors);
// Do some editing to myDoc.
// During validation, errors are added to the ArrayList for
// retrieval and printing by the printErrors method.
boolean isValid = reports.validate(validationOptions);
// Print the errors if the XML is invalid.
if (!isValid) {
Iterator iter = validationErrors.iterator();
while (iter.hasNext()) {
log.debug(">> " + iter.next() + "\n");
}
}
return reportsDocument;
}
/**
*
* @param subscriber
* @throws InvalidURIException
*/
public void registerNotificationURI(String subscriber)
throws InvalidURIException {
subscribers.add(subscriber);
}
/**
*
* @param subscriber
* @throws NoSuchSubscriberException
*/
public void unRegisterNotificationURI(String subscriber)
throws NoSuchSubscriberException {
subscribers.remove(subscriber);
}
/**
* @hibernate.id generator-class="assigned"
*/
public String getSpecName() {
return specName;
}
public void setSpecName(String x) {
specName = x;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -