1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.schema.registries;
21
22
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.Map;
26
27 import javax.naming.NamingException;
28
29 import org.apache.directory.shared.ldap.schema.ObjectClass;
30 import org.apache.directory.shared.ldap.util.StringTools;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35
36
37
38
39
40
41 public class DefaultObjectClassRegistry implements ObjectClassRegistry
42 {
43
44 private static final Logger LOG = LoggerFactory.getLogger( DefaultObjectClassRegistry.class );
45
46
47 private static final boolean IS_DEBUG = LOG.isDebugEnabled();
48
49
50 private final Map<String,ObjectClass> byOid;
51
52 private final OidRegistry oidRegistry;
53
54
55
56
57
58
59
60
61
62
63
64
65 public DefaultObjectClassRegistry( OidRegistry oidRegistry )
66 {
67 this.byOid = new HashMap<String,ObjectClass>();
68 this.oidRegistry = oidRegistry;
69 }
70
71
72
73
74
75
76
77 public void register( ObjectClass objectClass ) throws NamingException
78 {
79 if ( byOid.containsKey( objectClass.getOid() ) )
80 {
81 throw new NamingException( "objectClass w/ OID " + objectClass.getOid()
82 + " has already been registered!" );
83 }
84
85 if ( objectClass.getNamesRef() != null && objectClass.getNamesRef().length > 0 )
86 {
87 oidRegistry.register( objectClass.getName(), objectClass.getOid() );
88 }
89 else
90 {
91 oidRegistry.register( objectClass.getOid(), objectClass.getOid() );
92 }
93
94 byOid.put( objectClass.getOid(), objectClass );
95
96 if ( IS_DEBUG )
97 {
98 LOG.debug( "registered objectClass: " + objectClass );
99 }
100 }
101
102
103 public ObjectClass lookup( String id ) throws NamingException
104 {
105 if ( StringTools.isEmpty( id ) )
106 {
107 throw new NamingException( "name should not be empty" );
108 }
109
110 String oid = oidRegistry.getOid( id.toLowerCase() );
111
112 if ( !byOid.containsKey( oid ) )
113 {
114 throw new NamingException( "objectClass w/ OID " + oid + " not registered!" );
115 }
116
117 ObjectClass objectClass = byOid.get( oid );
118
119 if ( IS_DEBUG )
120 {
121 LOG.debug( "looked objectClass with OID '" + oid + "' and got back " + objectClass );
122 }
123 return objectClass;
124 }
125
126
127 public boolean hasObjectClass( String id )
128 {
129 if ( oidRegistry.hasOid( id ) )
130 {
131 try
132 {
133 return byOid.containsKey( oidRegistry.getOid( id ) );
134 }
135 catch ( NamingException e )
136 {
137 return false;
138 }
139 }
140
141 return false;
142 }
143
144
145 public String getSchemaName( String id ) throws NamingException
146 {
147 id = oidRegistry.getOid( id );
148 ObjectClass oc = byOid.get( id );
149 if ( oc != null )
150 {
151 return oc.getSchema();
152 }
153
154 throw new NamingException( "OID " + id + " not found in oid to " + "ObjectClass map!" );
155 }
156
157
158 public Iterator<ObjectClass> iterator()
159 {
160 return byOid.values().iterator();
161 }
162
163
164 public void unregister( String numericOid ) throws NamingException
165 {
166 if ( ! Character.isDigit( numericOid.charAt( 0 ) ) )
167 {
168 throw new NamingException( "Looks like the arg is not a numeric OID" );
169 }
170
171 byOid.remove( numericOid );
172 }
173 }