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 public class StandaloneLdapApiService extends DefaultLdapCodecService
58 {
59
60 private static final Logger LOG = LoggerFactory.getLogger( StandaloneLdapApiService.class );
61
62
63 public static final String CONTROLS_LIST = "apacheds.controls";
64
65
66 public static final String EXTENDED_OPERATIONS_LIST = "apacheds.extendedOperations";
67
68
69 private static final String OLD_DEFAULT_CONTROLS_LIST = "default.controls";
70
71
72 private static final String OLD_EXTRA_EXTENDED_OPERATION_LIST = "extra.extendedOperations";
73
74
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 public StandaloneLdapApiService() throws Exception
141 {
142 this( getControlsFromSystemProperties(), getExtendedOperationsFromSystemProperties() );
143 }
144
145
146 public StandaloneLdapApiService( List<String> controls, List<String> extendedOperations ) throws Exception
147 {
148 CodecFactoryUtil.loadStockControls( getControlFactories(), this );
149
150 CodecFactoryUtil.loadStockExtendedOperations( getExtendedOperationsFactories(), this );
151
152
153 loadControls( controls );
154
155
156 loadExtendedOperations( extendedOperations );
157
158 if ( getProtocolCodecFactory() == null )
159 {
160 try
161 {
162 @SuppressWarnings("unchecked")
163 Class<? extends ProtocolCodecFactory> clazz = ( Class<? extends ProtocolCodecFactory> )
164 Class.forName( DEFAULT_PROTOCOL_CODEC_FACTORY );
165 Constructor<? extends ProtocolCodecFactory> constructor =
166 clazz.getConstructor( LdapApiService.class );
167 if ( constructor != null )
168 {
169 setProtocolCodecFactory( constructor.newInstance( this ) );
170 }
171 else
172 {
173 setProtocolCodecFactory( clazz.newInstance() );
174 }
175 }
176 catch ( Exception cause )
177 {
178 throw new RuntimeException( "Failed to load default codec factory.", cause );
179 }
180 }
181 }
182
183
184
185
186
187
188
189 private static List<String> getControlsFromSystemProperties() throws Exception
190 {
191 List<String> controlsList = new ArrayList<String>();
192
193
194 String controlsString = System.getProperty( CONTROLS_LIST );
195
196 if ( !Strings.isEmpty( controlsString ) )
197 {
198 for ( String control : controlsString.split( "," ) )
199 {
200 controlsList.add( control );
201 }
202 }
203 else
204 {
205
206 String oldDefaultControlsString = System.getProperty( OLD_DEFAULT_CONTROLS_LIST );
207
208 if ( !Strings.isEmpty( oldDefaultControlsString ) )
209 {
210 for ( String control : oldDefaultControlsString.split( "," ) )
211 {
212 controlsList.add( control );
213 }
214 }
215 }
216
217 return controlsList;
218 }
219
220
221
222
223
224
225
226 private static List<String> getExtendedOperationsFromSystemProperties() throws Exception
227 {
228 List<String> extendedOperationsList = new ArrayList<String>();
229
230
231 String defaultExtendedOperationsList = System.getProperty( EXTENDED_OPERATIONS_LIST );
232
233 if ( !Strings.isEmpty( defaultExtendedOperationsList ) )
234 {
235 for ( String extendedOperation : defaultExtendedOperationsList.split( "," ) )
236 {
237 extendedOperationsList.add( extendedOperation );
238 }
239 }
240 else
241 {
242
243 String oldDefaultControlsString = System.getProperty( OLD_EXTRA_EXTENDED_OPERATION_LIST );
244
245 if ( !Strings.isEmpty( oldDefaultControlsString ) )
246 {
247 for ( String extendedOperation : oldDefaultControlsString.split( "," ) )
248 {
249 extendedOperationsList.add( extendedOperation );
250 }
251 }
252 }
253
254 return extendedOperationsList;
255 }
256
257
258
259
260
261 private void loadControls( List<String> controlsList ) throws Exception
262 {
263
264 if ( controlsList.size() > 0 )
265 {
266 for ( String controlFQCN : controlsList )
267 {
268 loadControl( controlFQCN );
269 }
270 }
271 }
272
273
274
275
276
277 private void loadControl( String controlFQCN ) throws Exception
278 {
279 if ( getControlFactories().containsKey( controlFQCN ) )
280 {
281 LOG.debug( "Factory for control {} was already loaded", controlFQCN );
282 return;
283 }
284
285 Class<?>[] types = new Class<?>[]
286 { LdapApiService.class };
287
288
289 @SuppressWarnings("unchecked")
290 Class<? extends ControlFactory<?>> clazz = ( Class<? extends ControlFactory<?>> ) Class
291 .forName( controlFQCN.trim() );
292 Constructor<?> constructor = clazz.getConstructor( types );
293
294 ControlFactory<?> factory = ( ControlFactory<?> ) constructor.newInstance( new Object[]
295 { this } );
296 getControlFactories().put( factory.getOid(), factory );
297
298 LOG.info( "Registered control factory: {}", factory.getOid() );
299 }
300
301
302
303
304
305 private void loadExtendedOperations( List<String> extendedOperationsList ) throws Exception
306 {
307
308 if ( extendedOperationsList.size() > 0 )
309 {
310 for ( String extendedOperationFQCN : extendedOperationsList )
311 {
312 loadExtendedOperation( extendedOperationFQCN );
313 }
314 }
315 }
316
317
318
319
320
321 private void loadExtendedOperation( String extendedOperationFQCN ) throws Exception
322 {
323 if ( getExtendedOperationsFactories().containsKey( extendedOperationFQCN ) )
324 {
325 LOG.debug( "Factory for extended operation {} was already loaded", extendedOperationFQCN );
326 return;
327 }
328
329 Class<?>[] types = new Class<?>[]
330 { LdapApiService.class };
331
332
333
334 @SuppressWarnings("unchecked")
335 Class<? extends ExtendedOperationFactory> clazz = ( Class<? extends ExtendedOperationFactory> ) Class
336 .forName( extendedOperationFQCN.trim() );
337 Constructor<?> constructor = clazz.getConstructor( types );
338
339 ExtendedOperationFactory factory = ( ExtendedOperationFactory ) constructor
340 .newInstance( new Object[]
341 { this } );
342 getExtendedOperationsFactories().put( factory.getOid(), factory );
343
344 LOG.info( "Registered pre-bundled extended operation factory: {}", factory.getOid() );
345 }
346 }