summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/certsrv/base/MetaAttributeDef.java
blob: e01d40f88b81cb468be1e3685f10a0ed0913d56e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.certsrv.base;


import java.util.Enumeration;
import java.util.Hashtable;

import netscape.security.util.ObjectIdentifier;


/**
 * A class representing a meta attribute defintion.
 * <P>
 *
 * @version $Revision$, $Date$
 */
public class MetaAttributeDef {

    private String mName;
    private ObjectIdentifier mOid;
    private Class mValueClass;
    private static Hashtable mNameToAttrDef = new Hashtable();
    private static Hashtable mOidToAttrDef = new Hashtable();

    private MetaAttributeDef() {
    }
	
    /**
     * Constructs a MetaAttribute defintion
     * <P>
     *
     * @param name attribute name
     * @param valueClass attribute value class
     * @param oid attribute object identifier
     */
    private MetaAttributeDef(String name, Class valueClass, 
        ObjectIdentifier oid) {
        mName = name;
        mValueClass = valueClass;
        mOid = oid;
    }
    
    /**
     * Gets an attribute OID.
     * <P>
     *
     * @return returns attribute OID or null if not defined.
     */
    public ObjectIdentifier getOID() { 
        return mOid; 
    }

    /**
     * Gets an Java class for the attribute values
     * <P>
     *
     * @return returns Java class for the attribute values
     */
    public Class getValueClass() { 
        return mValueClass; 
    }

    /**
     * Gets attribute name
     * <P>
     *
     * @return returns  attribute name
     */
    public String getName() { 
        return mName; 
    }
    
    /**
     * Registers new MetaAttribute defintion
     * Attribute is defined by name, Java class for attribute values and 
     * optional object identifier
     * <P>
     *
     * @param name attribute name
     * @param valueClass attribute value class
     * @param oid attribute object identifier
     * @exception IllegalArgumentException if name or valueClass are null, or
     * conflicting attribute definition already exists
     */
    public static MetaAttributeDef register(String name, Class valueClass, 
        ObjectIdentifier oid) {
        if (name == null) {
            throw new IllegalArgumentException(
                    "Attribute name must not be null");
        }
        if (valueClass == null) {
            throw new IllegalArgumentException(
                    "Attribute value class must not be null");
        }

        MetaAttributeDef newDef = new MetaAttributeDef(name, valueClass, oid);
        MetaAttributeDef oldDef;

        if ((oldDef = (MetaAttributeDef) mNameToAttrDef.get(name)) != null &&
            !oldDef.equals(newDef)) {
            throw new IllegalArgumentException(
                    "Attribute \'" + name + "\' is already defined");
        }
        if (oid != null &&
            (oldDef = (MetaAttributeDef) mOidToAttrDef.get(oid)) != null &&
            !oldDef.equals(newDef)) {
            throw new IllegalArgumentException(
                    "OID \'" + oid + "\' is already in use");
        }
        mNameToAttrDef.put(name, newDef);
        if (oid != null) {
            mOidToAttrDef.put(oid, newDef);
        }
        return newDef;
    }    
    
    /**
     * Compares this attribute definition with another, for equality.
     * <P>
     *
     * @return true iff names, valueClasses and object identifiers 
     * are identical.
     */
    public boolean equals(Object other) {
        if (other == this)
            return true;
	  
        if (other instanceof MetaAttributeDef) {
            MetaAttributeDef otherDef = (MetaAttributeDef) other;

            if ((mOid != null && otherDef.mOid != null && 
                    !mOid.equals(otherDef.mOid)) || 
                (mOid == null && otherDef.mOid != null) ||
                !mName.equals(otherDef.mName) ||  
                !mValueClass.equals(otherDef.mValueClass)) {
                return false;
            }
        }
        return false;
    }
	
    /**
     * Retrieves attribute definition by name
     * <P>
     *
     * @param name attribute name
     * @return attribute definition or null if not found
     */
    public static MetaAttributeDef forName(String name) {
        return (MetaAttributeDef) mNameToAttrDef.get(name);
    }

    /**
     * Retrieves attribute definition by object identifier
     * <P>
     *
     * @param oid attribute object identifier
     * @return attribute definition or null if not found
     */
    public static MetaAttributeDef forOID(ObjectIdentifier oid) {
        return (MetaAttributeDef) mOidToAttrDef.get(oid);
    }

    /**
     * Returns enumeration of the registered attribute names
     * <P>
     *
     * @return returns enumeration of the registered attribute names
     */
    public static Enumeration getAttributeNames() {
        return mNameToAttrDef.keys();
    }

    /**
     * Returns enumeration of the registered attribute object identifiers
     * <P>
     *
     * @return returns enumeration of the attribute object identifiers
     */
    public static Enumeration getAttributeNameOids() {
        return mOidToAttrDef.keys();
    }
}