Coverage Report - org.apache.fulcrum.template.BaseTemplateEngineService
 
Classes in this File Line Coverage Branch Coverage Complexity
BaseTemplateEngineService
0%
0/39
0%
0/12
1.583
 
 1  
 package org.apache.fulcrum.template;
 2  
 
 3  
 
 4  
 /*
 5  
  * Licensed to the Apache Software Foundation (ASF) under one
 6  
  * or more contributor license agreements.  See the NOTICE file
 7  
  * distributed with this work for additional information
 8  
  * regarding copyright ownership.  The ASF licenses this file
 9  
  * to you under the Apache License, Version 2.0 (the
 10  
  * "License"); you may not use this file except in compliance
 11  
  * with the License.  You may obtain a copy of the License at
 12  
  *
 13  
  *   http://www.apache.org/licenses/LICENSE-2.0
 14  
  *
 15  
  * Unless required by applicable law or agreed to in writing,
 16  
  * software distributed under the License is distributed on an
 17  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 18  
  * KIND, either express or implied.  See the License for the
 19  
  * specific language governing permissions and limitations
 20  
  * under the License.
 21  
  */
 22  
 
 23  
 
 24  
 import java.io.File;
 25  
 import java.io.OutputStream;
 26  
 import java.io.Writer;
 27  
 import java.util.ArrayList;
 28  
 import java.util.Hashtable;
 29  
 import java.util.List;
 30  
 
 31  
 import org.apache.avalon.framework.activity.Disposable;
 32  
 import org.apache.avalon.framework.configuration.Configurable;
 33  
 import org.apache.avalon.framework.configuration.Configuration;
 34  
 import org.apache.avalon.framework.configuration.ConfigurationException;
 35  
 import org.apache.avalon.framework.context.Context;
 36  
 import org.apache.avalon.framework.context.ContextException;
 37  
 import org.apache.avalon.framework.context.Contextualizable;
 38  
 import org.apache.avalon.framework.logger.AbstractLogEnabled;
 39  
 import org.apache.avalon.framework.service.ServiceException;
 40  
 import org.apache.avalon.framework.service.ServiceManager;
 41  
 import org.apache.avalon.framework.service.Serviceable;
 42  
 import org.apache.avalon.framework.thread.ThreadSafe;
 43  
 
 44  
 /**
 45  
  * The base implementation of Turbine {@link
 46  
  * org.apache.fulcrum.template.TemplateEngineService}.
 47  
  *
 48  
  * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
 49  
  * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
 50  
  * @version $Id: BaseTemplateEngineService.java 535465 2007-05-05 06:58:06Z tv $
 51  
  */
 52  0
 public abstract class BaseTemplateEngineService
 53  
     extends AbstractLogEnabled
 54  
     implements
 55  
         TemplateEngineService,
 56  
         Configurable,
 57  
         Contextualizable,
 58  
         ThreadSafe,
 59  
         Disposable,
 60  
         Serviceable
 61  
 {
 62  
 
 63  0
     private ServiceManager manager = null;
 64  
 
 65  
     /**
 66  
      * The application root
 67  
      */
 68  
     protected String applicationRoot;
 69  
 
 70  
     /**
 71  
      * A Map containing the configuration for the template
 72  
      * engine service. The configuration contains:
 73  
      *
 74  
      * 1) template extensions
 75  
      * 2) default page
 76  
      * 3) default screen
 77  
      * 4) default layout
 78  
      * 5) default navigation
 79  
      * 6) default error screen
 80  
      */
 81  0
     private Hashtable configuration = new Hashtable();
 82  
 
 83  
     /**
 84  
      * @see org.apache.fulcrum.template.TemplateEngineService#getTemplateEngineServiceConfiguration
 85  
      */
 86  
     public Hashtable getTemplateEngineServiceConfiguration()
 87  
     {
 88  0
         return configuration;
 89  
     }
 90  
 
 91  
     /**
 92  
      * @see org.apache.fulcrum.template.TemplateEngineService#getAssociatedFileExtensions
 93  
      */
 94  
     public String[] getAssociatedFileExtensions()
 95  
     {
 96  0
         return (String[]) configuration.get(TEMPLATE_EXTENSIONS);
 97  
     }
 98  
 
 99  
     /**
 100  
      * @see org.apache.fulcrum.template.TemplateEngineService#templateExists
 101  
      */
 102  
     public abstract boolean templateExists(String template);
 103  
 
 104  
     public abstract String handleRequest(
 105  
         TemplateContext context,
 106  
         String template)
 107  
         throws TemplateException;
 108  
 
 109  
     public abstract void handleRequest(
 110  
         TemplateContext context,
 111  
         String template,
 112  
         OutputStream os)
 113  
         throws TemplateException;
 114  
 
 115  
     /**
 116  
      * @see org.apache.fulcrum.template.TemplateEngineService
 117  
      */
 118  
     public abstract void handleRequest(
 119  
         TemplateContext context,
 120  
         String template,
 121  
         Writer writer)
 122  
         throws TemplateException;
 123  
 
 124  
     // ---------------- Avalon Lifecycle Methods ---------------------
 125  
 
 126  
     /**
 127  
      * Used by subclasses in their configure method
 128  
      */
 129  
     protected void registerConfiguration(Configuration conf, String defaultExt)
 130  
         throws ConfigurationException
 131  
     {
 132  0
         initConfiguration(conf, defaultExt);
 133  
 
 134  0
         TemplateService component = null;
 135  
         try
 136  
         {
 137  0
             component = (TemplateService) manager.lookup(TemplateService.ROLE);
 138  0
             component.registerTemplateEngineService(this);
 139  
         }
 140  0
         catch (ServiceException ce)
 141  
         {
 142  0
             throw new RuntimeException(ce.getMessage());
 143  
         }
 144  
         finally
 145  
         {
 146  0
             if (component != null)
 147  
             {
 148  0
                 manager.release(component);
 149  
             }
 150  
         }
 151  0
     }
 152  
 
 153  
     /**
 154  
      * Note engine file extension associations.  First attempts to
 155  
      * pull a list of custom extensions from the property file value
 156  
      * keyed by <code>template.extension</code>.  If none are defined,
 157  
      * uses the value keyed by
 158  
      * <code>template.default.extension</code>, defaulting to the
 159  
      * emergency value supplied by <code>defaultExt</code>.
 160  
      *
 161  
      * @param defaultExt The default used when the default defined in the
 162  
      *                   properties file is missing or misconfigured.
 163  
      */
 164  
     private void initConfiguration(Configuration conf, String defaultExt)
 165  
         throws ConfigurationException
 166  
     {
 167  
 
 168  0
         List extensionList = new ArrayList();
 169  0
         final Configuration[] extensions =
 170  
             conf.getChildren("template-extension");
 171  0
         if (extensions != null)
 172  
         {
 173  0
             for (int i = 0; i < extensions.length; i++)
 174  
             {
 175  0
                 extensionList.add(extensions[i].getValue());
 176  
             }
 177  
         }
 178  
 
 179  0
         String[] fileExtensionAssociations =
 180  
             (String[]) extensionList.toArray(new String[extensionList.size()]);
 181  
 
 182  0
         if (fileExtensionAssociations == null
 183  
             || fileExtensionAssociations.length == 0)
 184  
         {
 185  0
             Configuration defaultConf =
 186  
                 conf.getChild("default-template-extension", true);
 187  0
             fileExtensionAssociations = new String[1];
 188  0
             fileExtensionAssociations[0] = defaultConf.getValue(defaultExt);
 189  
         }
 190  
 
 191  0
         configuration.put(TEMPLATE_EXTENSIONS, fileExtensionAssociations);
 192  
 
 193  0
         configuration.put(
 194  
             DEFAULT_PAGE_TEMPLATE,
 195  
             conf.getChild("default-page-template", true).getValue());
 196  
 
 197  0
         configuration.put(
 198  
             DEFAULT_LAYOUT_TEMPLATE,
 199  
             conf.getChild("default-layout-template", true).getValue());
 200  0
     }
 201  
 
 202  
     /**
 203  
      * @see org.apache.fulcrum.ServiceBroker#getRealPath(String)
 204  
      */
 205  
     public String getRealPath(String path)
 206  
     {
 207  0
         String absolutePath = null;
 208  0
         if (applicationRoot == null)
 209  
         {
 210  0
             absolutePath = new File(path).getAbsolutePath();
 211  
         }
 212  
         else
 213  
         {
 214  0
             absolutePath = new File(applicationRoot, path).getAbsolutePath();
 215  
         }
 216  
 
 217  0
         return absolutePath;
 218  
     }
 219  
 
 220  
     // ---------------- Avalon Lifecycle Methods ---------------------
 221  
 
 222  
     public void contextualize(Context context) throws ContextException
 223  
     {
 224  0
         this.applicationRoot = context.get("urn:avalon:home").toString();
 225  0
     }
 226  
     /**
 227  
      * Avalon component lifecycle method
 228  
      */
 229  
     public void service(ServiceManager manager) throws ServiceException
 230  
     {
 231  0
         this.manager = manager;
 232  
 
 233  0
     }
 234  
 
 235  
     /**
 236  
      * Avalon component lifecycle method
 237  
      */
 238  
     public void dispose()
 239  
     {
 240  0
         manager = null;
 241  0
     }
 242  
 }