View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     https://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
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   * A factory that allows callers a means to get a handle on an LdapCodecService
30   * implementation regardless of the environment in which they're accessing it.
31   * In an OSGi environment, the BundleActivator binds the LdapCodecService 
32   * class member forever to the DefaultLdapCodecService. If in 
33   * 
34   * In a standard standalone mode, the Bundle
35   *
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public final class LdapApiServiceFactory
39  {
40      /** Logger for this class */
41      private static final Logger LOG = LoggerFactory.getLogger( LdapApiServiceFactory.class );
42  
43      /** The LdapCodecService singleton bound to this factory */
44      private static LdapApiService ldapCodecService;
45  
46      /** Whether or not the standalone implementation is being used */
47      private static boolean usingStandaloneImplementation;
48  
49  
50      /**
51       * Private constructor
52       */
53      private LdapApiServiceFactory()
54      {
55      }
56  
57  
58      /**
59       * Checks to see if the factory is initialized.
60       *
61       * @return true if initialized, false otherwise
62       */
63      public static boolean isInitialized()
64      {
65          return ldapCodecService != null;
66      }
67  
68  
69      /**
70       * Checks to see if the factory is using the standalone implementation.
71       *
72       * @return true if using the standalone implementation, false otherwise.
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       * Gets the singleton instance of the LdapCodecService.
89       *
90       * @return a valid instance implementation based on environment and the 
91       * availability of bindings.
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      * Initialization can only take place once. There after an exception 
106      * results.
107      * 
108      * @param ldapCodecService The LDAP Codec Service to initialize with.
109      */
110     public static void initialize( LdapApiService ldapCodecService )
111     {
112         /*
113          * If the class member is already set we have problems.
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          * If the argument is null, then we attempt discovery
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 }