View Javadoc

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   * This factory has a weak classloading dependency on {@link com.atlassian.security.auth.trustedapps.api.CertificateFactory}
13   * through an OSGi ServiceTracker, hence the trusted apps plugin does not need to be installed for this class to work
14   * (it will simply throw an exception saying that trusted apps isn't supported).
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                  // This probably won't happen, if trustedapps isn't installed then this method will return null,
46                  // so no cast will be attempted, so the class won't need to be loaded, and so this error won't be
47                  // thrown. Whether the class is loaded though may be platform dependent, I can't see anything in the
48                  // Java Language Specification that indicates what should happen, so for safety, we ignore this
49                  // exception here as it indicates that trusted apps is not installed.  It could also mean that the
50                  // class hasn't been wired appropriately, which the OSGi framework will warn us about anyway.
51              }
52              catch (ClassCastException cce)
53              {
54                  // This exception is possible if CertificateFactory has been uninstalled and installed. Warn.
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  }