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.Map;
24 import java.util.Properties;
25 import java.util.Stack;
26
27 import javax.naming.NamingException;
28
29 import org.apache.directory.server.schema.bootstrap.Schema;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34
35
36
37
38
39
40 public abstract class AbstractSchemaLoader implements SchemaLoader
41 {
42
43 private static final Logger LOG = LoggerFactory.getLogger( AbstractSchemaLoader.class );
44
45 protected SchemaLoaderListener listener;
46
47
48 public void setListener( SchemaLoaderListener listener )
49 {
50 this.listener = listener;
51 }
52
53
54 protected final void notifyListenerOrRegistries( Schema schema, Registries registries )
55 {
56 if ( listener != null )
57 {
58 listener.schemaLoaded( schema );
59 }
60
61 if ( registries instanceof SchemaLoaderListener )
62 {
63 if ( registries != listener )
64 {
65 SchemaLoaderListener listener = ( SchemaLoaderListener ) registries;
66 listener.schemaLoaded( schema );
67 }
68 }
69 }
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 protected final void loadDepsFirst( Schema rootAncestor, Stack<String> beenthere, Map<String,Schema> notLoaded, Schema schema,
87 Registries registries, Properties props ) throws Exception
88 {
89 if ( registries.getLoadedSchemas().containsKey( schema.getSchemaName() ) )
90 {
91 LOG.warn( "{} schema has already been loaded" + schema.getSchemaName() );
92 return;
93 }
94
95 beenthere.push( schema.getSchemaName() );
96 String[] deps = schema.getDependencies();
97
98
99 if ( deps == null || deps.length == 0 )
100 {
101 if ( rootAncestor == schema )
102 {
103 load( schema, registries, false );
104 }
105 else
106 {
107 load( schema, registries, true );
108 }
109
110 notLoaded.remove( schema.getSchemaName() );
111 beenthere.pop();
112 return;
113 }
114
115
116
117
118
119
120 for ( String depName : deps )
121 {
122
123
124
125
126
127
128
129 if ( !notLoaded.containsKey( depName ) )
130 {
131 continue;
132 }
133
134 Schema dep = notLoaded.get( depName );
135
136
137 if ( dep == null )
138 {
139
140 dep = getSchema( depName, props );
141 }
142
143 if ( beenthere.contains( dep.getSchemaName() ) )
144 {
145
146 beenthere.push( dep.getSchemaName() );
147 throw new NamingException( "schema dependency cycle detected: " + beenthere );
148 }
149
150 loadDepsFirst( rootAncestor, beenthere, notLoaded, dep, registries, props );
151 }
152
153
154 if ( rootAncestor == schema )
155 {
156 load( schema, registries, false );
157 }
158 else
159 {
160 load( schema, registries, true );
161 }
162
163 notLoaded.remove( schema.getSchemaName() );
164 beenthere.pop();
165 }
166 }