001    package 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    
024    import org.apache.turbine.pipeline.PipelineData;
025    import org.apache.turbine.services.TurbineServices;
026    import org.apache.turbine.util.RunData;
027    import 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 1078552 2011-03-06 19:58:46Z tv $
041     */
042    public 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         */
079        public static final boolean isRegistered()
080        {
081            return TurbineServices.getInstance()
082                    .isRegistered(PullService.SERVICE_NAME);
083        }
084    
085        /**
086         * Return the absolute path of the resources directory
087         * used by application tools.
088         *
089         * @return A directory path in the file system or null.
090         */
091        public static final String getAbsolutePathToResourcesDirectory()
092        {
093            return getService().getAbsolutePathToResourcesDirectory();
094        }
095    
096        /**
097         * Return the resources directory. This is relative
098         * to the webapp context.
099         *
100         * @return A directory path to the resources directory relative to the webapp root or null.
101         */
102        public static final String getResourcesDirectory()
103        {
104            return getService().getResourcesDirectory();
105        }
106    
107        /**
108         * Populate the given context with all request, session
109         * and persistent scope tools (it is assumed that the context
110         * already wraps the global context, and thus already contains
111         * the global tools).
112         *
113         * @param context a Velocity Context to populate
114         * @param data a RunData object for request specific data
115         */
116        public static void populateContext(Context context, PipelineData pipelineData)
117        {
118            getService().populateContext(context, pipelineData);
119        }
120    
121        /**
122         * Populate the given context with all request, session
123         * and persistent scope tools (it is assumed that the context
124         * already wraps the global context, and thus already contains
125         * the global tools).
126         *
127         * @param context a Velocity Context to populate
128         * @param data a RunData object for request specific data
129         */
130        public static void populateContext(Context context, RunData data)
131        {
132            getService().populateContext(context, data);
133        }
134    
135        /**
136         * Release tool instances from the given context to the
137         * object pool
138         *
139         * @param context a Velocity Context to release tools from
140         */
141        public static void releaseTools(Context context)
142        {
143            getService().releaseTools(context);
144        }
145    
146        /**
147         * Helper method that allows you to easily get a tool
148         * from a Context. Essentially, it just does the cast
149         * to an Application tool for you.
150         *
151         * @param context a Velocity Context to get tools from
152         * @param name the name of the tool to get
153         * @return ApplicationTool null if no tool could be found
154         */
155        public static ApplicationTool getTool(Context context,
156                                              String name)
157        {
158            try
159            {
160                return (ApplicationTool) context.get(name);
161            }
162            catch (Exception e)
163            {
164                // ignore
165            }
166            return null;
167        }
168    }