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.MatchingRule;
30
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35
36
37
38
39
40
41 public class DefaultMatchingRuleRegistry implements MatchingRuleRegistry
42 {
43
44 private static final Logger LOG = LoggerFactory.getLogger( DefaultMatchingRuleRegistry.class );
45
46 private final Map<String,MatchingRule> byOid;
47
48 private final OidRegistry oidRegistry;
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public DefaultMatchingRuleRegistry( OidRegistry oidRegistry )
64 {
65 this.oidRegistry = oidRegistry;
66 this.byOid = new HashMap<String,MatchingRule>();
67 }
68
69
70
71
72
73
74
75
76
77 public MatchingRule lookup( String id ) throws NamingException
78 {
79 id = oidRegistry.getOid( id );
80
81 if ( byOid.containsKey( id ) )
82 {
83 MatchingRule matchingRule = byOid.get( id );
84 if ( LOG.isDebugEnabled() )
85 {
86 LOG.debug( "lookup with id '"+id+"' of matchingRule: " + matchingRule );
87 }
88 return matchingRule;
89 }
90
91 throw new NamingException( "Unknown MatchingRule OID " + id );
92 }
93
94
95
96
97
98 public void register( MatchingRule matchingRule ) throws NamingException
99 {
100 if ( byOid.containsKey( matchingRule.getOid() ) )
101 {
102 throw new NamingException( "matchingRule w/ OID " + matchingRule.getOid()
103 + " has already been registered!" );
104 }
105
106 String[] names = matchingRule.getNamesRef();
107
108 for ( String name : names )
109 {
110 oidRegistry.register( name, matchingRule.getOid() );
111 }
112 oidRegistry.register( matchingRule.getOid(), matchingRule.getOid() );
113
114 byOid.put( matchingRule.getOid(), matchingRule );
115 if ( LOG.isDebugEnabled() )
116 {
117 LOG.debug( "registed matchingRule: " + matchingRule);
118 }
119 }
120
121
122
123
124
125 public boolean hasMatchingRule( String id )
126 {
127 if ( oidRegistry.hasOid( id ) )
128 {
129 try
130 {
131 return byOid.containsKey( oidRegistry.getOid( id ) );
132 }
133 catch ( NamingException e )
134 {
135 return false;
136 }
137 }
138
139 return false;
140 }
141
142
143 public String getSchemaName( String id ) throws NamingException
144 {
145 id = oidRegistry.getOid( id );
146 MatchingRule mr = byOid.get( id );
147 if ( mr != null )
148 {
149 return mr.getSchema();
150 }
151
152 throw new NamingException( "OID " + id + " not found in oid to " + "MatchingRule name map!" );
153 }
154
155
156 public Iterator<MatchingRule> iterator()
157 {
158 return byOid.values().iterator();
159 }
160
161
162 public void unregister( String numericOid ) throws NamingException
163 {
164 if ( ! Character.isDigit( numericOid.charAt( 0 ) ) )
165 {
166 throw new NamingException( "Looks like the arg is not a numeric OID" );
167 }
168
169 byOid.remove( numericOid );
170 }
171 }