001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.org
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package javax.management.relation;
009:
010: import java.util.HashMap;
011: import java.util.List;
012: import java.util.ArrayList;
013: import java.util.Map;
014:
015: /**
016: * A RelationTypeSupport object implements the RelationType interface.
017: * <P>It represents a relation type, providing role information for each role
018: * expected to be supported in every relation of that type.
019: *
020: * <P>A relation type includes a relation type name and a list of
021: * role infos (represented by RoleInfo objects).
022: *
023: * <P>A relation type has to be declared in the Relation Service:
024: * <P>- either using the createRelationType() method, where a RelationTypeSupport
025: * object will be created and kept in the Relation Service
026: * <P>- either using the addRelationType() method where the user has to doCreate
027: * an object implementing the RelationType interface, and this object will be
028: * used as representing a relation type in the Relation Service.
029: *
030: * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
031: */
032:
033: public class RelationTypeSupport implements RelationType {
034:
035: // Name
036: private String name = null;
037:
038: // HashMap mapping <role name (String)> -> <role info (RoleInfo object)>
039: private Map roleInfoMap = new HashMap();
040:
041: // Flag to specify whether the relation type has been declared in the
042: // Relation Service (so can no longer be updated)
043: private boolean isInRelationService = false;
044:
045: /**
046: * Constructor where all role definitions are dynamically created and
047: * passed as parameter.
048: *
049: * @param relationTypeName Name of relation type
050: * @param roleInfoArray List of role definitions (RoleInfo objects)
051: *
052: * @exception IllegalArgumentException if null parameter
053: * @exception InvalidRelationTypeException if:
054: * <P>- the same name has been used for two different roles
055: * <P>- no role info provided
056: * <P>- one null role info provided
057: */
058: public RelationTypeSupport(String relationTypeName,
059: RoleInfo[] roleInfoArray) throws IllegalArgumentException,
060: InvalidRelationTypeException {
061: if (relationTypeName == null || roleInfoArray == null
062: || roleInfoArray.length == 0) {
063: throw new IllegalArgumentException(
064: "Invalid parameter: relationTypeName and roleInfo can not be null or empty");
065: }
066: name = relationTypeName;
067: roleInfoMap = _constructRoleInfoMap(roleInfoArray);// Can throw InvalidRelationTypeException, ClassNotFoundException
068: }
069:
070: // Constructor to be used when creating an instance of a subclass
071: //
072: // -param relationTypeName Name of relation type
073: //
074: // -exception IllegalArgumentException if null parameter
075: protected RelationTypeSupport(String relationTypeName) {
076: if (relationTypeName == null) {
077: throw new IllegalArgumentException(
078: "Invalid parameter: relationTypeName can not be null");
079: }
080: name = relationTypeName;
081: }
082:
083: /**
084: * Returns the relation type name
085: */
086: public String getRelationTypeName() {
087: return name;
088: }
089:
090: /**
091: * Returns the list of role definitions (ArrayList of RoleInfo objects).
092: */
093: public List getRoleInfos() {
094: return new ArrayList(roleInfoMap.values());
095: }
096:
097: /**
098: * Returns the role info (RoleInfo object) for the given role info name
099: * (null if not found).
100: *
101: * @param roleName role info name
102: *
103: * @return RoleInfo object providing role definition
104: * does not exist
105: *
106: * @exception IllegalArgumentException if null parameter
107: * @exception RoleInfoNotFoundException if no role info with that name in
108: * relation type.
109: */
110: public RoleInfo getRoleInfo(String roleName)
111: throws IllegalArgumentException, RoleInfoNotFoundException {
112: if (roleName == null) {
113: throw new IllegalArgumentException(
114: "Invalid parameter: roleName can not be null");
115: }
116:
117: // No null RoleInfo allowed, so use get()
118: RoleInfo roleInfo = (RoleInfo) (roleInfoMap.get(roleName));
119:
120: if (roleInfo == null) {
121: throw new RoleInfoNotFoundException(
122: "No role info for role " + roleName);
123: }
124: return roleInfo;
125: }
126:
127: // Adds a role info
128: // This method of course shall not be used after the creation of the
129: // relation type, because updating it would invalidate that the relations
130: // created associated to that type still conform to it.
131: // Can throw a RuntimeException if trying to update a relation type
132: // decalred in the Relation Service.
133: //
134: // -param roleInfo role info to be added
135: //
136: // -exception IllegalArgumentException if null parameter
137: // -exception InvalidRelationTypeException if there is already a role
138: // info in current relation type with the same name
139: protected void addRoleInfo(RoleInfo roleInfo)
140: throws IllegalArgumentException,
141: InvalidRelationTypeException {
142:
143: if (roleInfo == null) {
144: throw new IllegalArgumentException(
145: "Invalid parameter: roleInfo can not be null");
146: }
147:
148: if (isInRelationService) {
149: throw new RuntimeException(
150: "Relation type cannot be updated as it is declared in the Relation Service.");
151: }
152:
153: String roleName = roleInfo.getName();
154:
155: // Checks if the role info has already been described
156: if (roleInfoMap.containsKey(roleName)) {
157: throw new InvalidRelationTypeException(
158: "Two role infos provided for role: " + roleInfo);
159: }
160: roleInfoMap.put(roleName, roleInfo);
161:
162: }
163:
164: // Sets the internal flag to specify that the relation type has been
165: // declared in the Relation Service
166: void setRelationServiceFlag(boolean flag) {
167: isInRelationService = flag;
168: }
169:
170: // check the roleInfoArray then construct it to roleInfoMap
171: private Map _constructRoleInfoMap(RoleInfo[] roleInfoArray)
172: throws IllegalArgumentException,
173: InvalidRelationTypeException {
174: Map _roleInfoMap = new HashMap();
175: for (int i = 0; i < roleInfoArray.length; i++) {
176: RoleInfo roleInfo = roleInfoArray[i];
177: if (roleInfo == null) {
178: throw new InvalidRelationTypeException(
179: "Null role info provided at index " + i
180: + " of roleInfoArray " + roleInfoArray);
181: }
182:
183: String roleName = roleInfo.getName();
184: if (_roleInfoMap.containsKey(roleName)) {
185: throw new InvalidRelationTypeException(
186: "Two role infos provided for role " + roleInfo);
187: }
188: _roleInfoMap.put(roleName, roleInfo);
189: }
190:
191: return _roleInfoMap;
192: }
193:
194: }
|