Line data Source code
1 : // Copyright (C) 2013 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.securestore; 16 : 17 : import com.google.common.base.Strings; 18 : import com.google.gerrit.common.Nullable; 19 : import com.google.gerrit.common.SiteLibraryLoaderUtil; 20 : import com.google.gerrit.server.config.SitePaths; 21 : import com.google.inject.Inject; 22 : import com.google.inject.Injector; 23 : import com.google.inject.Provider; 24 : import com.google.inject.Singleton; 25 : import java.nio.file.Path; 26 : 27 : @Singleton 28 : public class SecureStoreProvider implements Provider<SecureStore> { 29 : private final Path libdir; 30 : private final Injector injector; 31 : private final String className; 32 : 33 : @Inject 34 : protected SecureStoreProvider( 35 138 : Injector injector, SitePaths sitePaths, @Nullable @SecureStoreClassName String className) { 36 138 : this.injector = injector; 37 138 : this.libdir = sitePaths.lib_dir; 38 138 : this.className = className; 39 138 : } 40 : 41 : @Override 42 : public synchronized SecureStore get() { 43 138 : return injector.getInstance(getSecureStoreImpl()); 44 : } 45 : 46 : @SuppressWarnings("unchecked") 47 : private Class<? extends SecureStore> getSecureStoreImpl() { 48 138 : if (Strings.isNullOrEmpty(className)) { 49 0 : return DefaultSecureStore.class; 50 : } 51 : 52 138 : SiteLibraryLoaderUtil.loadSiteLib(libdir); 53 : try { 54 138 : return (Class<? extends SecureStore>) Class.forName(className); 55 0 : } catch (ClassNotFoundException e) { 56 0 : throw new RuntimeException(String.format("Cannot load secure store class: %s", className), e); 57 : } 58 : } 59 : }