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.api;
21
22
23 import org.apache.directory.api.ldap.codec.osgi.DefaultLdapCodecService;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27
28
29
30
31
32
33
34
35
36
37
38 public class LdapApiServiceFactory
39 {
40
41 private static final Logger LOG = LoggerFactory.getLogger( LdapApiServiceFactory.class );
42
43
44 private static LdapApiService ldapCodecService;
45
46
47 private static boolean usingStandaloneImplementation;
48
49
50
51
52
53
54
55 public static boolean isInitialized()
56 {
57 return ldapCodecService != null;
58 }
59
60
61
62
63
64
65
66 public static boolean isUsingStandaloneImplementation()
67 {
68 if ( !isInitialized() )
69 {
70 String msg = "Not initialized yet!";
71 LOG.error( msg );
72 throw new IllegalStateException( msg );
73 }
74
75 return usingStandaloneImplementation;
76 }
77
78
79
80
81
82
83
84
85 public static LdapApiService getSingleton()
86 {
87 if ( ldapCodecService == null )
88 {
89 initialize( null );
90 }
91
92 return ldapCodecService;
93 }
94
95
96
97
98
99
100
101
102 public static void initialize( LdapApiService ldapCodecService )
103 {
104
105
106
107
108 if ( LdapApiServiceFactory.ldapCodecService != null )
109 {
110 StringBuilder sb = new StringBuilder( "The LdapCodecService is already set to an instance of " );
111 sb.append( LdapApiServiceFactory.class.getName() );
112 LOG.error( sb.toString() );
113 throw new IllegalStateException( sb.toString() );
114 }
115
116
117
118
119
120 if ( ldapCodecService == null )
121 {
122 try
123 {
124 @SuppressWarnings("unchecked")
125 Class<? extends LdapApiService> serviceClass = ( Class<? extends LdapApiService> )
126 Class.forName( "org.apache.directory.api.ldap.codec.standalone.StandaloneLdapApiService" );
127 LdapApiServiceFactory.ldapCodecService = serviceClass.newInstance();
128 usingStandaloneImplementation = true;
129 }
130 catch ( Exception e )
131 {
132 LOG.error( "Failed to instantiate a viable instance, instantiating new instance of ", e );
133 }
134 }
135 else
136 {
137 usingStandaloneImplementation = false;
138 LdapApiServiceFactory.ldapCodecService = ldapCodecService;
139 }
140 }
141 }