Search in sources :

Example 1 with XmlConfigUtils

use of com.sun.jmx.examples.scandir.config.XmlConfigUtils in project jdk8u_jdk by JetBrains.

the class ScanDirConfig method save.

// see ScanDirConfigMXBean
public void save() throws IOException {
    if (filename == null)
        throw new UnsupportedOperationException("load");
    synchronized (this) {
        new XmlConfigUtils(filename).writeToFile(config);
        status = SAVED;
    }
    sendNotification(NOTIFICATION_SAVED);
}
Also used : XmlConfigUtils(com.sun.jmx.examples.scandir.config.XmlConfigUtils)

Example 2 with XmlConfigUtils

use of com.sun.jmx.examples.scandir.config.XmlConfigUtils 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);
}
Also used : ScanManagerConfig(com.sun.jmx.examples.scandir.config.ScanManagerConfig) DirectoryScannerConfig(com.sun.jmx.examples.scandir.config.DirectoryScannerConfig) XmlConfigUtils(com.sun.jmx.examples.scandir.config.XmlConfigUtils) File(java.io.File) FileMatch(com.sun.jmx.examples.scandir.config.FileMatch)

Example 3 with XmlConfigUtils

use of com.sun.jmx.examples.scandir.config.XmlConfigUtils 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());
}
Also used : LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ScanManagerConfig(com.sun.jmx.examples.scandir.config.ScanManagerConfig) DirectoryScannerConfig(com.sun.jmx.examples.scandir.config.DirectoryScannerConfig) XmlConfigUtils(com.sun.jmx.examples.scandir.config.XmlConfigUtils) File(java.io.File)

Example 4 with XmlConfigUtils

use of com.sun.jmx.examples.scandir.config.XmlConfigUtils in project jdk8u_jdk by JetBrains.

the class ScanDirConfigTest method testSave.

/**
     * Test of save method, of class com.sun.jmx.examples.scandir.ScanDirConfig.
     */
public void testSave() throws Exception {
    System.out.println("save");
    final File file = File.createTempFile("testconf", ".xml");
    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    final ScanManagerMXBean manager = ScanManager.register(mbs);
    try {
        final ScanDirConfigMXBean instance = manager.createOtherConfigurationMBean("testSave", file.getAbsolutePath());
        assertTrue(mbs.isRegistered(ScanManager.makeScanDirConfigName("testSave")));
        final ScanManagerConfig bean = new ScanManagerConfig("testSave");
        final DirectoryScannerConfig dir = new DirectoryScannerConfig("tmp");
        dir.setRootDirectory(file.getParent());
        bean.putScan(dir);
        instance.setConfiguration(bean);
        instance.save();
        final ScanManagerConfig loaded = new XmlConfigUtils(file.getAbsolutePath()).readFromFile();
        assertEquals(instance.getConfiguration(), loaded);
        assertEquals(bean, loaded);
        instance.getConfiguration().removeScan("tmp");
        instance.save();
        assertNotSame(loaded, instance.getConfiguration());
        final ScanManagerConfig loaded2 = new XmlConfigUtils(file.getAbsolutePath()).readFromFile();
        assertEquals(instance.getConfiguration(), loaded2);
    } 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());
}
Also used : ScanManagerConfig(com.sun.jmx.examples.scandir.config.ScanManagerConfig) DirectoryScannerConfig(com.sun.jmx.examples.scandir.config.DirectoryScannerConfig) XmlConfigUtils(com.sun.jmx.examples.scandir.config.XmlConfigUtils) File(java.io.File)

Aggregations

XmlConfigUtils (com.sun.jmx.examples.scandir.config.XmlConfigUtils)4 DirectoryScannerConfig (com.sun.jmx.examples.scandir.config.DirectoryScannerConfig)3 ScanManagerConfig (com.sun.jmx.examples.scandir.config.ScanManagerConfig)3 File (java.io.File)3 FileMatch (com.sun.jmx.examples.scandir.config.FileMatch)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1