View Javadoc

1   package org.apache.turbine.services.pull;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.turbine.services.TurbineServices;
23  import org.apache.turbine.util.RunData;
24  
25  import org.apache.velocity.context.Context;
26  
27  /***
28   * This is a Facade class for PullService.
29   *
30   * This class provides static methods that call related methods of the
31   * implementation of the PullService used by the System, according to
32   * the settings in TurbineResources.
33   *
34   * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
35   * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
36   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
37   * @version $Id: TurbinePull.java 534527 2007-05-02 16:10:59Z tv $
38   */
39  public abstract class TurbinePull
40  {
41      /***
42       * Utility method for accessing the service
43       * implementation
44       *
45       * @return a PullService implementation instance
46       */
47      public static PullService getService()
48      {
49          return (PullService) TurbineServices
50                  .getInstance().getService(PullService.SERVICE_NAME);
51      }
52  
53      /***
54       * Get the context containing global tools that will be
55       * use as part of the Turbine Pull Model.
56       *
57       * @return A Context object which contains the
58       *         Global Tool instances.
59       */
60      public static final Context getGlobalContext()
61      {
62          return getService().getGlobalContext();
63      }
64  
65      /***
66       * Checks whether this service has been registered.  This is
67       * required by the TurbineVelocityService so it can determine
68       * whether to attempt to place the ToolBox in the context.
69       * <p>
70       * So users can use Turbine with templates in the traditional
71       * manner. If the Pull Service is not listed in
72       * <code>TurbineResources.props</code>, or no tools are specified
73       * the TurbineVelocityService will behave in its traditional
74       * manner.
75       */
76      public static final boolean isRegistered()
77      {
78          return TurbineServices.getInstance()
79                  .isRegistered(PullService.SERVICE_NAME);
80      }
81  
82      /***
83       * Return the absolute path of the resources directory
84       * used by application tools.
85       *
86       * @return A directory path in the file system or null.
87       */
88      public static final String getAbsolutePathToResourcesDirectory()
89      {
90          return getService().getAbsolutePathToResourcesDirectory();
91      }
92  
93      /***
94       * Return the resources directory. This is relative
95       * to the webapp context.
96       *
97       * @return A directory path to the resources directory relative to the webapp root or null.
98       */
99      public static final String getResourcesDirectory()
100     {
101         return getService().getResourcesDirectory();
102     }
103 
104     /***
105      * Populate the given context with all request, session
106      * and persistent scope tools (it is assumed that the context
107      * already wraps the global context, and thus already contains
108      * the global tools).
109      *
110      * @param context a Velocity Context to populate
111      * @param data a RunData object for request specific data
112      */
113     public static void populateContext(Context context, RunData data)
114     {
115         getService().populateContext(context, data);
116     }
117 
118     /***
119      * Refresh the global tools. This is necessary
120      * for development work where tools depend
121      * on configuration information. The configuration
122      * information is typically cached after initialization
123      * but during development you might want the tool
124      * to refresh itself on each request.
125      * <p>
126      * If there are objects that don't implement
127      * the ApplicationTool interface, then they won't
128      * be refreshed.
129      * @deprecated No longer needed as Pull and Velocity Service are now more separate.
130      */
131     public static final void refreshGlobalTools()
132     {
133         getService().refreshGlobalTools();
134     }
135 
136     /***
137      * Shoud we refresh the tools
138      * on each request. For development purposes.
139      *
140      * @return true if we should refresh the tools on every request.
141      * @deprecated No longer needed as Pull and Velocity Service are now more separate.
142      */
143     public static final boolean refreshToolsPerRequest()
144     {
145         return getService().refreshToolsPerRequest();
146     }
147 
148     /***
149      * Release tool instances from the given context to the
150      * object pool
151      *
152      * @param context a Velocity Context to release tools from
153      */
154     public static void releaseTools(Context context)
155     {
156         getService().releaseTools(context);
157     }
158 
159     /***
160      * Helper method that allows you to easily get a tool
161      * from a Context. Essentially, it just does the cast
162      * to an Application tool for you.
163      *
164      * @param context a Velocity Context to get tools from
165      * @param name the name of the tool to get
166      * @return ApplicationTool null if no tool could be found
167      */
168     public static ApplicationTool getTool(Context context,
169                                           String name)
170     {
171         try
172         {
173             return (ApplicationTool) context.get(name);
174         }
175         catch (Exception e)
176         {
177         }
178         return null;
179     }
180 }