001: /*
002: * Copyright 2005-2007 Noelios Consulting.
003: *
004: * The contents of this file are subject to the terms of the Common Development
005: * and Distribution License (the "License"). You may not use this file except in
006: * compliance with the License.
007: *
008: * You can obtain a copy of the license at
009: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010: * language governing permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL HEADER in each file and
013: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014: * applicable, add the following below this CDDL HEADER, with the fields
015: * enclosed by brackets "[]" replaced with your own identifying information:
016: * Portions Copyright [yyyy] [name of copyright owner]
017: */
018:
019: package com.noelios.restlet;
020:
021: import org.restlet.Context;
022: import org.restlet.Filter;
023: import org.restlet.Restlet;
024: import org.restlet.service.LogService;
025: import org.restlet.util.Helper;
026:
027: /**
028: * Chain helper serving as base class for Application and Component helpers.
029: *
030: * @author Jerome Louvel (contact@noelios.com)
031: */
032: public abstract class ChainHelper extends Helper {
033: /** The first Restlet. */
034: private Restlet first;
035:
036: /** The last Filter. */
037: private Filter last;
038:
039: /** The parent context, typically the component's context. */
040: private Context parentContext;
041:
042: /**
043: * Constructor.
044: *
045: * @param parentContext
046: * The parent context, typically the component's context.
047: */
048: public ChainHelper(Context parentContext) {
049: this .parentContext = parentContext;
050: this .first = null;
051: }
052:
053: /**
054: * Returns the parent context, typically the component's context.
055: *
056: * @return The parent context.
057: */
058: public Context getParentContext() {
059: return this .parentContext;
060: }
061:
062: /**
063: * Adds a new filter to the chain.
064: *
065: * @param filter
066: * The filter to add.
067: */
068: protected void addFilter(Filter filter) {
069: if (getLast() != null) {
070: getLast().setNext(filter);
071: setLast(filter);
072: } else {
073: setFirst(filter);
074: setLast(filter);
075: }
076: }
077:
078: /**
079: * Creates a new log filter. Allows overriding.
080: *
081: * @param context
082: * The context.
083: * @param logService
084: * The log service descriptor.
085: * @return The new log filter.
086: */
087: protected Filter createLogFilter(Context context,
088: LogService logService) {
089: return new LogFilter(context, logService);
090: }
091:
092: /**
093: * Returns the first Restlet.
094: *
095: * @return the first Restlet.
096: */
097: protected Restlet getFirst() {
098: return this .first;
099: }
100:
101: /**
102: * Sets the first Restlet.
103: *
104: * @param first
105: * The first Restlet.
106: */
107: protected void setFirst(Restlet first) {
108: this .first = first;
109: }
110:
111: /**
112: * Returns the last Filter.
113: *
114: * @return the last Filter.
115: */
116: private Filter getLast() {
117: return this .last;
118: }
119:
120: /**
121: * Sets the last Filter.
122: *
123: * @param last
124: * The last Filter.
125: */
126: private void setLast(Filter last) {
127: this.last = last;
128: }
129:
130: }
|