1/*
2 * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package javax.management.relation;
27
28
29import com.sun.jmx.mbeanserver.GetPropertyAction;
30
31import java.io.IOException;
32import java.io.ObjectInputStream;
33import java.io.ObjectOutputStream;
34import java.io.ObjectStreamField;
35import java.io.Serializable;
36
37import java.security.AccessController;
38import java.util.Iterator;
39
40/**
41 * Represents the result of a multiple access to several roles of a relation
42 * (either for reading or writing).
43 *
44 * <p>The <b>serialVersionUID</b> of this class is <code>-6304063118040985512L</code>.
45 *
46 * @since 1.5
47 */
48@SuppressWarnings("serial")
49public class RoleResult implements Serializable {
50
51    // Serialization compatibility stuff:
52    // Two serial forms are supported in this class. The selected form depends
53    // on system property "jmx.serial.form":
54    //  - "1.0" for JMX 1.0
55    //  - any other value for JMX 1.1 and higher
56    //
57    // Serial version for old serial form
58    private static final long oldSerialVersionUID = 3786616013762091099L;
59    //
60    // Serial version for new serial form
61    private static final long newSerialVersionUID = -6304063118040985512L;
62    //
63    // Serializable fields in old serial form
64    private static final ObjectStreamField[] oldSerialPersistentFields =
65    {
66      new ObjectStreamField("myRoleList", RoleList.class),
67      new ObjectStreamField("myRoleUnresList", RoleUnresolvedList.class)
68    };
69    //
70    // Serializable fields in new serial form
71    private static final ObjectStreamField[] newSerialPersistentFields =
72    {
73      new ObjectStreamField("roleList", RoleList.class),
74      new ObjectStreamField("unresolvedRoleList", RoleUnresolvedList.class)
75    };
76    //
77    // Actual serial version and serial form
78    private static final long serialVersionUID;
79    /**
80     * @serialField roleList RoleList List of roles successfully accessed
81     * @serialField unresolvedRoleList RoleUnresolvedList List of roles unsuccessfully accessed
82     */
83    private static final ObjectStreamField[] serialPersistentFields;
84    private static boolean compat = false;
85    static {
86        try {
87            GetPropertyAction act = new GetPropertyAction("jmx.serial.form");
88            String form = AccessController.doPrivileged(act);
89            compat = (form != null && form.equals("1.0"));
90        } catch (Exception e) {
91            // OK : Too bad, no compat with 1.0
92        }
93        if (compat) {
94            serialPersistentFields = oldSerialPersistentFields;
95            serialVersionUID = oldSerialVersionUID;
96        } else {
97            serialPersistentFields = newSerialPersistentFields;
98            serialVersionUID = newSerialVersionUID;
99        }
100    }
101    //
102    // END Serialization compatibility stuff
103
104    //
105    // Private members
106    //
107
108    /**
109     * @serial List of roles successfully accessed
110     */
111    private RoleList roleList = null;
112
113    /**
114     * @serial List of roles unsuccessfully accessed
115     */
116    private RoleUnresolvedList unresolvedRoleList = null;
117
118    //
119    // Constructor
120    //
121
122    /**
123     * Constructor.
124     *
125     * @param list  list of roles successfully accessed.
126     * @param unresolvedList  list of roles not accessed (with problem
127     * descriptions).
128     */
129    public RoleResult(RoleList list,
130                      RoleUnresolvedList unresolvedList) {
131
132        setRoles(list);
133        setRolesUnresolved(unresolvedList);
134        return;
135    }
136
137    //
138    // Accessors
139    //
140
141    /**
142     * Retrieves list of roles successfully accessed.
143     *
144     * @return a RoleList
145     *
146     * @see #setRoles
147     */
148    public RoleList getRoles() {
149        return roleList;
150    }
151
152    /**
153     * Retrieves list of roles unsuccessfully accessed.
154     *
155     * @return a RoleUnresolvedList.
156     *
157     * @see #setRolesUnresolved
158     */
159    public RoleUnresolvedList getRolesUnresolved() {
160        return unresolvedRoleList;
161    }
162
163    /**
164     * Sets list of roles successfully accessed.
165     *
166     * @param list  list of roles successfully accessed
167     *
168     * @see #getRoles
169     */
170    public void setRoles(RoleList list) {
171        if (list != null) {
172
173            roleList = new RoleList();
174
175            for (Iterator<?> roleIter = list.iterator();
176                 roleIter.hasNext();) {
177                Role currRole = (Role)(roleIter.next());
178                roleList.add((Role)(currRole.clone()));
179            }
180        } else {
181            roleList = null;
182        }
183        return;
184    }
185
186    /**
187     * Sets list of roles unsuccessfully accessed.
188     *
189     * @param unresolvedList  list of roles unsuccessfully accessed
190     *
191     * @see #getRolesUnresolved
192     */
193    public void setRolesUnresolved(RoleUnresolvedList unresolvedList) {
194        if (unresolvedList != null) {
195
196            unresolvedRoleList = new RoleUnresolvedList();
197
198            for (Iterator<?> roleUnresIter = unresolvedList.iterator();
199                 roleUnresIter.hasNext();) {
200                RoleUnresolved currRoleUnres =
201                    (RoleUnresolved)(roleUnresIter.next());
202                unresolvedRoleList.add((RoleUnresolved)(currRoleUnres.clone()));
203            }
204        } else {
205            unresolvedRoleList = null;
206        }
207        return;
208    }
209
210    /**
211     * Deserializes a {@link RoleResult} from an {@link ObjectInputStream}.
212     */
213    private void readObject(ObjectInputStream in)
214            throws IOException, ClassNotFoundException {
215      if (compat)
216      {
217        // Read an object serialized in the old serial form
218        //
219        ObjectInputStream.GetField fields = in.readFields();
220        roleList = (RoleList) fields.get("myRoleList", null);
221        if (fields.defaulted("myRoleList"))
222        {
223          throw new NullPointerException("myRoleList");
224        }
225        unresolvedRoleList = (RoleUnresolvedList) fields.get("myRoleUnresList", null);
226        if (fields.defaulted("myRoleUnresList"))
227        {
228          throw new NullPointerException("myRoleUnresList");
229        }
230      }
231      else
232      {
233        // Read an object serialized in the new serial form
234        //
235        in.defaultReadObject();
236      }
237    }
238
239
240    /**
241     * Serializes a {@link RoleResult} to an {@link ObjectOutputStream}.
242     */
243    private void writeObject(ObjectOutputStream out)
244            throws IOException {
245      if (compat)
246      {
247        // Serializes this instance in the old serial form
248        //
249        ObjectOutputStream.PutField fields = out.putFields();
250        fields.put("myRoleList", roleList);
251        fields.put("myRoleUnresList", unresolvedRoleList);
252        out.writeFields();
253      }
254      else
255      {
256        // Serializes this instance in the new serial form
257        //
258        out.defaultWriteObject();
259      }
260    }
261}
262