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 org.restlet;
020:
021: import java.util.logging.Level;
022: import java.util.logging.Logger;
023:
024: import org.restlet.data.Request;
025: import org.restlet.data.Response;
026: import org.restlet.data.Status;
027:
028: /**
029: * Uniform class that provides a context and life cycle support. It has many
030: * subclasses that focus on specific ways to process calls. The context property
031: * is typically provided by a parent Component as a way to encapsulate access to
032: * shared features such as logging and client connectors.
033: *
034: * @author Jerome Louvel (contact@noelios.com)
035: */
036: public class Restlet extends Uniform {
037: /** Error message. */
038: private static final String UNABLE_TO_START = "Unable to start the Restlet";
039:
040: /** The context. */
041: private Context context;
042:
043: /** Indicates if the restlet was started. */
044: private boolean started;
045:
046: /**
047: * Constructor. Note that usage of this constructor is not recommended as
048: * the Restlet won't have a proper context set. In general you will prefer
049: * to use the other constructor and pass it the parent application's context
050: * or eventually the parent component's context if you don't use
051: * applications.
052: */
053: public Restlet() {
054: this (null);
055: }
056:
057: /**
058: * Constructor.
059: *
060: * @param context
061: * The context.
062: */
063: public Restlet(Context context) {
064: this .context = context;
065: this .started = false;
066: }
067:
068: /**
069: * Returns the context.
070: *
071: * @return The context.
072: */
073: public Context getContext() {
074: if (this .context == null) {
075: String logName = getClass().getCanonicalName();
076:
077: if (logName == null) {
078: logName = Restlet.class.getCanonicalName();
079: }
080:
081: this .context = new Context(logName);
082: }
083:
084: return this .context;
085: }
086:
087: /**
088: * Returns the context's logger.
089: *
090: * @return The context's logger.
091: */
092: public Logger getLogger() {
093: return (getContext() != null) ? getContext().getLogger()
094: : Logger.getLogger(getClass().getCanonicalName());
095: }
096:
097: /**
098: * Handles a call.
099: *
100: * @param request
101: * The request to handle.
102: * @param response
103: * The response to update.
104: */
105: public void handle(Request request, Response response) {
106: init(request, response);
107: }
108:
109: /**
110: * Initialize the Restlet by attempting to start it, unless it was already
111: * started. If an exception is thrown during the start action, then the
112: * response status is set to {@link Status#SERVER_ERROR_INTERNAL}.
113: *
114: * @param request
115: * The request to handle.
116: * @param response
117: * The response to update.
118: */
119: protected synchronized void init(Request request, Response response) {
120: // Check if the Restlet was started
121: if (isStopped()) {
122: try {
123: start();
124: } catch (Exception e) {
125: // Occurred while starting the Restlet
126: getContext().getLogger().log(Level.WARNING,
127: UNABLE_TO_START, e);
128: response.setStatus(Status.SERVER_ERROR_INTERNAL);
129: }
130:
131: if (!isStarted()) {
132: // No exception raised but the Restlet somehow couldn't be
133: // started
134: getContext().getLogger().log(Level.WARNING,
135: UNABLE_TO_START);
136: response.setStatus(Status.SERVER_ERROR_INTERNAL);
137: }
138: }
139: }
140:
141: /**
142: * Indicates if the Restlet is started.
143: *
144: * @return True if the Restlet is started.
145: */
146: public synchronized boolean isStarted() {
147: return this .started;
148: }
149:
150: /**
151: * Indicates if the Restlet is stopped.
152: *
153: * @return True if the Restlet is stopped.
154: */
155: public synchronized boolean isStopped() {
156: return !this .started;
157: }
158:
159: /**
160: * Sets the context.
161: *
162: * @param context
163: * The context.
164: */
165: public void setContext(Context context) {
166: this .context = context;
167: }
168:
169: /** Starts the Restlet. */
170: public synchronized void start() throws Exception {
171: this .started = true;
172: }
173:
174: /** Stops the Restlet. */
175: public synchronized void stop() throws Exception {
176: this .started = false;
177: }
178:
179: }
|