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      public Object getService(Bundle bundle, ServiceRegistration serviceRegistration) {
22          // Create a ServiceTracker for the template renderer.  This is needed because the template
23          // renderer may not yet be started, or it may come up or go down
24          ServiceTracker serviceTracker = new ServiceTracker(bundle.getBundleContext(), VelocityTemplateRenderer.class.getName(), null);
25          serviceTracker.open();
26          return new VelocityTemplateProcessor(serviceTracker);
27      }
28  
29      public void ungetService(Bundle bundle, ServiceRegistration serviceRegistration, Object service) {
30          ((VelocityTemplateProcessor) service).closeTemplateRendererServiceTracker();
31      }
32  
33      public String resolve(String s) {
34          throw new UnsupportedOperationException();
35      }
36  
37      public void writeTo(String s, Object o, OutputStream outputStream) throws IOException {
38          throw new UnsupportedOperationException();
39      }
40  }