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.i18n.I18n;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27
28
29
30
31
32
33
34
35
36
37
38 public final 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 private LdapApiServiceFactory()
54 {
55 }
56
57
58
59
60
61
62
63 public static boolean isInitialized()
64 {
65 return ldapCodecService != null;
66 }
67
68
69
70
71
72
73
74 public static boolean isUsingStandaloneImplementation()
75 {
76 if ( !isInitialized() )
77 {
78 String msg = I18n.err( I18n.ERR_05200_NOT_INITIALIZED_YET );
79 LOG.error( msg );
80 throw new IllegalStateException( msg );
81 }
82
83 return usingStandaloneImplementation;
84 }
85
86
87
88
89
90
91
92
93 public static LdapApiService getSingleton()
94 {
95 if ( ldapCodecService == null )
96 {
97 initialize( null );
98 }
99
100 return ldapCodecService;
101 }
102
103
104
105
106
107
108
109
110 public static void initialize( LdapApiService ldapCodecService )
111 {
112
113
114
115
116 if ( LdapApiServiceFactory.ldapCodecService != null )
117 {
118 String msg = I18n.err( I18n.ERR_05201_INSTANCE_ALREADY_SET, LdapApiServiceFactory.class.getName() );
119 LOG.error( msg );
120 throw new IllegalStateException( msg );
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( I18n.err( I18n.ERR_05202_FAILED_TO_INSTANCIATE, e.getMessage() ) );
140 }
141 }
142 else
143 {
144 usingStandaloneImplementation = false;
145 LdapApiServiceFactory.ldapCodecService = ldapCodecService;
146 }
147 }
148 }