1 package com.atlassian.sal.trustedapps;
2
3 import com.atlassian.plugin.StateAware;
4 import com.atlassian.sal.core.trusted.CertificateFactory;
5 import com.atlassian.security.auth.trustedapps.EncryptedCertificate;
6 import org.osgi.framework.BundleContext;
7 import org.osgi.util.tracker.ServiceTracker;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10
11
12
13
14
15
16 public class TrustedAppsPluginCertificateFactory implements CertificateFactory, StateAware
17 {
18 private static final Logger log = LoggerFactory.getLogger(TrustedAppsPluginCertificateFactory.class);
19 private static final String CERTIFICATE_FACTORY = "com.atlassian.security.auth.trustedapps.api.CertificateFactory";
20 private ServiceTracker serviceTracker;
21 private final BundleContext bundleContext;
22
23 public TrustedAppsPluginCertificateFactory(BundleContext bundleContext)
24 {
25 this.bundleContext = bundleContext;
26 serviceTracker = new ServiceTracker(bundleContext, CERTIFICATE_FACTORY, null);
27 serviceTracker.open();
28 }
29
30 public EncryptedCertificate createCertificate(String username)
31 {
32 if (serviceTracker != null)
33 {
34 try
35 {
36 com.atlassian.security.auth.trustedapps.api.CertificateFactory certificateFactory =
37 (com.atlassian.security.auth.trustedapps.api.CertificateFactory) serviceTracker.getService();
38 if (certificateFactory != null)
39 {
40 return certificateFactory.createCertificate(username);
41 }
42 }
43 catch (NoClassDefFoundError ncdfe)
44 {
45
46
47
48
49
50
51 }
52 catch (ClassCastException cce)
53 {
54
55 log.warn(
56 "A CertificateFactory was found, but a ClassCastException was thrown when attempting to cast it.",
57 cce);
58 }
59 }
60 throw new UnsupportedOperationException("Trusted apps support is not installed.");
61 }
62
63 public void enabled()
64 {
65 if (serviceTracker == null)
66 {
67 serviceTracker = new ServiceTracker(bundleContext, CERTIFICATE_FACTORY, null);
68 serviceTracker.open();
69 }
70 }
71
72 public void disabled()
73 {
74 serviceTracker.close();
75 serviceTracker = null;
76 }
77 }