You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IPublicKeyManagerProvider.java 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright 2014 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.guice;
  17. import org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import com.gitblit.IStoredSettings;
  20. import com.gitblit.Keys;
  21. import com.gitblit.manager.IRuntimeManager;
  22. import com.gitblit.transport.ssh.FileKeyManager;
  23. import com.gitblit.transport.ssh.IPublicKeyManager;
  24. import com.gitblit.transport.ssh.NullKeyManager;
  25. import com.gitblit.utils.StringUtils;
  26. import com.google.inject.Inject;
  27. import com.google.inject.Provider;
  28. import com.google.inject.Singleton;
  29. /**
  30. * Provides a lazily-instantiated IPublicKeyManager configured from IStoredSettings.
  31. *
  32. * @author James Moger
  33. *
  34. */
  35. @Singleton
  36. public class IPublicKeyManagerProvider implements Provider<IPublicKeyManager> {
  37. private final Logger logger = LoggerFactory.getLogger(getClass());
  38. private final IRuntimeManager runtimeManager;
  39. private volatile IPublicKeyManager manager;
  40. @Inject
  41. public IPublicKeyManagerProvider(IRuntimeManager runtimeManager) {
  42. this.runtimeManager = runtimeManager;
  43. }
  44. @Override
  45. public synchronized IPublicKeyManager get() {
  46. if (manager != null) {
  47. return manager;
  48. }
  49. IStoredSettings settings = runtimeManager.getSettings();
  50. String clazz = settings.getString(Keys.git.sshKeysManager, FileKeyManager.class.getName());
  51. if (StringUtils.isEmpty(clazz)) {
  52. clazz = FileKeyManager.class.getName();
  53. }
  54. try {
  55. Class<? extends IPublicKeyManager> mgrClass = (Class<? extends IPublicKeyManager>) Class.forName(clazz);
  56. manager = runtimeManager.getInjector().getInstance(mgrClass);
  57. } catch (Exception e) {
  58. logger.error("failed to create public key manager", e);
  59. manager = new NullKeyManager();
  60. }
  61. return manager;
  62. }
  63. }