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: import java.io.StreamCorruptedException;
030:
031: import java.util.ArrayList;
032: import java.util.Iterator;
033: import java.util.List;
034:
035: import org.jboss.mx.util.Serialization;
036:
037: /**
038: * An unresolved role. Used when a role could not be retrieved from a
039: * relation due to a problem. It has the role name, the value if that
040: * was passed and the problem type.
041: *
042: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>
043: * @version $Revision: 57200 $
044: */
045: public class RoleUnresolved implements Serializable {
046: // Attributes ----------------------------------------------------
047:
048: /**
049: * The role name
050: */
051: private String roleName;
052:
053: /**
054: * An ordered list of MBean object names.
055: */
056: private List roleValue;
057:
058: /**
059: * The problem type.
060: */
061: private int problemType;
062:
063: // Static --------------------------------------------------------
064:
065: private static final long serialVersionUID;
066: private static final ObjectStreamField[] serialPersistentFields;
067:
068: static {
069: switch (Serialization.version) {
070: case Serialization.V1R0:
071: serialVersionUID = -9026457686611660144L;
072: serialPersistentFields = new ObjectStreamField[] {
073: new ObjectStreamField("myRoleName", String.class),
074: new ObjectStreamField("myRoleValue",
075: ArrayList.class),
076: new ObjectStreamField("myPbType", Integer.TYPE) };
077: break;
078: default:
079: serialVersionUID = -48350262537070138L;
080: serialPersistentFields = new ObjectStreamField[] {
081: new ObjectStreamField("roleName", String.class),
082: new ObjectStreamField("roleValue", List.class),
083: new ObjectStreamField("problemType", Integer.TYPE) };
084: }
085: }
086:
087: // Constructors --------------------------------------------------
088:
089: /**
090: * Construct a new unresolved role.<p>
091: *
092: * See {@link RoleStatus} for the problem types.<p>
093: *
094: * The passed list must be an ArrayList.
095: *
096: * @param roleName the role name
097: * @param roleValue the MBean object names in the role can be null
098: * @param problemType the problem type.
099: * @exception IllegalArgumentException for null values or
100: * incorrect problem type.
101: */
102: public RoleUnresolved(String roleName, List roleValue,
103: int problemType) throws IllegalArgumentException {
104: if (roleName == null)
105: throw new IllegalArgumentException("Null roleName");
106: if (roleValue == null)
107: throw new IllegalArgumentException("Null roleValue");
108: if (RoleStatus.isRoleStatus(problemType) == false)
109: throw new IllegalArgumentException("Invalid problem type.");
110: this .roleName = roleName;
111: this .roleValue = new ArrayList(roleValue);
112: this .problemType = problemType;
113: }
114:
115: // Public ---------------------------------------------------------
116:
117: /**
118: * Retrieve the problem type.
119: *
120: * @return the problem type.
121: */
122: public int getProblemType() {
123: return problemType;
124: }
125:
126: /**
127: * Retrieve the role name.
128: *
129: * @return the role name.
130: */
131: public String getRoleName() {
132: return roleName;
133: }
134:
135: /**
136: * Retrieve the role value.
137: *
138: * @return a list of MBean object names.
139: */
140: public List getRoleValue() {
141: return roleValue;
142: }
143:
144: /**
145: * Set the problem type.
146: *
147: * @param problemType the problem type.
148: * @exception IllegalArgumentException for an invalid problem type
149: */
150: public void setProblemType(int problemType)
151: throws IllegalArgumentException {
152: if (RoleStatus.isRoleStatus(problemType) == false)
153: throw new IllegalArgumentException("Invalid problem type.");
154: this .problemType = problemType;
155: }
156:
157: /**
158: * Set the role name.
159: *
160: * @param roleName the role name.
161: * @exception IllegalArgumentException for a null name
162: */
163: public void setRoleName(String roleName)
164: throws IllegalArgumentException {
165: if (roleName == null)
166: throw new IllegalArgumentException("Null roleName");
167: this .roleName = roleName;
168: }
169:
170: /**
171: * Set the role value it must be an ArrayList.
172: * A list of mbean object names.
173: *
174: * @param roleValue the role value.
175: */
176: public void setRoleValue(List roleValue) {
177: this .roleValue = new ArrayList(roleValue);
178: }
179:
180: // Object Overrides -------------------------------------------------
181:
182: /**
183: * Clones the object.
184: */
185: public synchronized Object clone() {
186: return new RoleUnresolved(roleName, roleValue, problemType);
187: /* try
188: {
189: RoleUnresolved clone = (RoleUnresolved) super.clone();
190: clone.roleName = this.roleName;
191: clone.problemType = this.problemType;
192: clone.roleValue = new ArrayList(this.roleValue);
193: return clone;
194: }
195: catch (CloneNotSupportedException e)
196: {
197: throw new RuntimeException(e.toString());
198: }
199: */}
200:
201: /**
202: * Formats the unresolved role for output.
203: */
204: public synchronized String toString() {
205: StringBuffer buffer = new StringBuffer("Problem (");
206: buffer.append(problemType); // REVIEW?????
207: buffer.append(") Role Name (");
208: buffer.append(roleName);
209: buffer.append(") ObjectNames (");
210: Iterator iterator = roleValue.iterator();
211: while (iterator.hasNext()) {
212: buffer.append(iterator.next());
213: if (iterator.hasNext())
214: buffer.append(" ");
215: }
216: buffer.append(")");
217: return buffer.toString();
218: }
219:
220: private void readObject(ObjectInputStream ois) throws IOException,
221: ClassNotFoundException {
222: switch (Serialization.version) {
223: case Serialization.V1R0:
224: ObjectInputStream.GetField getField = ois.readFields();
225: roleName = (String) getField.get("myRoleName", null);
226: roleValue = (List) getField.get("myRoleValue", null);
227: problemType = getField.get("myPbType", -1);
228: if (problemType == -1)
229: throw new StreamCorruptedException("No problem type?");
230: break;
231: default:
232: ois.defaultReadObject();
233: }
234: }
235:
236: private void writeObject(ObjectOutputStream oos) throws IOException {
237: switch (Serialization.version) {
238: case Serialization.V1R0:
239: ObjectOutputStream.PutField putField = oos.putFields();
240: putField.put("myRoleName", roleName);
241: putField.put("myRoleValue", roleValue);
242: putField.put("myPbType", problemType);
243: oos.writeFields();
244: break;
245: default:
246: oos.defaultWriteObject();
247: }
248: }
249: }
|