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 * https://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.i18n.I18n; 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 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 final 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 * Private constructor 052 */ 053 private LdapApiServiceFactory() 054 { 055 } 056 057 058 /** 059 * Checks to see if the factory is initialized. 060 * 061 * @return true if initialized, false otherwise 062 */ 063 public static boolean isInitialized() 064 { 065 return ldapCodecService != null; 066 } 067 068 069 /** 070 * Checks to see if the factory is using the standalone implementation. 071 * 072 * @return true if using the standalone implementation, false otherwise. 073 */ 074 public static boolean isUsingStandaloneImplementation() 075 { 076 if ( !isInitialized() ) 077 { 078 String msg = I18n.err( I18n.ERR_05200_NOT_INITIALIZED_YET ); 079 LOG.error( msg ); 080 throw new IllegalStateException( msg ); 081 } 082 083 return usingStandaloneImplementation; 084 } 085 086 087 /** 088 * Gets the singleton instance of the LdapCodecService. 089 * 090 * @return a valid instance implementation based on environment and the 091 * availability of bindings. 092 */ 093 public static LdapApiService getSingleton() 094 { 095 if ( ldapCodecService == null ) 096 { 097 initialize( null ); 098 } 099 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}