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   *     http://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.ldap.codec.osgi.DefaultLdapCodecService;
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 {@link 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 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       * Checks to see if the factory is initialized.
52       *
53       * @return true if initialized, false otherwise
54       */
55      public static boolean isInitialized()
56      {
57          return ldapCodecService != null;
58      }
59  
60  
61      /**
62       * Checks to see if the factory is using the standalone implementation.
63       *
64       * @return true if using the standalone implementation, false otherwise.
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       * Gets the singleton instance of the LdapCodecService.
81       *
82       * @return a valid instance implementation based on environment and the 
83       * availability of bindings.
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       * Initialization can only take place once. There after an exception 
98       * results.
99       * 
100      * @param ldapCodecService The LDAP Codec Service to initialize with.
101      */
102     public static void initialize( LdapApiService ldapCodecService )
103     {
104         /*
105          * If the class member is already set we have problems.
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          * If the argument is null, then we attempt discovery
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 }