1 package org.apache.turbine.services.pull; 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 org.apache.turbine.pipeline.PipelineData; 25 import org.apache.turbine.services.TurbineServices; 26 import org.apache.turbine.util.RunData; 27 import org.apache.velocity.context.Context; 28 29 /** 30 * This is a Facade class for PullService. 31 * 32 * This class provides static methods that call related methods of the 33 * implementation of the PullService used by the System, according to 34 * the settings in TurbineResources. 35 * 36 * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a> 37 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a> 38 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> 39 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a> 40 * @version $Id: TurbinePull.java 1706239 2015-10-01 13:18:35Z tv $ 41 */ 42 public abstract class TurbinePull 43 { 44 /** 45 * Utility method for accessing the service 46 * implementation 47 * 48 * @return a PullService implementation instance 49 */ 50 public static PullService getService() 51 { 52 return (PullService) TurbineServices 53 .getInstance().getService(PullService.SERVICE_NAME); 54 } 55 56 /** 57 * Get the context containing global tools that will be 58 * use as part of the Turbine Pull Model. 59 * 60 * @return A Context object which contains the 61 * Global Tool instances. 62 */ 63 public static final Context getGlobalContext() 64 { 65 return getService().getGlobalContext(); 66 } 67 68 /** 69 * Checks whether this service has been registered. This is 70 * required by the TurbineVelocityService so it can determine 71 * whether to attempt to place the ToolBox in the context. 72 * <p> 73 * So users can use Turbine with templates in the traditional 74 * manner. If the Pull Service is not listed in 75 * <code>TurbineResources.props</code>, or no tools are specified 76 * the TurbineVelocityService will behave in its traditional 77 * manner. 78 * @return true if the service is registered within Turbine 79 */ 80 public static final boolean isRegistered() 81 { 82 return TurbineServices.getInstance() 83 .isRegistered(PullService.SERVICE_NAME); 84 } 85 86 /** 87 * Return the absolute path of the resources directory 88 * used by application tools. 89 * 90 * @return A directory path in the file system or null. 91 */ 92 public static final String getAbsolutePathToResourcesDirectory() 93 { 94 return getService().getAbsolutePathToResourcesDirectory(); 95 } 96 97 /** 98 * Return the resources directory. This is relative 99 * to the webapp context. 100 * 101 * @return A directory path to the resources directory relative to the webapp root or null. 102 */ 103 public static final String getResourcesDirectory() 104 { 105 return getService().getResourcesDirectory(); 106 } 107 108 /** 109 * Populate the given context with all request, session 110 * and persistent scope tools (it is assumed that the context 111 * already wraps the global context, and thus already contains 112 * the global tools). 113 * 114 * @param context a Velocity Context to populate 115 * @param pipelineData a RunData object for request specific data 116 */ 117 public static void populateContext(Context context, PipelineData pipelineData) 118 { 119 getService().populateContext(context, pipelineData); 120 } 121 122 /** 123 * Populate the given context with all request, session 124 * and persistent scope tools (it is assumed that the context 125 * already wraps the global context, and thus already contains 126 * the global tools). 127 * 128 * @param context a Velocity Context to populate 129 * @param data a RunData object for request specific data 130 */ 131 public static void populateContext(Context context, RunData data) 132 { 133 getService().populateContext(context, data); 134 } 135 136 /** 137 * Release tool instances from the given context to the 138 * object pool 139 * 140 * @param context a Velocity Context to release tools from 141 */ 142 public static void releaseTools(Context context) 143 { 144 getService().releaseTools(context); 145 } 146 147 /** 148 * Helper method that allows you to easily get a tool 149 * from a Context. Essentially, it just does the cast 150 * to an Application tool for you. 151 * 152 * @param context a Velocity Context to get tools from 153 * @param name the name of the tool to get 154 * @return ApplicationTool null if no tool could be found 155 */ 156 public static ApplicationTool getTool(Context context, 157 String name) 158 { 159 try 160 { 161 return (ApplicationTool) context.get(name); 162 } 163 catch (Exception e) 164 { 165 // ignore 166 } 167 return null; 168 } 169 }