View Javadoc

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