001: package org.apache.turbine.services.pull;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.turbine.services.TurbineServices;
023: import org.apache.turbine.util.RunData;
024:
025: import org.apache.velocity.context.Context;
026:
027: /**
028: * This is a Facade class for PullService.
029: *
030: * This class provides static methods that call related methods of the
031: * implementation of the PullService used by the System, according to
032: * the settings in TurbineResources.
033: *
034: * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
035: * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
036: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
037: * @version $Id: TurbinePull.java 534527 2007-05-02 16:10:59Z tv $
038: */
039: public abstract class TurbinePull {
040: /**
041: * Utility method for accessing the service
042: * implementation
043: *
044: * @return a PullService implementation instance
045: */
046: public static PullService getService() {
047: return (PullService) TurbineServices.getInstance().getService(
048: PullService.SERVICE_NAME);
049: }
050:
051: /**
052: * Get the context containing global tools that will be
053: * use as part of the Turbine Pull Model.
054: *
055: * @return A Context object which contains the
056: * Global Tool instances.
057: */
058: public static final Context getGlobalContext() {
059: return getService().getGlobalContext();
060: }
061:
062: /**
063: * Checks whether this service has been registered. This is
064: * required by the TurbineVelocityService so it can determine
065: * whether to attempt to place the ToolBox in the context.
066: * <p>
067: * So users can use Turbine with templates in the traditional
068: * manner. If the Pull Service is not listed in
069: * <code>TurbineResources.props</code>, or no tools are specified
070: * the TurbineVelocityService will behave in its traditional
071: * manner.
072: */
073: public static final boolean isRegistered() {
074: return TurbineServices.getInstance().isRegistered(
075: PullService.SERVICE_NAME);
076: }
077:
078: /**
079: * Return the absolute path of the resources directory
080: * used by application tools.
081: *
082: * @return A directory path in the file system or null.
083: */
084: public static final String getAbsolutePathToResourcesDirectory() {
085: return getService().getAbsolutePathToResourcesDirectory();
086: }
087:
088: /**
089: * Return the resources directory. This is relative
090: * to the webapp context.
091: *
092: * @return A directory path to the resources directory relative to the webapp root or null.
093: */
094: public static final String getResourcesDirectory() {
095: return getService().getResourcesDirectory();
096: }
097:
098: /**
099: * Populate the given context with all request, session
100: * and persistent scope tools (it is assumed that the context
101: * already wraps the global context, and thus already contains
102: * the global tools).
103: *
104: * @param context a Velocity Context to populate
105: * @param data a RunData object for request specific data
106: */
107: public static void populateContext(Context context, RunData data) {
108: getService().populateContext(context, data);
109: }
110:
111: /**
112: * Refresh the global tools. This is necessary
113: * for development work where tools depend
114: * on configuration information. The configuration
115: * information is typically cached after initialization
116: * but during development you might want the tool
117: * to refresh itself on each request.
118: * <p>
119: * If there are objects that don't implement
120: * the ApplicationTool interface, then they won't
121: * be refreshed.
122: * @deprecated No longer needed as Pull and Velocity Service are now more separate.
123: */
124: public static final void refreshGlobalTools() {
125: getService().refreshGlobalTools();
126: }
127:
128: /**
129: * Shoud we refresh the tools
130: * on each request. For development purposes.
131: *
132: * @return true if we should refresh the tools on every request.
133: * @deprecated No longer needed as Pull and Velocity Service are now more separate.
134: */
135: public static final boolean refreshToolsPerRequest() {
136: return getService().refreshToolsPerRequest();
137: }
138:
139: /**
140: * Release tool instances from the given context to the
141: * object pool
142: *
143: * @param context a Velocity Context to release tools from
144: */
145: public static void releaseTools(Context context) {
146: getService().releaseTools(context);
147: }
148:
149: /**
150: * Helper method that allows you to easily get a tool
151: * from a Context. Essentially, it just does the cast
152: * to an Application tool for you.
153: *
154: * @param context a Velocity Context to get tools from
155: * @param name the name of the tool to get
156: * @return ApplicationTool null if no tool could be found
157: */
158: public static ApplicationTool getTool(Context context, String name) {
159: try {
160: return (ApplicationTool) context.get(name);
161: } catch (Exception e) {
162: }
163: return null;
164: }
165: }
|