001/** 002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 003 * 004 * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved 005 * 006 * The contents of this file are subject to the terms 007 * of the Common Development and Distribution License 008 * (the License). You may not use this file except in 009 * compliance with the License. 010 * 011 * You can obtain a copy of the License at 012 * https://opensso.dev.java.net/public/CDDLv1.0.html or 013 * opensso/legal/CDDLv1.0.txt 014 * See the License for the specific language governing 015 * permission and limitations under the License. 016 * 017 * When distributing Covered Code, include this CDDL 018 * Header Notice in each file and include the License file 019 * at opensso/legal/CDDLv1.0.txt. 020 * If applicable, add the following below the CDDL Header, 021 * with the fields enclosed by brackets [] replaced by 022 * your own identifying information: 023 * "Portions Copyrighted [year] [name of copyright owner]" 024 * 025 * $Id: DataStoreProviderManager.java,v 1.4 2008/08/06 17:28:13 exu Exp $ 026 * 027 */ 028 029package com.sun.identity.plugin.datastore; 030 031import java.util.HashMap; 032import java.util.Map; 033import java.util.ResourceBundle; 034 035import com.sun.identity.shared.debug.Debug; 036import com.sun.identity.shared.locale.Locale; 037import com.sun.identity.common.SystemConfigurationUtil; 038 039/** 040 * This is a singleton class used to manage DataStore providers. 041 * @supported.all.api 042 */ 043public final class DataStoreProviderManager { 044 045 /** 046 * Default. 047 */ 048 public static final String DEFAULT = "default"; 049 050 /** 051 * Prefix for provider attribute. 052 */ 053 public static final String PROVIDER_ATTR_PREFIX = 054 "com.sun.identity.plugin.datastore.class."; 055 056 /** 057 * Attribute name for default provider. 058 */ 059 public static final String DEFAULT_PROVIDER_ATTR = 060 "com.sun.identity.plugin.datastore.class.default"; 061 062 private Map providerMap = null; 063 private DataStoreProvider defaultProvider = null; 064 private static DataStoreProviderManager instance = 065 new DataStoreProviderManager(); 066 private Debug debug = Debug.getInstance("libPlugins"); 067 private ResourceBundle bundle = Locale.getInstallResourceBundle( 068 "libDataStoreProvider"); 069 070 071 private DataStoreProviderManager() { 072 providerMap = new HashMap(); 073 try { 074 defaultProvider = getDefaultProvider(); 075 defaultProvider.init(DEFAULT); 076 } catch (DataStoreProviderException de) { 077 debug.error("DataStoreProviderManager: " 078 + "exception obtaining default provider:", de); 079 } 080 } 081 082 /** 083 * Gets the singleton instance of <code>DataStoreProviderManager</code>. 084 * @return The singleton <code>DataStoreProviderManager</code> instance 085 * @throws DataStoreProviderException if unable to get the singleton 086 * <code>DataStoreProviderManager</code> instance. 087 */ 088 public static DataStoreProviderManager getInstance() 089 throws DataStoreProviderException 090 { 091 return instance; 092 } 093 094 095 /** 096 * Gets the provider associated with the component. 097 * When <code>null</code> componentName is passed in, default provider 098 * is returned. 099 * @param componentName component name, such as saml, saml2, id-ff, disco, 100 * authnsvc, and idpp. 101 * @return datastore provider for the calling component 102 * @throws DataStoreProviderException if an error occurred. 103 * 104 */ 105 public DataStoreProvider getDataStoreProvider(String componentName) 106 throws DataStoreProviderException 107 { 108 if ((componentName == null) || (componentName.length() == 0)) { 109 return defaultProvider; 110 } else { 111 DataStoreProvider provider = (DataStoreProvider) 112 providerMap.get(componentName); 113 if (provider != null) { 114 return provider; 115 } 116 String className = SystemConfigurationUtil.getProperty( 117 PROVIDER_ATTR_PREFIX + componentName); 118 if ((className != null) && (className.length() > 0)) { 119 try { 120 provider = (DataStoreProvider) 121 Class.forName(className).newInstance(); 122 } catch (Exception e) { 123 debug.error("DataStoreProviderManage" 124 + "r.getDataStoreProvider: exception while " 125 + "instanciating provider:" + className + ":", e); 126 throw new DataStoreProviderException(e); 127 } 128 } 129 if (provider == null) { 130 if (debug.messageEnabled()) { 131 debug.message("DataStoreProviderMana" 132 + "ger.getDataStoreProvider: no provider specified " 133 + "for " + componentName + ", using default."); 134 } 135 provider = getDefaultProvider(); 136 } 137 provider.init(componentName); 138 synchronized(providerMap) { 139 providerMap.put(componentName, provider); 140 } 141 return provider; 142 } 143 } 144 145 private DataStoreProvider getDefaultProvider() 146 throws DataStoreProviderException 147 { 148 String className = 149 SystemConfigurationUtil.getProperty(DEFAULT_PROVIDER_ATTR); 150 if ((className == null) || (className.length() == 0)) { 151 throw new DataStoreProviderException( 152 bundle.getString("defaultProviderNotDefined")); 153 } 154 try { 155 return ((DataStoreProvider)Class.forName( 156 className).newInstance()); 157 } catch (Exception e) { 158 throw new DataStoreProviderException(e); 159 } 160 } 161}