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