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