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.ArrayList;
022: import java.util.List;
023:
024: import org.restlet.data.Request;
025: import org.restlet.data.Response;
026: import org.restlet.service.LogService;
027: import org.restlet.service.StatusService;
028: import org.restlet.util.ClientList;
029: import org.restlet.util.Engine;
030: import org.restlet.util.Helper;
031: import org.restlet.util.ServerList;
032:
033: /**
034: * Restlet managing a set of Connectors, VirtualHosts and Applications.
035: * Applications are expected to be directly attached to VirtualHosts. Components
036: * also expose several services: access logging and status setting. <br>
037: * <br>
038: * From an architectural point of view, here is the REST definition: "A
039: * component is an abstract unit of software instructions and internal state
040: * that provides a transformation of data via its interface." Roy T. Fielding<br>
041: * <br>
042: *
043: * @see <a
044: * href="http://roy.gbiv.com/pubs/dissertation/software_arch.htm#sec_1_2_1">Source
045: * dissertation</a>
046: * @author Jerome Louvel (contact@noelios.com)
047: */
048: public class Component extends Restlet {
049: /** The modifiable list of client connectors. */
050: private ClientList clients;
051:
052: /** The modifiable list of server connectors. */
053: private ServerList servers;
054:
055: /** The modifiable list of virtual hosts. */
056: private List<VirtualHost> hosts;
057:
058: /** The default host. */
059: private VirtualHost defaultHost;
060:
061: /** The helper provided by the implementation. */
062: private Helper helper;
063:
064: /** The log service. */
065: private LogService logService;
066:
067: /** The status service. */
068: private StatusService statusService;
069:
070: /**
071: * Constructor.
072: */
073: public Component() {
074: super (null);
075:
076: if (Engine.getInstance() != null) {
077: this .helper = Engine.getInstance().createHelper(this );
078: if (this .helper != null) {
079: setContext(this .helper.createContext(getClass()
080: .getCanonicalName()));
081: this .hosts = null;
082: this .defaultHost = new VirtualHost(getContext());
083: this .logService = null;
084: this .statusService = null;
085: }
086: }
087: }
088:
089: /**
090: * Returns the modifiable list of client connectors.
091: *
092: * @return The modifiable list of client connectors.
093: */
094: public ClientList getClients() {
095: if (this .clients == null)
096: this .clients = new ClientList(getContext());
097: return this .clients;
098: }
099:
100: /**
101: * Returns the modifiable list of server connectors.
102: *
103: * @return The modifiable list of server connectors.
104: */
105: public ServerList getServers() {
106: if (this .servers == null)
107: this .servers = new ServerList(getContext(), this );
108: return this .servers;
109: }
110:
111: /**
112: * Starts the component and all its connectors.
113: */
114: @Override
115: public void start() throws Exception {
116: if (isStopped()) {
117: if (this .clients != null) {
118: for (Client client : this .clients) {
119: client.start();
120: }
121: }
122:
123: if (this .servers != null) {
124: for (Server server : this .servers) {
125: server.start();
126: }
127: }
128:
129: if (getHelper() != null)
130: getHelper().start();
131:
132: super .start();
133: }
134: }
135:
136: /**
137: * Stops the component and all its connectors.
138: */
139: @Override
140: public void stop() throws Exception {
141: if (getHelper() != null)
142: getHelper().stop();
143:
144: if (this .clients != null) {
145: for (Client client : this .clients) {
146: client.stop();
147: }
148: }
149:
150: if (this .servers != null) {
151: for (Server server : this .servers) {
152: server.stop();
153: }
154: }
155:
156: super .stop();
157: }
158:
159: /**
160: * Returns the default virtual host.
161: *
162: * @return The default virtual host.
163: */
164: public VirtualHost getDefaultHost() {
165: return this .defaultHost;
166: }
167:
168: /**
169: * Returns the helper provided by the implementation.
170: *
171: * @return The helper provided by the implementation.
172: */
173: private Helper getHelper() {
174: return this .helper;
175: }
176:
177: /**
178: * Returns the modifiable list of host routers.
179: *
180: * @return The modifiable list of host routers.
181: */
182: public List<VirtualHost> getHosts() {
183: if (this .hosts == null)
184: this .hosts = new ArrayList<VirtualHost>();
185: return this .hosts;
186: }
187:
188: /**
189: * Returns the global log service. On the first call, if no log service was
190: * defined via the {@link #setLogService(LogService)} method, then a default
191: * logger service is created. This default service is enabled by default and
192: * has a logger name composed of the canonical name of the current
193: * component's class or subclass, appended with the instance hash code
194: * between parenthesis (eg. "com.mycompany.MyComponent(1439)").
195: *
196: * @return The global log service.
197: */
198: public LogService getLogService() {
199: if (this .logService == null) {
200: this .logService = new LogService(true);
201: this .logService.setLoggerName(getClass().getCanonicalName()
202: + " (" + hashCode() + ")");
203: }
204:
205: return this .logService;
206: }
207:
208: /**
209: * Returns the status service. This service is enabled by default.
210: *
211: * @return The status service.
212: */
213: public StatusService getStatusService() {
214: if (this .statusService == null)
215: this .statusService = new StatusService(true);
216: return this .statusService;
217: }
218:
219: /**
220: * Handles a call.
221: *
222: * @param request
223: * The request to handle.
224: * @param response
225: * The response to update.
226: */
227: public void handle(Request request, Response response) {
228: init(request, response);
229: if (getHelper() != null)
230: getHelper().handle(request, response);
231: }
232:
233: /**
234: * Sets the global log service.
235: *
236: * @param logService
237: * The global log service.
238: */
239: public void setLogService(LogService logService) {
240: this .logService = logService;
241: }
242:
243: /**
244: * Sets the status service.
245: *
246: * @param statusService
247: * The status service.
248: */
249: public void setStatusService(StatusService statusService) {
250: this.statusService = statusService;
251: }
252:
253: }
|