Java Code Examples for com.sun.jmx.examples.scandir.config.XmlConfigUtils

The following examples show how to use com.sun.jmx.examples.scandir.config.XmlConfigUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
static String guessConfigName(String configFileName,String defaultFile) {
    try {
        if (configFileName == null) return DEFAULT;
        final File f = new File(configFileName);
        if (f.canRead()) {
            final String confname = XmlConfigUtils.read(f).getName();
            if (confname != null && confname.length()>0) return confname;
        }
        final File f2 = new File(defaultFile);
        if (f.equals(f2)) return DEFAULT;
        final String guess = getBasename(f.getName());
        if (guess == null) return DEFAULT;
        if (guess.length()==0) return DEFAULT;
        return guess;
    } catch (Exception x) {
        return DEFAULT;
    }
}
 
Example 2
/**
 * Test of load method, of class com.sun.jmx.examples.scandir.ScanDirConfig.
 */
public void testLoad() throws Exception {
    System.out.println("load");

    final File file = File.createTempFile("testconf",".xml");
    final ScanDirConfig instance = new ScanDirConfig(file.getAbsolutePath());
    final ScanManagerConfig bean =
            new  ScanManagerConfig("testLoad");
    final DirectoryScannerConfig dir =
            new DirectoryScannerConfig("tmp");
    dir.setRootDirectory(file.getParent());
    bean.putScan(dir);
    XmlConfigUtils.write(bean,new FileOutputStream(file),false);
    instance.load();

    assertEquals(bean,instance.getConfiguration());
    bean.removeScan(dir.getName());
    XmlConfigUtils.write(bean,new FileOutputStream(file),false);

    assertNotSame(bean,instance.getConfiguration());

    instance.load();

    assertEquals(bean,instance.getConfiguration());

}
 
Example 3
Source Project: jdk8u_jdk   Source File: ScanDirConfig.java    License: GNU General Public License v2.0 6 votes vote down vote up
static String guessConfigName(String configFileName,String defaultFile) {
    try {
        if (configFileName == null) return DEFAULT;
        final File f = new File(configFileName);
        if (f.canRead()) {
            final String confname = XmlConfigUtils.read(f).getName();
            if (confname != null && confname.length()>0) return confname;
        }
        final File f2 = new File(defaultFile);
        if (f.equals(f2)) return DEFAULT;
        final String guess = getBasename(f.getName());
        if (guess == null) return DEFAULT;
        if (guess.length()==0) return DEFAULT;
        return guess;
    } catch (Exception x) {
        return DEFAULT;
    }
}
 
Example 4
Source Project: TencentKona-8   Source File: ScanDirConfig.java    License: GNU General Public License v2.0 6 votes vote down vote up
static String guessConfigName(String configFileName,String defaultFile) {
    try {
        if (configFileName == null) return DEFAULT;
        final File f = new File(configFileName);
        if (f.canRead()) {
            final String confname = XmlConfigUtils.read(f).getName();
            if (confname != null && confname.length()>0) return confname;
        }
        final File f2 = new File(defaultFile);
        if (f.equals(f2)) return DEFAULT;
        final String guess = getBasename(f.getName());
        if (guess == null) return DEFAULT;
        if (guess.length()==0) return DEFAULT;
        return guess;
    } catch (Exception x) {
        return DEFAULT;
    }
}
 
Example 5
Source Project: jdk8u_jdk   Source File: ScanDirConfigTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of load method, of class com.sun.jmx.examples.scandir.ScanDirConfig.
 */
public void testLoad() throws Exception {
    System.out.println("load");

    final File file = File.createTempFile("testconf",".xml");
    final ScanDirConfig instance = new ScanDirConfig(file.getAbsolutePath());
    final ScanManagerConfig bean =
            new  ScanManagerConfig("testLoad");
    final DirectoryScannerConfig dir =
            new DirectoryScannerConfig("tmp");
    dir.setRootDirectory(file.getParent());
    bean.putScan(dir);
    XmlConfigUtils.write(bean,new FileOutputStream(file),false);
    instance.load();

    assertEquals(bean,instance.getConfiguration());
    bean.removeScan(dir.getName());
    XmlConfigUtils.write(bean,new FileOutputStream(file),false);

    assertNotSame(bean,instance.getConfiguration());

    instance.load();

    assertEquals(bean,instance.getConfiguration());

}
 
Example 6
/**
 * Test of getXmlConfigString method, of class com.sun.jmx.examples.scandir.ScanDirConfig.
 */
public void testGetXmlConfigString() throws Exception {
    System.out.println("getXmlConfigString");

    try {
        final File file = File.createTempFile("testconf",".xml");
        final ScanDirConfig instance = new ScanDirConfig(file.getAbsolutePath());
        final ScanManagerConfig bean =
            new  ScanManagerConfig("testGetXmlConfigString");
        final DirectoryScannerConfig dir =
            new DirectoryScannerConfig("tmp");
        dir.setRootDirectory(file.getParent());
        bean.putScan(dir);
        instance.setConfiguration(bean);
        System.out.println("Expected: " + XmlConfigUtils.toString(bean));
        System.out.println("Received: " +
                instance.getConfiguration().toString());
        assertEquals(XmlConfigUtils.toString(bean),
            instance.getConfiguration().toString());
    } catch (Exception x) {
        x.printStackTrace();
        throw x;
    }
}
 
Example 7
Source Project: jdk8u60   Source File: ScanDirConfig.java    License: GNU General Public License v2.0 6 votes vote down vote up
static String guessConfigName(String configFileName,String defaultFile) {
    try {
        if (configFileName == null) return DEFAULT;
        final File f = new File(configFileName);
        if (f.canRead()) {
            final String confname = XmlConfigUtils.read(f).getName();
            if (confname != null && confname.length()>0) return confname;
        }
        final File f2 = new File(defaultFile);
        if (f.equals(f2)) return DEFAULT;
        final String guess = getBasename(f.getName());
        if (guess == null) return DEFAULT;
        if (guess.length()==0) return DEFAULT;
        return guess;
    } catch (Exception x) {
        return DEFAULT;
    }
}
 
Example 8
Source Project: jdk8u60   Source File: ScanDirConfigTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of load method, of class com.sun.jmx.examples.scandir.ScanDirConfig.
 */
public void testLoad() throws Exception {
    System.out.println("load");

    final File file = File.createTempFile("testconf",".xml");
    final ScanDirConfig instance = new ScanDirConfig(file.getAbsolutePath());
    final ScanManagerConfig bean =
            new  ScanManagerConfig("testLoad");
    final DirectoryScannerConfig dir =
            new DirectoryScannerConfig("tmp");
    dir.setRootDirectory(file.getParent());
    bean.putScan(dir);
    XmlConfigUtils.write(bean,new FileOutputStream(file),false);
    instance.load();

    assertEquals(bean,instance.getConfiguration());
    bean.removeScan(dir.getName());
    XmlConfigUtils.write(bean,new FileOutputStream(file),false);

    assertNotSame(bean,instance.getConfiguration());

    instance.load();

    assertEquals(bean,instance.getConfiguration());

}
 
Example 9
Source Project: jdk8u60   Source File: ScanDirConfigTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of getXmlConfigString method, of class com.sun.jmx.examples.scandir.ScanDirConfig.
 */
public void testGetXmlConfigString() throws Exception {
    System.out.println("getXmlConfigString");

    try {
        final File file = File.createTempFile("testconf",".xml");
        final ScanDirConfig instance = new ScanDirConfig(file.getAbsolutePath());
        final ScanManagerConfig bean =
            new  ScanManagerConfig("testGetXmlConfigString");
        final DirectoryScannerConfig dir =
            new DirectoryScannerConfig("tmp");
        dir.setRootDirectory(file.getParent());
        bean.putScan(dir);
        instance.setConfiguration(bean);
        System.out.println("Expected: " + XmlConfigUtils.toString(bean));
        System.out.println("Received: " +
                instance.getConfiguration().toString());
        assertEquals(XmlConfigUtils.toString(bean),
            instance.getConfiguration().toString());
    } catch (Exception x) {
        x.printStackTrace();
        throw x;
    }
}
 
Example 10
Source Project: openjdk-jdk8u   Source File: ScanDirConfig.java    License: GNU General Public License v2.0 6 votes vote down vote up
static String guessConfigName(String configFileName,String defaultFile) {
    try {
        if (configFileName == null) return DEFAULT;
        final File f = new File(configFileName);
        if (f.canRead()) {
            final String confname = XmlConfigUtils.read(f).getName();
            if (confname != null && confname.length()>0) return confname;
        }
        final File f2 = new File(defaultFile);
        if (f.equals(f2)) return DEFAULT;
        final String guess = getBasename(f.getName());
        if (guess == null) return DEFAULT;
        if (guess.length()==0) return DEFAULT;
        return guess;
    } catch (Exception x) {
        return DEFAULT;
    }
}
 
Example 11
/**
 * Test of load method, of class com.sun.jmx.examples.scandir.ScanDirConfig.
 */
public void testLoad() throws Exception {
    System.out.println("load");

    final File file = File.createTempFile("testconf",".xml");
    final ScanDirConfig instance = new ScanDirConfig(file.getAbsolutePath());
    final ScanManagerConfig bean =
            new  ScanManagerConfig("testLoad");
    final DirectoryScannerConfig dir =
            new DirectoryScannerConfig("tmp");
    dir.setRootDirectory(file.getParent());
    bean.putScan(dir);
    XmlConfigUtils.write(bean,new FileOutputStream(file),false);
    instance.load();

    assertEquals(bean,instance.getConfiguration());
    bean.removeScan(dir.getName());
    XmlConfigUtils.write(bean,new FileOutputStream(file),false);

    assertNotSame(bean,instance.getConfiguration());

    instance.load();

    assertEquals(bean,instance.getConfiguration());

}
 
Example 12
static String guessConfigName(String configFileName,String defaultFile) {
    try {
        if (configFileName == null) return DEFAULT;
        final File f = new File(configFileName);
        if (f.canRead()) {
            final String confname = XmlConfigUtils.read(f).getName();
            if (confname != null && confname.length()>0) return confname;
        }
        final File f2 = new File(defaultFile);
        if (f.equals(f2)) return DEFAULT;
        final String guess = getBasename(f.getName());
        if (guess == null) return DEFAULT;
        if (guess.length()==0) return DEFAULT;
        return guess;
    } catch (Exception x) {
        return DEFAULT;
    }
}
 
Example 13
Source Project: openjdk-8   Source File: ScanDirConfig.java    License: GNU General Public License v2.0 6 votes vote down vote up
static String guessConfigName(String configFileName,String defaultFile) {
    try {
        if (configFileName == null) return DEFAULT;
        final File f = new File(configFileName);
        if (f.canRead()) {
            final String confname = XmlConfigUtils.read(f).getName();
            if (confname != null && confname.length()>0) return confname;
        }
        final File f2 = new File(defaultFile);
        if (f.equals(f2)) return DEFAULT;
        final String guess = getBasename(f.getName());
        if (guess == null) return DEFAULT;
        if (guess.length()==0) return DEFAULT;
        return guess;
    } catch (Exception x) {
        return DEFAULT;
    }
}
 
Example 14
Source Project: openjdk-8   Source File: ScanDirConfigTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of getXmlConfigString method, of class com.sun.jmx.examples.scandir.ScanDirConfig.
 */
public void testGetXmlConfigString() throws Exception {
    System.out.println("getXmlConfigString");

    try {
        final File file = File.createTempFile("testconf",".xml");
        final ScanDirConfig instance = new ScanDirConfig(file.getAbsolutePath());
        final ScanManagerConfig bean =
            new  ScanManagerConfig("testGetXmlConfigString");
        final DirectoryScannerConfig dir =
            new DirectoryScannerConfig("tmp");
        dir.setRootDirectory(file.getParent());
        bean.putScan(dir);
        instance.setConfiguration(bean);
        System.out.println("Expected: " + XmlConfigUtils.toString(bean));
        System.out.println("Received: " +
                instance.getConfiguration().toString());
        assertEquals(XmlConfigUtils.toString(bean),
            instance.getConfiguration().toString());
    } catch (Exception x) {
        x.printStackTrace();
        throw x;
    }
}
 
Example 15
Source Project: jdk8u-jdk   Source File: ScanDirConfig.java    License: GNU General Public License v2.0 6 votes vote down vote up
static String guessConfigName(String configFileName,String defaultFile) {
    try {
        if (configFileName == null) return DEFAULT;
        final File f = new File(configFileName);
        if (f.canRead()) {
            final String confname = XmlConfigUtils.read(f).getName();
            if (confname != null && confname.length()>0) return confname;
        }
        final File f2 = new File(defaultFile);
        if (f.equals(f2)) return DEFAULT;
        final String guess = getBasename(f.getName());
        if (guess == null) return DEFAULT;
        if (guess.length()==0) return DEFAULT;
        return guess;
    } catch (Exception x) {
        return DEFAULT;
    }
}
 
Example 16
Source Project: jdk8u-jdk   Source File: ScanDirConfigTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of load method, of class com.sun.jmx.examples.scandir.ScanDirConfig.
 */
public void testLoad() throws Exception {
    System.out.println("load");

    final File file = File.createTempFile("testconf",".xml");
    final ScanDirConfig instance = new ScanDirConfig(file.getAbsolutePath());
    final ScanManagerConfig bean =
            new  ScanManagerConfig("testLoad");
    final DirectoryScannerConfig dir =
            new DirectoryScannerConfig("tmp");
    dir.setRootDirectory(file.getParent());
    bean.putScan(dir);
    XmlConfigUtils.write(bean,new FileOutputStream(file),false);
    instance.load();

    assertEquals(bean,instance.getConfiguration());
    bean.removeScan(dir.getName());
    XmlConfigUtils.write(bean,new FileOutputStream(file),false);

    assertNotSame(bean,instance.getConfiguration());

    instance.load();

    assertEquals(bean,instance.getConfiguration());

}
 
Example 17
Source Project: jdk8u-jdk   Source File: ScanDirConfigTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of getXmlConfigString method, of class com.sun.jmx.examples.scandir.ScanDirConfig.
 */
public void testGetXmlConfigString() throws Exception {
    System.out.println("getXmlConfigString");

    try {
        final File file = File.createTempFile("testconf",".xml");
        final ScanDirConfig instance = new ScanDirConfig(file.getAbsolutePath());
        final ScanManagerConfig bean =
            new  ScanManagerConfig("testGetXmlConfigString");
        final DirectoryScannerConfig dir =
            new DirectoryScannerConfig("tmp");
        dir.setRootDirectory(file.getParent());
        bean.putScan(dir);
        instance.setConfiguration(bean);
        System.out.println("Expected: " + XmlConfigUtils.toString(bean));
        System.out.println("Received: " +
                instance.getConfiguration().toString());
        assertEquals(XmlConfigUtils.toString(bean),
            instance.getConfiguration().toString());
    } catch (Exception x) {
        x.printStackTrace();
        throw x;
    }
}
 
Example 18
Source Project: jdk8u-jdk   Source File: ScanDirConfigTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of getXmlConfigString method, of class com.sun.jmx.examples.scandir.ScanDirConfig.
 */
public void testGetXmlConfigString() throws Exception {
    System.out.println("getXmlConfigString");

    try {
        final File file = File.createTempFile("testconf",".xml");
        final ScanDirConfig instance = new ScanDirConfig(file.getAbsolutePath());
        final ScanManagerConfig bean =
            new  ScanManagerConfig("testGetXmlConfigString");
        final DirectoryScannerConfig dir =
            new DirectoryScannerConfig("tmp");
        dir.setRootDirectory(file.getParent());
        bean.putScan(dir);
        instance.setConfiguration(bean);
        System.out.println("Expected: " + XmlConfigUtils.toString(bean));
        System.out.println("Received: " +
                instance.getConfiguration().toString());
        assertEquals(XmlConfigUtils.toString(bean),
            instance.getConfiguration().toString());
    } catch (Exception x) {
        x.printStackTrace();
        throw x;
    }
}
 
Example 19
Source Project: hottub   Source File: ScanDirConfig.java    License: GNU General Public License v2.0 6 votes vote down vote up
static String guessConfigName(String configFileName,String defaultFile) {
    try {
        if (configFileName == null) return DEFAULT;
        final File f = new File(configFileName);
        if (f.canRead()) {
            final String confname = XmlConfigUtils.read(f).getName();
            if (confname != null && confname.length()>0) return confname;
        }
        final File f2 = new File(defaultFile);
        if (f.equals(f2)) return DEFAULT;
        final String guess = getBasename(f.getName());
        if (guess == null) return DEFAULT;
        if (guess.length()==0) return DEFAULT;
        return guess;
    } catch (Exception x) {
        return DEFAULT;
    }
}
 
Example 20
/**
 * Test of getXmlConfigString method, of class com.sun.jmx.examples.scandir.ScanDirConfig.
 */
public void testGetXmlConfigString() throws Exception {
    System.out.println("getXmlConfigString");

    try {
        final File file = File.createTempFile("testconf",".xml");
        final ScanDirConfig instance = new ScanDirConfig(file.getAbsolutePath());
        final ScanManagerConfig bean =
            new  ScanManagerConfig("testGetXmlConfigString");
        final DirectoryScannerConfig dir =
            new DirectoryScannerConfig("tmp");
        dir.setRootDirectory(file.getParent());
        bean.putScan(dir);
        instance.setConfiguration(bean);
        System.out.println("Expected: " + XmlConfigUtils.toString(bean));
        System.out.println("Received: " +
                instance.getConfiguration().toString());
        assertEquals(XmlConfigUtils.toString(bean),
            instance.getConfiguration().toString());
    } catch (Exception x) {
        x.printStackTrace();
        throw x;
    }
}
 
Example 21
/**
 * 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());
}
 
Example 22
public void save() throws IOException {
    if (filename == null)
        throw new UnsupportedOperationException("load");
    synchronized (this) {
        new XmlConfigUtils(filename).writeToFile(config);
        status = SAVED;
    }
    sendNotification(NOTIFICATION_SAVED);
}
 
Example 23
/**
 * Constructs a new {@code DirectoryScanner}.
 * <p>This constructor is
 * package protected, and this MBean cannot be created by a remote
 * client, because it needs a reference to the {@link ResultLogManager},
 * which cannot be provided from remote.
 * </p>
 * <p>This is a conscious design choice: {@code DirectoryScanner} MBeans
 * are expected to be completely managed (created, registered, unregistered)
 * by the {@link ScanManager} which does provide this reference.
 * </p>
 *
 * @param config This {@code DirectoryScanner} configuration.
 * @param logManager The info log manager with which to log the info
 *        records.
 * @throws IllegalArgumentException if one of the parameter is null, or if
 *         the provided {@code config} doesn't have its {@code name} set,
 *         or if the {@link DirectoryScannerConfig#getRootDirectory
 *         root directory} provided in the {@code config} is not acceptable
 *         (not provided or not found or not readable, etc...).
 **/
public DirectoryScanner(DirectoryScannerConfig config,
                        ResultLogManager logManager)
    throws IllegalArgumentException {
    if (logManager == null)
        throw new IllegalArgumentException("log=null");
    if (config == null)
        throw new IllegalArgumentException("config=null");
    if (config.getName() == null)
        throw new IllegalArgumentException("config.name=null");

     broadcaster = new NotificationBroadcasterSupport();

     // Clone the config: ensure data encapsulation.
     //
     this.config = XmlConfigUtils.xmlClone(config);

     // Checks that the provided root directory is valid.
     // Throws IllegalArgumentException if it isn't.
     //
     rootFile = validateRoot(config.getRootDirectory());

     // Initialize the Set<Action> for which this DirectoryScanner
     // is configured.
     //
     if (config.getActions() == null)
         actions = Collections.emptySet();
     else
         actions = EnumSet.copyOf(Arrays.asList(config.getActions()));
     this.logManager = logManager;
}
 
Example 24
private void logToFile(ResultRecord record) throws IOException {
    final String basename;
    final long   maxRecords;
    synchronized (this) {
        if (logFile == null) return;
        basename = getLogFileName(false);
        maxRecords = fileCapacity;
    }

    // Get the stream into which we should log.
    final OutputStream stream =
            checkLogFile(basename,maxRecords,false);

    // logging to file now disabled - too bad.
    if (stream == null) return;

    synchronized (this) {
        try {
            XmlConfigUtils.write(record,stream,true);
            stream.flush();
            // don't increment logCount if we were not logging in logStream.
            if (stream == logStream) logCount++;
        } catch (JAXBException x) {
            final IllegalArgumentException iae =
                    new IllegalArgumentException("bad record",x);
            LOG.finest("Failed to log record: "+x);
            throw iae;
        }
    }
}
 
Example 25
/**
 * 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());
}
 
Example 26
/**
 * 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);
}
 
Example 27
Source Project: TencentKona-8   Source File: ScanDirConfig.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void load() throws IOException {
    if (filename == null)
        throw new UnsupportedOperationException("load");

    synchronized(this) {
        config = new XmlConfigUtils(filename).readFromFile();
        if (configname != null) config = config.copy(configname);
        else configname = config.getName();

        status=LOADED;
    }
    sendNotification(NOTIFICATION_LOADED);
}
 
Example 28
Source Project: TencentKona-8   Source File: ScanDirConfig.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void save() throws IOException {
    if (filename == null)
        throw new UnsupportedOperationException("load");
    synchronized (this) {
        new XmlConfigUtils(filename).writeToFile(config);
        status = SAVED;
    }
    sendNotification(NOTIFICATION_SAVED);
}
 
Example 29
private void logToFile(ResultRecord record) throws IOException {
    final String basename;
    final long   maxRecords;
    synchronized (this) {
        if (logFile == null) return;
        basename = getLogFileName(false);
        maxRecords = fileCapacity;
    }

    // Get the stream into which we should log.
    final OutputStream stream =
            checkLogFile(basename,maxRecords,false);

    // logging to file now disabled - too bad.
    if (stream == null) return;

    synchronized (this) {
        try {
            XmlConfigUtils.write(record,stream,true);
            stream.flush();
            // don't increment logCount if we were not logging in logStream.
            if (stream == logStream) logCount++;
        } catch (JAXBException x) {
            final IllegalArgumentException iae =
                    new IllegalArgumentException("bad record",x);
            LOG.finest("Failed to log record: "+x);
            throw iae;
        }
    }
}
 
Example 30
/**
 * Constructs a new {@code DirectoryScanner}.
 * <p>This constructor is
 * package protected, and this MBean cannot be created by a remote
 * client, because it needs a reference to the {@link ResultLogManager},
 * which cannot be provided from remote.
 * </p>
 * <p>This is a conscious design choice: {@code DirectoryScanner} MBeans
 * are expected to be completely managed (created, registered, unregistered)
 * by the {@link ScanManager} which does provide this reference.
 * </p>
 *
 * @param config This {@code DirectoryScanner} configuration.
 * @param logManager The info log manager with which to log the info
 *        records.
 * @throws IllegalArgumentException if one of the parameter is null, or if
 *         the provided {@code config} doesn't have its {@code name} set,
 *         or if the {@link DirectoryScannerConfig#getRootDirectory
 *         root directory} provided in the {@code config} is not acceptable
 *         (not provided or not found or not readable, etc...).
 **/
public DirectoryScanner(DirectoryScannerConfig config,
                        ResultLogManager logManager)
    throws IllegalArgumentException {
    if (logManager == null)
        throw new IllegalArgumentException("log=null");
    if (config == null)
        throw new IllegalArgumentException("config=null");
    if (config.getName() == null)
        throw new IllegalArgumentException("config.name=null");

     broadcaster = new NotificationBroadcasterSupport();

     // Clone the config: ensure data encapsulation.
     //
     this.config = XmlConfigUtils.xmlClone(config);

     // Checks that the provided root directory is valid.
     // Throws IllegalArgumentException if it isn't.
     //
     rootFile = validateRoot(config.getRootDirectory());

     // Initialize the Set<Action> for which this DirectoryScanner
     // is configured.
     //
     if (config.getActions() == null)
         actions = Collections.emptySet();
     else
         actions = EnumSet.copyOf(Arrays.asList(config.getActions()));
     this.logManager = logManager;
}