use of com.sun.jmx.examples.scandir.config.DirectoryScannerConfig in project jdk8u_jdk by JetBrains.
the class ScanDirConfig method addDirectoryScanner.
// see ScanDirConfigMXBean
public DirectoryScannerConfig addDirectoryScanner(String name, String dir, String filePattern, long sizeExceedsMaxBytes, long sinceLastModified) {
final DirectoryScannerConfig scanner = new DirectoryScannerConfig(name);
scanner.setRootDirectory(dir);
if (filePattern != null || sizeExceedsMaxBytes > 0 || sinceLastModified > 0) {
final FileMatch filter = new FileMatch();
filter.setFilePattern(filePattern);
filter.setSizeExceedsMaxBytes(sizeExceedsMaxBytes);
if (sinceLastModified > 0)
filter.setLastModifiedBefore(new Date(new Date().getTime() - sinceLastModified));
scanner.addIncludeFiles(filter);
}
synchronized (this) {
config.putScan(scanner);
status = MODIFIED;
}
LOG.fine("config: " + config);
sendNotification(NOTIFICATION_MODIFIED);
return scanner;
}
use of com.sun.jmx.examples.scandir.config.DirectoryScannerConfig in project jdk8u_jdk by JetBrains.
the class ScanDirConfigTest method testAddDirectoryScanner.
/**
* Test of addDirectoryScanner method, of class
* com.sun.jmx.examples.scandir.ScanDirConfig.
*/
public void testAddDirectoryScanner() throws IOException {
System.out.println("addDirectoryScanner");
System.out.println("save");
final File file = File.createTempFile("testconf", ".xml");
final ScanDirConfig instance = new ScanDirConfig(file.getAbsolutePath());
final ScanManagerConfig bean = new ScanManagerConfig("testSave");
final DirectoryScannerConfig dir = new DirectoryScannerConfig("tmp");
dir.setRootDirectory(file.getParent());
FileMatch filter = new FileMatch();
filter.setFilePattern(".*");
dir.setIncludeFiles(new FileMatch[] { filter });
instance.setConfiguration(bean);
instance.addDirectoryScanner(dir.getName(), dir.getRootDirectory(), filter.getFilePattern(), filter.getSizeExceedsMaxBytes(), 0);
instance.save();
final ScanManagerConfig loaded = new XmlConfigUtils(file.getAbsolutePath()).readFromFile();
assertNotNull(loaded.getScan(dir.getName()));
assertEquals(dir, loaded.getScan(dir.getName()));
assertEquals(instance.getConfiguration(), loaded);
assertEquals(instance.getConfiguration().getScan(dir.getName()), dir);
}
use of com.sun.jmx.examples.scandir.config.DirectoryScannerConfig in project jdk8u_jdk by JetBrains.
the class ScanDirConfigTest method testAddNotificationListener.
/**
* Test of addNotificationListener method, of class
* com.sun.jmx.examples.scandir.ScanDirConfig.
*/
public void testAddNotificationListener() throws Exception {
System.out.println("addNotificationListener");
final File file = File.createTempFile("testconf", ".xml");
final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
final ScanManagerMXBean manager = ScanManager.register(mbs);
try {
final ScanDirConfigMXBean instance = TestUtils.makeNotificationEmitter(manager.createOtherConfigurationMBean("testSave", file.getAbsolutePath()), ScanDirConfigMXBean.class);
assertTrue(mbs.isRegistered(ScanManager.makeScanDirConfigName("testSave")));
DirectoryScannerConfig dir = instance.addDirectoryScanner("tmp", file.getParent(), ".*", 0, 0);
final BlockingQueue<Notification> queue = new LinkedBlockingQueue<Notification>();
final NotificationListener listener = new NotificationListener() {
public void handleNotification(Notification notification, Object handback) {
queue.add(notification);
}
};
NotificationFilter filter = null;
Object handback = null;
((NotificationEmitter) instance).addNotificationListener(listener, filter, handback);
instance.save();
final ScanManagerConfig loaded = new XmlConfigUtils(file.getAbsolutePath()).readFromFile();
assertEquals(instance.getConfiguration(), loaded);
final ScanManagerConfig newConfig = instance.getConfiguration();
newConfig.removeScan("tmp");
instance.setConfiguration(newConfig);
instance.save();
assertNotSame(loaded, instance.getConfiguration());
final ScanManagerConfig loaded2 = new XmlConfigUtils(file.getAbsolutePath()).readFromFile();
assertEquals(instance.getConfiguration(), loaded2);
instance.load();
for (int i = 0; i < 4; i++) {
final Notification n = queue.poll(3, TimeUnit.SECONDS);
assertNotNull(n);
assertEquals(TestUtils.getObjectName(instance), n.getSource());
switch(i) {
case 0:
case 2:
assertEquals(ScanDirConfig.NOTIFICATION_SAVED, n.getType());
break;
case 1:
assertEquals(ScanDirConfig.NOTIFICATION_MODIFIED, n.getType());
break;
case 3:
assertEquals(ScanDirConfig.NOTIFICATION_LOADED, n.getType());
break;
default:
break;
}
}
} finally {
manager.close();
mbs.unregisterMBean(ScanManager.SCAN_MANAGER_NAME);
}
final ObjectName all = new ObjectName(ScanManager.SCAN_MANAGER_NAME.getDomain() + ":*");
assertEquals(0, mbs.queryNames(all, null).size());
}
use of com.sun.jmx.examples.scandir.config.DirectoryScannerConfig in project jdk8u_jdk by JetBrains.
the class DirectoryScannerTest method testAddNotificationListener.
/**
* Test of addNotificationListener method, of class com.sun.jmx.examples.scandir.DirectoryScanner.
*/
public void testAddNotificationListener() throws Exception {
System.out.println("addNotificationListener");
final ScanManagerMXBean manager = ScanManager.register();
final Call op = new Call() {
public void call() throws Exception {
manager.start();
}
public void cancel() throws Exception {
manager.stop();
}
};
try {
final String tmpdir = System.getProperty("java.io.tmpdir");
final ScanDirConfigMXBean config = manager.getConfigurationMBean();
final DirectoryScannerConfig bean = config.addDirectoryScanner("test1", tmpdir, ".*", 0, 0);
manager.applyConfiguration(true);
final DirectoryScannerMXBean proxy = manager.getDirectoryScanners().get("test1");
doTestOperation(proxy, op, EnumSet.of(RUNNING, SCHEDULED), "scan");
} finally {
try {
ManagementFactory.getPlatformMBeanServer().unregisterMBean(ScanManager.SCAN_MANAGER_NAME);
} catch (Exception x) {
System.err.println("Failed to cleanup: " + x);
}
}
}
use of com.sun.jmx.examples.scandir.config.DirectoryScannerConfig in project jdk8u_jdk by JetBrains.
the class DirectoryScannerTest method testGetRootDirectory.
/**
* Test of getRootDirectory method, of class com.sun.jmx.examples.scandir.DirectoryScanner.
*/
public void testGetRootDirectory() throws Exception {
System.out.println("getRootDirectory");
final ScanManagerMXBean manager = ScanManager.register();
try {
final String tmpdir = System.getProperty("java.io.tmpdir");
final ScanDirConfigMXBean config = manager.getConfigurationMBean();
System.err.println("Configuration MXBean is: " + config);
final DirectoryScannerConfig bean = config.addDirectoryScanner("test", tmpdir, ".*", 0, 0);
final String root = bean.getRootDirectory();
if (root == null)
throw new NullPointerException("bean.getRootDirectory()");
if (config.getConfiguration().getScan("test").getRootDirectory() == null)
throw new NullPointerException("config.getConfig().getScan(\"test\").getRootDirectory()");
manager.applyConfiguration(true);
final DirectoryScannerMXBean proxy = manager.getDirectoryScanners().get("test");
final File tmpFile = new File(tmpdir);
final File rootFile = new File(proxy.getRootDirectory());
assertEquals(tmpFile, rootFile);
} catch (Exception x) {
x.printStackTrace();
throw x;
} finally {
try {
ManagementFactory.getPlatformMBeanServer().unregisterMBean(ScanManager.SCAN_MANAGER_NAME);
} catch (Exception x) {
System.err.println("Failed to cleanup: " + x);
}
}
}
Aggregations