001package org.apache.turbine.services.pull;
002
003
004/*
005 * Licensed to the Apache Software Foundation (ASF) under one
006 * or more contributor license agreements.  See the NOTICE file
007 * distributed with this work for additional information
008 * regarding copyright ownership.  The ASF licenses this file
009 * to you under the Apache License, Version 2.0 (the
010 * "License"); you may not use this file except in compliance
011 * with the License.  You may obtain a copy of the License at
012 *
013 *   http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing,
016 * software distributed under the License is distributed on an
017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018 * KIND, either express or implied.  See the License for the
019 * specific language governing permissions and limitations
020 * under the License.
021 */
022
023
024import org.apache.turbine.pipeline.PipelineData;
025import org.apache.turbine.services.TurbineServices;
026import org.apache.turbine.util.RunData;
027import org.apache.velocity.context.Context;
028
029/**
030 * This is a Facade class for PullService.
031 *
032 * This class provides static methods that call related methods of the
033 * implementation of the PullService used by the System, according to
034 * the settings in TurbineResources.
035 *
036 * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
037 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
038 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
039 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
040 * @version $Id: TurbinePull.java 1706239 2015-10-01 13:18:35Z tv $
041 */
042public abstract class TurbinePull
043{
044    /**
045     * Utility method for accessing the service
046     * implementation
047     *
048     * @return a PullService implementation instance
049     */
050    public static PullService getService()
051    {
052        return (PullService) TurbineServices
053                .getInstance().getService(PullService.SERVICE_NAME);
054    }
055
056    /**
057     * Get the context containing global tools that will be
058     * use as part of the Turbine Pull Model.
059     *
060     * @return A Context object which contains the
061     *         Global Tool instances.
062     */
063    public static final Context getGlobalContext()
064    {
065        return getService().getGlobalContext();
066    }
067
068    /**
069     * Checks whether this service has been registered.  This is
070     * required by the TurbineVelocityService so it can determine
071     * whether to attempt to place the ToolBox in the context.
072     * <p>
073     * So users can use Turbine with templates in the traditional
074     * manner. If the Pull Service is not listed in
075     * <code>TurbineResources.props</code>, or no tools are specified
076     * the TurbineVelocityService will behave in its traditional
077     * manner.
078     * @return true if the service is registered within Turbine
079     */
080    public static final boolean isRegistered()
081    {
082        return TurbineServices.getInstance()
083                .isRegistered(PullService.SERVICE_NAME);
084    }
085
086    /**
087     * Return the absolute path of the resources directory
088     * used by application tools.
089     *
090     * @return A directory path in the file system or null.
091     */
092    public static final String getAbsolutePathToResourcesDirectory()
093    {
094        return getService().getAbsolutePathToResourcesDirectory();
095    }
096
097    /**
098     * Return the resources directory. This is relative
099     * 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}