Home | History | Annotate | Download | only in config
      1 /*
      2  * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  *
      8  *   - Redistributions of source code must retain the above copyright
      9  *     notice, this list of conditions and the following disclaimer.
     10  *
     11  *   - Redistributions in binary form must reproduce the above copyright
     12  *     notice, this list of conditions and the following disclaimer in the
     13  *     documentation and/or other materials provided with the distribution.
     14  *
     15  *   - Neither the name of Oracle nor the names of its
     16  *     contributors may be used to endorse or promote products derived
     17  *     from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
     20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * This source code is provided to illustrate the usage of a given feature
     34  * or technique and has been deliberately simplified. Additional steps
     35  * required for a production-quality application, such as security checks,
     36  * input validation and proper error handling, might not be present in
     37  * this sample code.
     38  */
     39 
     40 
     41 package com.sun.jmx.examples.scandir.config;
     42 
     43 import java.util.Arrays;
     44 import java.util.LinkedHashMap;
     45 import java.util.Map;
     46 import javax.xml.bind.annotation.XmlAttribute;
     47 import javax.xml.bind.annotation.XmlElement;
     48 import javax.xml.bind.annotation.XmlElementRef;
     49 import javax.xml.bind.annotation.XmlElementWrapper;
     50 import javax.xml.bind.annotation.XmlRootElement;
     51 
     52 
     53 /**
     54  * The <code>ScanManagerConfig</code> Java Bean is used to model
     55  * the configuration of the {@link
     56  * com.sun.jmx.examples.scandir.ScanManagerMXBean ScanManagerMXBean}.
     57  *
     58  * The {@link
     59  * com.sun.jmx.examples.scandir.ScanManagerMXBean ScanManagerMXBean} will
     60  * use this configuration to initialize the {@link
     61  * com.sun.jmx.examples.scandir.ResultLogManagerMXBean ResultLogManagerMXBean}
     62  * and create the {@link
     63  * com.sun.jmx.examples.scandir.DirectoryScannerMXBean DirectoryScannerMXBeans}
     64  * <p>
     65  * This class is annotated for XML binding.
     66  * </p>
     67  *
     68  * @author Sun Microsystems, 2006 - All rights reserved.
     69  **/
     70 @XmlRootElement(name="ScanManager",
     71         namespace="jmx:com.sun.jmx.examples.scandir.config")
     72 public class ScanManagerConfig {
     73 
     74     // A logger for this class
     75     //
     76     // private static final Logger LOG =
     77     //        Logger.getLogger(ScanManagerConfig.class.getName());
     78 
     79     /**
     80      * A set of DirectoryScannerConfig objects indexed by their names.
     81      **/
     82     private final Map<String, DirectoryScannerConfig> directoryScanners;
     83 
     84     /**
     85      * The initial Result Log configuration.
     86      */
     87     private ResultLogConfig initialResultLogConfig;
     88 
     89     /**
     90      * Holds value of property name. The name of the configuration
     91      *       usually corresponds to
     92      *       the value of the {@code name=} key of the {@code ObjectName}
     93      *       of the {@link
     94      *       com.sun.jmx.examples.scandir.ScanDirConfigMXBean
     95      *       ScanDirConfigMXBean} which owns this configuration.
     96      **/
     97     private String name;
     98 
     99     /**
    100      * Creates a new instance of ScanManagerConfig.
    101      * <p>You should not use this constructor directly, but use
    102      *    {@link #ScanManagerConfig(String)} instead.
    103      * </p>
    104      * <p>This constructor is tagged deprecated so that the compiler
    105      *    will generate a warning if it is used by mistake.
    106      * </p>
    107      * @deprecated Use {@link #ScanManagerConfig(String)} instead. This
    108      *             constructor is used through reflection by the XML
    109      *             binding framework.
    110      */
    111     public ScanManagerConfig() {
    112         this(null,true);
    113     }
    114 
    115     /**
    116      * Creates a new instance of ScanManagerConfig.
    117      * @param name The name of the configuration which usually corresponds to
    118      *       the value of the {@code name=} key of the {@code ObjectName}
    119      *       of the {@link
    120      *       com.sun.jmx.examples.scandir.ScanDirConfigMXBean
    121      *       ScanDirConfigMXBean} which owns this configuration.
    122      **/
    123     public ScanManagerConfig(String name) {
    124         this(name,false);
    125     }
    126 
    127     // Our private constructor...
    128     private ScanManagerConfig(String name, boolean allowsNull) {
    129         if (name == null && allowsNull==false)
    130             throw new IllegalArgumentException("name=null");
    131         this.name = name;
    132         directoryScanners = new LinkedHashMap<String,DirectoryScannerConfig>();
    133         this.initialResultLogConfig = new ResultLogConfig();
    134         this.initialResultLogConfig.setMemoryMaxRecords(1024);
    135     }
    136 
    137     // Creates an array for deep equality.
    138     private Object[] toArray() {
    139         final Object[] thisconfig = {
    140             name,directoryScanners,initialResultLogConfig
    141         };
    142         return thisconfig;
    143     }
    144 
    145     // equals
    146     @Override
    147     public boolean equals(Object o) {
    148         if (o == this) return true;
    149         if (!(o instanceof ScanManagerConfig)) return false;
    150         final ScanManagerConfig other = (ScanManagerConfig)o;
    151         if (this.directoryScanners.size() != other.directoryScanners.size())
    152             return false;
    153         return Arrays.deepEquals(toArray(),other.toArray());
    154     }
    155 
    156     @Override
    157     public int hashCode() {
    158         final String key = name;
    159         if (key == null) return 0;
    160         else return key.hashCode();
    161     }
    162 
    163     /**
    164      * Gets the name of this configuration. The name of the configuration
    165      *       usually corresponds to
    166      *       the value of the {@code name=} key of the {@code ObjectName}
    167      *       of the {@link
    168      *       com.sun.jmx.examples.scandir.ScanDirConfigMXBean
    169      *       ScanDirConfigMXBean} which owns this configuration.
    170      * @return The name of this configuration.
    171      */
    172     @XmlAttribute(name="name",required=true)
    173     public String getName() {
    174         return this.name;
    175     }
    176 
    177     /**
    178      * Sets the name of this configuration. The name of the configuration
    179      *       usually corresponds to
    180      *       the value of the {@code name=} key of the {@code ObjectName}
    181      *       of the {@link
    182      *       com.sun.jmx.examples.scandir.ScanDirConfigMXBean
    183      *       ScanDirConfigMXBean} which owns this configuration.
    184      *       <p>Once set this value cannot change.</p>
    185      * @param name The name of this configuration.
    186      */
    187     public void setName(String name) {
    188         if (this.name == null)
    189             this.name = name;
    190         else if (name == null)
    191             throw new IllegalArgumentException("name=null");
    192         else if (!name.equals(this.name))
    193             throw new IllegalArgumentException("name="+name);
    194     }
    195 
    196    /**
    197     * Gets the list of Directory Scanner configured by this
    198     * configuration. From each element in this list, the
    199     * {@link com.sun.jmx.examples.scandir.ScanManagerMXBean ScanManagerMXBean}
    200     * will create, initialize, and register a {@link
    201     * com.sun.jmx.examples.scandir.DirectoryScannerMXBean}.
    202     * @return The list of Directory Scanner configured by this configuration.
    203     */
    204     @XmlElementWrapper(name="DirectoryScannerList",
    205             namespace=XmlConfigUtils.NAMESPACE)
    206     @XmlElementRef
    207     public DirectoryScannerConfig[] getScanList() {
    208         return directoryScanners.values().toArray(new DirectoryScannerConfig[0]);
    209     }
    210 
    211    /**
    212     * Sets the list of Directory Scanner configured by this
    213     * configuration. From each element in this list, the
    214     * {@link com.sun.jmx.examples.scandir.ScanManagerMXBean ScanManagerMXBean}
    215     * will create, initialize, and register a {@link
    216     * com.sun.jmx.examples.scandir.DirectoryScannerMXBean}.
    217     * @param scans The list of Directory Scanner configured by this configuration.
    218     */
    219     public void setScanList(DirectoryScannerConfig[] scans) {
    220         directoryScanners.clear();
    221         for (DirectoryScannerConfig scan : scans)
    222             directoryScanners.put(scan.getName(),scan);
    223     }
    224 
    225     /**
    226      * Get a directory scanner by its name.
    227      *
    228      * @param name The name of the directory scanner. This is the
    229      *             value returned by {@link
    230      *             DirectoryScannerConfig#getName()}.
    231      * @return The named {@code DirectoryScannerConfig}
    232      */
    233     public DirectoryScannerConfig getScan(String name) {
    234         return directoryScanners.get(name);
    235     }
    236 
    237     /**
    238      * Adds a directory scanner to the list.
    239      * <p>If a directory scanner
    240      * configuration by that name already exists in the list, it will
    241      * be replaced by the given <var>scan</var>.
    242      * </p>
    243      * @param scan The {@code DirectoryScannerConfig} to add to the list.
    244      * @return The replaced {@code DirectoryScannerConfig}, or {@code null}
    245      *         if there was no {@code DirectoryScannerConfig} by that name
    246      *         in the list.
    247      */
    248     public DirectoryScannerConfig putScan(DirectoryScannerConfig scan) {
    249         return this.directoryScanners.put(scan.getName(),scan);
    250     }
    251 
    252     // XML value of  this object.
    253     public String toString() {
    254         return XmlConfigUtils.toString(this);
    255     }
    256 
    257     /**
    258      * Removes the named directory scanner from the list.
    259      *
    260      * @param name The name of the directory scanner. This is the
    261      *             value returned by {@link
    262      *             DirectoryScannerConfig#getName()}.
    263      * @return The removed {@code DirectoryScannerConfig}, or {@code null}
    264      *         if there was no directory scanner by that name in the list.
    265      */
    266     public DirectoryScannerConfig removeScan(String name) {
    267        return this.directoryScanners.remove(name);
    268     }
    269 
    270     /**
    271      * Gets the initial Result Log Configuration.
    272      * @return The initial Result Log Configuration.
    273      */
    274     @XmlElement(name="InitialResultLogConfig",namespace=XmlConfigUtils.NAMESPACE)
    275     public ResultLogConfig getInitialResultLogConfig() {
    276         return this.initialResultLogConfig;
    277     }
    278 
    279     /**
    280      * Sets the initial Result Log Configuration.
    281      * @param initialLogConfig The initial Result Log Configuration.
    282      */
    283     public void setInitialResultLogConfig(ResultLogConfig initialLogConfig) {
    284         this.initialResultLogConfig = initialLogConfig;
    285     }
    286 
    287     /**
    288      * Creates a copy of this object, with the specified name.
    289      * @param newname the name of the copy.
    290      * @return A copy of this object.
    291      **/
    292     public ScanManagerConfig copy(String newname) {
    293         return copy(newname,this);
    294     }
    295 
    296     // Copy by XML cloning, then change the name.
    297     //
    298     private static ScanManagerConfig
    299             copy(String newname, ScanManagerConfig other) {
    300         ScanManagerConfig newbean = XmlConfigUtils.xmlClone(other);
    301         newbean.name = newname;
    302         return newbean;
    303     }
    304 }
    305