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