KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ldap > server > schema > GlobalNormalizerRegistry


1 /*
2  * Copyright 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package org.apache.ldap.server.schema;
18
19
20 import org.apache.ldap.common.schema.Normalizer;
21 import org.apache.ldap.server.SystemPartition;
22 import org.apache.ldap.server.schema.bootstrap.BootstrapNormalizerRegistry;
23
24 import javax.naming.NamingException JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28
29 /**
30  * A simple POJO implementation of the NormalizerRegistry service interface.
31  *
32  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
33  * @version $Rev: 169198 $
34  */

35 public class GlobalNormalizerRegistry implements NormalizerRegistry
36 {
37     /** the normalizers in this registry */
38     private final Map JavaDoc normalizers;
39     /** maps an OID to a schema name*/
40     private final Map JavaDoc oidToSchema;
41     /** the monitor for delivering callback events */
42     private NormalizerRegistryMonitor monitor;
43     /** the underlying bootstrap registry to delegate on misses to */
44     private BootstrapNormalizerRegistry bootstrap;
45     /** the system partition where we keep attributeType updates */
46     private SystemPartition systemPartition;
47
48
49     // ------------------------------------------------------------------------
50
// C O N S T R U C T O R S
51
// ------------------------------------------------------------------------
52

53
54     /**
55      * Creates a default NormalizerRegistry by initializing the map and the
56      * montior.
57      */

58     public GlobalNormalizerRegistry( SystemPartition systemPartition,
59             BootstrapNormalizerRegistry bootstrap )
60     {
61         this.oidToSchema = new HashMap JavaDoc();
62         this.normalizers = new HashMap JavaDoc();
63         this.monitor = new NormalizerRegistryMonitorAdapter();
64
65         this.bootstrap = bootstrap;
66         if ( this.bootstrap == null )
67         {
68             throw new NullPointerException JavaDoc( "the bootstrap registry cannot be null" ) ;
69         }
70
71         this.systemPartition = systemPartition;
72         if ( this.systemPartition == null )
73         {
74             throw new NullPointerException JavaDoc( "the system partition cannot be null" ) ;
75         }
76     }
77
78
79     /**
80      * Sets the monitor used by this registry.
81      *
82      * @param monitor the monitor to set for registry event callbacks
83      */

84     public void setMonitor( NormalizerRegistryMonitor monitor )
85     {
86         this.monitor = monitor;
87     }
88
89
90     // ------------------------------------------------------------------------
91
// Service Methods
92
// ------------------------------------------------------------------------
93

94
95     public void register( String JavaDoc schema, String JavaDoc oid, Normalizer normalizer )
96             throws NamingException JavaDoc
97     {
98         if ( normalizers.containsKey( oid ) || bootstrap.hasNormalizer( oid ) )
99         {
100             NamingException JavaDoc e = new NamingException JavaDoc( "Normalizer with OID "
101                 + oid + " already registered!" );
102             monitor.registerFailed( oid, normalizer, e );
103             throw e;
104         }
105
106         oidToSchema.put( oid, schema );
107         normalizers.put( oid, normalizer );
108         monitor.registered( oid, normalizer );
109     }
110
111
112     public Normalizer lookup( String JavaDoc oid ) throws NamingException JavaDoc
113     {
114         Normalizer c;
115         NamingException JavaDoc e;
116
117         if ( normalizers.containsKey( oid ) )
118         {
119             c = ( Normalizer ) normalizers.get( oid );
120             monitor.lookedUp( oid, c );
121             return c;
122         }
123
124         if ( bootstrap.hasNormalizer( oid ) )
125         {
126             c = bootstrap.lookup( oid );
127             monitor.lookedUp( oid, c );
128             return c;
129         }
130
131         e = new NamingException JavaDoc( "Normalizer not found for OID: " + oid );
132         monitor.lookupFailed( oid, e );
133         throw e;
134     }
135
136
137     public boolean hasNormalizer( String JavaDoc oid )
138     {
139         return normalizers.containsKey( oid ) || bootstrap.hasNormalizer( oid );
140     }
141
142
143     public String JavaDoc getSchemaName( String JavaDoc oid ) throws NamingException JavaDoc
144     {
145         if ( ! Character.isDigit( oid.charAt( 0 ) ) )
146         {
147             throw new NamingException JavaDoc( "OID " + oid + " is not a numeric OID" );
148         }
149         
150         if ( oidToSchema.containsKey( oid ) )
151         {
152             return ( String JavaDoc ) oidToSchema.get( oid );
153         }
154
155         if ( bootstrap.hasNormalizer( oid ) )
156         {
157             return bootstrap.getSchemaName( oid );
158         }
159
160         throw new NamingException JavaDoc( "OID " + oid + " not found in oid to " +
161             "schema name map!" );
162     }
163 }
164
Popular Tags