Coverage Report - org.apache.tapestry.portlet.ApplicationPortlet
 
Classes in this File Line Coverage Branch Coverage Complexity
ApplicationPortlet
0%
0/41
0%
0/8
2
 
 1  
 // Copyright 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.portlet;
 16  
 
 17  
 import java.io.IOException;
 18  
 import java.util.Locale;
 19  
 
 20  
 import javax.portlet.ActionRequest;
 21  
 import javax.portlet.ActionResponse;
 22  
 import javax.portlet.Portlet;
 23  
 import javax.portlet.PortletConfig;
 24  
 import javax.portlet.PortletException;
 25  
 import javax.portlet.RenderRequest;
 26  
 import javax.portlet.RenderResponse;
 27  
 
 28  
 import org.apache.hivemind.ClassResolver;
 29  
 import org.apache.hivemind.Registry;
 30  
 import org.apache.hivemind.Resource;
 31  
 import org.apache.hivemind.impl.DefaultClassResolver;
 32  
 import org.apache.hivemind.impl.RegistryBuilder;
 33  
 import org.apache.hivemind.impl.XmlModuleDescriptorProvider;
 34  
 import org.apache.tapestry.web.WebContext;
 35  
 import org.apache.tapestry.web.WebContextResource;
 36  
 
 37  
 /**
 38  
  * Portlet implementation for Tapestry Portlet applilcations. It's job is to create and manage the
 39  
  * HiveMind registry, to use the <code>tapestry.portlet.PortletApplicationInitializer</code>
 40  
  * service to initialize HiveMind, and the delegate requests to the
 41  
  * <code>tapestry.portlet.ActionRequestServicer</code> and
 42  
  * <code>tapestry.portlet.RenderRequestServicer</code> services.
 43  
  * 
 44  
  * @author Howard M. Lewis Ship
 45  
  * @since 4.0
 46  
  */
 47  0
 public class ApplicationPortlet implements Portlet
 48  
 {
 49  
     Registry _registry;
 50  
 
 51  
     ActionRequestServicer _actionRequestServicer;
 52  
 
 53  
     RenderRequestServicer _renderRequestServicer;
 54  
 
 55  
     public void destroy()
 56  
     {
 57  
         try
 58  
         {
 59  0
             _registry.shutdown();
 60  
         }
 61  
         finally
 62  
         {
 63  0
             _registry = null;
 64  0
             _actionRequestServicer = null;
 65  0
             _renderRequestServicer = null;
 66  0
         }
 67  0
     }
 68  
 
 69  
     public void init(PortletConfig config) throws PortletException
 70  
     {
 71  0
         _registry = constructRegistry(config);
 72  
 
 73  0
         PortletApplicationInitializer initializer = (PortletApplicationInitializer) _registry.getService(
 74  
                         "tapestry.portlet.PortletApplicationInitializer",
 75  0
                         PortletApplicationInitializer.class);
 76  
 
 77  0
         initializer.initialize(config);
 78  
 
 79  0
         _actionRequestServicer = (ActionRequestServicer) _registry.getService(
 80  
                 "tapestry.portlet.ActionRequestServicer",
 81  
                 ActionRequestServicer.class);
 82  
 
 83  0
         _renderRequestServicer = (RenderRequestServicer) _registry.getService(
 84  
                 "tapestry.portlet.RenderRequestServicer",
 85  
                 RenderRequestServicer.class);
 86  0
     }
 87  
 
 88  
     /**
 89  
      * Constructs the Registry. The Registry is constructed from the classpath, plus two optional
 90  
      * files:
 91  
      * <ul>
 92  
      * <li>WEB-INF/ <em>name</em> /hivemodule.xml</li>
 93  
      * <li>WEB-INF/hivemodule.xml</li>
 94  
      * </ul>.
 95  
      * <p>
 96  
      * Where <em>name</em> is the name of the portlet.
 97  
      */
 98  
 
 99  
     protected Registry constructRegistry(PortletConfig config)
 100  
     {
 101  0
         RegistryBuilder builder = new RegistryBuilder();
 102  
 
 103  0
         ClassResolver resolver = new DefaultClassResolver();
 104  
 
 105  0
         builder.addModuleDescriptorProvider(new XmlModuleDescriptorProvider(resolver));
 106  
 
 107  0
         String name = config.getPortletName();
 108  0
         WebContext context = new PortletWebContext(config.getPortletContext());
 109  
 
 110  0
         addModuleIfExists(builder, resolver, context, "/WEB-INF/" + name + "/hivemodule.xml");
 111  0
         addModuleIfExists(builder, resolver, context, "/WEB-INF/hivemodule.xml");
 112  
 
 113  0
         return builder.constructRegistry(Locale.getDefault());
 114  
     }
 115  
 
 116  
     /**
 117  
      * Looks for a file in the context; if it exists, it is expected to be a HiveMind module
 118  
      * descriptor, and is added to the builder.
 119  
      * 
 120  
      * @since 4.0
 121  
      */
 122  
 
 123  
     protected void addModuleIfExists(RegistryBuilder builder, ClassResolver resolver,
 124  
             WebContext context, String path)
 125  
     {
 126  0
         Resource r = new WebContextResource(context, path);
 127  
 
 128  0
         if (r.getResourceURL() == null)
 129  0
             return;
 130  
 
 131  0
         builder.addModuleDescriptorProvider(new XmlModuleDescriptorProvider(resolver, r));
 132  0
     }
 133  
 
 134  
     public void processAction(ActionRequest request, ActionResponse response)
 135  
             throws PortletException, IOException
 136  
     {
 137  
         try
 138  
         {
 139  0
             _registry.setupThread();
 140  
 
 141  0
             _actionRequestServicer.service(request, response);
 142  
         }
 143  0
         catch (RuntimeException ex)
 144  
         {
 145  0
             throw new PortletException(PortletMessages.errorProcessingAction(ex), ex);
 146  
         }
 147  
         finally
 148  
         {
 149  0
             _registry.cleanupThread();
 150  0
         }
 151  0
     }
 152  
 
 153  
     public void render(RenderRequest request, RenderResponse response) throws PortletException,
 154  
             IOException
 155  
     {
 156  
         try
 157  
         {
 158  0
             _registry.setupThread();
 159  
 
 160  0
             _renderRequestServicer.service(request, response);
 161  
         }
 162  0
         catch (RuntimeException ex)
 163  
         {
 164  0
             throw new PortletException(PortletMessages.errorProcessingRender(ex), ex);
 165  
         }
 166  
         finally
 167  
         {
 168  0
             _registry.cleanupThread();
 169  0
         }
 170  0
     }
 171  
 }