001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.directory.api.ldap.codec.api; 021 022 023import org.apache.directory.api.ldap.codec.osgi.DefaultLdapCodecService; 024import org.slf4j.Logger; 025import org.slf4j.LoggerFactory; 026 027 028/** 029 * A factory that allows callers a means to get a handle on an LdapCodecService 030 * implementation regardless of the environment in which they're accessing it. 031 * In an OSGi environment, the BundleActivator binds the LdapCodecService 032 * class member forever to the {@link DefaultLdapCodecService}. If in 033 * 034 * In a standard standalone mode, the Bundle 035 * 036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 037 */ 038public class LdapApiServiceFactory 039{ 040 /** Logger for this class */ 041 private static final Logger LOG = LoggerFactory.getLogger( LdapApiServiceFactory.class ); 042 043 /** The LdapCodecService singleton bound to this factory */ 044 private static LdapApiService ldapCodecService; 045 046 /** Whether or not the standalone implementation is being used */ 047 private static boolean usingStandaloneImplementation; 048 049 050 /** 051 * Checks to see if the factory is initialized. 052 * 053 * @return true if initialized, false otherwise 054 */ 055 public static boolean isInitialized() 056 { 057 return ldapCodecService != null; 058 } 059 060 061 /** 062 * Checks to see if the factory is using the standalone implementation. 063 * 064 * @return true if using the standalone implementation, false otherwise. 065 */ 066 public static boolean isUsingStandaloneImplementation() 067 { 068 if ( !isInitialized() ) 069 { 070 String msg = "Not initialized yet!"; 071 LOG.error( msg ); 072 throw new IllegalStateException( msg ); 073 } 074 075 return usingStandaloneImplementation; 076 } 077 078 079 /** 080 * Gets the singleton instance of the LdapCodecService. 081 * 082 * @return a valid instance implementation based on environment and the 083 * availability of bindings. 084 */ 085 public static LdapApiService getSingleton() 086 { 087 if ( ldapCodecService == null ) 088 { 089 initialize( null ); 090 } 091 092 return ldapCodecService; 093 } 094 095 096 /** 097 * Initialization can only take place once. There after an exception 098 * results. 099 * 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}