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.ArrayList;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
28
29 import javax.naming.NamingException;
30
31 import org.apache.directory.shared.ldap.schema.Normalizer;
32 import org.apache.directory.shared.ldap.schema.syntax.NormalizerDescription;
33
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37
38
39
40
41
42
43
44 public class DefaultNormalizerRegistry implements NormalizerRegistry
45 {
46
47 private static final Logger LOG = LoggerFactory.getLogger( DefaultNormalizerRegistry.class );
48
49
50 private static final boolean DEBUG = LOG.isDebugEnabled();
51
52
53 private final Map<String,Normalizer> byOid;
54
55
56 private final Map<String,NormalizerDescription> oidToDescription;
57
58
59
60
61
62
63
64
65
66
67 public DefaultNormalizerRegistry()
68 {
69 this.byOid = new HashMap<String, Normalizer>();
70 this.oidToDescription = new HashMap<String, NormalizerDescription>();
71 }
72
73
74
75
76
77
78
79 public void register( NormalizerDescription description, Normalizer normalizer ) throws NamingException
80 {
81 String oid = description.getNumericOid();
82 if ( byOid.containsKey( oid ) )
83 {
84 throw new NamingException( "Normalizer already " + "registered for OID " + oid );
85 }
86
87 oidToDescription.put( oid, description );
88 byOid.put( oid, normalizer );
89
90 if ( DEBUG )
91 {
92 LOG.debug( "registered normalizer with oid: " + oid );
93 }
94 }
95
96
97 public Normalizer lookup( String oid ) throws NamingException
98 {
99 if ( !byOid.containsKey( oid ) )
100 {
101 throw new NamingException( "Normalizer for OID " + oid + " does not exist!" );
102 }
103
104 Normalizer normalizer = byOid.get( oid );
105
106 if ( DEBUG )
107 {
108 LOG.debug( "registered normalizer with oid: " + oid );
109 }
110
111 return normalizer;
112 }
113
114
115 public boolean hasNormalizer( String oid )
116 {
117 return byOid.containsKey( oid );
118 }
119
120
121 public String getSchemaName( String oid ) throws NamingException
122 {
123 if ( ! Character.isDigit( oid.charAt( 0 ) ) )
124 {
125 throw new NamingException( "Looks like the arg is not a numeric OID" );
126 }
127
128 if ( oidToDescription.containsKey( oid ) )
129 {
130 return getSchema( oidToDescription.get( oid ) );
131 }
132
133 throw new NamingException( "OID " + oid + " not found in oid to " + "schema name map!" );
134 }
135
136
137 private static String getSchema( NormalizerDescription desc )
138 {
139 List values = desc.getExtensions().get( "X-SCHEMA" );
140
141 if ( values == null || values.size() == 0 )
142 {
143 return "other";
144 }
145
146 return desc.getExtensions().get( "X-SCHEMA" ).get( 0 );
147 }
148
149
150 public Iterator<String> oidIterator()
151 {
152 return byOid.keySet().iterator();
153 }
154
155
156 public void unregister( String oid ) throws NamingException
157 {
158 if ( ! Character.isDigit( oid.charAt( 0 ) ) )
159 {
160 throw new NamingException( "OID " + oid + " is not a numeric OID" );
161 }
162
163 this.byOid.remove( oid );
164 this.oidToDescription.remove( oid );
165 }
166
167
168 public void unregisterSchemaElements( String schemaName )
169 {
170 List<String> oids = new ArrayList<String>( byOid.keySet() );
171 for ( String oid : oids )
172 {
173 NormalizerDescription description = oidToDescription.get( oid );
174 String schemaNameForOid = getSchema( description );
175 if ( schemaNameForOid.equalsIgnoreCase( schemaName ) )
176 {
177 byOid.remove( oid );
178 oidToDescription.remove( oid );
179 }
180 }
181 }
182
183
184 public void renameSchema( String originalSchemaName, String newSchemaName )
185 {
186 List<String> oids = new ArrayList<String>( byOid.keySet() );
187 for ( String oid : oids )
188 {
189 NormalizerDescription description = oidToDescription.get( oid );
190 String schemaNameForOid = getSchema( description );
191 if ( schemaNameForOid.equalsIgnoreCase( originalSchemaName ) )
192 {
193 List<String> schemaExt = description.getExtensions().get( "X-SCHEMA" );
194 schemaExt.clear();
195 schemaExt.add( newSchemaName );
196 }
197 }
198 }
199
200
201 public Iterator<NormalizerDescription> normalizerDescriptionIterator()
202 {
203 return oidToDescription.values().iterator();
204 }
205 }