001: /**
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */package javax.management.relation;
008:
009: import java.util.ArrayList;
010: import java.util.HashMap;
011: import java.util.List;
012: import java.util.Map;
013: import javax.management.RuntimeOperationsException;
014:
015: /**
016: * @version $Revision: 1.10 $
017: */
018: public class RelationTypeSupport implements RelationType {
019: private static final long serialVersionUID = 4611072955724144607L;
020:
021: private String typeName;
022: private Map roleName2InfoMap = new HashMap();
023: private boolean isInRelationService;
024:
025: public RelationTypeSupport(String relationTypeName,
026: RoleInfo[] roleInfo) throws IllegalArgumentException,
027: InvalidRelationTypeException {
028: if (relationTypeName == null || roleInfo == null)
029: throw new IllegalArgumentException("Illegal Null Value");
030: this .typeName = relationTypeName;
031: // determines if the roleInfo[] are valid
032: checkRoleInfos(roleInfo);
033: // no exceptions thrown in validate add the roleInfos to our HashMap
034: addRoleInfos(roleInfo);
035: }
036:
037: protected RelationTypeSupport(String relationTypeName) {
038: if (relationTypeName == null)
039: throw new IllegalArgumentException("Null RelationType Name");
040: this .typeName = relationTypeName;
041: }
042:
043: private void addRoleInfos(RoleInfo[] roleInfos)
044: throws IllegalArgumentException {
045: if (roleInfos == null)
046: throw new IllegalArgumentException("Null RoleInfo[]");
047: synchronized (roleName2InfoMap) {
048: for (int i = 0; i < roleInfos.length; i++) {
049: RoleInfo currentRoleInfo = roleInfos[i];
050: addRoleNameToRoleInfo(currentRoleInfo.getName(),
051: currentRoleInfo);
052: }
053: }
054: }
055:
056: public String getRelationTypeName() {
057: return typeName;
058: }
059:
060: public RoleInfo getRoleInfo(String roleInfoName)
061: throws RoleInfoNotFoundException, IllegalArgumentException {
062: if (roleInfoName == null)
063: throw new IllegalArgumentException(
064: "roleInfo Name cannot have a null value");
065: RoleInfo roleInfo = (RoleInfo) (roleName2InfoMap
066: .get(roleInfoName));
067: if (roleInfo == null) {
068: throw new RoleInfoNotFoundException(
069: "No role info for role named " + roleInfoName);
070: }
071: return roleInfo;
072: }
073:
074: public List getRoleInfos() {
075: return new ArrayList(roleName2InfoMap.values());
076: }
077:
078: protected void addRoleInfo(RoleInfo roleInfo)
079: throws IllegalArgumentException,
080: InvalidRelationTypeException {
081: if (roleInfo == null)
082: throw new IllegalArgumentException(
083: "Cannot add a null roleInfo in the relation service");
084: if (isInRelationService)
085: throw new RuntimeOperationsException(
086: null,
087: "RoleInfo cannot be added as the relation type is already declared in the relation service");
088: String roleName = roleInfo.getName();
089: // roleName already present cannot have conflicting names
090: if (roleName2InfoMap.containsKey(roleName)) {
091: throw new InvalidRelationTypeException(
092: "Already a roleInfo declared for roleName "
093: + roleName);
094: }
095:
096: // no problems add the roleName as the key in our Map and the roleInfo as the value
097: addRoleNameToRoleInfo(roleName, roleInfo);
098: }
099:
100: private void addRoleNameToRoleInfo(String roleName,
101: RoleInfo roleInfo) {
102: synchronized (roleName2InfoMap) {
103: roleName2InfoMap.put(roleName, roleInfo);
104: }
105: }
106:
107: // validates the the RoleInfo[] conforms
108: static void checkRoleInfos(RoleInfo[] roleInfo)
109: throws IllegalArgumentException,
110: InvalidRelationTypeException {
111: if (roleInfo == null)
112: throw new IllegalArgumentException("RoleInfo is null.");
113: if (roleInfo.length == 0)
114: throw new InvalidRelationTypeException("RoleInfo is empty");
115: ArrayList roleNameList = new ArrayList();
116: for (int i = 0; i < roleInfo.length; ++i) {
117: RoleInfo currentRoleInfo = roleInfo[i];
118: if (currentRoleInfo == null)
119: throw new InvalidRelationTypeException("Null roleInfo");
120: String roleName = currentRoleInfo.getName();
121: if (roleNameList.contains(roleName)) {
122: throw new InvalidRelationTypeException(
123: "Two RoleInfos provided for role " + roleName);
124: }
125: roleNameList.add(roleName);
126: }
127: }
128:
129: void setRelationServiceFlag(boolean value) {
130: isInRelationService = value;
131: }
132: }
|