C H A P T E R  2

Creating Custom Filters and Connectors

This chapter describes how to create your own custom filters and connectors. The Sun Java System RFID Software Toolkit includes a sample template for creating a filter. The procedures in this chapter use the RFID Software Toolkit in conjunction with the NetBeanstrademark IDE, version 4.1 to describe the creation of a sample custom filter. The following topics are included:


Setting Up Your NetBeans Environment

The Sun Java System RFID Software Toolkit includes a NetBeans plug-in (.nbm), com-sun-autoid-toolkit.nbm, that contains various templates and code examples. One of these templates is the EPCTypeFilter. You can us it to simplify the creation of custom components for the RFID Software. The RFID Software Toolkit .nbm contains source templates to use as a basis for your custom code.

To install the RFID Software Toolkit NetBeans plug-in, you first download and install the NetBeans 4.1 IDE. Then, you need to set up your development environment using the following procedures:


procedure icon  To Download and Install NetBeans

1. Download NetBeans 4.1 from http://java.sun.com/j2se/1.4.2/download-netbeans.html.

2. Install the NetBeans IDE according to the instructions.

3. Use the following procedures to set up the NetBeans environment for your RFID component development.


procedure icon  To Download and Install the RFID Software Toolkit

1. Download the RFID Software Toolkit, RfidToolkit.zip, from the Sun Partner Advantage web site http://partneradvantage.sun.com/.

The software toolkit is available at the following URL: http://partneradvantage.sun.com/protected/partners/technology
/rfid/
once you log in to the membership center.

2. Unzip the file to a directory on your system.

3. After launching NetBeans, choose Tools right arrow Update Center.

The Update Center Wizard appears.


Screen capture of Update Center Wizard Module Location pane. Buttons are Proxy Configuration, Next and Cancel. Radio buttons are Check the Web for Available Updates and New Modules or Install Manually Downlaoded Modules.

4. Select Install Manually Downloaded Modules (.nbm files) and click Next.


Screen capture of Update Center Wizard module selection pane. Buttons are Add, Back, and Cancel.

5. Click Add and browse to the com-sun-autoid-toolkit.nbm file from the unzipped RfidToolkit.zip archive.

6. Select the NBM and click OK.


Screen capture of Update Center wizard showing RFID Software Toolkit selected for installation into NetBeans. Buttons are Add, Back, Next, and Cancel.

7. Click Next.

The RfidToolkit appears in the Include in Install list.


Screen capture of NetBeans Update Center Wizard ready to install the RFID Software Toolkit nbm.

8. Click Next and accept the license agreement by clicking Accept.

You see a short visual cue that the IDE updater is installing the module.

9. When the installation is complete, click Next.

The View Certificates and Install Modules page appears.

10. Select the check box next to RfidToolkit - version 3.

An Unsigned Module dialog box appears.

11. Select Yes and click Finish.

Now you are ready to set up your samples. To use the RFID filter project, proceed to the next section.


procedure icon  To Set Up the Example Filter Project

1. In NetBeans, choose File right arrow New Project.

The New Project wizard appears.

2. Under Categories, select RfidToolkit.

3. Under Projects, select RfidFilter.


Screen capture showing New Project wizard with RfidFilter selected. Buttons are Next and Cancel.

4. Click Next.


Screen capture showing New Java Class Library Name and Location pane. Data fields are RFID Project Name, RFID Project Location, Project Folder, Create Package, and Filter Name. Buttons are Back and Cancel.

5. In the RFID Project Name field, type EPCTypeFilter.

Notice that when you type the RFID Project Name, the IDE automatically suggests this name for the name of the RFID Project Folder.

6. For the RFID Project Location field, click Browse, navigate to any directory on your computer and create a new folder called Projects.

Your RFID projects will be stored in this location.

7. Confirm that Set as Main Project is selected.

8. In the Create Package field, type the value, com.mycompany.

This is the package name of the filter.

9. In the Filter Name field, type EPCTypeFilter.


Screen capture of New Java Class Library Name and Location pane with data fields for EPCTypeFilter example. Buttons are Back, Finish and Cancel.

10. Click Finish.

Your RFID filter project appears in the Projects window.


Screen capture of NetBeans Projects window showing the new EPCTypeFilter project.



Note - To use the built-in JUnit tests, you need to add JAR files to the RFID library. Continue to the next procedure.




procedure icon  To Create the RFID Library for the Custom Component Examples

Use this procedure to create a library that is used for both the RFID filter and connector projects.

1. In the IDE Projects window, under the EPCTypeFilter node, right-click Libraries.

2. Select Add Library from the context menu.

A dialog appears. You need to create a new library called RFID.


Screen capture showing the Add Library dialog. Buttons are Manage Libraries, Cancel, and Help.



Note - Adding a new RFID library only has to be done once. Use this procedure for the first RFID custom component project. After the library has been added, you can go directly from step 5 to step 11.



3. Click Manage Libraries in the dialog.

4. Click New Library and type the following values:

5. Click OK.

6. Under Libraries, confirm that the RFID library is selected.

7. Now add the required JAR files as listed for your platform by performing these steps for each of the JAR files to be added:

a. Click Add Jar/Folder on the right hand side of the dialog.

b. Browse to the JAR file location and select the JAR file.

c. Click Add Jar/Folder to add the JAR file to the library.

The following tables show the location of the JAR files for each supported platform.


TABLE 2-1 Location of JAR Files on Solaris

Location

Name of Required JAR file

/opt/SUNWrfid/lib

sun-rfid-common.jar

/opt/SUNWrfid/lib/util

concurrent.jar

/usr/share/lib

jaxb-api.jar

jaxb-impl.jar

xsdlib.jar

jaxb-libs.jar

jax-qname.jar

namespace.jar

relaxngDatatype.jar

/opt/SUNWjdmk/5.1/lib

jmx.jar


TABLE 2-2 Location of JAR Files on Linux

Location

Name of Required JAR file

/opt/sun/rfidem/lib

sun-rfid-common.jar

/opt/sun/rfidem/lib/utils

concurrent.jar

jaxb-api.jar

jaxb-impl.jar

xsdlib.jar

jaxb-libs.jar

jax-qname.jar

namespace.jar

relaxngDatatype.jar

/opt/sun/jdmk/5.1/lib

jmx.jar


TABLE 2-3 Location of JAR Files on Windows

Location

Name of Required JAR file

C:\Program Files\Sun\RFID Software\rfidem\lib

sun-rfid-common.jar

C:\Program Files\Sun\RFID Software\rfidem\lib\utils

concurrent.jar

jaxb-api.jar

jaxb-impl.jar

xsdlib.jar

jaxb-libs.jar

jax-qname.jar

namespace.jar

relaxngDatatype.jar

C:\Sun\rfidem\lib\SUNWjdmk\5.1\lib

jmx.jar


  

8. When you are finished adding the JAR files to the RFID library, confirm that your Library class path is complete.

The dialog should appear similar to the following screen capture (from a Windows system).


Screen capture of Library Manager dialog showing the RFID jars to be added to the RFID Class Library.

9. Click OK.

This takes you back to the Add Library dialog. You only add a Library once.

10. Select the RFID library from the list and click Add Library.

The libraries appear in the Projects windows as shown in the following screen capture. You should now be able to build and test successfully.


Screen capture showing the Projects window with the newly added RFID class library jars.


procedure icon  To Build and Test the Sample Filter Project

1. Right-click EPCTypeFilter project in the Projects window.

2. Select Clean and Build Project from the context menu.

The project should compile with no errors.

3. Right-click EPCTypeFilter again and select Test Project from the context menu.

A successful test is indicated by the message "Build Successful" in the bottom panel.

An unsuccessful test indicates the specific failure as the last item in the output window. If the test fails, it is likely that you do not have all of the RFID libraries added to the project. See To Create the RFID Library for the Custom Component Examples for the complete list of required libraries and instructions for adding them.

At this point, you have a sample filter that builds and test cleanly. Proceed to the next section to learn more about customizing the sample filter.


Creating a Custom Filter

This section shows how EPCTypeFilter was created using the NetBeans IDE. To follow these steps, you must have Netbeans 4.1 installed properly with the com-sun-autoid-toolkit.nbm module installed and the RFID libraries configured as described in the previous section, Setting Up Your NetBeans Environment.

This section contains the following procedures:

Understanding the Sample EPCTypeFilter

EPCTypeFilter is an example filter that screens for specific EPC types and sizes. It is a real-world example of a filter that you can usefully deploy. It contains enough detail to enable you to use the ideas on filter development of all types.

EPCTypeFilter filters out identifiers that do not match the set of EPC types that are configured in the filter. For example, if the EPCTypeFilter is configured to accept SGLN Identifiers (identifiers of class com.sun.autoid.identity.EPC_SGLN_64 or com.sun.autoid.identity.EPC_SGLN_96), then all Identifier objects that are not of type SGLN are excluded from the filter's output.

Similarly, EPCTypeFilter filters out Identifier objects that do not match the configured EPC sizes. So if EPCTypeFilter is configured to accept only 96-bit tags, then all Identifier objects representing tags of other sizes are excluded from the filter's output.

You can use multiple rules. You can specify that the filter only accept SGLN and SSCC Identifiers from 96-bit tags. The result is that Identifier objects of class com.sun.autoid.identity.EPC_SGLN_96 and com.sun.autoid.identity.EPC_SSCC_96 pass through EPCTypeFilter.

The first step in creating a functional EPCTypeFilter example is to define the properties that are needed for configuring the filter. The filter description makes it clear that you need two properties:

It is good practice to add a description of these types to the Javadoc comments. The procedure starts by completing the class documentation.


procedure icon  To Customize the Sample Filter

1. Launch NetBeans.

2. If the EPCTypeFilter project is not open, open it.

3. Modify the class documentation to include text that describes the filter's functionality and the new configuration properties that you are adding to EPCTypeFilter.

a. Expand the Source Packages node in the Projects window.

b. Expand the com.mycompany node.

c. Double-click EPCTypeFilter.java.

The source code appears in the Source Editor of the IDE.

d. Modify the class documentation to look like the following:


CODE EXAMPLE 2-1 Example Class Documentation

/*

* 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).

*

* 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 passes through the filter.

*

* This filter assumes that all specified EPCSizes apply to all EPCTypes,

* and does 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. 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 the

* filter, the default behavior is to throw an exception.

* This behavior can be overridden by setting the DieOnUnknownEvent

* property to "false".

*

* Properties for EPCTypeFilter are as follows:

* LogLevel - set the logging level see java.util.logging.level

* EPCTypes - `DoD', GIAI', `GID', `GRAI', `SGLN', `SGTIN', `SSCC',

* `TYPEIII', `TYPE1'

*

* EPCSizes - `96', `64' (default is `64,96')

* DieOnUnknownEvent - default = true if this filter should throw an

* exception if an unrecognized event is passed in. The value

* should be false otherwise.

*/


As you can see, the properties, EPCTypes and EPCSizes, are used to identify the types and sizes of EPCs that pass through EPCTypeFilter.



Note - As you add the necessary code modifications, use the NetBeans Source Editor option Reformat Code to keep the code formatted properly. To do this, right-click in the background of the Source Editor and select Reformat Code from the context menu.



4. Add a Map object to hold the configuration.

Because the filtering process uses this field, it is important that the information be stored in a manner that enables the filter to quickly determine whether or not a specific Identifier object should pass through or be excluded. The example uses a Map object for this purpose. The key is the Identifier object's class. A Boolean value indicates whether the class is passed through the filter. This implements the essence of the filtering mechanism as simple, fast look up. Add the epcMap field at the end of the list of fields near the top of the class as follows:

5. Now initialize the epcMap object.

This code creates entries in the epcMap for every available type of EPC. Initially all entries are set to Boolean.FALSE, indicating that none of the EPCs are accepted. In a subsequent step, the filter reads the Properties object passed to the constructor and directs that information to the initialization method so the appropriate entries are set to Boolean.TRUE, indicating that they should pass through the filter. Add the initializeEPCMap method after the constructor as follows:

The sample filter takes advantage of the format of the EPC class names to populate the map. The form holds true, except for the com.sun.autoid.identity.DoD_64 and com.sun.autoid.identity.DoD_96 classes. These are dealt with by catching the ClassNotFound exception that results from trying to instantiate an EPC_DoD_64 or EPC_DoD_96 class, and trying the form that is not preceded by "EPC_".

6. To complete the initialization of the map, read the EPCTypes and EPCSizes properties from the Properties object that is passed to the constructor.

The types and sizes are represented as comma-separated Identifier objects matching the name and size of the allowed identifiers. For example, if the EPCTypes is set to "SGLN,DoD" and EPCSizes is set to "64,96", EPCTypeFilter is configured to accept all Identifier objects of class as follows:

Knowing that the property is a String containing a comma-separated list and the initializeEPCMap method accepts a collection, the final initialization code converts the comma-separated list into a collection and call the initialization method. Add this code to the constructor:

7. Add code to modify the processOneIdentifier method to filter out all Identifier objects that were not specified in the configuration.

This modification requires getting the class of the Identifier object and checking the epcMap contents to see whether the Identifier object should pass through or be rejected.

Change the processOneIdentifier method as follows:

An Identifier object is accepted by adding the object to the identifiersToSend Collection object. Only Identifier objects that are added to this Collection object are sent. The code that sends the event can be found in the process method.

With this code modification, the EPCTypeFilter example is complete. The example shows the initialization of the filter, the scheme used to determine if an EPC type should be accepted or rejected by the filter, and the mechanics of filtering the Identifier objects.


procedure icon   To Compile the Customized Filter

1. Save the EPCTypeFilter.java file.

2. Right-click the EPCTypeFilter node in the Projects window.

3. Select Clean and Build Project from the context menu.

You see BUILD SUCCESSFUL at the bottom of the output window.

Using the Filter Template JUnit Test

A JUnit test is created as part of the filter template. This test needs to be modified because your new filter requires initialization of the EPCTypes and EPCSizes properties.


procedure icon  To Modify and Run the JUnit Test

1. Expand the Test Packages node in the Projects window.

2. Expand the com.sun.rfid.filter node.

3. Double-click the EPCTypeFilterTest.java file.

The JUnit test source code appears in the main window.

4. Modify the setUp method to initialize the EPCTypes and EPCSizes properties.

You have initialized EPCTypeFilter to accept only Identifier objects of type SGLN and SSCC, of both 96-bit and 64-bit sizes. This filter now accepts Identifier objects of the following classes:

You can run the test now to see if it is working.

5. Right-click the EPCTypeFilter node in the Projects window.

6. Select Test Project from the context menu.

7. Check the output window for the result.

You find that the test indicates a failure as follows:


Testcase: testProcessIdentifierListEvent(com.sun.rfid.filter.EPCTypeFilterTest):	FAILED
Expected 13 identifiers expected:<13> but was:<4>

This message indicates the filter is actually working. Of the 13 Identifier objects passed in to this filter, only four were accepted. To fix this last problem, proceed to the next step.

8. Modify the testProcessIdentifierEvent method in the EPCTypeFilterTest.java file.

With this modification, the unit test now specifies how many Identifier objects to expect in the result.

9. Modify the testProcessIdentifierListEvent method in the EPCTypeFilterTest.java file.

This change again indicates how many Identifiers are expected to pass through the filter.

Change the JUnit code as follows:

10. Modify the testProcessDeltaEvent method in the EPCTypeFilterTest.java file.

This modification is specific to the DeltaEvents method and is the same modification as in Step 8 and Step 9.

11. Save the EPCTypeFilterTest.java file and test the filter again. (See Step 5 through Step 7).

Look for "BUILD SUCCESSFUL" in the last line of the output window.


Integrating Custom Components With the RFID Event Manager

This section describes how to integrate the EPCTypeFilter custom filter into the RFID Event Manager. The RFID Configuration Manager enables you to connect various RFID Event Manager components (adapter, filters and connectors) to create a processing chain known as Business Processing Semantics (BPS). See Sun Java System RFID Software 3.0 Administration Guide for more details. For this example, you use the RFID Configuration Manager to add the EPCTypeFilter custom filter to an existing BPS, the default Demo Configuration Object.


procedure icon  To Add the EPCTypeFilter Custom Filter to the Demo Configuration Object

1. If you have not already done so, start the RFID Configuration Manager.

2. Add the new EPCTypeFilter to the RFID Configuration Manager and configure its properties by using these steps:

a. From the RFID Configuration Manager menu, choose Roles right arrow Edit.

The RFID Role and Component Editor window appears.

b. In the navigation tree, under the Roles node, select the Demo role.

c. Choose Filter right arrow New.

Set up the filter properties as shown in the following screen capture.


Screen capture showing filter details. Properties are EPCTypes, LogLevel, EPCSizes, and DieOnUnknownEvent. Buttons are Ok and Cancel.

d. Click Ok.

The EPCTypeFilter filter is added to the navigation tree in the left pane.

3. Add the EPCTypeFilter to the Demo Role by following these steps:

a. In the navigation tree, expand the Components node and expand the Filters node.

b. Right-click EPCTypeFilter and choose Add to Role from the contextual menu.

A dialog box appears and prompts you to assign a unique name to this component.


Screen capture showing empty data field for unique component name. Buttons are Ok and Cancel.

c. Type RfidEPCTypeFilter and click Ok.

The RfidEPCTypeFilter component appears in the drawing pane.

4. Connect the components in the necessary order by following these steps:

a. Select and right-click the arrow that connects the Rfid Delta filter and the RfidFile logger components.


Screen capture of a portion of the RFID Event Manager drawing pane showing the contextual menu for deleting a connection arrow between the RfidDelta filter and RfidFileLogger connector.

b. Choose Delete from the contextual menu.

The arrow disappears.

c. Connect the RfidDelta component to the RfidEPCTypeFilter component.

To do so, click the port (the small square at the center of each component) on the RfidDelta component and drag a line to the RfidEPCTypeFilter component.

d. Connect the RfidEPCTypeFilter to the RfidFile logger component.

Click the port on the RfidEPCTypeFilter component and drag a line to the RfidFile logger component. The drawing pane shows the new component flow as seen in the following screen capture.


Screen capture showing the RfidDelta filter passing tags to the new RfidEPCTypeFilter, which passes filtered tags to the RfidFileLogger component.

5. Add the JAR file for the new EPCTypeFilter component by following these steps:

a. From the RFID Role and Component Editor menu, choose Plug-in right arrow Add Implementation Class JAR.


Screen capture of RFID Event Manager menu option to add implementation class JAR file.

b. Use the file chooser to navigate to the EPCTypeFilter.jar, select the JAR file and click Ok.

6. Close the RFID Role and Component Editor window.

7. Delete any existing Demo Configuration Object.

This is necessary so that you can create a new Demo Configuration Object to pick up the changes that you made to the Demo role.

8. Create a new Demo Configuration Object by using the following steps:

a. Choose Configuration right arrow New.

b. Select Demo as the Base Role.

c. In the Configuration Object Name field, type EPCTypeFilter.

d. Select the PMLReader as the Input Point and click Ok.

9. Choose File right arrow Save and save your new EPCTypeFilter Configuration Object.

Now you are ready to start the RFID Event Manager and use the new Demo Configuration Object.


Creating a Custom Connector

The Sun Java System RFID Software Toolkit NetBeans plug-in (.nbm), com-sun-autoid-toolkit.nbm, also contains a template called RfidConnector. You can use it to simplify the creation of custom connectors for the RFID Software.

To install the RFID Software Toolkit NetBeans plug-in, you first download and install the NetBeans 4.1 IDE. Then, if you have not already done so, you need to set up your development environment using the following procedures:


procedure icon  To Create a Sample Connector Project

1. Follow the steps in the procedure To Set Up the Example Filter Project with one change. In Step 3, select RfidConnector instead of RfidFilter.

For the purposes of this example, name your sample connector project, RFIDConnector.

2. Confirm the default name for the connector, MyConnector.

3. If you have not already done so, perform the procedure To Create the RFID Library for the Custom Component Examples to create the necessary RFID library.

This library is used for both the sample filter and the sample connector.

4. Now, make the following changes to the sample code so that the project will build correctly:

a. Open the file, MyConnector.java.

b. In the IDE's Source editor, search for the following line of code and remove the line of code from the java source file:


 tagsOut = new LinkedList();

c. Search for the following line of code:


this.passCounter += identifierCount;

d. Change this line to the following:


this.identifierCounter += identifierCount;

e. Search for the following line of code:


private long identifierCounter = 0L;

f. Just after this line of code, add the following code:


private long discardCounter = 0L;

5. Right-click the RFIDConnector project in the Projects window.

6. Select Clean and Build from the context menu.

The project should compile with no errors.

7. Now you can read through the comments in this code and modify it to create your custom connector.