+

Search Tips   |   Advanced Search

Create a rendering plug-in class

A rendering plug-in is a reusable class created to perform a task at render time. It can be referenced within web content using a plug-in tag. For example, we could write a plug-in that uses attributes from the current user's profile to determine whether the body of the plug-in tag is rendered or not. A rendering plug-in class requires us to reference a set of web content API methods.


Create a plug-in class

  1. Create a Java class that implements the interface com.ibm.workplace.wcm.api.plugin.rendering.RenderingPlugin or RenderingPluginDefinition. This class implement the following methods:

    • public String getName() .

      This name is used as the "name" parameter of the plug-in tag. See Create a plug-in tag for further information.

    • public Boolean render(RenderingPluginModel p_model) throws RenderingPluginException

  2. Implement render()method. This method contains the code that is run when the plug-in is invoked during rendering of a layout containing a "plug-in" tag that references the custom plug-in. Returning true renders the body markup defined in the plug-in tag. If false is returned, the body of the plug-in tag is skipped. If the plug-in tag has no body markup then the return value is ignored.

  3. Methods inherited from com.ibm.portal.Localized must also be implemented.

      public String getTitle(Locale displayLocale) {}
      This method returns the title for the rendering plug-in that will be used to allow selection of the rendering plug-in.

      public ListModel<Locale> getLocales()
      This method returns a list of locales supported by this rendering plug-in.

      public String getDescription(Locale p_arg0)
      This method returns a description of the rendering plug-in.

    See the Javadoc documentation for further information. The Javadoc files for Web Content Manager are located in the PORTAL_HOME/doc/Javadoc/spi_docs/com/ibm/workplace/wcm directory.

  4. The rendering plugin can optionally implement RenderingPluginDefinition instead of RenderingPlugin in order to define its type and parameters. This is recommended, because it allows the rendering plugins to be used more easily.

    To do this, implement the methods:

    • public RenderingPluginType getType()

    • public List<RenderingPluginParameter>getParameters()

    When creating a custom rendering plug-in:

    • Do not use "name" as a parameter in the rendering plug-in, because the RenderngPluginModel.getPluginaramters() method will not be returned.

    • Use "id" as a rendering parameter, but it will not be displayed in read mode.

For example:
package test;
 import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
 import com.ibm.portal.ListModel;
import com.ibm.portal.ModelException;
import com.ibm.workplace.wcm.api.plugin.rendering.RenderingPluginDefinition;
import com.ibm.workplace.wcm.api.plugin.rendering.RenderingPluginException;
import com.ibm.workplace.wcm.api.plugin.rendering.RenderingPluginModel;
import com.ibm.workplace.wcm.api.plugin.rendering.RenderingPluginParameter;
import com.ibm.workplace.wcm.api.plugin.rendering.RenderingPluginParameterAdapter;
import com.ibm.workplace.wcm.api.plugin.rendering.RenderingPluginParameterImpl;
import com.ibm.workplace.wcm.api.plugin.rendering.RenderingPluginType;
import com.ibm.workplace.wcm.api.plugin.rendering.RenderingPluginTypes;
import com.ibm.workplace.wcm.api.plugin.rendering.RenderingPluginParameter.Required;
import com.ibm.workplace.wcm.api.plugin.rendering.ValueOptionImpl;
 /**
 * A simple rendering plugin to demonstrate the use of the <code>RenderingPlugin</code> API.
 */
public class SimpleRenderingPlugin implements RenderingPluginDefinition
{
   /** The 'render body' parameter */
   private static final String RENDER_BODY_PARAM = "renderbody";
       /**
    * A simple list model holding locales.
    */
   protected static class SimpleLocaleListModel<K> implements ListModel<Locale>
   {
      /** the list of locales of this list model */
      final List<Locale> m_localeList = new ArrayList<Locale>();
       /**
       * Constructs this simple list model holding the given locales.
       *         
       * @param p_locales        
       *           the locales of this list model. May be <code>null</code>.
       */
      public SimpleLocaleListModel(final Locale[] p_locales)
      {
         if (p_locales != null)
         {
            for (int i = 0; i < p_locales.length; ++i)
            {
 m_localeList.add(p_locales[i]);
            }
         }
      }
       @Override
      public Iterator<Locale> iterator() throws ModelException
      {
         return m_localeList.iterator();
      }
   }
    /** a list model that only contains the English language locale */
   private static final ListModel<Locale> ENGLISH_ONLY = new SimpleLocaleListModel<Locale>(new Locale[]{Locale.ENGLISH});
    @Override
   public String getDescription(final Locale p_locale)
   {
      return "This is a simple rendering plugin.";
   }
    @Override
   public ListModel<Locale> getLocales()
   {
      return ENGLISH_ONLY;
   }
    @Override
   public String getName()
   {
      return "SimpleRenderingPlugin";
   }
    @Override
   public String getTitle(final Locale p_locale)
   {
      return "SimpleRenderingPlugin";
   }
    @Override
   public boolean isShownInAuthoringUI()
   {
      return false;
   }
    @Override
   public boolean render(final RenderingPluginModel p_model) throws RenderingPluginException
   {
      final Map<String, List<String>> params = p_model.getPluginParameters();
       // determine whether the inner contents of the plugin should actually be rendered       
      final boolean renderBody;
      final List<String> renderBodyList = params.get(RENDER_BODY_PARAM);
      if (renderBodyList != null && renderBodyList.get(0).equals("false"))
      {
         renderBody = false;
      }
      else       {
         renderBody = true;
      }
       // render the output of the plugin to the writer provided by the RenderingPluginModel
      final Writer writer = p_model.getWriter();
      try       {
         writer.write("<b>Simple RenderingPlugin</b>");
          final Set<String> keys = params.keySet();
          final Iterator<String> iter = keys.iterator();
         while (iter.hasNext())
         {
            String key = iter.next();
            writer.write("<br>" + key + " = " + params.get(key));
         }
          writer.write("<br><br>");
      }
      catch (IOException e)
      {
         e.printStackTrace();
      }
       return renderBody;
   }
    @Override
   public RenderingPluginType getType()
   {
      return RenderingPluginTypes.CONTENT;
   }
    @Override
   public List<RenderingPluginParameter> getParameters()
   {
      List<RenderingPluginParameter> parameters = new ArrayList<RenderingPluginParameter>();
      RenderingPluginParameterAdapter renderBodyParam = new RenderingPluginParameterImpl(RENDER_BODY_PARAM). required(Required.NOT_REQUIRED_SHOW_BY_DEFAULT).allowedValues(new ValueOptionImpl("true"), new ValueOptionImpl("false"));
      parameters.add(renderBodyParam);
      return parameters;
   }
}


Create a plugin.xml file

A plugin.xml file is needed whether the deployment is done using a WAR or EAR, or using a loose jar. If deploying an application in a WAR or EAR, include the plugin.xml file in the application's "WEB-INF" folder. When using a jar, include the plugin.xml in the root of the jar.

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<plugin
   
   name="Simple Rendering Plug-in Test"
   version="1.0.0"
   provider-name="IBM">
       <extension
     point="com.ibm.workplace.wcm.api.RenderingPlugin"
     >
     <provider class="test.SimpleRenderingPlugin"/>
   </extension>         </plugin>

  • Each plug-in is represented by a single <extension></extension> tag.

  • The value of the point attribute must be "com.ibm.workplace.wcm.api.RenderingPlugin".

  • Provide an ID value of the choice.

  • Specify the provider class for the plug-in.

Naming conventions:

If we create a plug-in application with the same names and IDs as an existing plug-in, the new plug-in will not be registered. When creating plug-in applications ensure the following are unique across the system:

  • The plug-in ID, plug-in name, and extension ID of the plugin.xml file.

  • The fully qualified class name plus path of all classes within the application.

  • The file path of any files within the application.


Parent Create custom plug-ins