KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > relation > RoleValidator


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package javax.management.relation;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import javax.management.MBeanServer JavaDoc;
29 import javax.management.MBeanException JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31
32 /**
33  * This is a helper class for performing role validation. It is used by
34  * both the RelationSupport and RelationService classes.<p>
35  *
36  * It is package private and NOT part of the specification.
37  *
38  * <p><b>Revisions:</b>
39  * <p><b>20020311 Adrian Brock:</b>
40  * <ul>
41  * <li>ValidateRole always failed
42  * <li>Throws wrong exception when not writable
43  * </ul>
44  *
45  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
46  * @version $Revision: 37459 $
47  *
48  */

49 class RoleValidator
50 {
51   // Constants ---------------------------------------------------
52

53   // Static ------------------------------------------------------
54

55   /**
56    * Check a role for a relation type
57    *
58    * @param relationService the relation service object name
59    * @param server the MBeanServer of the relation service
60    * @param relationTypeName the relation to validate against
61    * @param role the role to validate
62    * @param write pass to true to check for a writable role
63    * @return zero for success or a RoleStatus value for failure
64    * @exception RelationTypeNotFoundException when the relation type
65    * does not exist in the relation service
66    */

67   public static int checkRole(ObjectName JavaDoc relationService, MBeanServer JavaDoc server,
68                        String JavaDoc relationTypeName, Role JavaDoc role, boolean write)
69     throws RelationTypeNotFoundException JavaDoc
70   {
71     // Get the role information
72
RoleInfo JavaDoc roleInfo = null;
73     try
74     {
75       roleInfo = (RoleInfo JavaDoc) server.invoke(relationService, "getRoleInfo",
76         new Object JavaDoc[] { relationTypeName, role.getRoleName() },
77         new String JavaDoc[] { "java.lang.String", "java.lang.String" });
78     }
79     catch (MBeanException JavaDoc mbe)
80     {
81       Exception JavaDoc e=mbe.getTargetException();
82       if (e instanceof RelationTypeNotFoundException JavaDoc)
83         throw (RelationTypeNotFoundException JavaDoc) e;
84       if (e instanceof RoleInfoNotFoundException JavaDoc)
85         return RoleStatus.NO_ROLE_WITH_NAME;
86       throw new RuntimeException JavaDoc(e.toString());
87     }
88     catch (Exception JavaDoc e)
89     {
90       throw new RuntimeException JavaDoc(e.toString());
91     }
92
93     // Check if the role is writable
94
if (write == true && roleInfo.isWritable() == false)
95       return RoleStatus.ROLE_NOT_WRITABLE;
96
97     // Check the cardinality of the role
98
ArrayList JavaDoc mbeans = (ArrayList JavaDoc) role.getRoleValue();
99     int beanCount = mbeans.size();
100     int minimum = roleInfo.getMinDegree();
101     if (minimum != RoleInfo.ROLE_CARDINALITY_INFINITY && minimum > beanCount)
102       return RoleStatus.LESS_THAN_MIN_ROLE_DEGREE;
103     int maximum = roleInfo.getMaxDegree();
104     if (maximum != RoleInfo.ROLE_CARDINALITY_INFINITY && maximum < beanCount)
105       return RoleStatus.MORE_THAN_MAX_ROLE_DEGREE;
106
107     // Check the MBeans
108
String JavaDoc className = roleInfo.getRefMBeanClassName();
109     Iterator JavaDoc iterator = mbeans.iterator();
110     while (iterator.hasNext())
111     {
112       try
113       {
114         ObjectName JavaDoc objectName = (ObjectName JavaDoc) iterator.next();
115         if (server.isInstanceOf(objectName, className) == false)
116           return RoleStatus.REF_MBEAN_OF_INCORRECT_CLASS;
117       }
118       catch (Exception JavaDoc e)
119       {
120         return RoleStatus.REF_MBEAN_NOT_REGISTERED;
121       }
122     }
123     // All done
124
return 0;
125   }
126
127   /**
128    * Check the Roles for a relation Type.
129    *
130    * @param relationService the relation service object name
131    * @param server the MBeanServer of the relation service
132    * @param relationTypeName the relation to validate against
133    * @param roleList the roles to validate
134    * @param write pass to true to check for a writable role
135    * @return a RoleResult containing resolved and unresolved roles
136    * @exception RelationTypeNotFoundException when the relation type
137    * does not exist in the relation service
138    */

139   public static RoleResult JavaDoc checkRoles(ObjectName JavaDoc relationService,
140               MBeanServer JavaDoc server, String JavaDoc relationTypeName, RoleList JavaDoc roleList,
141               boolean write)
142     throws RelationTypeNotFoundException JavaDoc
143   {
144     // Set up the return value
145
RoleList JavaDoc resolved = new RoleList JavaDoc();
146     RoleUnresolvedList JavaDoc unresolved = new RoleUnresolvedList JavaDoc();
147     RoleResult JavaDoc result = new RoleResult JavaDoc(resolved, unresolved);
148
149     // Check each role
150
Iterator JavaDoc iterator = roleList.iterator();
151     while (iterator.hasNext())
152     {
153       Role JavaDoc role = (Role JavaDoc) iterator.next();
154       int status = checkRole(relationService, server, relationTypeName, role,
155                              write);
156       if (status == 0)
157         resolved.add(role);
158       else
159         unresolved.add(new RoleUnresolved JavaDoc(role.getRoleName(),
160                                           role.getRoleValue(), status));
161     }
162     // All Done
163
return result;
164   }
165
166   /**
167    * Validate a role for a relation Type.
168    *
169    * @param relationService the relation service object name
170    * @param server the MBeanServer of the relation service
171    * @param relationTypeName the relation to validate against
172    * @param role the role to validate
173    * @param write pass to true to check for a writable role
174    * @exception InvalidRoleValueException when a role does not match its
175    * definition in the relation type's roleinfos
176    * @exception RelationTypeNotFoundException when the relation type
177    * does not exist in the relation service
178    * @exception RoleNotFoundException when a role does not exist
179    * in the role
180    */

181   public static void validateRole(ObjectName JavaDoc relationService,
182          MBeanServer JavaDoc server, String JavaDoc relationTypeName, Role JavaDoc role, boolean write)
183     throws InvalidRoleValueException JavaDoc, RelationTypeNotFoundException JavaDoc,
184            RoleNotFoundException JavaDoc
185   {
186     int status = checkRole(relationService, server, relationTypeName, role,
187                            write);
188
189     if (status == RoleStatus.NO_ROLE_WITH_NAME)
190       throw new RoleNotFoundException JavaDoc(role.getRoleName());
191     if (status == RoleStatus.ROLE_NOT_WRITABLE)
192       throw new RoleNotFoundException JavaDoc(role.getRoleName() + " not writable");
193     else if (status != 0)
194       throw new InvalidRoleValueException JavaDoc(role.getRoleName());
195   }
196
197   /**
198    * Validate the Roles for a relation Type.
199    *
200    * @param relationService the relation service object name
201    * @param server the MBeanServer of the relation service
202    * @param relationTypeName the relation to validate against
203    * @param roleList the roles to validate
204    * @param write pass to true to check for a writable role
205    * @exception InvalidRoleValueException when there is a duplicate role name
206    * or a role does not match its definition in the
207    * relation type's roleinfos
208    * @exception RelationTypeNotFoundException when the relation type
209    * does not exist in the relation service
210    * @exception RoleNotFoundException when a role does not exist
211    * in the role
212    */

213   public static void validateRoles(ObjectName JavaDoc relationService,
214               MBeanServer JavaDoc server, String JavaDoc relationTypeName, RoleList JavaDoc roleList,
215               boolean write)
216     throws InvalidRoleValueException JavaDoc, RelationTypeNotFoundException JavaDoc,
217            RoleNotFoundException JavaDoc
218   {
219     Iterator JavaDoc iterator;
220
221     // Check for duplicate roles
222
HashSet JavaDoc roleNames = new HashSet JavaDoc();
223     iterator = roleList.iterator();
224     while (iterator.hasNext())
225     {
226       Object JavaDoc roleName = iterator.next();
227       if (roleNames.contains(roleName))
228         throw new InvalidRoleValueException JavaDoc("Duplicate role " + roleName);
229       roleNames.add(roleName);
230     }
231
232     // Check the roles
233
RoleResult JavaDoc result = checkRoles(relationService, server, relationTypeName,
234                                    roleList, write);
235     RoleUnresolvedList JavaDoc errors = result.getRolesUnresolved();
236     iterator = errors.iterator();
237     if (iterator.hasNext())
238     {
239       RoleUnresolved JavaDoc unresolved = (RoleUnresolved JavaDoc) iterator.next();
240       int status = unresolved.getProblemType();
241       if (status == RoleStatus.NO_ROLE_WITH_NAME)
242         throw new RoleNotFoundException JavaDoc(unresolved.getRoleName());
243       if (status == RoleStatus.ROLE_NOT_WRITABLE)
244         throw new RoleNotFoundException JavaDoc(unresolved.getRoleName() + " not writable");
245       else
246         throw new InvalidRoleValueException JavaDoc(unresolved.getRoleName());
247     }
248   }
249 }
250
Popular Tags