View Javadoc

1   /*
2    * ====================
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4    * 
5    * Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved.     
6    * 
7    * The contents of this file are subject to the terms of the Common Development 
8    * and Distribution License("CDDL") (the "License").  You may not use this file 
9    * except in compliance with the License.
10   * 
11   * You can obtain a copy of the License at 
12   * http://IdentityConnectors.dev.java.net/legal/license.txt
13   * See the License for the specific language governing permissions and limitations 
14   * under the License. 
15   * 
16   * When distributing the Covered Code, include this CDDL Header Notice in each file
17   * and include the License file at identityconnectors/legal/license.txt.
18   * If applicable, add the following below this CDDL Header, with the fields 
19   * enclosed by brackets [] replaced by your own identifying information: 
20   * "Portions Copyrighted [year] [name of copyright owner]"
21   * ====================
22   */
23  package org.identityconnectors.framework.common.objects;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import org.identityconnectors.common.CollectionUtil;
30  
31  
32  /**
33   * A ConnectorObject represents an object (e.g., an Account or a Group) on the
34   * target resource. Each ConnectorObject represents a resource object as a UID
35   * and a bag of attributes.
36   * 
37   * The developer of a Connector will use a {@link ConnectorObjectBuilder} to
38   * construct instances of ConnectorObject.
39   */
40  public final class ConnectorObject {
41      final ObjectClass _objectClass;
42      final Map<String, Attribute> attrs;
43  
44      /**
45       * Public only for serialization; please use {@link ConnectorObjectBuilder}.
46       * 
47       * @throws IllegalArgumentException
48       *             iff {@link Name} or {@link Uid} is missing from the set.
49       */
50      public ConnectorObject(ObjectClass objectClass,
51              Set<? extends Attribute> set) {
52          if (objectClass == null) {
53              throw new IllegalArgumentException("ObjectClass may not be null");
54          }
55          if (set == null || set.size() == 0) {
56              final String MSG = "The set can not be null or empty.";
57              throw new IllegalArgumentException(MSG);
58          }
59          _objectClass = objectClass;
60          // create an easy look map..
61          this.attrs = AttributeUtil.toMap(set);
62          // make sure the Uid was added..
63          if (!this.attrs.containsKey(Uid.NAME)) {
64              final String MSG = "The Attribute set must contain a 'Uid'.";
65              throw new IllegalArgumentException(MSG);
66          }
67          // make sure the Name attribute was added..
68          if (!this.attrs.containsKey(Name.NAME)) {
69              final String MSG = "The Attribute set must contain a 'Name'.";
70              throw new IllegalArgumentException(MSG);
71          }
72      }
73  
74      /**
75       * Get the set of attributes that represent this object. This includes the
76       * {@link Uid} and all {@link OperationalAttributes}.
77       */
78      public Set<Attribute> getAttributes() {
79          // create a copy/unmodifiable set..
80          return CollectionUtil.newReadOnlySet(this.attrs.values());
81      }
82  
83      /**
84       * Get an attribute by if it exists else null.
85       */
86      public Attribute getAttributeByName(String name) {
87          // no need to clone since it has no setters
88          return this.attrs.get(name);
89      }
90  
91      /**
92       * Get the native identifier for this object.
93       */
94      public Uid getUid() {
95          return (Uid) this.attrs.get(Uid.NAME);
96      }
97  
98      /**
99       * Gets the {@link Name} of the object.
100      */
101     public Name getName() {
102         return (Name) this.attrs.get(Name.NAME);
103     }
104 
105     /**
106      * Gets the {@link ObjectClass} for this object.
107      */
108     public ObjectClass getObjectClass() {
109         return _objectClass;
110     }
111 
112     @Override
113     public boolean equals(Object obj) {
114         if (obj instanceof ConnectorObject) {
115             ConnectorObject other = (ConnectorObject)obj;
116             if (!_objectClass.equals(other.getObjectClass())) {
117                 return false;
118             }
119             return CollectionUtil.equals(getAttributes(),other.getAttributes());                       
120         }
121         return false;
122     }
123 
124     @Override
125     public int hashCode() {
126         return getAttributes().hashCode();
127     }
128 
129     @Override
130     public String toString() {
131         // poor man's consistent toString()..
132         Map<String, Object> map = new HashMap<String, Object>();
133         map.put("Uid", this.getUid());
134         map.put("ObjectClass", this.getObjectClass());
135         map.put("Name", this.getName());
136         map.put("Attributes", this.getAttributes());
137         return map.toString();
138     }
139 
140 }