001: package org.tigris.scarab.util;
002:
003: /* ================================================================
004: * Copyright (c) 2000-2002 CollabNet. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are
008: * met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowlegement: "This product includes
019: * software developed by Collab.Net <http://www.Collab.Net/>."
020: * Alternately, this acknowlegement may appear in the software itself, if
021: * and wherever such third-party acknowlegements normally appear.
022: *
023: * 4. The hosted project names must not be used to endorse or promote
024: * products derived from this software without prior written
025: * permission. For written permission, please contact info@collab.net.
026: *
027: * 5. Products derived from this software may not use the "Tigris" or
028: * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
029: * prior written permission of Collab.Net.
030: *
031: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
032: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
033: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
034: * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
035: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
036: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
037: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
038: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
039: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
040: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
041: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
042: *
043: * ====================================================================
044: *
045: * This software consists of voluntary contributions made by many
046: * individuals on behalf of Collab.Net.
047: */
048:
049: // Turbine
050: import org.apache.turbine.RunData;
051: import org.apache.turbine.services.pull.ApplicationTool;
052: import org.apache.fulcrum.pool.InitableRecyclable;
053:
054: /**
055: * A pull tool to render links to static content.
056: *
057: * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
058: * @version $Id: StaticLink.java 3461 2002-02-13 05:18:24Z jmcnally $
059: */
060: public class StaticLink implements InitableRecyclable, ApplicationTool {
061: private RunData data;
062: private String path;
063:
064: /**
065: * Constructor.
066: */
067: public StaticLink() {
068: }
069:
070: /**
071: * This will initialise a StaticLink object that was
072: * constructed with the default constructor (ApplicationTool
073: * method).
074: *
075: * @param data assumed to be a RunData object
076: */
077: public void init(Object data) {
078: // we just blithely cast to RunData as if another object
079: // or null is passed in we'll throw an appropriate runtime
080: // exception.
081: this .data = (RunData) data;
082: }
083:
084: /**
085: * Implementation of ApplicationTool interface.
086: *
087: */
088: public void refresh() {
089: path = null;
090: }
091:
092: /**
093: * Path to a file. The path will be relative to the directory returned
094: * by getPrefix().
095: *
096: * @param t A String with the file name.
097: * @return A StaticLink.
098: */
099: public StaticLink setPath(String t) {
100: path = t;
101: return this ;
102: }
103:
104: /**
105: * Prints out the url.
106: *
107: * @return a <code>String</code> url
108: */
109: public String toString() {
110: String prefix = getPrefix();
111: return new StringBuffer(prefix.length() + path.length())
112: .append(prefix).append(path).toString();
113: }
114:
115: /**
116: * Prefix that will be added to the path given in setPage.
117: * This implementation returns the context path.
118: *
119: * @return a <code>String</code> value
120: */
121: protected String getPrefix() {
122: return data.getRequest().getContextPath();
123: }
124:
125: /**
126: * Give subclasses access to the RunData, so they do not have to
127: * reimplement the pooling code, just to get at it.
128: */
129: protected RunData getRunData() {
130: return data;
131: }
132:
133: // ****************************************************************
134: // ****************************************************************
135: // Implementation of Recyclable
136: // ****************************************************************
137: // ****************************************************************
138:
139: private boolean disposed = false;
140:
141: /**
142: * Recycles the object by removing its disposed flag.
143: */
144: public void recycle() {
145: disposed = false;
146: }
147:
148: /**
149: * Disposes the object by setting its disposed flag.
150: */
151: public void dispose() {
152: data = null;
153: refresh();
154: disposed = true;
155: }
156:
157: /**
158: * Checks whether the object is disposed.
159: *
160: * @return true, if the object is disposed.
161: */
162: public boolean isDisposed() {
163: return disposed;
164: }
165: }
|