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.application;
020:
021: import java.util.logging.Logger;
022:
023: import org.restlet.Application;
024: import org.restlet.Client;
025: import org.restlet.Context;
026: import org.restlet.Uniform;
027: import org.restlet.data.Protocol;
028:
029: import com.noelios.restlet.TemplateDispatcher;
030:
031: /**
032: * Context based on a parent component's context but dedicated to an
033: * application. This is important to allow contextual access to application's
034: * resources.
035: *
036: * @author Jerome Louvel (contact@noelios.com)
037: */
038: public class ApplicationContext extends Context {
039: /** The WAR client. */
040: private Client warClient;
041:
042: /** The application delegate. */
043: private Application application;
044:
045: /** The parent context. */
046: private Context parentContext;
047:
048: /**
049: * Constructor.
050: *
051: * @param application
052: * The application.
053: * @param parentContext
054: * The parent context.
055: * @param logger
056: * The logger instance of use.
057: */
058: public ApplicationContext(Application application,
059: Context parentContext, Logger logger) {
060: super (getLoggerName(application));
061: this .application = application;
062: this .parentContext = parentContext;
063: this .warClient = null;
064:
065: // Set the application as an attribute for usage by other services
066: // like the ConnectorService
067: getAttributes().put(Application.KEY, application);
068:
069: }
070:
071: /**
072: * Returns a non-null logger name.
073: *
074: * @param application
075: * The application.
076: * @return The logger name.
077: */
078: private static String getLoggerName(Application application) {
079: String result = application.getClass().getCanonicalName();
080: if (result == null)
081: result = "org.restlet.application";
082: return result;
083: }
084:
085: /**
086: * Returns a call dispatcher.
087: *
088: * @return A call dispatcher.
089: */
090: public Uniform getDispatcher() {
091: return new TemplateDispatcher(this , new ApplicationDispatcher(
092: this ));
093: }
094:
095: /**
096: * Returns the application.
097: *
098: * @return the application.
099: */
100: protected Application getApplication() {
101: return this .application;
102: }
103:
104: /**
105: * Returns the WAR client.
106: *
107: * @return the WAR client.
108: */
109: protected Client getWarClient() {
110: if (this .warClient == null) {
111: this .warClient = new Client(Protocol.WAR);
112: }
113:
114: return this .warClient;
115: }
116:
117: /**
118: * Sets the WAR client.
119: *
120: * @param warClient
121: * the WAR client.
122: */
123: protected void setWarClient(Client warClient) {
124: this .warClient = warClient;
125: }
126:
127: /**
128: * Returns the parent context.
129: *
130: * @return The parent context.
131: */
132: protected Context getParentContext() {
133: return this.parentContext;
134: }
135:
136: }
|