📄 epctypefilter.java
字号:
/* * EPCTypeFilter.java * * Created on December 15, 2005, 2:29 PM */package com.sun.rfid.filter;import java.rmi.RemoteException;import java.util.*;import java.util.logging.*;import com.sun.autoid.container.*;import com.sun.autoid.ems.*;import com.sun.autoid.event.*;import com.sun.autoid.util.*;import com.sun.autoid.identity.*;/** * The EPCTypeFilter filter plugs into the Sun Microsystems RFID Event Manager. Filters are * situated between input devices and connectors (also known as loggers) and are used to * modify or remove identifiers from a stream of identifiers coming from an input device * (usually an RFID reader). * <p> * The EPCTypeFilter passes only specific types of EPCS, which are user configurable. This * filter identifies the type of identifier, and if it is in the set of configured EPCTypes * and has one of the configured EPCSizes, the Identifier will pass through the filter. * <p> * This filter assumes that all specified EPCSizes apply to all EPCTypes, and does fairly * rigorous checking to ensure that there is no misconfiguration. As a result, it may be * necessary to configure two or more EPCTypeFilter instances in combination to achieve the * desired effect. * <p> * This filter recognizes events of type IdentifierEvent, IdentifierListEvent, and DeltaEvent, * and outputs events of the same types. If an event is passed to this filter that is not * recognized by this filter, the default behavior is to throw an exception. This behavior * can be overridden by setting the DieOnUnknownEvent property to "false". * <p> * <b>Properties for EPCTypeFilter:</b> * <ul> * <li><b>LogLevel</b> set the logging level @see java.util.logging.Level</li> * <li><b>EPCTypes</b> 'DoD', GIAI', 'GID', 'GRAI', 'SGLN', 'SGTIN', 'SSCC', 'TYPEIII', 'TYPE1'</li> * <li><b>EPCSizes</b> '96', '64' (default is '64,96')</li> * <li><b>DieOnUnknownEvent</b> 'true' (default) if this filter should throw an exception if an * unrecognized event is passed in, 'false' otherwise.</li> * </ul> */public class EPCTypeFilter extends AbstractComponent implements Registerable, EPCTypeFilterMBean { /** * Flag used by the processOneIdentifier method that indicates a tag was seen by the reader. */ public static final int TAG_SEEN = 1; /** * Flag used by the processOneIdentifier method that indicates a tag entered the reader's * detection range. */ public static final int TAG_IN = 2; /** * Flag used by the processOneIdentifier method that indicates a tag exited the reader's * detection range. */ public static final int TAG_OUT = 3; /** * The logger used to record events. */ private Logger logger = Logger.getLogger("com.sun.rfid.filter.EPCTypeFilter"); /** * The string representing the AbstractComponent name. */ public static final String MY_NAME = "EPCTypeFilter"; /** * Keeps track of the number of events discarded by this filter. */ private long discardCounter = 0L; /** * Keeps track of the number of events passed through this filter. */ private long passCounter = 0L; /** * Specifies the behavior when an unknown event type is passed to this filter. * The default behavior is to throw an exception. This can be overridden * by setting the "DieOnUnknownEvent" property to "false". */ private boolean dieOnUnknownEvent = true; /** * A map that contains EPCs of interest. This map will associate a Class object with * a Boolean value indicating whether we are interested in it or not. */ private Map epcMap = new HashMap(); /** * Instantiate a EPCTypeFilter filter with the specified properties. * * @param properties the Properties object to use for initialization of the filter */ public EPCTypeFilter(Properties properties) { super(EPCTypeFilter.MY_NAME, properties); /* * Set the appropriate log level based on the "LogLevel" property. */ String logLevel = properties.getProperty("LogLevel"); if (null != logLevel) { this.setLogLevel(logLevel); } String dieOnUnknown = properties.getProperty("DieOnUnknownEvent"); if (null != dieOnUnknown) { this.dieOnUnknownEvent = Boolean.valueOf(dieOnUnknown).booleanValue(); } /* * Log the properties if the logger is appropriately configured. */ if (logger.getLevel().intValue() <= Level.CONFIG.intValue()) { Enumeration e = properties.propertyNames(); while (e.hasMoreElements()) { String propertyName = (String)e.nextElement(); String value = (String)properties.getProperty(propertyName); logger.log(Level.CONFIG, "EPCTypeFilter {0}={1}", new Object[] {propertyName, value}); } } /* * Read the interesting identifier types and sizes and prepare the map accordingly. */ String epcTypes = properties.getProperty("EPCTypes"); if (null == epcTypes) { throw new IllegalArgumentException("\"EPCTypes\" property must be configured"); } Collection epcNames = new LinkedList(); StringTokenizer tokenizer = new StringTokenizer(epcTypes, " ,\t\n\r\f"); while (tokenizer.hasMoreTokens()) { String epcType = tokenizer.nextToken(); epcNames.add(epcType); } /* * Read the size(s) that we are interested in. */ String sizes = properties.getProperty("EPCSizes"); if (sizes == null) { /* * By default, look for 64 and 96 bit tags of this type. */ sizes = "64,96"; } Collection epcSizes = new LinkedList(); tokenizer = new StringTokenizer(sizes, " ,\t\n\r\f"); while (tokenizer.hasMoreElements()) { String size = tokenizer.nextToken(); epcSizes.add(size); } try { this.initializeEPCMap(epcNames, epcSizes); } catch (ClassNotFoundException cnfe) { throw new IllegalArgumentException("Could not initialize the EPCTypeFilter: " + cnfe.getMessage()); } } /** * Initializes the EPC map, which maps a Class instance to a Boolean, indicating * our interest in the EPC type. * * @param epcTypes the set of types we are interested in * @param sizes the set of sizes we are interested in */ private void initializeEPCMap(Collection epcTypes, Collection epcSizes) throws ClassNotFoundException { this.epcMap.put(com.sun.autoid.identity.DoD_64.class, Boolean.FALSE); this.epcMap.put(com.sun.autoid.identity.DoD_96.class, Boolean.FALSE); this.epcMap.put(com.sun.autoid.identity.EPC_GIAI_64.class, Boolean.FALSE); this.epcMap.put(com.sun.autoid.identity.EPC_GIAI_96.class, Boolean.FALSE); this.epcMap.put(com.sun.autoid.identity.EPC_GID_96.class, Boolean.FALSE); this.epcMap.put(com.sun.autoid.identity.EPC_GRAI_64.class, Boolean.FALSE); this.epcMap.put(com.sun.autoid.identity.EPC_GRAI_96.class, Boolean.FALSE); this.epcMap.put(com.sun.autoid.identity.EPC_SGLN_64.class, Boolean.FALSE); this.epcMap.put(com.sun.autoid.identity.EPC_SGLN_96.class, Boolean.FALSE); this.epcMap.put(com.sun.autoid.identity.EPC_SGTIN_64.class, Boolean.FALSE); this.epcMap.put(com.sun.autoid.identity.EPC_SGTIN_96.class, Boolean.FALSE); this.epcMap.put(com.sun.autoid.identity.EPC_SSCC_64.class, Boolean.FALSE); this.epcMap.put(com.sun.autoid.identity.EPC_SSCC_96.class, Boolean.FALSE); this.epcMap.put(com.sun.autoid.identity.EPC_TYPEIII_64.class, Boolean.FALSE); this.epcMap.put(com.sun.autoid.identity.EPC_TYPEI_64.class, Boolean.FALSE); /* * Now go through and set the EPC classes we are interested in to True. */ Iterator types = epcTypes.iterator(); while (types.hasNext()) { String type = (String)types.next(); Iterator sizes = epcSizes.iterator(); while (sizes.hasNext()) { String size = (String)sizes.next(); Class epcClass = null; try { epcClass = Class.forName("com.sun.autoid.identity.EPC_" + type + "_" + size); } catch (ClassNotFoundException cnfe) { /* * There is one EPC that doesn't fit the above template. */ epcClass = Class.forName("com.sun.autoid.identity." + type + "_" + size); } this.epcMap.put(epcClass, Boolean.TRUE); this.logger.log(Level.CONFIG, "EPCTypeFilter adding EPC Type {0} of size {1}", new Object[] {type, size}); } } } /** * Process the event. This method is called each time the reader sends an * event to the event manager. The event type could be one of: * * <ul><li>IdentifierEvent</li> * <li>IdentifierListEvent</li> * <li>DeltaEvent</li></ul> * * @param event the Event object that holds the Identifier(s) to be * run through the filter */ protected void process(Event event) throws Exception { List tagsIn = new LinkedList(); List tagsOut = null; /* * Keep a total of the incoming events for later processing. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -