001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package javax.management.relation;
023:
024: import java.io.IOException;
025: import java.io.ObjectInputStream;
026: import java.io.ObjectOutputStream;
027: import java.io.ObjectStreamField;
028: import java.io.Serializable;
029:
030: import java.util.ArrayList;
031: import java.util.Iterator;
032: import java.util.List;
033:
034: import org.jboss.mx.util.Serialization;
035:
036: /**
037: * A role is a role name and an ordered list of object names to
038: * the MBeans in the role.
039: *
040: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>
041: * @version $Revision: 57200 $
042: *
043: * <p><b>Revisions:</b>
044: * <p><b>20020716 Adrian Brock:</b>
045: * <ul>
046: * <li> Serialization
047: * </ul>
048: */
049: public class Role implements Serializable {
050: // Attributes ----------------------------------------------------
051:
052: /**
053: * The role name
054: */
055: private String name;
056:
057: /**
058: * An ordered list of MBean object names.
059: */
060: private List objectNameList;
061:
062: // Static --------------------------------------------------------
063:
064: private static final long serialVersionUID;
065: private static final ObjectStreamField[] serialPersistentFields;
066:
067: static {
068: switch (Serialization.version) {
069: case Serialization.V1R0:
070: serialVersionUID = -1959486389343113026L;
071: serialPersistentFields = new ObjectStreamField[] {
072: new ObjectStreamField("myName", String.class),
073: new ObjectStreamField("myObjNameList", List.class) };
074: break;
075: default:
076: serialVersionUID = -279985518429862552L;
077: serialPersistentFields = new ObjectStreamField[] {
078: new ObjectStreamField("name", String.class),
079: new ObjectStreamField("objectNameList", List.class) };
080: }
081: }
082:
083: /**
084: * Formats the role value for output.<p>
085: *
086: * The spec says it should be a comma separated list of object names.
087: * But the RI uses new lines which makes more sense for object names.
088: *
089: * @param roleValue the role value to print
090: * @return the string representation
091: * @exception IllegalArgumentException for null value.
092: */
093: public static String roleValueToString(List roleValue)
094: throws IllegalArgumentException {
095: if (roleValue == null)
096: throw new IllegalArgumentException("null roleValue");
097: StringBuffer buffer = new StringBuffer();
098: Iterator iterator = roleValue.iterator();
099: while (iterator.hasNext()) {
100: buffer.append(iterator.next());
101: if (iterator.hasNext())
102: buffer.append("\n");
103: }
104: return buffer.toString();
105: }
106:
107: // Constructors --------------------------------------------------
108:
109: /**
110: * Construct a new role.<p>
111: *
112: * No validation is performed until the role is set of in a
113: * relation. Passed parameters must not be null.<p>
114: *
115: * The passed list must be an ArrayList.
116: *
117: * @param roleName the role name
118: * @param roleValue the MBean object names in the role
119: * @exception IllegalArgumentException for null values.
120: */
121: public Role(String roleName, List roleValue)
122: throws IllegalArgumentException {
123: setRoleName(roleName);
124: setRoleValue(roleValue);
125: }
126:
127: // Public ---------------------------------------------------------
128:
129: /**
130: * Retrieve the role name.
131: *
132: * @return the role name.
133: */
134: public String getRoleName() {
135: return name;
136: }
137:
138: /**
139: * Retrieve the role value.
140: *
141: * @return a list of MBean object names.
142: */
143: public List getRoleValue() {
144: return new ArrayList(objectNameList);
145: }
146:
147: /**
148: * Set the role name.
149: *
150: * @param roleName the role name.
151: * @exception IllegalArgumentException for a null value
152: */
153: public void setRoleName(String roleName)
154: throws IllegalArgumentException {
155: if (roleName == null)
156: throw new IllegalArgumentException("Null roleName");
157: name = roleName;
158: }
159:
160: /**
161: * Set the role value it must be an ArrayList.
162: * A list of mbean object names.
163: *
164: * @param roleValue the role value.
165: * @exception IllegalArgumentException for a null value or not an
166: * array list
167: */
168: public void setRoleValue(List roleValue)
169: throws IllegalArgumentException {
170: if (roleValue == null)
171: throw new IllegalArgumentException("Null roleValue");
172: objectNameList = new ArrayList(roleValue);
173: }
174:
175: // Object Overrides -------------------------------------------------
176:
177: /**
178: * Clones the object.
179: *
180: * @todo fix this not to use the copy constructor
181: *
182: * @return a copy of the role
183: * @throws CloneNotSupportedException
184: */
185: public synchronized Object clone() {
186: return new Role(name, objectNameList);
187: /* try
188: {
189: Role clone = (Role) super.clone();
190: clone.name = this.name;
191: clone.objectNameList = new ArrayList(this.objectNameList);
192: return clone;
193: }
194: catch (CloneNotSupportedException e)
195: {
196: throw new RuntimeException(e.toString());
197: }
198: */}
199:
200: /**
201: * Formats the role for output.
202: *
203: * @return a human readable string
204: */
205: public synchronized String toString() {
206: StringBuffer buffer = new StringBuffer();
207: buffer.append("Role@");
208: buffer.append(System.identityHashCode(this ));
209: buffer.append(" RoleName(");
210: buffer.append(name);
211: buffer.append(") ObjectNames (");
212: Iterator iterator = objectNameList.iterator();
213: while (iterator.hasNext()) {
214: buffer.append(iterator.next());
215: if (iterator.hasNext())
216: buffer.append(" & ");
217: }
218: buffer.append(")");
219: return buffer.toString();
220: }
221:
222: // Private -----------------------------------------------------
223:
224: private void readObject(ObjectInputStream ois) throws IOException,
225: ClassNotFoundException {
226: switch (Serialization.version) {
227: case Serialization.V1R0:
228: ObjectInputStream.GetField getField = ois.readFields();
229: name = (String) getField.get("myName", null);
230: objectNameList = (List) getField.get("myObjNameList", null);
231: break;
232: default:
233: ois.defaultReadObject();
234: }
235: }
236:
237: private void writeObject(ObjectOutputStream oos) throws IOException {
238: switch (Serialization.version) {
239: case Serialization.V1R0:
240: ObjectOutputStream.PutField putField = oos.putFields();
241: putField.put("myName", name);
242: putField.put("myObjNameList", objectNameList);
243: oos.writeFields();
244: break;
245: default:
246: oos.defaultWriteObject();
247: }
248: }
249: }
|