View Javadoc

1   package com.atlassian.plugins.rest.module.template;
2   
3   import com.atlassian.templaterenderer.velocity.one.six.VelocityTemplateRenderer;
4   import com.sun.jersey.spi.template.TemplateProcessor;
5   import org.osgi.framework.Bundle;
6   import org.osgi.framework.ServiceFactory;
7   import org.osgi.framework.ServiceRegistration;
8   import org.osgi.util.tracker.ServiceTracker;
9   
10  import java.io.IOException;
11  import java.io.OutputStream;
12  
13  /**
14   * OSGi Service Factory for creating velocity template processors, with the right bundle context.
15   */
16  //////////////////
17  // TODO The TemplateProcessor implementation is a workaround for a Spring DM bug that is fixed in
18  // version 1.2.0.  It can be removed once the plugins system has upgraded to 1.2.0
19  /////////////////
20  public class VelocityTemplateProcessorServiceFactory implements ServiceFactory, TemplateProcessor
21  {
22      public Object getService(Bundle bundle, ServiceRegistration serviceRegistration)
23      {
24          // Create a ServiceTracker for the template renderer.  This is needed because the template
25          // renderer may not yet be started, or it may come up or go down
26          ServiceTracker serviceTracker = new ServiceTracker(bundle.getBundleContext(), VelocityTemplateRenderer.class.getName(), null);
27          serviceTracker.open();
28          return new VelocityTemplateProcessor(serviceTracker);
29      }
30  
31      public void ungetService(Bundle bundle, ServiceRegistration serviceRegistration, Object service)
32      {
33          ((VelocityTemplateProcessor) service).closeTemplateRendererServiceTracker();
34      }
35  
36      public String resolve(String s)
37      {
38          throw new UnsupportedOperationException();
39      }
40  
41      public void writeTo(String s, Object o, OutputStream outputStream) throws IOException
42      {
43          throw new UnsupportedOperationException();
44      }
45  }