Adapter Development Kit 9.12 | webMethods Adapter Development Kit Documentation | webMethods Adapter Development Kit Installation and User’s Documentation | Polling Notifications | Polling Notification Implementation | Example Polling Notification Class
 
Example Polling Notification Class
package com.wm.MyAdapter.notifications;

import com.wm.adk.cci.record.WmRecord;
import com.wm.adk.cci.record.WmRecordFactory;
import com.wm.adk.connection.WmManagedConnection;
import com.wm.adk.error.AdapterException;
import com.wm.adk.metadata.ResourceDomainValues;
import com.wm.adk.metadata.WmAdapterAccess;
import com.wm.adk.metadata.WmTemplateDescriptor;
import com.wm.adk.notification.WmPollingNotification;

import java.io.File;
import java.util.ArrayList;
import java.util.Locale;
import javax.resource.ResourceException;

import com.wm.MyAdapter.MyAdapter;

public class SimpleNotification extends WmPollingNotification
{
public static final String NOTIFICATION_SETUP_GROUP = "SimpleNotification";

public static final String DIRECTORY_PARM = "directory";
public static final String CHECK_ADDED_PARM = "checkAdded";
public static final String CHECK_DELETED_PARM = "checkDeleted";
public static final String SIG_FIELD_NAMES_PARM = "fieldNames";
public static final String SIG_FIELD_TYPES_PARM = "fieldTypes";
public static final String SIG_PARM = "signature";

public static final String DIRECTORIES_RD =
"SimpleNotification.directories.rd";
public static final String FIELD_NAMES_RD =
"SimpleNotification.fieldNames.rd";
public static final String FIELD_TYPES_RD =
"SimpleNotification.fieldTypes.rd";

private String _directory;
private boolean _checkAdded;
private boolean _checkDeleted;
private String[] _fieldNames;
private String[] _fieldTypes;

public void setDirectory(String val){_directory = val;}
public void setCheckAdded(boolean val){_checkAdded = val;}
public void setCheckDeleted(boolean val){_checkDeleted = val;}
public void setFieldNames(String[] val){_fieldNames = val;}
public void setFieldTypes(String[] val){_fieldTypes = val;}
public void setSignature(String[] val){}

private ArrayList _fileList = new ArrayList();

public SimpleNotification(){}

public void fillWmTemplateDescriptor(WmTemplateDescriptor descriptor, Locale l)
throws ResourceException
{
descriptor.createGroup(NOTIFICATION_SETUP_GROUP,
new String[]{DIRECTORY_PARM, CHECK_ADDED_PARM, CHECK_DELETED_PARM,
SIG_FIELD_NAMES_PARM, SIG_FIELD_TYPES_PARM, SIG_PARM});
descriptor.createFieldMap(
new String[]{SIG_FIELD_NAMES_PARM, SIG_FIELD_TYPES_PARM, SIG_PARM},
false);

descriptor.setHidden(SIG_FIELD_NAMES_PARM);
descriptor.setHidden(SIG_FIELD_TYPES_PARM);
descriptor.setHidden(SIG_PARM);
descriptor.createTuple(
new String[]{SIG_FIELD_NAMES_PARM, SIG_FIELD_TYPES_PARM});

descriptor.setResourceDomain(DIRECTORY_PARM, DIRECTORIES_RD, null);
descriptor.setResourceDomain(SIG_FIELD_NAMES_PARM, FIELD_NAMES_RD, null);
descriptor.setResourceDomain(SIG_FIELD_TYPES_PARM, FIELD_TYPES_RD, null);
descriptor.setResourceDomain(SIG_PARM, WmTemplateDescriptor.OUTPUT_FIELD_NAMES,
new String[]{SIG_FIELD_NAMES_PARM, SIG_FIELD_TYPES_PARM});
descriptor.setDescriptions(
MyAdapter.getInstance().getAdapterResourceBundleManager(),l);
}

public void runNotification() throws ResourceException
{
File thisDir = new File(_directory);
File [] newList = thisDir.listFiles();
ArrayList scratchCopy = new ArrayList(this._fileList);

for (int nlIndex = 0;nlIndex < newList.length;nlIndex++)
{
String name = newList[nlIndex].getName();
if(newList[nlIndex].isFile())
{
if(scratchCopy.contains(name))
{
scratchCopy.remove(name);
}
else
{
this._fileList.add(name);
if(this._checkAdded)
{
this.doNotify(createNotice(name,_directory,true,false));
}
}
}
else
{
scratchCopy.remove(name);
}

}
// now anything left in the scratch copy is missing from the directory

String[] deadList = new String[scratchCopy.size()];
scratchCopy.toArray(deadList);
for(int dlIndex = 0; dlIndex < deadList.length;dlIndex++)
{
this._fileList.remove(deadList[dlIndex]);
if(this._checkDeleted)
{
this.doNotify(createNotice(deadList[dlIndex], _directory,false,true));
}
}

}


public Boolean adapterCheckValue(WmManagedConnection connection,
String resourceDomainName, String[][] values, String testValue)
throws AdapterException
{

boolean result = true;

if(resourceDomainName.equals(DIRECTORIES_RD))
{
File testDir = new File(testValue);
if (!testDir.exists())
{
result = false;
}
else if(!testDir.isDirectory())
{
result = false;
}
}

return new Boolean(result);
}

public ResourceDomainValues[] adapterResourceDomainLookup(
WmManagedConnection connection, String resourceDomainName,
String[][] values) throws AdapterException
{
ResourceDomainValues[] results = null;

if (resourceDomainName.equals(FIELD_NAMES_RD) ||
resourceDomainName.equals(FIELD_TYPES_RD))
{
ResourceDomainValues names =
new ResourceDomainValues(FIELD_NAMES_RD,new String[] {
"FileName", "Path","isAdded","isDeleted"});

ResourceDomainValues types =
new ResourceDomainValues(FIELD_TYPES_RD,new String[] {
"java.lang.String", "java.lang.String",
"java.lang.Boolean","java.lang.Boolean"});
results = new ResourceDomainValues[] {names,types};
}

return results;
}

public void registerResourceDomain( WmManagedConnection connection,
WmAdapterAccess access) throws AdapterException
{
access.addResourceDomainLookup(this.getClass().getName(),
FIELD_NAMES_RD,connection);
access.addResourceDomainLookup(this.getClass().getName(),
FIELD_TYPES_RD,connection);

ResourceDomainValues rd = new ResourceDomainValues(DIRECTORIES_RD,
new String[] {""});
rd.setComplete(false);
rd.setCanValidate(true);
access.addResourceDomain(rd);
access.addCheckValue(DIRECTORIES_RD, connection);
}

private WmRecord createNotice(String file, String dir, boolean isAdded,
boolean isDeleted)
{
WmRecord notice =
WmRecordFactory.getFactory().createWmRecord("notUsed");
notice.put("FileName",file);
notice.put("Path",dir);
notice.put("isAdded",new Boolean(isAdded));
notice.put("isDeleted", new Boolean(isDeleted));

return notice;
}

}