Showing the changes to java/pkg/javax/management/relation/RelationSupport.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,1731 @@ | |||
| 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 | |||
| 26 | package javax.management.relation; | ||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | import java.util.ArrayList; | ||
| 31 | |||
| 32 | import java.util.HashMap; | ||
| 33 | import java.util.Iterator; | ||
| 34 | import java.util.Map; | ||
| 35 | import java.util.List; | ||
| 36 | |||
| 37 | import java.util.concurrent.atomic.AtomicBoolean; | ||
| 38 | import static com.sun.jmx.defaults.JmxProperties.RELATION_LOGGER; | ||
| 39 | import static com.sun.jmx.mbeanserver.Util.cast; | ||
| 40 | import javax.management.InstanceNotFoundException; | ||
| 41 | import javax.management.MBeanException; | ||
| 42 | import javax.management.MBeanRegistration; | ||
| 43 | import javax.management.MBeanServer; | ||
| 44 | import javax.management.ObjectName; | ||
| 45 | import javax.management.ReflectionException; | ||
| 46 | |||
| 47 | /** | ||
| 48 | * A RelationSupport object is used internally by the Relation Service to | ||
| 49 | * represent simple relations (only roles, no properties or methods), with an | ||
| 50 | * unlimited number of roles, of any relation type. As internal representation, | ||
| 51 | * it is not exposed to the user. | ||
| 52 | * <P>RelationSupport class conforms to the design patterns of standard MBean. So | ||
| 53 | * the user can decide to instantiate a RelationSupport object himself as | ||
| 54 | * a MBean (as it follows the MBean design patterns), to register it in the | ||
| 55 | * MBean Server, and then to add it in the Relation Service. | ||
| 56 | * <P>The user can also, when creating his own MBean relation class, have it | ||
| 57 | * extending RelationSupport, to retrieve the implementations of required | ||
| 58 | * interfaces (see below). | ||
| 59 | * <P>It is also possible to have in a user relation MBean class a member | ||
| 60 | * being a RelationSupport object, and to implement the required interfaces by | ||
| 61 | * delegating all to this member. | ||
| 62 | * <P> RelationSupport implements the Relation interface (to be handled by the | ||
| 63 | * Relation Service). | ||
| 64 | * <P>It implements also the MBeanRegistration interface to be able to retrieve | ||
| 65 | * the MBean Server where it is registered (if registered as a MBean) to access | ||
| 66 | * to its Relation Service. | ||
| 67 | * | ||
| 68 | * @since 1.5 | ||
| 69 | */ | ||
| 70 | public class RelationSupport | ||
| 71 | implements RelationSupportMBean, MBeanRegistration { | ||
| 72 | |||
| 73 | // | ||
| 74 | // Private members | ||
| 75 | // | ||
| 76 | |||
| 77 | // Relation identifier (expected to be unique in the Relation Service where | ||
| 78 | // the RelationSupport object will be added) | ||
| 79 | private String myRelId = null; | ||
| 80 | |||
| 81 | // ObjectName of the Relation Service where the relation will be added | ||
| 82 | // REQUIRED if the RelationSupport is created by the user to be registered as | ||
| 83 | // a MBean, as it will have to access the Relation Service via the MBean | ||
| 84 | // Server to perform the check regarding the relation type. | ||
| 85 | // Is null if current object is directly created by the Relation Service, | ||
| 86 | // as the object will directly access it. | ||
| 87 | private ObjectName myRelServiceName = null; | ||
| 88 | |||
| 89 | // Reference to the MBean Server where the Relation Service is | ||
| 90 | // registered | ||
| 91 | // REQUIRED if the RelationSupport is created by the user to be registered as | ||
| 92 | // a MBean, as it will have to access the Relation Service via the MBean | ||
| 93 | // Server to perform the check regarding the relation type. | ||
| 94 | // If the Relationbase object is created by the Relation Service (use of | ||
| 95 | // createRelation() method), this is null as not needed, direct access to | ||
| 96 | // the Relation Service. | ||
| 97 | // If the Relationbase object is created by the user and registered as a | ||
| 98 | // MBean, this is set by the preRegister() method below. | ||
| 99 | private MBeanServer myRelServiceMBeanServer = null; | ||
| 100 | |||
| 101 | // Relation type name (must be known in the Relation Service where the | ||
| 102 | // relation will be added) | ||
| 103 | private String myRelTypeName = null; | ||
| 104 | |||
| 105 | // Role map, mapping <role-name> -> <Role> | ||
| 106 | // Initialized by role list in the constructor, then updated: | ||
| 107 | // - if the relation is a MBean, via setRole() and setRoles() methods, or | ||
| 108 | // via Relation Service setRole() and setRoles() methods | ||
| 109 | // - if the relation is internal to the Relation Service, via | ||
| 110 | // setRoleInt() and setRolesInt() methods. | ||
| 111 | private final Map<String,Role> myRoleName2ValueMap = new HashMap<String,Role>(); | ||
| 112 | |||
| 113 | // Flag to indicate if the object has been added in the Relation Service | ||
| 114 | private final AtomicBoolean myInRelServFlg = new AtomicBoolean(); | ||
| 115 | |||
| 116 | // | ||
| 117 | // Constructors | ||
| 118 | // | ||
| 119 | |||
| 120 | /** | ||
| 121 | * Creates a {@code RelationSupport} object. | ||
| 122 | * <P>This constructor has to be used when the RelationSupport object will | ||
| 123 | * be registered as a MBean by the user, or when creating a user relation | ||
| 124 | * MBean whose class extends RelationSupport. | ||
| 125 | * <P>Nothing is done at the Relation Service level, i.e. | ||
| 126 | * the {@code RelationSupport} object is not added to the | ||
| 127 | * {@code RelationService} and no checks are performed to | ||
| 128 | * see if the provided values are correct. | ||
| 129 | * The object is always created, EXCEPT if: | ||
| 130 | * <P>- any of the required parameters is {@code null}. | ||
| 131 | * <P>- the same name is used for two roles. | ||
| 132 | * <P>To be handled as a relation, the {@code RelationSupport} object has | ||
| 133 | * to be added to the Relation Service using the Relation Service method | ||
| 134 | * addRelation(). | ||
| 135 | * | ||
| 136 | * @param relationId relation identifier, to identify the relation in the | ||
| 137 | * Relation Service. | ||
| 138 | * <P>Expected to be unique in the given Relation Service. | ||
| 139 | * @param relationServiceName ObjectName of the Relation Service where | ||
| 140 | * the relation will be registered. | ||
| 141 | * <P>This parameter is required as it is the Relation Service that is | ||
| 142 | * aware of the definition of the relation type of the given relation, | ||
| 143 | * so that will be able to check update operations (set). | ||
| 144 | * @param relationTypeName Name of relation type. | ||
| 145 | * <P>Expected to have been created in the given Relation Service. | ||
| 146 | * @param list list of roles (Role objects) to initialize the | ||
| 147 | * relation. Can be {@code null}. | ||
| 148 | * <P>Expected to conform to relation info in associated relation type. | ||
| 149 | * | ||
| 150 | * @exception InvalidRoleValueException if the same name is used for two | ||
| 151 | * roles. | ||
| 152 | * @exception IllegalArgumentException if any of the required parameters | ||
| 153 | * (relation id, relation service ObjectName, or relation type name) is | ||
| 154 | * {@code null}. | ||
| 155 | */ | ||
| 156 | public RelationSupport(String relationId, | ||
| 157 | ObjectName relationServiceName, | ||
| 158 | String relationTypeName, | ||
| 159 | RoleList list) | ||
| 160 | throws InvalidRoleValueException, | ||
| 161 | IllegalArgumentException { | ||
| 162 | |||
| 163 | super(); | ||
| 164 | |||
| 165 | RELATION_LOGGER.entering(RelationSupport.class.getName(), | ||
| 166 | "RelationSupport"); | ||
| 167 | |||
| 168 | // Can throw InvalidRoleValueException and IllegalArgumentException | ||
| 169 | initMembers(relationId, | ||
| 170 | relationServiceName, | ||
| 171 | null, | ||
| 172 | relationTypeName, | ||
| 173 | list); | ||
| 174 | |||
| 175 | RELATION_LOGGER.exiting(RelationSupport.class.getName(), | ||
| 176 | "RelationSupport"); | ||
| 177 | } | ||
| 178 | |||
| 179 | /** | ||
| 180 | * Creates a {@code RelationSupport} object. | ||
| 181 | * <P>This constructor has to be used when the user relation MBean | ||
| 182 | * implements the interfaces expected to be supported by a relation by | ||
| 183 | * delegating to a RelationSupport object. | ||
| 184 | * <P>This object needs to know the Relation Service expected to handle the | ||
| 185 | * relation. So it has to know the MBean Server where the Relation Service | ||
| 186 | * is registered. | ||
| 187 | * <P>According to a limitation, a relation MBean must be registered in the | ||
| 188 | * same MBean Server as the Relation Service expected to handle it. So the | ||
| 189 | * user relation MBean has to be created and registered, and then the | ||
| 190 | * wrapped RelationSupport object can be created within the identified MBean | ||
| 191 | * Server. | ||
| 192 | * <P>Nothing is done at the Relation Service level, i.e. | ||
| 193 | * the {@code RelationSupport} object is not added to the | ||
| 194 | * {@code RelationService} and no checks are performed to | ||
| 195 | * see if the provided values are correct. | ||
| 196 | * The object is always created, EXCEPT if: | ||
| 197 | * <P>- any of the required parameters is {@code null}. | ||
| 198 | * <P>- the same name is used for two roles. | ||
| 199 | * <P>To be handled as a relation, the {@code RelationSupport} object has | ||
| 200 | * to be added to the Relation Service using the Relation Service method | ||
| 201 | * addRelation(). | ||
| 202 | * | ||
| 203 | * @param relationId relation identifier, to identify the relation in the | ||
| 204 | * Relation Service. | ||
| 205 | * <P>Expected to be unique in the given Relation Service. | ||
| 206 | * @param relationServiceName ObjectName of the Relation Service where | ||
| 207 | * the relation will be registered. | ||
| 208 | * <P>This parameter is required as it is the Relation Service that is | ||
| 209 | * aware of the definition of the relation type of the given relation, | ||
| 210 | * so that will be able to check update operations (set). | ||
| 211 | * @param relationServiceMBeanServer MBean Server where the wrapping MBean | ||
| 212 | * is or will be registered. | ||
| 213 | * <P>Expected to be the MBean Server where the Relation Service is or will | ||
| 214 | * be registered. | ||
| 215 | * @param relationTypeName Name of relation type. | ||
| 216 | * <P>Expected to have been created in the given Relation Service. | ||
| 217 | * @param list list of roles (Role objects) to initialize the | ||
| 218 | * relation. Can be {@code null}. | ||
| 219 | * <P>Expected to conform to relation info in associated relation type. | ||
| 220 | * | ||
| 221 | * @exception InvalidRoleValueException if the same name is used for two | ||
| 222 | * roles. | ||
| 223 | * @exception IllegalArgumentException if any of the required parameters | ||
| 224 | * (relation id, relation service ObjectName, relation service MBeanServer, | ||
| 225 | * or relation type name) is {@code null}. | ||
| 226 | */ | ||
| 227 | public RelationSupport(String relationId, | ||
| 228 | ObjectName relationServiceName, | ||
| 229 | MBeanServer relationServiceMBeanServer, | ||
| 230 | String relationTypeName, | ||
| 231 | RoleList list) | ||
| 232 | throws InvalidRoleValueException, | ||
| 233 | IllegalArgumentException { | ||
| 234 | |||
| 235 | super(); | ||
| 236 | |||
| 237 | if (relationServiceMBeanServer == null) { | ||
| 238 | String excMsg = "Invalid parameter."; | ||
| 239 | throw new IllegalArgumentException(excMsg); | ||
| 240 | } | ||
| 241 | |||
| 242 | RELATION_LOGGER.entering(RelationSupport.class.getName(), | ||
| 243 | "RelationSupport"); | ||
| 244 | |||
| 245 | // Can throw InvalidRoleValueException and | ||
| 246 | // IllegalArgumentException | ||
| 247 | initMembers(relationId, | ||
| 248 | relationServiceName, | ||
| 249 | relationServiceMBeanServer, | ||
| 250 | relationTypeName, | ||
| 251 | list); | ||
| 252 | |||
| 253 | RELATION_LOGGER.exiting(RelationSupport.class.getName(), | ||
| 254 | "RelationSupport"); | ||
| 255 | } | ||
| 256 | |||
| 257 | // | ||
| 258 | // Relation Interface | ||
| 259 | // | ||
| 260 | |||
| 261 | /** | ||
| 262 | * Retrieves role value for given role name. | ||
| 263 | * <P>Checks if the role exists and is readable according to the relation | ||
| 264 | * type. | ||
| 265 | * | ||
| 266 | * @param roleName name of role | ||
| 267 | * | ||
| 268 | * @return the ArrayList of ObjectName objects being the role value | ||
| 269 | * | ||
| 270 | * @exception IllegalArgumentException if null role name | ||
| 271 | * @exception RoleNotFoundException if: | ||
| 272 | * <P>- there is no role with given name | ||
| 273 | * <P>- the role is not readable. | ||
| 274 | * @exception RelationServiceNotRegisteredException if the Relation | ||
| 275 | * Service is not registered in the MBean Server | ||
| 276 | * | ||
| 277 | * @see #setRole | ||
| 278 | */ | ||
| 279 | public List<ObjectName> getRole(String roleName) | ||
| 280 | throws IllegalArgumentException, | ||
| 281 | RoleNotFoundException, | ||
| 282 | RelationServiceNotRegisteredException { | ||
| 283 | |||
| 284 | if (roleName == null) { | ||
| 285 | String excMsg = "Invalid parameter."; | ||
| 286 | throw new IllegalArgumentException(excMsg); | ||
| 287 | } | ||
| 288 | |||
| 289 | RELATION_LOGGER.entering(RelationSupport.class.getName(), | ||
| 290 | "getRole", roleName); | ||
| 291 | |||
| 292 | // Can throw RoleNotFoundException and | ||
| 293 | // RelationServiceNotRegisteredException | ||
| 294 | List<ObjectName> result = cast( | ||
| 295 | getRoleInt(roleName, false, null, false)); | ||
| 296 | |||
| 297 | RELATION_LOGGER.exiting(RelationSupport.class.getName(), "getRole"); | ||
| 298 | return result; | ||
| 299 | } | ||
| 300 | |||
| 301 | /** | ||
| 302 | * Retrieves values of roles with given names. | ||
| 303 | * <P>Checks for each role if it exists and is readable according to the | ||
| 304 | * relation type. | ||
| 305 | * | ||
| 306 | * @param roleNameArray array of names of roles to be retrieved | ||
| 307 | * | ||
| 308 | * @return a RoleResult object, including a RoleList (for roles | ||
| 309 | * successfully retrieved) and a RoleUnresolvedList (for roles not | ||
| 310 | * retrieved). | ||
| 311 | * | ||
| 312 | * @exception IllegalArgumentException if null role name | ||
| 313 | * @exception RelationServiceNotRegisteredException if the Relation | ||
| 314 | * Service is not registered in the MBean Server | ||
| 315 | * | ||
| 316 | * @see #setRoles | ||
| 317 | */ | ||
| 318 | public RoleResult getRoles(String[] roleNameArray) | ||
| 319 | throws IllegalArgumentException, | ||
| 320 | RelationServiceNotRegisteredException { | ||
| 321 | |||
| 322 | if (roleNameArray == null) { | ||
| 323 | String excMsg = "Invalid parameter."; | ||
| 324 | throw new IllegalArgumentException(excMsg); | ||
| 325 | } | ||
| 326 | |||
| 327 | RELATION_LOGGER.entering(RelationSupport.class.getName(), "getRoles"); | ||
| 328 | |||
| 329 | // Can throw RelationServiceNotRegisteredException | ||
| 330 | RoleResult result = getRolesInt(roleNameArray, false, null); | ||
| 331 | |||
| 332 | RELATION_LOGGER.exiting(RelationSupport.class.getName(), "getRoles"); | ||
| 333 | return result; | ||
| 334 | } | ||
| 335 | |||
| 336 | /** | ||
| 337 | * Returns all roles present in the relation. | ||
| 338 | * | ||
| 339 | * @return a RoleResult object, including a RoleList (for roles | ||
| 340 | * successfully retrieved) and a RoleUnresolvedList (for roles not | ||
| 341 | * readable). | ||
| 342 | * | ||
| 343 | * @exception RelationServiceNotRegisteredException if the Relation | ||
| 344 | * Service is not registered in the MBean Server | ||
| 345 | */ | ||
| 346 | public RoleResult getAllRoles() | ||
| 347 | throws RelationServiceNotRegisteredException { | ||
| 348 | |||
| 349 | RELATION_LOGGER.entering(RelationSupport.class.getName(), | ||
| 350 | "getAllRoles"); | ||
| 351 | |||
| 352 | RoleResult result = null; | ||
| 353 | try { | ||
| 354 | result = getAllRolesInt(false, null); | ||
| 355 | } catch (IllegalArgumentException exc) { | ||
| 356 | // OK : Invalid parameters, ignore... | ||
| 357 | } | ||
| 358 | |||
| 359 | RELATION_LOGGER.exiting(RelationSupport.class.getName(), "getAllRoles"); | ||
| 360 | return result; | ||
| 361 | } | ||
| 362 | |||
| 363 | /** | ||
| 364 | * Returns all roles in the relation without checking read mode. | ||
| 365 | * | ||
| 366 | * @return a RoleList | ||
| 367 | */ | ||
| 368 | public RoleList retrieveAllRoles() { | ||
| 369 | |||
| 370 | RELATION_LOGGER.entering(RelationSupport.class.getName(), | ||
| 371 | "retrieveAllRoles"); | ||
| 372 | |||
| 373 | RoleList result; | ||
| 374 | synchronized(myRoleName2ValueMap) { | ||
| 375 | result = | ||
| 376 | new RoleList(new ArrayList<Role>(myRoleName2ValueMap.values())); | ||
| 377 | } | ||
| 378 | |||
| 379 | RELATION_LOGGER.exiting(RelationSupport.class.getName(), | ||
| 380 | "retrieveAllRoles"); | ||
| 381 | return result; | ||
| 382 | } | ||
| 383 | |||
| 384 | /** | ||
| 385 | * Returns the number of MBeans currently referenced in the given role. | ||
| 386 | * | ||
| 387 | * @param roleName name of role | ||
| 388 | * | ||
| 389 | * @return the number of currently referenced MBeans in that role | ||
| 390 | * | ||
| 391 | * @exception IllegalArgumentException if null role name | ||
| 392 | * @exception RoleNotFoundException if there is no role with given name | ||
| 393 | */ | ||
| 394 | public Integer getRoleCardinality(String roleName) | ||
| 395 | throws IllegalArgumentException, | ||
| 396 | RoleNotFoundException { | ||
| 397 | |||
| 398 | if (roleName == null) { | ||
| 399 | String excMsg = "Invalid parameter."; | ||
| 400 | throw new IllegalArgumentException(excMsg); | ||
| 401 | } | ||
| 402 | |||
| 403 | RELATION_LOGGER.entering(RelationSupport.class.getName(), | ||
| 404 | "getRoleCardinality", roleName); | ||
| 405 | |||
| 406 | // Try to retrieve the role | ||
| 407 | Role role; | ||
| 408 | synchronized(myRoleName2ValueMap) { | ||
| 409 | // No null Role is allowed, so direct use of get() | ||
| 410 | role = (myRoleName2ValueMap.get(roleName)); | ||
| 411 | } | ||
| 412 | if (role == null) { | ||
| 413 | int pbType = RoleStatus.NO_ROLE_WITH_NAME; | ||
| 414 | // Will throw a RoleNotFoundException | ||
| 415 | // | ||
| 416 | // Will not throw InvalidRoleValueException, so catch it for the | ||
| 417 | // compiler | ||
| 418 | try { | ||
| 419 | RelationService.throwRoleProblemException(pbType, | ||
| 420 | roleName); | ||
| 421 | } catch (InvalidRoleValueException exc) { | ||
| 422 | // OK : Do not throw InvalidRoleValueException as | ||
| 423 | // a RoleNotFoundException will be thrown. | ||
| 424 | } | ||
| 425 | } | ||
| 426 | |||
| 427 | List<ObjectName> roleValue = role.getRoleValue(); | ||
| 428 | |||
| 429 | RELATION_LOGGER.exiting(RelationSupport.class.getName(), | ||
| 430 | "getRoleCardinality"); | ||
| 431 | return roleValue.size(); | ||
| 432 | } | ||
| 433 | |||
| 434 | /** | ||
| 435 | * Sets the given role. | ||
| 436 | * <P>Will check the role according to its corresponding role definition | ||
| 437 | * provided in relation's relation type | ||
| 438 | * <P>Will send a notification (RelationNotification with type | ||
| 439 | * RELATION_BASIC_UPDATE or RELATION_MBEAN_UPDATE, depending if the | ||
| 440 | * relation is a MBean or not). | ||
| 441 | * | ||
| 442 | * @param role role to be set (name and new value) | ||
| 443 | * | ||
| 444 | * @exception IllegalArgumentException if null role | ||
| 445 | * @exception RoleNotFoundException if there is no role with the supplied | ||
| 446 | * role's name or if the role is not writable (no test on the write access | ||
| 447 | * mode performed when initializing the role) | ||
| 448 | * @exception InvalidRoleValueException if value provided for | ||
| 449 | * role is not valid, i.e.: | ||
| 450 | * <P>- the number of referenced MBeans in given value is less than | ||
| 451 | * expected minimum degree | ||
| 452 | * <P>- the number of referenced MBeans in provided value exceeds expected | ||
| 453 | * maximum degree | ||
| 454 | * <P>- one referenced MBean in the value is not an Object of the MBean | ||
| 455 | * class expected for that role | ||
| 456 | * <P>- a MBean provided for that role does not exist | ||
| 457 | * @exception RelationServiceNotRegisteredException if the Relation | ||
| 458 | * Service is not registered in the MBean Server | ||
| 459 | * @exception RelationTypeNotFoundException if the relation type has not | ||
| 460 | * been declared in the Relation Service | ||
| 461 | * @exception RelationNotFoundException if the relation has not been | ||
| 462 | * added in the Relation Service. | ||
| 463 | * | ||
| 464 | * @see #getRole | ||
| 465 | */ | ||
| 466 | public void setRole(Role role) | ||
| 467 | throws IllegalArgumentException, | ||
| 468 | RoleNotFoundException, | ||
| 469 | RelationTypeNotFoundException, | ||
| 470 | InvalidRoleValueException, | ||
| 471 | RelationServiceNotRegisteredException, | ||
| 472 | RelationNotFoundException { | ||
| 473 | |||
| 474 | if (role == null) { | ||
| 475 | String excMsg = "Invalid parameter."; | ||
| 476 | throw new IllegalArgumentException(excMsg); | ||
| 477 | } | ||
| 478 | |||
| 479 | RELATION_LOGGER.entering(RelationSupport.class.getName(), | ||
| 480 | "setRole", role); | ||
| 481 | |||
| 482 | // Will return null :) | ||
| 483 | Object result = setRoleInt(role, false, null, false); | ||
| 484 | |||
| 485 | RELATION_LOGGER.exiting(RelationSupport.class.getName(), "setRole"); | ||
| 486 | return; | ||
| 487 | } | ||
| 488 | |||
| 489 | /** | ||
| 490 | * Sets the given roles. | ||
| 491 | * <P>Will check the role according to its corresponding role definition | ||
| 492 | * provided in relation's relation type | ||
| 493 | * <P>Will send one notification (RelationNotification with type | ||
| 494 | * RELATION_BASIC_UPDATE or RELATION_MBEAN_UPDATE, depending if the | ||
| 495 | * relation is a MBean or not) per updated role. | ||
| 496 | * | ||
| 497 | * @param list list of roles to be set | ||
| 498 | * | ||
| 499 | * @return a RoleResult object, including a RoleList (for roles | ||
| 500 | * successfully set) and a RoleUnresolvedList (for roles not | ||
| 501 | * set). | ||
| 502 | * | ||
| 503 | * @exception IllegalArgumentException if null role list | ||
| 504 | * @exception RelationServiceNotRegisteredException if the Relation | ||
| 505 | * Service is not registered in the MBean Server | ||
| 506 | * @exception RelationTypeNotFoundException if the relation type has not | ||
| 507 | * been declared in the Relation Service. | ||
| 508 | * @exception RelationNotFoundException if the relation MBean has not been | ||
| 509 | * added in the Relation Service. | ||
| 510 | * | ||
| 511 | * @see #getRoles | ||
| 512 | */ | ||
| 513 | public RoleResult setRoles(RoleList list) | ||
| 514 | throws IllegalArgumentException, | ||
| 515 | RelationServiceNotRegisteredException, | ||
| 516 | RelationTypeNotFoundException, | ||
| 517 | RelationNotFoundException { | ||
| 518 | |||
| 519 | if (list == null) { | ||
| 520 | String excMsg = "Invalid parameter."; | ||
| 521 | throw new IllegalArgumentException(excMsg); | ||
| 522 | } | ||
| 523 | |||
| 524 | RELATION_LOGGER.entering(RelationSupport.class.getName(), | ||
| 525 | "setRoles", list); | ||
| 526 | |||
| 527 | RoleResult result = setRolesInt(list, false, null); | ||
| 528 | |||
| 529 | RELATION_LOGGER.exiting(RelationSupport.class.getName(), "setRoles"); | ||
| 530 | return result; | ||
| 531 | } | ||
| 532 | |||
| 533 | /** | ||
| 534 | * Callback used by the Relation Service when a MBean referenced in a role | ||
| 535 | * is unregistered. | ||
| 536 | * <P>The Relation Service will call this method to let the relation | ||
| 537 | * take action to reflect the impact of such unregistration. | ||
| 538 | * <P>BEWARE. the user is not expected to call this method. | ||
| 539 | * <P>Current implementation is to set the role with its current value | ||
| 540 | * (list of ObjectNames of referenced MBeans) without the unregistered | ||
| 541 | * one. | ||
| 542 | * | ||
| 543 | * @param objectName ObjectName of unregistered MBean | ||
| 544 | * @param roleName name of role where the MBean is referenced | ||
| 545 | * | ||
| 546 | * @exception IllegalArgumentException if null parameter | ||
| 547 | * @exception RoleNotFoundException if role does not exist in the | ||
| 548 | * relation or is not writable | ||
| 549 | * @exception InvalidRoleValueException if role value does not conform to | ||
| 550 | * the associated role info (this will never happen when called from the | ||
| 551 | * Relation Service) | ||
| 552 | * @exception RelationServiceNotRegisteredException if the Relation | ||
| 553 | * Service is not registered in the MBean Server | ||
| 554 | * @exception RelationTypeNotFoundException if the relation type has not | ||
| 555 | * been declared in the Relation Service. | ||
| 556 | * @exception RelationNotFoundException if this method is called for a | ||
| 557 | * relation MBean not added in the Relation Service. | ||
| 558 | */ | ||
| 559 | public void handleMBeanUnregistration(ObjectName objectName, | ||
| 560 | String roleName) | ||
| 561 | throws IllegalArgumentException, | ||
| 562 | RoleNotFoundException, | ||
| 563 | InvalidRoleValueException, | ||
| 564 | RelationServiceNotRegisteredException, | ||
| 565 | RelationTypeNotFoundException, | ||
| 566 | RelationNotFoundException { | ||
| 567 | |||
| 568 | if (objectName == null || roleName == null) { | ||
| 569 | String excMsg = "Invalid parameter."; | ||
| 570 | throw new IllegalArgumentException(excMsg); | ||
| 571 | } | ||
| 572 | |||
| 573 | RELATION_LOGGER.entering(RelationSupport.class.getName(), | ||
| 574 | "handleMBeanUnregistration", | ||
| 575 | new Object[]{objectName, roleName}); | ||
| 576 | |||
| 577 | // Can throw RoleNotFoundException, InvalidRoleValueException, | ||
| 578 | // or RelationTypeNotFoundException | ||
| 579 | handleMBeanUnregistrationInt(objectName, | ||
| 580 | roleName, | ||
| 581 | false, | ||
| 582 | null); | ||
| 583 | |||
| 584 | RELATION_LOGGER.exiting(RelationSupport.class.getName(), | ||
| 585 | "handleMBeanUnregistration"); | ||
| 586 | return; | ||
| 587 | } | ||
| 588 | |||
| 589 | /** | ||
| 590 | * Retrieves MBeans referenced in the various roles of the relation. | ||
| 591 | * | ||
| 592 | * @return a HashMap mapping: | ||
| 593 | * <P> ObjectName -> ArrayList of String (role names) | ||
| 594 | */ | ||
| 595 | public Map<ObjectName,List<String>> getReferencedMBeans() { | ||
| 596 | |||
| 597 | RELATION_LOGGER.entering(RelationSupport.class.getName(), | ||
| 598 | "getReferencedMBeans"); | ||
| 599 | |||
| 600 | Map<ObjectName,List<String>> refMBeanMap = | ||
| 601 | new HashMap<ObjectName,List<String>>(); | ||
| 602 | |||
| 603 | synchronized(myRoleName2ValueMap) { | ||
| 604 | |||
| 605 | for (Role currRole : myRoleName2ValueMap.values()) { | ||
| 606 | |||
| 607 | String currRoleName = currRole.getRoleName(); | ||
| 608 | // Retrieves ObjectNames of MBeans referenced in current role | ||
| 609 | List<ObjectName> currRefMBeanList = currRole.getRoleValue(); | ||
| 610 | |||
| 611 | for (ObjectName currRoleObjName : currRefMBeanList) { | ||
| 612 | |||
| 613 | // Sees if current MBean has been already referenced in | ||
| 614 | // roles already seen | ||
| 615 | List<String> mbeanRoleNameList = | ||
| 616 | refMBeanMap.get(currRoleObjName); | ||
| 617 | |||
| 618 | boolean newRefFlg = false; | ||
| 619 | if (mbeanRoleNameList == null) { | ||
| 620 | newRefFlg = true; | ||
| 621 | mbeanRoleNameList = new ArrayList<String>(); | ||
| 622 | } | ||
| 623 | mbeanRoleNameList.add(currRoleName); | ||
| 624 | if (newRefFlg) { | ||
| 625 | refMBeanMap.put(currRoleObjName, mbeanRoleNameList); | ||
| 626 | } | ||
| 627 | } | ||
| 628 | } | ||
| 629 | } | ||
| 630 | |||
| 631 | RELATION_LOGGER.exiting(RelationSupport.class.getName(), | ||
| 632 | "getReferencedMBeans"); | ||
| 633 | return refMBeanMap; | ||
| 634 | } | ||
| 635 | |||
| 636 | /** | ||
| 637 | * Returns name of associated relation type. | ||
| 638 | */ | ||
| 639 | public String getRelationTypeName() { | ||
| 640 | return myRelTypeName; | ||
| 641 | } | ||
| 642 | |||
| 643 | /** | ||
| 644 | * Returns ObjectName of the Relation Service handling the relation. | ||
| 645 | * | ||
| 646 | * @return the ObjectName of the Relation Service. | ||
| 647 | */ | ||
| 648 | public ObjectName getRelationServiceName() { | ||
| 649 | return myRelServiceName; | ||
| 650 | } | ||
| 651 | |||
| 652 | /** | ||
| 653 | * Returns relation identifier (used to uniquely identify the relation | ||
| 654 | * inside the Relation Service). | ||
| 655 | * | ||
| 656 | * @return the relation id. | ||
| 657 | */ | ||
| 658 | public String getRelationId() { | ||
| 659 | return myRelId; | ||
| 660 | } | ||
| 661 | |||
| 662 | // | ||
| 663 | // MBeanRegistration interface | ||
| 664 | // | ||
| 665 | |||
| 666 | // Pre-registration: retrieves the MBean Server (useful to access to the | ||
| 667 | // Relation Service) | ||
| 668 | // This is the way to retrieve the MBean Server when the relation object is | ||
| 669 | // a MBean created by the user outside of the Relation Service. | ||
| 670 | // | ||
| 671 | // No exception thrown. | ||
| 672 | public ObjectName preRegister(MBeanServer server, | ||
| 673 | ObjectName name) | ||
| 674 | throws Exception { | ||
| 675 | |||
| 676 | myRelServiceMBeanServer = server; | ||
| 677 | return name; | ||
| 678 | } | ||
| 679 | |||
| 680 | // Post-registration: does nothing | ||
| 681 | public void postRegister(Boolean registrationDone) { | ||
| 682 | return; | ||
| 683 | } | ||
| 684 | |||
| 685 | // Pre-unregistration: does nothing | ||
| 686 | public void preDeregister() | ||
| 687 | throws Exception { | ||
| 688 | return; | ||
| 689 | } | ||
| 690 | |||
| 691 | // Post-unregistration: does nothing | ||
| 692 | public void postDeregister() { | ||
| 693 | return; | ||
| 694 | } | ||
| 695 | |||
| 696 | // | ||
| 697 | // Others | ||
| 698 | // | ||
| 699 | |||
| 700 | /** | ||
| 701 | * Returns an internal flag specifying if the object is still handled by | ||
| 702 | * the Relation Service. | ||
| 703 | */ | ||
| 704 | public Boolean isInRelationService() { | ||
| 705 | return myInRelServFlg.get(); | ||
| 706 | } | ||
| 707 | |||
| 708 | public void setRelationServiceManagementFlag(Boolean flag) | ||
| 709 | throws IllegalArgumentException { | ||
| 710 | |||
| 711 | if (flag == null) { | ||
| 712 | String excMsg = "Invalid parameter."; | ||
| 713 | throw new IllegalArgumentException(excMsg); | ||
| 714 | } | ||
| 715 | myInRelServFlg.set(flag); | ||
| 716 | } | ||
| 717 | |||
| 718 | // | ||
| 719 | // Misc | ||
| 720 | // | ||
| 721 | |||
| 722 | // Gets the role with given name | ||
| 723 | // Checks if the role exists and is readable according to the relation | ||
| 724 | // type. | ||
| 725 | // | ||
| 726 | // This method is called in getRole() above. | ||
| 727 | // It is also called in the Relation Service getRole() method. | ||
| 728 | // It is also called in getRolesInt() below (used for getRoles() above | ||
| 729 | // and for Relation Service getRoles() method). | ||
| 730 | // | ||
| 731 | // Depending on parameters reflecting its use (either in the scope of | ||
| 732 | // getting a single role or of getting several roles), will return: | ||
| 733 | // - in case of success: | ||
| 734 | // - for single role retrieval, the ArrayList of ObjectNames being the | ||
| 735 | // role value | ||
| 736 | // - for multi-role retrieval, the Role object itself | ||
| 737 | // - in case of failure (except critical exceptions): | ||
| 738 | // - for single role retrieval, if role does not exist or is not | ||
| 739 | // readable, an RoleNotFoundException exception is raised | ||
| 740 | // - for multi-role retrieval, a RoleUnresolved object | ||
| 741 | // | ||
| 742 | // -param roleName name of role to be retrieved | ||
| 743 | // -param relationServCallFlg true if call from the Relation Service; this | ||
| 744 | // will happen if the current RelationSupport object has been created by | ||
| 745 | // the Relation Service (via createRelation()) method, so direct access is | ||
| 746 | // possible. | ||
| 747 | // -param relationServ reference to Relation Service object, if object | ||
| 748 | // created by Relation Service. | ||
| 749 | // -param multiRoleFlg true if getting the role in the scope of a | ||
| 750 | // multiple retrieval. | ||
| 751 | // | ||
| 752 | // -return: | ||
| 753 | // - for single role retrieval (multiRoleFlg false): | ||
| 754 | // - ArrayList of ObjectName objects, value of role with given name, if | ||
| 755 | // the role can be retrieved | ||
| 756 | // - raise a RoleNotFoundException exception else | ||
| 757 | // - for multi-role retrieval (multiRoleFlg true): | ||
| 758 | // - the Role object for given role name if role can be retrieved | ||
| 759 | // - a RoleUnresolved object with problem. | ||
| 760 | // | ||
| 761 | // -exception IllegalArgumentException if null parameter | ||
| 762 | // -exception RoleNotFoundException if multiRoleFlg is false and: | ||
| 763 | // - there is no role with given name | ||
| 764 | // or | ||
| 765 | // - the role is not readable. | ||
| 766 | // -exception RelationServiceNotRegisteredException if the Relation | ||
| 767 | // Service is not registered in the MBean Server | ||
| 768 | Object getRoleInt(String roleName, | ||
| 769 | boolean relationServCallFlg, | ||
| 770 | RelationService relationServ, | ||
| 771 | boolean multiRoleFlg) | ||
| 772 | throws IllegalArgumentException, | ||
| 773 | RoleNotFoundException, | ||
| 774 | RelationServiceNotRegisteredException { | ||
| 775 | |||
| 776 | if (roleName == null || | ||
| 777 | (relationServCallFlg && relationServ == null)) { | ||
| 778 | String excMsg = "Invalid parameter."; | ||
| 779 | throw new IllegalArgumentException(excMsg); | ||
| 780 | } | ||
| 781 | |||
| 782 | RELATION_LOGGER.entering(RelationSupport.class.getName(), | ||
| 783 | "getRoleInt", roleName); | ||
| 784 | |||
| 785 | int pbType = 0; | ||
| 786 | |||
| 787 | Role role; | ||
| 788 | synchronized(myRoleName2ValueMap) { | ||
| 789 | // No null Role is allowed, so direct use of get() | ||
| 790 | role = (myRoleName2ValueMap.get(roleName)); | ||
| 791 | } | ||
| 792 | |||
| 793 | if (role == null) { | ||
| 794 | pbType = RoleStatus.NO_ROLE_WITH_NAME; | ||
| 795 | |||
| 796 | } else { | ||
| 797 | // Checks if the role is readable | ||
| 798 | Integer status; | ||
| 799 | |||
| 800 | if (relationServCallFlg) { | ||
| 801 | |||
| 802 | // Call from the Relation Service, so direct access to it, | ||
| 803 | // avoiding MBean Server | ||
| 804 | // Shall not throw a RelationTypeNotFoundException | ||
| 805 | try { | ||
| 806 | status = relationServ.checkRoleReading(roleName, | ||
| 807 | myRelTypeName); | ||
| 808 | } catch (RelationTypeNotFoundException exc) { | ||
| 809 | throw new RuntimeException(exc.getMessage()); | ||
| 810 | } | ||
| 811 | |||
| 812 | } else { | ||
| 813 | |||
| 814 | // Call from getRole() method above | ||
| 815 | // So we have a MBean. We must access the Relation Service | ||
| 816 | // via the MBean Server. | ||
| 817 | Object[] params = new Object[2]; | ||
| 818 | params[0] = roleName; | ||
| 819 | params[1] = myRelTypeName; | ||
| 820 | String[] signature = new String[2]; | ||
| 821 | signature[0] = "java.lang.String"; | ||
| 822 | signature[1] = "java.lang.String"; | ||
| 823 | // Can throw InstanceNotFoundException if the Relation | ||
| 824 | // Service is not registered (to be catched in any case and | ||
| 825 | // transformed into RelationServiceNotRegisteredException). | ||
| 826 | // | ||
| 827 | // Shall not throw a MBeanException, or a ReflectionException | ||
| 828 | // or an InstanceNotFoundException | ||
| 829 | try { | ||
| 830 | status = (Integer) | ||
| 831 | (myRelServiceMBeanServer.invoke(myRelServiceName, | ||
| 832 | "checkRoleReading", | ||
| 833 | params, | ||
| 834 | signature)); | ||
| 835 | } catch (MBeanException exc1) { | ||
| 836 | throw new RuntimeException("incorrect relation type"); | ||
| 837 | } catch (ReflectionException exc2) { | ||
| 838 | throw new RuntimeException(exc2.getMessage()); | ||
| 839 | } catch (InstanceNotFoundException exc3) { | ||
| 840 | throw new RelationServiceNotRegisteredException( | ||
| 841 | exc3.getMessage()); | ||
| 842 | } | ||
| 843 | } | ||
| 844 | |||
| 845 | pbType = status.intValue(); | ||
| 846 | } | ||
| 847 | |||
| 848 | Object result; | ||
| 849 | |||
| 850 | if (pbType == 0) { | ||
| 851 | // Role can be retrieved | ||
| 852 | |||
| 853 | if (!(multiRoleFlg)) { | ||
| 854 | // Single role retrieved: returns its value | ||
| 855 | // Note: no need to test if role value (list) not null before | ||
| 856 | // cloning, null value not allowed, empty list if | ||
| 857 | // nothing. | ||
| 858 | result = new ArrayList<ObjectName>(role.getRoleValue()); | ||
| 859 | |||
| 860 | } else { | ||
| 861 | // Role retrieved during multi-role retrieval: returns the | ||
| 862 | // role | ||
| 863 | result = (Role)(role.clone()); | ||
| 864 | } | ||
| 865 | |||
| 866 | } else { | ||
| 867 | // Role not retrieved | ||
| 868 | |||
| 869 | if (!(multiRoleFlg)) { | ||
| 870 | // Problem when retrieving a simple role: either role not | ||
| 871 | // found or not readable, so raises a RoleNotFoundException. | ||
| 872 | try { | ||
| 873 | RelationService.throwRoleProblemException(pbType, | ||
| 874 | roleName); | ||
| 875 | // To keep compiler happy :) | ||
| 876 | return null; | ||
| 877 | } catch (InvalidRoleValueException exc) { | ||
| 878 | throw new RuntimeException(exc.getMessage()); | ||
| 879 | } | ||
| 880 | |||
| 881 | } else { | ||
| 882 | // Problem when retrieving a role in a multi-role retrieval: | ||
| 883 | // returns a RoleUnresolved object | ||
| 884 | result = new RoleUnresolved(roleName, null, pbType); | ||
| 885 | } | ||
| 886 | } | ||
| 887 | |||
| 888 | RELATION_LOGGER.exiting(RelationSupport.class.getName(), "getRoleInt"); | ||
| 889 | return result; | ||
| 890 | } | ||
| 891 | |||
| 892 | // Gets the given roles | ||
| 893 | // For each role, verifies if the role exists and is readable according to | ||
| 894 | // the relation type. | ||
| 895 | // | ||
| 896 | // This method is called in getRoles() above and in Relation Service | ||
| 897 | // getRoles() method. | ||
| 898 | // | ||
| 899 | // -param roleNameArray array of names of roles to be retrieved | ||
| 900 | // -param relationServCallFlg true if call from the Relation Service; this | ||
| 901 | // will happen if the current RelationSupport object has been created by | ||
| 902 | // the Relation Service (via createRelation()) method, so direct access is | ||
| 903 | // possible. | ||
| 904 | // -param relationServ reference to Relation Service object, if object | ||
| 905 | // created by Relation Service. | ||
| 906 | // | ||
| 907 | // -return a RoleResult object | ||
| 908 | // | ||
| 909 | // -exception IllegalArgumentException if null parameter | ||
| 910 | // -exception RelationServiceNotRegisteredException if the Relation | ||
| 911 | // Service is not registered in the MBean Server | ||
| 912 | RoleResult getRolesInt(String[] roleNameArray, | ||
| 913 | boolean relationServCallFlg, | ||
| 914 | RelationService relationServ) | ||
| 915 | throws IllegalArgumentException, | ||
| 916 | RelationServiceNotRegisteredException { | ||
| 917 | |||
| 918 | if (roleNameArray == null || | ||
| 919 | (relationServCallFlg && relationServ == null)) { | ||
| 920 | String excMsg = "Invalid parameter."; | ||
| 921 | throw new IllegalArgumentException(excMsg); | ||
| 922 | } | ||
| 923 | |||
| 924 | RELATION_LOGGER.entering(RelationSupport.class.getName(), | ||
| 925 | "getRolesInt"); | ||
| 926 | |||
| 927 | RoleList roleList = new RoleList(); | ||
| 928 | RoleUnresolvedList roleUnresList = new RoleUnresolvedList(); | ||
| 929 | |||
| 930 | for (int i = 0; i < roleNameArray.length; i++) { | ||
| 931 | String currRoleName = roleNameArray[i]; | ||
| 932 | |||
| 933 | Object currResult; | ||
| 934 | |||
| 935 | // Can throw RelationServiceNotRegisteredException | ||
| 936 | // | ||
| 937 | // RoleNotFoundException: not possible but catch it for compiler :) | ||
| 938 | try { | ||
| 939 | currResult = getRoleInt(currRoleName, | ||
| 940 | relationServCallFlg, | ||
| 941 | relationServ, | ||
| 942 | true); | ||
| 943 | |||
| 944 | } catch (RoleNotFoundException exc) { | ||
| 945 | return null; // :) | ||
| 946 | } | ||
| 947 | |||
| 948 | if (currResult instanceof Role) { | ||
| 949 | // Can throw IllegalArgumentException if role is null | ||
| 950 | // (normally should not happen :( | ||
| 951 | try { | ||
| 952 | roleList.add((Role)currResult); | ||
| 953 | } catch (IllegalArgumentException exc) { | ||
| 954 | throw new RuntimeException(exc.getMessage()); | ||
| 955 | } | ||
| 956 | |||
| 957 | } else if (currResult instanceof RoleUnresolved) { | ||
| 958 | // Can throw IllegalArgumentException if role is null | ||
| 959 | // (normally should not happen :( | ||
| 960 | try { | ||
| 961 | roleUnresList.add((RoleUnresolved)currResult); | ||
| 962 | } catch (IllegalArgumentException exc) { | ||
| 963 | throw new RuntimeException(exc.getMessage()); | ||
| 964 | } | ||
| 965 | } | ||
| 966 | } | ||
| 967 | |||
| 968 | RoleResult result = new RoleResult(roleList, roleUnresList); | ||
| 969 | RELATION_LOGGER.exiting(RelationSupport.class.getName(), | ||
| 970 | "getRolesInt"); | ||
| 971 | return result; | ||
| 972 | } | ||
| 973 | |||
| 974 | // Returns all roles present in the relation | ||
| 975 | // | ||
| 976 | // -return a RoleResult object, including a RoleList (for roles | ||
| 977 | // successfully retrieved) and a RoleUnresolvedList (for roles not | ||
| 978 | // readable). | ||
| 979 | // | ||
| 980 | // -exception IllegalArgumentException if null parameter | ||
| 981 | // -exception RelationServiceNotRegisteredException if the Relation | ||
| 982 | // Service is not registered in the MBean Server | ||
| 983 | // | ||
| 984 | RoleResult getAllRolesInt(boolean relationServCallFlg, | ||
| 985 | RelationService relationServ) | ||
| 986 | throws IllegalArgumentException, | ||
| 987 | RelationServiceNotRegisteredException { | ||
| 988 | |||
| 989 | if (relationServCallFlg && relationServ == null) { | ||
| 990 | String excMsg = "Invalid parameter."; | ||
| 991 | throw new IllegalArgumentException(excMsg); | ||
| 992 | } | ||
| 993 | |||
| 994 | RELATION_LOGGER.entering(RelationSupport.class.getName(), | ||
| 995 | "getAllRolesInt"); | ||
| 996 | |||
| 997 | List<String> roleNameList; | ||
| 998 | synchronized(myRoleName2ValueMap) { | ||
| 999 | roleNameList = | ||
| 1000 | new ArrayList<String>(myRoleName2ValueMap.keySet()); | ||
| 1001 | } | ||
| 1002 | String[] roleNames = new String[roleNameList.size()]; | ||
| 1003 | roleNameList.toArray(roleNames); | ||
| 1004 | |||
| 1005 | RoleResult result = getRolesInt(roleNames, | ||
| 1006 | relationServCallFlg, | ||
| 1007 | relationServ); | ||
| 1008 | |||
| 1009 | RELATION_LOGGER.exiting(RelationSupport.class.getName(), | ||
| 1010 | "getAllRolesInt"); | ||
| 1011 | return result; | ||
| 1012 | } | ||
| 1013 | |||
| 1014 | // Sets the role with given value | ||
| 1015 | // | ||
| 1016 | // This method is called in setRole() above. | ||
| 1017 | // It is also called by the Relation Service setRole() method. | ||
| 1018 | // It is also called in setRolesInt() method below (used in setRoles() | ||
| 1019 | // above and in RelationService setRoles() method). | ||
| 1020 | // | ||
| 1021 | // Will check the role according to its corresponding role definition | ||
| 1022 | // provided in relation's relation type | ||
| 1023 | // Will send a notification (RelationNotification with type | ||
| 1024 | // RELATION_BASIC_UPDATE or RELATION_MBEAN_UPDATE, depending if the | ||
| 1025 | // relation is a MBean or not) if not initialization of role. | ||
| 1026 | // | ||
| 1027 | // -param aRole role to be set (name and new value) | ||
| 1028 | // -param relationServCallFlg true if call from the Relation Service; this | ||
| 1029 | // will happen if the current RelationSupport object has been created by | ||
| 1030 | // the Relation Service (via createRelation()) method, so direct access is | ||
| 1031 | // possible. | ||
| 1032 | // -param relationServ reference to Relation Service object, if internal | ||
| 1033 | // relation | ||
| 1034 | // -param multiRoleFlg true if getting the role in the scope of a | ||
| 1035 | // multiple retrieval. | ||
| 1036 | // | ||
| 1037 | // -return (except other "critical" exceptions): | ||
| 1038 | // - for single role retrieval (multiRoleFlg false): | ||
| 1039 | // - null if the role has been set | ||
| 1040 | // - raise an InvalidRoleValueException | ||
| 1041 | // else | ||
| 1042 | // - for multi-role retrieval (multiRoleFlg true): | ||
| 1043 | // - the Role object for given role name if role has been set | ||
| 1044 | // - a RoleUnresolved object with problem else. | ||
| 1045 | // | ||
| 1046 | // -exception IllegalArgumentException if null parameter | ||
| 1047 | // -exception RoleNotFoundException if multiRoleFlg is false and: | ||
| 1048 | // - internal relation and the role does not exist | ||
| 1049 | // or | ||
| 1050 | // - existing role (i.e. not initializing it) and the role is not | ||
| 1051 | // writable. | ||
| 1052 | // -exception InvalidRoleValueException ifmultiRoleFlg is false and | ||
| 1053 | // value provided for: | ||
| 1054 | // - the number of referenced MBeans in given value is less than | ||
| 1055 | // expected minimum degree | ||
| 1056 | // or | ||
| 1057 | // - the number of referenced MBeans in provided value exceeds expected | ||
| 1058 | // maximum degree | ||
| 1059 | // or | ||
| 1060 | // - one referenced MBean in the value is not an Object of the MBean | ||
| 1061 | // class expected for that role | ||
| 1062 | // or | ||
| 1063 | // - a MBean provided for that role does not exist | ||
| 1064 | // -exception RelationServiceNotRegisteredException if the Relation | ||
| 1065 | // Service is not registered in the MBean Server | ||
| 1066 | // -exception RelationTypeNotFoundException if relation type unknown | ||
| 1067 | // -exception RelationNotFoundException if a relation MBean has not been | ||
| 1068 | // added in the Relation Service | ||
| 1069 | Object setRoleInt(Role aRole, | ||
| 1070 | boolean relationServCallFlg, | ||
| 1071 | RelationService relationServ, | ||
| 1072 | boolean multiRoleFlg) | ||
| 1073 | throws IllegalArgumentException, | ||
| 1074 | RoleNotFoundException, | ||
| 1075 | InvalidRoleValueException, | ||
| 1076 | RelationServiceNotRegisteredException, | ||
| 1077 | RelationTypeNotFoundException, | ||
| 1078 | RelationNotFoundException { | ||
| 1079 | |||
| 1080 | if (aRole == null || | ||
| 1081 | (relationServCallFlg && relationServ == null)) { | ||
| 1082 | String excMsg = "Invalid parameter."; | ||
| 1083 | throw new IllegalArgumentException(excMsg); | ||
| 1084 | } | ||
| 1085 | |||
| 1086 | RELATION_LOGGER.entering(RelationSupport.class.getName(), | ||
| 1087 | "setRoleInt", new Object[] {aRole, relationServCallFlg, | ||
| 1088 | relationServ, multiRoleFlg}); | ||
| 1089 | |||
| 1090 | String roleName = aRole.getRoleName(); | ||
| 1091 | int pbType = 0; | ||
| 1092 | |||
| 1093 | // Checks if role exists in the relation | ||
| 1094 | // No error if the role does not exist in the relation, to be able to | ||
| 1095 | // handle initialization of role when creating the relation | ||
| 1096 | // (roles provided in the RoleList parameter are directly set but | ||
| 1097 | // roles automatically initialized are set using setRole()) | ||
| 1098 | Role role; | ||
| 1099 | synchronized(myRoleName2ValueMap) { | ||
| 1100 | role = (myRoleName2ValueMap.get(roleName)); | ||
| 1101 | } | ||
| 1102 | |||
| 1103 | List<ObjectName> oldRoleValue; | ||
| 1104 | Boolean initFlg; | ||
| 1105 | |||
| 1106 | if (role == null) { | ||
| 1107 | initFlg = true; | ||
| 1108 | oldRoleValue = new ArrayList<ObjectName>(); | ||
| 1109 | |||
| 1110 | } else { | ||
| 1111 | initFlg = false; | ||
| 1112 | oldRoleValue = role.getRoleValue(); | ||
| 1113 | } | ||
| 1114 | |||
| 1115 | // Checks if the role can be set: is writable (except if | ||
| 1116 | // initialization) and correct value | ||
| 1117 | try { | ||
| 1118 | Integer status; | ||
| 1119 | |||
| 1120 | if (relationServCallFlg) { | ||
| 1121 | |||
| 1122 | // Call from the Relation Service, so direct access to it, | ||
| 1123 | // avoiding MBean Server | ||
| 1124 | // | ||
| 1125 | // Shall not raise a RelationTypeNotFoundException | ||
| 1126 | status = relationServ.checkRoleWriting(aRole, | ||
| 1127 | myRelTypeName, | ||
| 1128 | initFlg); | ||
| 1129 | |||
| 1130 | } else { | ||
| 1131 | |||
| 1132 | // Call from setRole() method above | ||
| 1133 | // So we have a MBean. We must access the Relation Service | ||
| 1134 | // via the MBean Server. | ||
| 1135 | Object[] params = new Object[3]; | ||
| 1136 | params[0] = aRole; | ||
| 1137 | params[1] = myRelTypeName; | ||
| 1138 | params[2] = initFlg; | ||
| 1139 | String[] signature = new String[3]; | ||
| 1140 | signature[0] = "javax.management.relation.Role"; | ||
| 1141 | signature[1] = "java.lang.String"; | ||
| 1142 | signature[2] = "java.lang.Boolean"; | ||
| 1143 | // Can throw InstanceNotFoundException if the Relation Service | ||
| 1144 | // is not registered (to be transformed into | ||
| 1145 | // RelationServiceNotRegisteredException in any case). | ||
| 1146 | // | ||
| 1147 | // Can throw a MBeanException wrapping a | ||
| 1148 | // RelationTypeNotFoundException: | ||
| 1149 | // throw wrapped exception. | ||
| 1150 | // | ||
| 1151 | // Shall not throw a ReflectionException | ||
| 1152 | status = (Integer) | ||
| 1153 | (myRelServiceMBeanServer.invoke(myRelServiceName, | ||
| 1154 | "checkRoleWriting", | ||
| 1155 | params, | ||
| 1156 | signature)); | ||
| 1157 | } | ||
| 1158 | |||
| 1159 | pbType = status.intValue(); | ||
| 1160 | |||
| 1161 | } catch (MBeanException exc2) { | ||
| 1162 | |||
| 1163 | // Retrieves underlying exception | ||
| 1164 | Exception wrappedExc = exc2.getTargetException(); | ||
| 1165 | if (wrappedExc instanceof RelationTypeNotFoundException) { | ||
| 1166 | throw ((RelationTypeNotFoundException)wrappedExc); | ||
| 1167 | |||
| 1168 | } else { | ||
| 1169 | throw new RuntimeException(wrappedExc.getMessage()); | ||
| 1170 | } | ||
| 1171 | |||
| 1172 | } catch (ReflectionException exc3) { | ||
| 1173 | throw new RuntimeException(exc3.getMessage()); | ||
| 1174 | |||
| 1175 | } catch (RelationTypeNotFoundException exc4) { | ||
| 1176 | throw new RuntimeException(exc4.getMessage()); | ||
| 1177 | |||
| 1178 | } catch (InstanceNotFoundException exc5) { | ||
| 1179 | throw new RelationServiceNotRegisteredException(exc5.getMessage()); | ||
| 1180 | } | ||
| 1181 | |||
| 1182 | Object result = null; | ||
| 1183 | |||
| 1184 | if (pbType == 0) { | ||
| 1185 | // Role can be set | ||
| 1186 | if (!(initFlg.booleanValue())) { | ||
| 1187 | |||
| 1188 | // Not initializing the role | ||
| 1189 | // If role being initialized: | ||
| 1190 | // - do not send an update notification | ||
| 1191 | // - do not try to update internal map of Relation Service | ||
| 1192 | // listing referenced MBeans, as role is initialized to an | ||
| 1193 | // empty list | ||
| 1194 | |||
| 1195 | // Sends a notification (RelationNotification) | ||
| 1196 | // Can throw a RelationNotFoundException | ||
| 1197 | sendRoleUpdateNotification(aRole, | ||
| 1198 | oldRoleValue, | ||
| 1199 | relationServCallFlg, | ||
| 1200 | relationServ); | ||
| 1201 | |||
| 1202 | // Updates the role map of the Relation Service | ||
| 1203 | // Can throw RelationNotFoundException | ||
| 1204 | updateRelationServiceMap(aRole, | ||
| 1205 | oldRoleValue, | ||
| 1206 | relationServCallFlg, | ||
| 1207 | relationServ); | ||
| 1208 | |||
| 1209 | } | ||
| 1210 | |||
| 1211 | // Sets the role | ||
| 1212 | synchronized(myRoleName2ValueMap) { | ||
| 1213 | myRoleName2ValueMap.put(roleName, | ||
| 1214 | (Role)(aRole.clone())); | ||
| 1215 | } | ||
| 1216 | |||
| 1217 | // Single role set: returns null: nothing to set in result | ||
| 1218 | |||
| 1219 | if (multiRoleFlg) { | ||
| 1220 | // Multi-roles retrieval: returns the role | ||
| 1221 | result = aRole; | ||
| 1222 | } | ||
| 1223 | |||
| 1224 | } else { | ||
| 1225 | |||
| 1226 | // Role not set | ||
| 1227 | |||
| 1228 | if (!(multiRoleFlg)) { | ||
| 1229 | // Problem when setting a simple role: either role not | ||
| 1230 | // found, not writable, or incorrect value: | ||
| 1231 | // raises appropriate exception, RoleNotFoundException or | ||
| 1232 | // InvalidRoleValueException | ||
| 1233 | RelationService.throwRoleProblemException(pbType, | ||
| 1234 | roleName); | ||
| 1235 | // To keep compiler happy :) | ||
| 1236 | return null; | ||
| 1237 | |||
| 1238 | } else { | ||
| 1239 | // Problem when retrieving a role in a multi-role retrieval: | ||
| 1240 | // returns a RoleUnresolved object | ||
| 1241 | result = new RoleUnresolved(roleName, | ||
| 1242 | aRole.getRoleValue(), | ||
| 1243 | pbType); | ||
| 1244 | } | ||
| 1245 | } | ||
| 1246 | |||
| 1247 | RELATION_LOGGER.exiting(RelationSupport.class.getName(), "setRoleInt"); | ||
| 1248 | return result; | ||
| 1249 | } | ||
| 1250 | |||
| 1251 | // Requires the Relation Service to send a notification | ||
| 1252 | // RelationNotification, with type being either: | ||
| 1253 | // - RelationNotification.RELATION_BASIC_UPDATE if the updated relation is | ||
| 1254 | // a relation internal to the Relation Service | ||
| 1255 | // - RelationNotification.RELATION_MBEAN_UPDATE if the updated relation is | ||
| 1256 | // a relation MBean. | ||
| 1257 | // | ||
| 1258 | // -param newRole new role | ||
| 1259 | // -param oldRoleValue old role value (ArrayList of ObjectNames) | ||
| 1260 | // -param relationServCallFlg true if call from the Relation Service; this | ||
| 1261 | // will happen if the current RelationSupport object has been created by | ||
| 1262 | // the Relation Service (via createRelation()) method, so direct access is | ||
| 1263 | // possible. | ||
| 1264 | // -param relationServ reference to Relation Service object, if object | ||
| 1265 | // created by Relation Service. | ||
| 1266 | // | ||
| 1267 | // -exception IllegalArgumentException if null parameter provided | ||
| 1268 | // -exception RelationServiceNotRegisteredException if the Relation | ||
| 1269 | // Service is not registered in the MBean Server | ||
| 1270 | // -exception RelationNotFoundException if: | ||
| 1271 | // - relation MBean | ||
| 1272 | // and | ||
| 1273 | // - it has not been added into the Relation Service | ||
| 1274 | private void sendRoleUpdateNotification(Role newRole, | ||
| 1275 | List<ObjectName> oldRoleValue, | ||
| 1276 | boolean relationServCallFlg, | ||
| 1277 | RelationService relationServ) | ||
| 1278 | throws IllegalArgumentException, | ||
| 1279 | RelationServiceNotRegisteredException, | ||
| 1280 | RelationNotFoundException { | ||
| 1281 | |||
| 1282 | if (newRole == null || | ||
| 1283 | oldRoleValue == null || | ||
| 1284 | (relationServCallFlg && relationServ == null)) { | ||
| 1285 | String excMsg = "Invalid parameter."; | ||
| 1286 | throw new IllegalArgumentException(excMsg); | ||
| 1287 | } | ||
| 1288 | |||
| 1289 | RELATION_LOGGER.entering(RelationSupport.class.getName(), | ||
| 1290 | "sendRoleUpdateNotification", new Object[] {newRole, | ||
| 1291 | oldRoleValue, relationServCallFlg, relationServ}); | ||
| 1292 | |||
| 1293 | if (relationServCallFlg) { | ||
| 1294 | // Direct call to the Relation Service | ||
| 1295 | // Shall not throw a RelationNotFoundException for an internal | ||
| 1296 | // relation | ||
| 1297 | try { | ||
| 1298 | relationServ.sendRoleUpdateNotification(myRelId, | ||
| 1299 | newRole, | ||
| 1300 | oldRoleValue); | ||
| 1301 | } catch (RelationNotFoundException exc) { | ||
| 1302 | throw new RuntimeException(exc.getMessage()); | ||
| 1303 | } | ||
| 1304 | |||
| 1305 | } else { | ||
| 1306 | |||
| 1307 | Object[] params = new Object[3]; | ||
| 1308 | params[0] = myRelId; | ||
| 1309 | params[1] = newRole; | ||
| 1310 | params[2] = oldRoleValue; | ||
| 1311 | String[] signature = new String[3]; | ||
| 1312 | signature[0] = "java.lang.String"; | ||
| 1313 | signature[1] = "javax.management.relation.Role"; | ||
| 1314 | signature[2] = "java.util.List"; | ||
| 1315 | |||
| 1316 | // Can throw InstanceNotFoundException if the Relation Service | ||
| 1317 | // is not registered (to be transformed). | ||
| 1318 | // | ||
| 1319 | // Can throw a MBeanException wrapping a | ||
| 1320 | // RelationNotFoundException (to be raised in any case): wrapped | ||
| 1321 | // exception to be thrown | ||
| 1322 | // | ||
| 1323 | // Shall not throw a ReflectionException | ||
| 1324 | try { | ||
| 1325 | myRelServiceMBeanServer.invoke(myRelServiceName, | ||
| 1326 | "sendRoleUpdateNotification", | ||
| 1327 | params, | ||
| 1328 | signature); | ||
| 1329 | } catch (ReflectionException exc1) { | ||
| 1330 | throw new RuntimeException(exc1.getMessage()); | ||
| 1331 | } catch (InstanceNotFoundException exc2) { | ||
| 1332 | throw new RelationServiceNotRegisteredException( | ||
| 1333 | exc2.getMessage()); | ||
| 1334 | } catch (MBeanException exc3) { | ||
| 1335 | Exception wrappedExc = exc3.getTargetException(); | ||
| 1336 | if (wrappedExc instanceof RelationNotFoundException) { | ||
| 1337 | throw ((RelationNotFoundException)wrappedExc); | ||
| 1338 | } else { | ||
| 1339 | throw new RuntimeException(wrappedExc.getMessage()); | ||
| 1340 | } | ||
| 1341 | } | ||
| 1342 | } | ||
| 1343 | |||
| 1344 | RELATION_LOGGER.exiting(RelationSupport.class.getName(), | ||
| 1345 | "sendRoleUpdateNotification"); | ||
| 1346 | return; | ||
| 1347 | } | ||
| 1348 | |||
| 1349 | // Requires the Relation Service to update its internal map handling | ||
| 1350 | // MBeans referenced in relations. | ||
| 1351 | // The Relation Service will also update its recording as a listener to | ||
| 1352 | // be informed about unregistration of new referenced MBeans, and no longer | ||
| 1353 | // informed of MBeans no longer referenced. | ||
| 1354 | // | ||
| 1355 | // -param newRole new role | ||
| 1356 | // -param oldRoleValue old role value (ArrayList of ObjectNames) | ||
| 1357 | // -param relationServCallFlg true if call from the Relation Service; this | ||
| 1358 | // will happen if the current RelationSupport object has been created by | ||
| 1359 | // the Relation Service (via createRelation()) method, so direct access is | ||
| 1360 | // possible. | ||
| 1361 | // -param relationServ reference to Relation Service object, if object | ||
| 1362 | // created by Relation Service. | ||
| 1363 | // | ||
| 1364 | // -exception IllegalArgumentException if null parameter | ||
| 1365 | // -exception RelationServiceNotRegisteredException if the Relation | ||
| 1366 | // Service is not registered in the MBean Server | ||
| 1367 | // -exception RelationNotFoundException if: | ||
| 1368 | // - relation MBean | ||
| 1369 | // and | ||
| 1370 | // - the relation is not added in the Relation Service | ||
| 1371 | private void updateRelationServiceMap(Role newRole, | ||
| 1372 | List<ObjectName> oldRoleValue, | ||
| 1373 | boolean relationServCallFlg, | ||
| 1374 | RelationService relationServ) | ||
| 1375 | throws IllegalArgumentException, | ||
| 1376 | RelationServiceNotRegisteredException, | ||
| 1377 | RelationNotFoundException { | ||
| 1378 | |||
| 1379 | if (newRole == null || | ||
| 1380 | oldRoleValue == null || | ||
| 1381 | (relationServCallFlg && relationServ == null)) { | ||
| 1382 | String excMsg = "Invalid parameter."; | ||
| 1383 | throw new IllegalArgumentException(excMsg); | ||
| 1384 | } | ||
| 1385 | |||
| 1386 | RELATION_LOGGER.entering(RelationSupport.class.getName(), | ||
| 1387 | "updateRelationServiceMap", new Object[] {newRole, | ||
| 1388 | oldRoleValue, relationServCallFlg, relationServ}); | ||
| 1389 | |||
| 1390 | if (relationServCallFlg) { | ||
| 1391 | // Direct call to the Relation Service | ||
| 1392 | // Shall not throw a RelationNotFoundException | ||
| 1393 | try { | ||
| 1394 | relationServ.updateRoleMap(myRelId, | ||
| 1395 | newRole, | ||
| 1396 | oldRoleValue); | ||
| 1397 | } catch (RelationNotFoundException exc) { | ||
| 1398 | throw new RuntimeException(exc.getMessage()); | ||
| 1399 | } | ||
| 1400 | |||
| 1401 | } else { | ||
| 1402 | Object[] params = new Object[3]; | ||
| 1403 | params[0] = myRelId; | ||
| 1404 | params[1] = newRole; | ||
| 1405 | params[2] = oldRoleValue; | ||
| 1406 | String[] signature = new String[3]; | ||
| 1407 | signature[0] = "java.lang.String"; | ||
| 1408 | signature[1] = "javax.management.relation.Role"; | ||
| 1409 | signature[2] = "java.util.List"; | ||
| 1410 | // Can throw InstanceNotFoundException if the Relation Service | ||
| 1411 | // is not registered (to be transformed). | ||
| 1412 | // Can throw a MBeanException wrapping a RelationNotFoundException: | ||
| 1413 | // wrapped exception to be thrown | ||
| 1414 | // | ||
| 1415 | // Shall not throw a ReflectionException | ||
| 1416 | try { | ||
| 1417 | myRelServiceMBeanServer.invoke(myRelServiceName, | ||
| 1418 | "updateRoleMap", | ||
| 1419 | params, | ||
| 1420 | signature); | ||
| 1421 | } catch (ReflectionException exc1) { | ||
| 1422 | throw new RuntimeException(exc1.getMessage()); | ||
| 1423 | } catch (InstanceNotFoundException exc2) { | ||
| 1424 | throw new | ||
| 1425 | RelationServiceNotRegisteredException(exc2.getMessage()); | ||
| 1426 | } catch (MBeanException exc3) { | ||
| 1427 | Exception wrappedExc = exc3.getTargetException(); | ||
| 1428 | if (wrappedExc instanceof RelationNotFoundException) { | ||
| 1429 | throw ((RelationNotFoundException)wrappedExc); | ||
| 1430 | } else { | ||
| 1431 | throw new RuntimeException(wrappedExc.getMessage()); | ||
| 1432 | } | ||
| 1433 | } | ||
| 1434 | } | ||
| 1435 | |||
| 1436 | RELATION_LOGGER.exiting(RelationSupport.class.getName(), | ||
| 1437 | "updateRelationServiceMap"); | ||
| 1438 | return; | ||
| 1439 | } | ||
| 1440 | |||
| 1441 | // Sets the given roles | ||
| 1442 | // For each role: | ||
| 1443 | // - will check the role according to its corresponding role definition | ||
| 1444 | // provided in relation's relation type | ||
| 1445 | // - will send a notification (RelationNotification with type | ||
| 1446 | // RELATION_BASIC_UPDATE or RELATION_MBEAN_UPDATE, depending if the | ||
| 1447 | // relation is a MBean or not) for each updated role. | ||
| 1448 | // | ||
| 1449 | // This method is called in setRoles() above and in Relation Service | ||
| 1450 | // setRoles() method. | ||
| 1451 | // | ||
| 1452 | // -param list list of roles to be set | ||
| 1453 | // -param relationServCallFlg true if call from the Relation Service; this | ||
| 1454 | // will happen if the current RelationSupport object has been created by | ||
| 1455 | // the Relation Service (via createRelation()) method, so direct access is | ||
| 1456 | // possible. | ||
| 1457 | // -param relationServ reference to Relation Service object, if object | ||
| 1458 | // created by Relation Service. | ||
| 1459 | // | ||
| 1460 | // -return a RoleResult object | ||
| 1461 | // | ||
| 1462 | // -exception IllegalArgumentException if null parameter | ||
| 1463 | // -exception RelationServiceNotRegisteredException if the Relation | ||
| 1464 | // Service is not registered in the MBean Server | ||
| 1465 | // -exception RelationTypeNotFoundException if: | ||
| 1466 | // - relation MBean | ||
| 1467 | // and | ||
| 1468 | // - unknown relation type | ||
| 1469 | // -exception RelationNotFoundException if: | ||
| 1470 | // - relation MBean | ||
| 1471 | // and | ||
| 1472 | // - not added in the RS | ||
| 1473 | RoleResult setRolesInt(RoleList list, | ||
| 1474 | boolean relationServCallFlg, | ||
| 1475 | RelationService relationServ) | ||
| 1476 | throws IllegalArgumentException, | ||
| 1477 | RelationServiceNotRegisteredException, | ||
| 1478 | RelationTypeNotFoundException, | ||
| 1479 | RelationNotFoundException { | ||
| 1480 | |||
| 1481 | if (list == null || | ||
| 1482 | (relationServCallFlg && relationServ == null)) { | ||
| 1483 | String excMsg = "Invalid parameter."; | ||
| 1484 | throw new IllegalArgumentException(excMsg); | ||
| 1485 | } | ||
| 1486 | |||
| 1487 | RELATION_LOGGER.entering(RelationSupport.class.getName(), | ||
| 1488 | "setRolesInt", | ||
| 1489 | new Object[] {list, relationServCallFlg, relationServ}); | ||
| 1490 | |||
| 1491 | RoleList roleList = new RoleList(); | ||
| 1492 | RoleUnresolvedList roleUnresList = new RoleUnresolvedList(); | ||
| 1493 | |||
| 1494 | for (Role currRole : list.asList()) { | ||
| 1495 | |||
| 1496 | Object currResult = null; | ||
| 1497 | // Can throw: | ||
| 1498 | // RelationServiceNotRegisteredException, | ||
| 1499 | // RelationTypeNotFoundException | ||
| 1500 | // | ||
| 1501 | // Will not throw, due to parameters, RoleNotFoundException or | ||
| 1502 | // InvalidRoleValueException, but catch them to keep compiler | ||
| 1503 | // happy | ||
| 1504 | try { | ||
| 1505 | currResult = setRoleInt(currRole, | ||
| 1506 | relationServCallFlg, | ||
| 1507 | relationServ, | ||
| 1508 | true); | ||
| 1509 | } catch (RoleNotFoundException exc1) { | ||
| 1510 | // OK : Do not throw a RoleNotFoundException. | ||
| 1511 | } catch (InvalidRoleValueException exc2) { | ||
| 1512 | // OK : Do not throw an InvalidRoleValueException. | ||
| 1513 | } | ||
| 1514 | |||
| 1515 | if (currResult instanceof Role) { | ||
| 1516 | // Can throw IllegalArgumentException if role is null | ||
| 1517 | // (normally should not happen :( | ||
| 1518 | try { | ||
| 1519 | roleList.add((Role)currResult); | ||
| 1520 | } catch (IllegalArgumentException exc) { | ||
| 1521 | throw new RuntimeException(exc.getMessage()); | ||
| 1522 | } | ||
| 1523 | |||
| 1524 | } else if (currResult instanceof RoleUnresolved) { | ||
| 1525 | // Can throw IllegalArgumentException if role is null | ||
| 1526 | // (normally should not happen :( | ||
| 1527 | try { | ||
| 1528 | roleUnresList.add((RoleUnresolved)currResult); | ||
| 1529 | } catch (IllegalArgumentException exc) { | ||
| 1530 | throw new RuntimeException(exc.getMessage()); | ||
| 1531 | } | ||
| 1532 | } | ||
| 1533 | } | ||
| 1534 | |||
| 1535 | RoleResult result = new RoleResult(roleList, roleUnresList); | ||
| 1536 | |||
| 1537 | RELATION_LOGGER.exiting(RelationSupport.class.getName(), "setRolesInt"); | ||
| 1538 | return result; | ||
| 1539 | } | ||
| 1540 | |||
| 1541 | // Initializes all members | ||
| 1542 | // | ||
| 1543 | // -param relationId relation identifier, to identify the relation in the | ||
| 1544 | // Relation Service. | ||
| 1545 | // Expected to be unique in the given Relation Service. | ||
| 1546 | // -param relationServiceName ObjectName of the Relation Service where | ||
| 1547 | // the relation will be registered. | ||
| 1548 | // It is required as this is the Relation Service that is aware of the | ||
| 1549 | // definition of the relation type of given relation, so that will be able | ||
| 1550 | // to check update operations (set). Direct access via the Relation | ||
| 1551 | // Service (RelationService.setRole()) do not need this information but | ||
| 1552 | // as any user relation is a MBean, setRole() is part of its management | ||
| 1553 | // interface and can be called directly on the user relation MBean. So the | ||
| 1554 | // user relation MBean must be aware of the Relation Service where it will | ||
| 1555 | // be added. | ||
| 1556 | // -param relationTypeName Name of relation type. | ||
| 1557 | // Expected to have been created in given Relation Service. | ||
| 1558 | // -param list list of roles (Role objects) to initialized the | ||
| 1559 | // relation. Can be null. | ||
| 1560 | // Expected to conform to relation info in associated relation type. | ||
| 1561 | // | ||
| 1562 | // -exception InvalidRoleValueException if the same name is used for two | ||
| 1563 | // roles. | ||
| 1564 | // -exception IllegalArgumentException if a required value (Relation | ||
| 1565 | // Service Object Name, etc.) is not provided as parameter. | ||
| 1566 | private void initMembers(String relationId, | ||
| 1567 | ObjectName relationServiceName, | ||
| 1568 | MBeanServer relationServiceMBeanServer, | ||
| 1569 | String relationTypeName, | ||
| 1570 | RoleList list) | ||
| 1571 | throws InvalidRoleValueException, | ||
| 1572 | IllegalArgumentException { | ||
| 1573 | |||
| 1574 | if (relationId == null || | ||
| 1575 | relationServiceName == null || | ||
| 1576 | relationTypeName == null) { | ||
| 1577 | String excMsg = "Invalid parameter."; | ||
| 1578 | throw new IllegalArgumentException(excMsg); | ||
| 1579 | } | ||
| 1580 | |||
| 1581 | RELATION_LOGGER.entering(RelationSupport.class.getName(), | ||
| 1582 | "initMembers", new Object[] {relationId, relationServiceName, | ||
| 1583 | relationServiceMBeanServer, relationTypeName, list}); | ||
| 1584 | |||
| 1585 | myRelId = relationId; | ||
| 1586 | myRelServiceName = relationServiceName; | ||
| 1587 | myRelServiceMBeanServer = relationServiceMBeanServer; | ||
| 1588 | myRelTypeName = relationTypeName; | ||
| 1589 | // Can throw InvalidRoleValueException | ||
| 1590 | initRoleMap(list); | ||
| 1591 | |||
| 1592 | RELATION_LOGGER.exiting(RelationSupport.class.getName(), "initMembers"); | ||
| 1593 | return; | ||
| 1594 | } | ||
| 1595 | |||
| 1596 | // Initialize the internal role map from given RoleList parameter | ||
| 1597 | // | ||
| 1598 | // -param list role list. Can be null. | ||
| 1599 | // As it is a RoleList object, it cannot include null (rejected). | ||
| 1600 | // | ||
| 1601 | // -exception InvalidRoleValueException if the same role name is used for | ||
| 1602 | // several roles. | ||
| 1603 | // | ||
| 1604 | private void initRoleMap(RoleList list) | ||
| 1605 | throws InvalidRoleValueException { | ||
| 1606 | |||
| 1607 | if (list == null) { | ||
| 1608 | return; | ||
| 1609 | } | ||
| 1610 | |||
| 1611 | RELATION_LOGGER.entering(RelationSupport.class.getName(), | ||
| 1612 | "initRoleMap", list); | ||
| 1613 | |||
| 1614 | synchronized(myRoleName2ValueMap) { | ||
| 1615 | |||
| 1616 | for (Role currRole : list.asList()) { | ||
| 1617 | |||
| 1618 | // No need to check if role is null, it is not allowed to store | ||
| 1619 | // a null role in a RoleList :) | ||
| 1620 | String currRoleName = currRole.getRoleName(); | ||
| 1621 | |||
| 1622 | if (myRoleName2ValueMap.containsKey(currRoleName)) { | ||
| 1623 | // Role already provided in current list | ||
| 1624 | StringBuilder excMsgStrB = new StringBuilder("Role name "); | ||
| 1625 | excMsgStrB.append(currRoleName); | ||
| 1626 | excMsgStrB.append(" used for two roles."); | ||
| 1627 | throw new InvalidRoleValueException(excMsgStrB.toString()); | ||
| 1628 | } | ||
| 1629 | |||
| 1630 | myRoleName2ValueMap.put(currRoleName, | ||
| 1631 | (Role)(currRole.clone())); | ||
| 1632 | } | ||
| 1633 | } | ||
| 1634 | |||
| 1635 | RELATION_LOGGER.exiting(RelationSupport.class.getName(), "initRoleMap"); | ||
| 1636 | return; | ||
| 1637 | } | ||
| 1638 | |||
| 1639 | // Callback used by the Relation Service when a MBean referenced in a role | ||
| 1640 | // is unregistered. | ||
| 1641 | // The Relation Service will call this method to let the relation | ||
| 1642 | // take action to reflect the impact of such unregistration. | ||
| 1643 | // Current implementation is to set the role with its current value | ||
| 1644 | // (list of ObjectNames of referenced MBeans) without the unregistered | ||
| 1645 | // one. | ||
| 1646 | // | ||
| 1647 | // -param objectName ObjectName of unregistered MBean | ||
| 1648 | // -param roleName name of role where the MBean is referenced | ||
| 1649 | // -param relationServCallFlg true if call from the Relation Service; this | ||
| 1650 | // will happen if the current RelationSupport object has been created by | ||
| 1651 | // the Relation Service (via createRelation()) method, so direct access is | ||
| 1652 | // possible. | ||
| 1653 | // -param relationServ reference to Relation Service object, if internal | ||
| 1654 | // relation | ||
| 1655 | // | ||
| 1656 | // -exception IllegalArgumentException if null parameter | ||
| 1657 | // -exception RoleNotFoundException if: | ||
| 1658 | // - the role does not exist | ||
| 1659 | // or | ||
| 1660 | // - role not writable. | ||
| 1661 | // -exception InvalidRoleValueException if value provided for: | ||
| 1662 | // - the number of referenced MBeans in given value is less than | ||
| 1663 | // expected minimum degree | ||
| 1664 | // or | ||
| 1665 | // - the number of referenced MBeans in provided value exceeds expected | ||
| 1666 | // maximum degree | ||
| 1667 | // or | ||
| 1668 | // - one referenced MBean in the value is not an Object of the MBean | ||
| 1669 | // class expected for that role | ||
| 1670 | // or | ||
| 1671 | // - a MBean provided for that role does not exist | ||
| 1672 | // -exception RelationServiceNotRegisteredException if the Relation | ||
| 1673 | // Service is not registered in the MBean Server | ||
| 1674 | // -exception RelationTypeNotFoundException if unknown relation type | ||
| 1675 | // -exception RelationNotFoundException if current relation has not been | ||
| 1676 | // added in the RS | ||
| 1677 | void handleMBeanUnregistrationInt(ObjectName objectName, | ||
| 1678 | String roleName, | ||
| 1679 | boolean relationServCallFlg, | ||
| 1680 | RelationService relationServ) | ||
| 1681 | throws IllegalArgumentException, | ||
| 1682 | RoleNotFoundException, | ||
| 1683 | InvalidRoleValueException, | ||
| 1684 | RelationServiceNotRegisteredException, | ||
| 1685 | RelationTypeNotFoundException, | ||
| 1686 | RelationNotFoundException { | ||
| 1687 | |||
| 1688 | if (objectName == null || | ||
| 1689 | roleName == null || | ||
| 1690 | (relationServCallFlg && relationServ == null)) { | ||
| 1691 | String excMsg = "Invalid parameter."; | ||
| 1692 | throw new IllegalArgumentException(excMsg); | ||
| 1693 | } | ||
| 1694 | |||
| 1695 | RELATION_LOGGER.entering(RelationSupport.class.getName(), | ||
| 1696 | "handleMBeanUnregistrationInt", new Object[] {objectName, | ||
| 1697 | roleName, relationServCallFlg, relationServ}); | ||
| 1698 | |||
| 1699 | // Retrieves current role value | ||
| 1700 | Role role; | ||
| 1701 | synchronized(myRoleName2ValueMap) { | ||
| 1702 | role = (myRoleName2ValueMap.get(roleName)); | ||
| 1703 | } | ||
| 1704 | |||
| 1705 | if (role == null) { | ||
| 1706 | StringBuilder excMsgStrB = new StringBuilder(); | ||
| 1707 | String excMsg = "No role with name "; | ||
| 1708 | excMsgStrB.append(excMsg); | ||
| 1709 | excMsgStrB.append(roleName); | ||
| 1710 | throw new RoleNotFoundException(excMsgStrB.toString()); | ||
| 1711 | } | ||
| 1712 | List<ObjectName> currRoleValue = role.getRoleValue(); | ||
| 1713 | |||
| 1714 | // Note: no need to test if list not null before cloning, null value | ||
| 1715 | // not allowed for role value. | ||
| 1716 | List<ObjectName> newRoleValue = new ArrayList<ObjectName>(currRoleValue); | ||
| 1717 | newRoleValue.remove(objectName); | ||
| 1718 | Role newRole = new Role(roleName, newRoleValue); | ||
| 1719 | |||
| 1720 | // Can throw InvalidRoleValueException, | ||
| 1721 | // RelationTypeNotFoundException | ||
| 1722 | // (RoleNotFoundException already detected) | ||
| 1723 | Object result = | ||
| 1724 | setRoleInt(newRole, relationServCallFlg, relationServ, false); | ||
| 1725 | |||
| 1726 | RELATION_LOGGER.exiting(RelationSupport.class.getName(), | ||
| 1727 | "handleMBeanUnregistrationInt"); | ||
| 1728 | return; | ||
| 1729 | } | ||
| 1730 | |||
| 1731 | } | ||