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.slf4j.Logger; 024import org.slf4j.LoggerFactory; 025 026 027/** 028 * A factory that allows callers a means to get a handle on an LdapCodecService 029 * implementation regardless of the environment in which they're accessing it. 030 * In an OSGi environment, the BundleActivator binds the LdapCodecService 031 * class member forever to the DefaultLdapCodecService. If in 032 * 033 * In a standard standalone mode, the Bundle 034 * 035 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 036 */ 037public final class LdapApiServiceFactory 038{ 039 /** Logger for this class */ 040 private static final Logger LOG = LoggerFactory.getLogger( LdapApiServiceFactory.class ); 041 042 /** The LdapCodecService singleton bound to this factory */ 043 private static LdapApiService ldapCodecService; 044 045 /** Whether or not the standalone implementation is being used */ 046 private static boolean usingStandaloneImplementation; 047 048 049 /** 050 * Private constructor 051 */ 052 private LdapApiServiceFactory() 053 { 054 } 055 056 057 /** 058 * Checks to see if the factory is initialized. 059 * 060 * @return true if initialized, false otherwise 061 */ 062 public static boolean isInitialized() 063 { 064 return ldapCodecService != null; 065 } 066 067 068 /** 069 * Checks to see if the factory is using the standalone implementation. 070 * 071 * @return true if using the standalone implementation, false otherwise. 072 */ 073 public static boolean isUsingStandaloneImplementation() 074 { 075 if ( !isInitialized() ) 076 { 077 String msg = "Not initialized yet!"; 078 LOG.error( msg ); 079 throw new IllegalStateException( msg ); 080 } 081 082 return usingStandaloneImplementation; 083 } 084 085 086 /** 087 * Gets the singleton instance of the LdapCodecService. 088 * 089 * @return a valid instance implementation based on environment and the 090 * availability of bindings. 091 */ 092 public static LdapApiService getSingleton() 093 { 094 if ( ldapCodecService == null ) 095 { 096 initialize( null ); 097 } 098 099 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}