1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.ldap.codec.standalone;
21
22
23 import java.lang.reflect.Constructor;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.apache.directory.api.ldap.codec.api.ControlFactory;
28 import org.apache.directory.api.ldap.codec.api.ExtendedOperationFactory;
29 import org.apache.directory.api.ldap.codec.api.LdapApiService;
30 import org.apache.directory.api.ldap.codec.osgi.DefaultLdapCodecService;
31 import org.apache.directory.api.util.Strings;
32 import org.apache.mina.filter.codec.ProtocolCodecFactory;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public class StandaloneLdapApiService extends DefaultLdapCodecService
60 {
61
62 private static final Logger LOG = LoggerFactory.getLogger( StandaloneLdapApiService.class );
63
64
65 public static final String CONTROLS_LIST = "apacheds.controls";
66
67
68 public static final String EXTENDED_OPERATIONS_LIST = "apacheds.extendedOperations";
69
70
71 private static final String OLD_DEFAULT_CONTROLS_LIST = "default.controls";
72
73
74 private static final String OLD_EXTRA_EXTENDED_OPERATION_LIST = "extra.extendedOperations";
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143 public StandaloneLdapApiService() throws Exception
144 {
145 this( getControlsFromSystemProperties(), getExtendedOperationsFromSystemProperties() );
146 }
147
148
149
150
151
152
153
154
155
156 public StandaloneLdapApiService( List<String> controls, List<String> extendedOperations ) throws Exception
157 {
158 CodecFactoryUtil.loadStockControls( getControlFactories(), this );
159
160 CodecFactoryUtil.loadStockExtendedOperations( getExtendedOperationsFactories(), this );
161
162
163 loadControls( controls );
164
165
166 loadExtendedOperations( extendedOperations );
167
168 if ( getProtocolCodecFactory() == null )
169 {
170 try
171 {
172 @SuppressWarnings("unchecked")
173 Class<? extends ProtocolCodecFactory> clazz = ( Class<? extends ProtocolCodecFactory> )
174 Class.forName( DEFAULT_PROTOCOL_CODEC_FACTORY );
175 Constructor<? extends ProtocolCodecFactory> constructor =
176 clazz.getConstructor( LdapApiService.class );
177 if ( constructor != null )
178 {
179 setProtocolCodecFactory( constructor.newInstance( this ) );
180 }
181 else
182 {
183 setProtocolCodecFactory( clazz.newInstance() );
184 }
185 }
186 catch ( Exception cause )
187 {
188 throw new RuntimeException( "Failed to load default codec factory.", cause );
189 }
190 }
191 }
192
193
194
195
196
197
198
199 private static List<String> getControlsFromSystemProperties() throws Exception
200 {
201 List<String> controlsList = new ArrayList<>();
202
203
204 String controlsString = System.getProperty( CONTROLS_LIST );
205
206 if ( !Strings.isEmpty( controlsString ) )
207 {
208 for ( String control : controlsString.split( "," ) )
209 {
210 controlsList.add( control );
211 }
212 }
213 else
214 {
215
216 String oldDefaultControlsString = System.getProperty( OLD_DEFAULT_CONTROLS_LIST );
217
218 if ( !Strings.isEmpty( oldDefaultControlsString ) )
219 {
220 for ( String control : oldDefaultControlsString.split( "," ) )
221 {
222 controlsList.add( control );
223 }
224 }
225 }
226
227 return controlsList;
228 }
229
230
231
232
233
234
235
236 private static List<String> getExtendedOperationsFromSystemProperties() throws Exception
237 {
238 List<String> extendedOperationsList = new ArrayList<>();
239
240
241 String defaultExtendedOperationsList = System.getProperty( EXTENDED_OPERATIONS_LIST );
242
243 if ( !Strings.isEmpty( defaultExtendedOperationsList ) )
244 {
245 for ( String extendedOperation : defaultExtendedOperationsList.split( "," ) )
246 {
247 extendedOperationsList.add( extendedOperation );
248 }
249 }
250 else
251 {
252
253 String oldDefaultControlsString = System.getProperty( OLD_EXTRA_EXTENDED_OPERATION_LIST );
254
255 if ( !Strings.isEmpty( oldDefaultControlsString ) )
256 {
257 for ( String extendedOperation : oldDefaultControlsString.split( "," ) )
258 {
259 extendedOperationsList.add( extendedOperation );
260 }
261 }
262 }
263
264 return extendedOperationsList;
265 }
266
267
268
269
270
271 private void loadControls( List<String> controlsList ) throws Exception
272 {
273
274 if ( !controlsList.isEmpty() )
275 {
276 for ( String controlFQCN : controlsList )
277 {
278 loadControl( controlFQCN );
279 }
280 }
281 }
282
283
284
285
286
287 private void loadControl( String controlFQCN ) throws Exception
288 {
289 if ( getControlFactories().containsKey( controlFQCN ) )
290 {
291 LOG.debug( "Factory for control {} was already loaded", controlFQCN );
292 return;
293 }
294
295 Class<?>[] types = new Class<?>[]
296 { LdapApiService.class };
297
298
299 @SuppressWarnings("unchecked")
300 Class<? extends ControlFactory<?>> clazz = ( Class<? extends ControlFactory<?>> ) Class
301 .forName( controlFQCN.trim() );
302 Constructor<?> constructor = clazz.getConstructor( types );
303
304 ControlFactory<?> factory = ( ControlFactory<?> ) constructor.newInstance( new Object[]
305 { this } );
306 getControlFactories().put( factory.getOid(), factory );
307
308 LOG.info( "Registered control factory: {}", factory.getOid() );
309 }
310
311
312
313
314
315 private void loadExtendedOperations( List<String> extendedOperationsList ) throws Exception
316 {
317
318 if ( !extendedOperationsList.isEmpty() )
319 {
320 for ( String extendedOperationFQCN : extendedOperationsList )
321 {
322 loadExtendedOperation( extendedOperationFQCN );
323 }
324 }
325 }
326
327
328
329
330
331 private void loadExtendedOperation( String extendedOperationFQCN ) throws Exception
332 {
333 if ( getExtendedOperationsFactories().containsKey( extendedOperationFQCN ) )
334 {
335 LOG.debug( "Factory for extended operation {} was already loaded", extendedOperationFQCN );
336 return;
337 }
338
339 Class<?>[] types = new Class<?>[]
340 { LdapApiService.class };
341
342
343
344 @SuppressWarnings("unchecked")
345 Class<? extends ExtendedOperationFactory> clazz = ( Class<? extends ExtendedOperationFactory> ) Class
346 .forName( extendedOperationFQCN.trim() );
347 Constructor<?> constructor = clazz.getConstructor( types );
348
349 ExtendedOperationFactory factory = ( ExtendedOperationFactory ) constructor
350 .newInstance( new Object[]
351 { this } );
352 getExtendedOperationsFactories().put( factory.getOid(), factory );
353
354 LOG.info( "Registered pre-bundled extended operation factory: {}", factory.getOid() );
355 }
356 }