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.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  
27  /**
28   * A factory that allows callers a means to get a handle on an LdapCodecService
29   * implementation regardless of the environment in which they're accessing it.
30   * In an OSGi environment, the BundleActivator binds the LdapCodecService 
31   * class member forever to the DefaultLdapCodecService. If in 
32   * 
33   * In a standard standalone mode, the Bundle
34   *
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   */
37  public final class LdapApiServiceFactory
38  {
39      /** Logger for this class */
40      private static final Logger LOG = LoggerFactory.getLogger( LdapApiServiceFactory.class );
41  
42      /** The LdapCodecService singleton bound to this factory */
43      private static LdapApiService ldapCodecService;
44  
45      /** Whether or not the standalone implementation is being used */
46      private static boolean usingStandaloneImplementation;
47  
48  
49      /**
50       * Private constructor
51       */
52      private LdapApiServiceFactory()
53      {
54      }
55  
56  
57      /**
58       * Checks to see if the factory is initialized.
59       *
60       * @return true if initialized, false otherwise
61       */
62      public static boolean isInitialized()
63      {
64          return ldapCodecService != null;
65      }
66  
67  
68      /**
69       * Checks to see if the factory is using the standalone implementation.
70       *
71       * @return true if using the standalone implementation, false otherwise.
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       * Gets the singleton instance of the LdapCodecService.
88       *
89       * @return a valid instance implementation based on environment and the 
90       * availability of bindings.
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      * Initialization can only take place once. There after an exception 
105      * results.
106      * 
107      * @param ldapCodecService The LDAP Codec Service to initialize with.
108      */
109     public static void initialize( LdapApiService ldapCodecService )
110     {
111         /*
112          * If the class member is already set we have problems.
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          * 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( "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 }