Adapter Development Kit 6.5 | webMethods Adapter Development Kit Documentation | webMethods Adapter Development Kit Installation and User’s Documentation | Polling Notifications | Polling Notification Implementation | Example Polling Notification: SimpleNotification
 
Example Polling Notification: SimpleNotification
1. package com.mycompany.adapter.myadapter.notifications;
2.
3. import java.io.File;
4. import java.util.ArrayList;
5. import java.util.Locale;
6. import javax.resource.ResourceException;
7.
8. import com.mycompany.adapter.myadapter.services.ResourceDomainHandler;
9. import com.wm.adk.cci.record.WmRecord;
10. import com.wm.adk.cci.record.WmRecordFactory;
11. import com.wm.adk.connection.WmManagedConnection;
12. import com.wm.adk.error.AdapterException;
13. import com.wm.adk.metadata.ResourceDomainValues;
14. import com.wm.adk.metadata.WmAdapterAccess;
15. import com.wm.adk.metadata.WmTemplateDescriptor;
16. import com.wm.adk.notification.WmPollingNotification;
17.
18. public class SimpleNotification extends WmPollingNotification
19. implements ResourceDomainHandler
20. {
21.
22. public static final String DIRECTORY_PARM = "directory";
23. public static final String CHECK_ADDED_PARM = "checkAdded";
24. public static final String CHECK_DELETED_PARM = "checkDeleted";
25.
26. public static final String SIG_FIELD_NAMES_PARM = "fieldNames";
27. public static final String SIG_FIELD_TYPES_PARM = "fieldTypes";
28. public static final String SIG_PARM = "signature";
29.
30. public static final String NOTIFICATION_SETUP_GROUP =
31. "SimpleNotification.setup";
32.
33. public static final String FIELD_NAMES_RD =
"SimpleNotification.fieldNames.rd";
34. public static final String FIELD_TYPES_RD =
"SimpleNotification.fieldTypes.rd";
35. public static final String DIRECTORIES_RD =
36. "SimpleNotification.direcotries.rd";
37.
38. private String _directory;
39. private boolean _checkAdded;
40. private boolean _checkDeleted;
41. private String[] _fieldNames;
42. private String[] _fieldTypes;
43.
44.
45. private ArrayList _fileList = new ArrayList();
46.
47. public void setDirectory(String val){_directory = val;}
48. public void setCheckAdded(boolean val){_checkAdded = val;}
49. public void setCheckDeleted(boolean val){_checkDeleted = val;}
50. public void setFieldNames(String[] val){_fieldNames = val;}
51. public void setFieldTypes(String[] val){_fieldTypes = val;}
52. public void setSignature(String[] val){}
53.
54. public SimpleNotification(){}
55.
56. public void runNotification() throws ResourceException
57. {
58. File thisDir = new File(_directory);
59. File [] newList = thisDir.listFiles();
60. ArrayList scratchCopy = new ArrayList(this._fileList);
61.
62. for (int nlIndex = 0;nlIndex < newList.length;nlIndex++)
63. {
64. String name = newList[nlIndex].getName();
65. if(newList[nlIndex].isFile())
66. {
67. if(scratchCopy.contains(name))
68. {
69. scratchCopy.remove(name);
70. }
71. else
72. {
73. this._fileList.add(name);
74. if(this._checkAdded)
75. {
76. this.doNotify(createNotice(name,_directory,true,false));
77. }
78. }
79. }
80. else
81. {
82. scratchCopy.remove(name);
83. }
84.
85. }
86. // now anything left in the scratch copy is missing from the directory
87.
88. String[] deadList = new String[scratchCopy.size()];
89. scratchCopy.toArray(deadList);
90. for(int dlIndex = 0; dlIndex < deadList.length;dlIndex++)
91. {
92. this._fileList.remove(deadList[dlIndex]);
93. if(this._checkDeleted)
94. {
95. this.doNotify(createNotice(deadList[dlIndex],
_directory,false,true));
96. }
97.
98. }
99.
100. }
101.
102.
103. public void fillWmTemplateDescriptor(WmTemplateDescriptor descriptor
Locale l)
104. throws ResourceException
105. {
106. descriptor.createGroup(NOTIFICATION_SETUP_GROUP,
107. new String[]{DIRECTORY_PARM,
108. CHECK_ADDED_PARM,
109. CHECK_DELETED_PARM,
110. SIG_FIELD_NAMES_PARM,
111. SIG_FIELD_TYPES_PARM,
112. SIG_PARM});
113. descriptor.createFieldMap(new String[]
114. {SIG_FIELD_NAMES_PARM,
115. SIG_FIELD_TYPES_PARM,
116. SIG_PARM},false);
117. descriptor.setHidden(SIG_FIELD_NAMES_PARM);
118. descriptor.setHidden(SIG_FIELD_TYPES_PARM);
119. descriptor.setHidden(SIG_PARM);
120. descriptor.createTuple(
121. new String[]{SIG_FIELD_NAMES_PARM,SIG_FIELD_TYPES_PARM});
122.
123. descriptor.setResourceDomain(DIRECTORY_PARM,DIRECTORIES_RD,null);
124.
125. descriptor.setResourceDomain(SIG_FIELD_NAMES_PARM,FIELD_NAMES_RD,null);
126. descriptor.setResourceDomain(SIG_FIELD_TYPES_PARM,FIELD_TYPES_RD,null);
127. descriptor.setResourceDomain(SIG_PARM,
128. WmTemplateDescriptor.OUTPUT_FIELD_NAMES,
129. new String[]{SIG_FIELD_NAMES_PARM,SIG_FIELD_TYPES_PARM});
130. descriptor.setDescriptions(
MyAdapter.getInstance().getAdapterResourceBundleManager(),l);
131. }
132.
133. public Boolean adapterCheckValue(
134. WmManagedConnection connection,
135. String resourceDomainName,
136. String[][] values,
137. String testValue)
138. throws AdapterException
139. {
140.
141. boolean result = true;
142.
143. if(resourceDomainName.equals(DIRECTORIES_RD))
144. {
145. File testDir = new File(testValue);
146. if (!testDir.exists())
147. {
148. result = false;
149. }
150. else if(!testDir.isDirectory())
151. {
152. result = false;
153. }
154. }
155.
156. return new Boolean(result);
157. }
158.
159. public ResourceDomainValues[] adapterResourceDomainLookup(
160. WmManagedConnection connection,
161. String resourceDomainName,
162. String[][] values)
163. throws AdapterException
164. {
165. ResourceDomainValues[] results = null;
166.
167. if (resourceDomainName.equals(FIELD_NAMES_RD)
168. || resourceDomainName.equals(FIELD_TYPES_RD))
169. {
170. ResourceDomainValues names =
171. new ResourceDomainValues(FIELD_NAMES_RD,new String[] {
172. "FileName", "Path","isAdded","isDeleted"});
173.
174. ResourceDomainValues types =
175. new ResourceDomainValues(FIELD_TYPES_RD,new String[] {
176. "java.lang.String", "java.lang.String",
177. "java.lang.Boolean","java.lang.Boolean"});
178. results = new ResourceDomainValues[] {names,types};
179. }
180.
181. return results;
182. }
183.
184. public void registerResourceDomain(
185. WmManagedConnection connection,
186. WmAdapterAccess access)
187. throws AdapterException
188. {
189.
190. access.addResourceDomainLookup(this.getClass().getName(),
191. FIELD_NAMES_RD,connection);
192. access.addResourceDomainLookup(this.getClass().getName(),
193. FIELD_TYPES_RD,connection);
194.
195. ResourceDomainValues rd = new ResourceDomainValues(DIRECTORIES_RD,
196. new String[] {""});
197. rd.setComplete(false);
198. rd.setCanValidate(true);
199. access.addResourceDomain(rd);
200. access.addCheckValue( DIRECTORIES_RD,connection);
201.
202. }
203.
204. private WmRecord createNotice(String file,
205. String dir, boolean isAdded, boolean isDeleted)
206. {
207. WmRecord notice =
WmRecordFactory.getFactory().createWmRecord("notUsed");
208. notice.put("FileName",file);
209. notice.put("Path",dir);
210. notice.put("isAdded",new Boolean(isAdded));
211. notice.put("isDeleted", new Boolean(isDeleted));
212.
213. return notice;
214.
215. }
216.
217. }