KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > examples > scandir > config > ScanManagerConfig


1 /*
2  * ScanManagerConfig.java
3  *
4  * Created on July 13, 2006, 3:42 PM
5  *
6  * @(#)ScanManagerConfig.java 1.4 06/08/03
7  *
8  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * -Redistribution of source code must retain the above copyright notice, this
14  * list of conditions and the following disclaimer.
15  *
16  * -Redistribution in binary form must reproduce the above copyright notice,
17  * this list of conditions and the following disclaimer in the documentation
18  * and/or other materials provided with the distribution.
19  *
20  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
21  * be used to endorse or promote products derived from this software without
22  * specific prior written permission.
23  *
24  * This software is provided "AS IS," without a warranty of any kind. ALL
25  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
26  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
27  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
28  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
29  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
30  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
31  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
32  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
33  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
34  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
35  *
36  * You acknowledge that this software is not designed, licensed or intended
37  * for use in the design, construction, operation or maintenance of any
38  * nuclear facility.
39  */

40
41 package com.sun.jmx.examples.scandir.config;
42
43 import java.util.Arrays JavaDoc;
44 import java.util.LinkedHashMap JavaDoc;
45 import java.util.Map JavaDoc;
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 JavaDoc<String JavaDoc, 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 JavaDoc 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 JavaDoc name) {
124         this(name,false);
125     }
126     
127     // Our private constructor...
128
private ScanManagerConfig(String JavaDoc name, boolean allowsNull) {
129         if (name == null && allowsNull==false)
130             throw new IllegalArgumentException JavaDoc("name=null");
131         this.name = name;
132         directoryScanners = new LinkedHashMap JavaDoc<String JavaDoc,DirectoryScannerConfig>();
133         this.initialResultLogConfig = new ResultLogConfig();
134         this.initialResultLogConfig.setMemoryMaxRecords(1024);
135     }
136     
137     // Creates an array for deep equality.
138
private Object JavaDoc[] toArray() {
139         final Object JavaDoc[] thisconfig = {
140             name,directoryScanners,initialResultLogConfig
141         };
142         return thisconfig;
143     }
144     
145     // equals
146
@Override JavaDoc
147     public boolean equals(Object JavaDoc 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 JavaDoc
157     public int hashCode() {
158         final String JavaDoc 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 JavaDoc 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 JavaDoc name) {
188         if (this.name == null)
189             this.name = name;
190         else if (name == null)
191             throw new IllegalArgumentException JavaDoc("name=null");
192         else if (!name.equals(this.name))
193             throw new IllegalArgumentException JavaDoc("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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc newname, ScanManagerConfig other) {
300         ScanManagerConfig newbean = XmlConfigUtils.xmlClone(other);
301         newbean.name = newname;
302         return newbean;
303     }
304 }
305
Popular Tags