41 package com.sun.jmx.examples.scandir;
52 import java.io.FileFilter;
53 import java.io.IOException;
54 import java.util.Arrays;
55 import java.util.Collections;
56 import java.util.EnumSet;
57 import java.util.HashSet;
58 import java.util.LinkedList;
60 import java.util.logging.Level;
61 import java.util.logging.Logger;
62 import javax.management.AttributeChangeNotification;
63 import javax.management.InstanceNotFoundException;
64 import javax.management.ListenerNotFoundException;
65 import javax.management.MBeanNotificationInfo;
66 import javax.management.Notification;
67 import javax.management.NotificationBroadcasterSupport;
68 import javax.management.NotificationEmitter;
69 import javax.management.NotificationFilter;
70 import javax.management.NotificationListener;
116 "com.sun.jmx.examples.scandir.filematch";
121 private static final Logger
LOG =
180 throws IllegalArgumentException {
182 throw new IllegalArgumentException(
"log=null");
184 throw new IllegalArgumentException(
"config=null");
185 if (
config.getName() ==
null)
186 throw new IllegalArgumentException(
"config.name=null");
188 broadcaster =
new NotificationBroadcasterSupport();
202 if (
config.getActions() ==
null)
203 actions = Collections.emptySet();
205 actions = EnumSet.copyOf(Arrays.asList(
config.getActions()));
234 if (currentOrLastTask ==
null)
return "Never Run";
246 synchronized (
this) {
247 final LinkedList<File> list;
251 throw new IllegalStateException(
state.toString());
255 list =
new LinkedList<File>();
259 throw new IllegalStateException(String.valueOf(
state));
291 final Set<Action> taken =
new HashSet<Action>();
292 boolean logresult =
false;
320 LOG.fine(
"Failed to execute action: " +action +
321 " - action not supported");
328 taken.add(LOGRESULT);
330 taken.remove(LOGRESULT);
333 LOG.finest(
"File processed: "+taken+
" - "+
file.getAbsolutePath());
344 System.out.println(
"DELETE not implemented for safety reasons.");
346 }
catch (Exception x) {
347 LOG.fine(
"Failed to delete: "+
file.getAbsolutePath());
355 final Notification n =
358 file.getAbsolutePath());
368 }
catch (Exception x) {
369 LOG.fine(
"Failed to notify: "+
file.getAbsolutePath());
379 }
catch (Exception x) {
380 LOG.fine(
"Failed to log: "+
file.getAbsolutePath());
396 private final LinkedList<File>
list;
404 private volatile String
info=
"Not started";
431 task.
info =
"In Progress";
436 final FileFilter filter =
config.buildFileFilter();
444 while (!list.isEmpty() &&
state == RUNNING) {
448 final File current = list.poll();
459 if (current.isFile()) {
468 if (current.isDirectory()) {
471 final File[] content = current.listFiles(filter);
472 if (content ==
null)
continue;
475 list.addAll(0,Arrays.asList(content));
483 if (list.isEmpty()) {
484 task.
info =
"Successfully Completed";
487 }
catch (Exception x) {
490 task.
info =
"Failed: "+x;
491 if (
LOG.isLoggable(Level.FINEST))
492 LOG.log(Level.FINEST,
"scan task failed: "+x,x);
493 else if (
LOG.isLoggable(Level.FINE))
494 LOG.log(Level.FINE,
"scan task failed: "+x);
503 task.
info =
"Error: "+e;
515 NotificationFilter filter, Object handback)
516 throws IllegalArgumentException {
517 broadcaster.addNotificationListener(listener, filter, handback);
526 if (old == desired)
return;
528 final AttributeChangeNotification n =
529 new AttributeChangeNotification(
this,
530 getNextSeqNumber(),System.currentTimeMillis(),
531 "state change",
"State",
ScanState.class.getName(),
532 String.valueOf(old),String.valueOf(desired));
542 return new MBeanNotificationInfo[] {
543 new MBeanNotificationInfo(
545 Notification.class.getName(),
546 "Emitted when a file that matches the scan criteria is found"
548 new MBeanNotificationInfo(
549 new String[] {AttributeChangeNotification.ATTRIBUTE_CHANGE},
550 AttributeChangeNotification.class.getName(),
551 "Emitted when the State attribute changes"
560 throws ListenerNotFoundException {
568 NotificationFilter filter, Object handback)
569 throws ListenerNotFoundException {
570 broadcaster.removeNotificationListener(listener, filter, handback);
580 throw new IllegalArgumentException(
"no root specified");
581 if (root.length() == 0)
582 throw new IllegalArgumentException(
"specified root \"\" is invalid");
583 final File f =
new File(root);
585 throw new IllegalArgumentException(
"can't read "+root);
586 if (!f.isDirectory())
587 throw new IllegalArgumentException(
"no such directory: "+root);