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.Arrays;
022: import java.util.List;
023:
024: import org.restlet.data.Protocol;
025: import org.restlet.data.Request;
026: import org.restlet.data.Response;
027: import org.restlet.util.Engine;
028: import org.restlet.util.Helper;
029:
030: /**
031: * Connector acting as a generic server. It internally uses one of the available
032: * connectors registered with the current Restlet implementation.
033: *
034: * @author Jerome Louvel (contact@noelios.com)
035: */
036: public class Server extends Connector {
037: /** The listening address if specified. */
038: private String address;
039:
040: /** The listening port if specified. */
041: private int port;
042:
043: /** The target Restlet. */
044: private Restlet target;
045:
046: /** The helper provided by the implementation. */
047: private Helper helper;
048:
049: /**
050: * Constructor.
051: *
052: * @param context
053: * The context.
054: * @param protocols
055: * The connector protocols.
056: * @param port
057: * The listening port.
058: * @param target
059: * The target Restlet.
060: */
061: public Server(Context context, List<Protocol> protocols, int port,
062: Restlet target) {
063: this (context, protocols, null, port, target);
064: }
065:
066: /**
067: * Constructor.
068: *
069: * @param context
070: * The context.
071: * @param protocols
072: * The connector protocols.
073: * @param address
074: * The optional listening IP address (useful if multiple IP
075: * addresses available).
076: * @param port
077: * The listening port.
078: * @param target
079: * The target Restlet.
080: */
081: public Server(Context context, List<Protocol> protocols,
082: String address, int port, Restlet target) {
083: super (context, protocols);
084: this .address = address;
085: this .port = port;
086: this .target = target;
087:
088: if (Engine.getInstance() != null) {
089: this .helper = Engine.getInstance().createHelper(this );
090: }
091: }
092:
093: /**
094: * Constructor.
095: *
096: * @param context
097: * The context.
098: * @param protocol
099: * The connector protocol.
100: * @param port
101: * The listening port.
102: * @param target
103: * The target Restlet.
104: */
105: public Server(Context context, Protocol protocol, int port,
106: Restlet target) {
107: this (context, protocol, null, port, target);
108: }
109:
110: /**
111: * Constructor using the protocol's default port.
112: *
113: * @param context
114: * The context.
115: * @param protocol
116: * The connector protocol.
117: * @param target
118: * The target Restlet.
119: */
120: public Server(Context context, Protocol protocol, Restlet target) {
121: this (context, protocol, null, protocol.getDefaultPort(), target);
122: }
123:
124: /**
125: * Constructor.
126: *
127: * @param context
128: * The context.
129: * @param protocol
130: * The connector protocol.
131: * @param address
132: * The optional listening IP address (useful if multiple IP
133: * addresses available).
134: * @param port
135: * The listening port.
136: * @param target
137: * The target Restlet.
138: */
139: public Server(Context context, Protocol protocol, String address,
140: int port, Restlet target) {
141: this (context, Arrays.asList(protocol), address, port, target);
142: }
143:
144: /**
145: * Constructor.
146: *
147: * @param protocols
148: * The connector protocols.
149: * @param port
150: * The listening port.
151: * @param target
152: * The target Restlet.
153: */
154: public Server(List<Protocol> protocols, int port, Restlet target) {
155: this (null, protocols, port, target);
156: }
157:
158: /**
159: * Constructor.
160: *
161: * @param protocols
162: * The connector protocols.
163: * @param address
164: * The optional listening IP address (useful if multiple IP
165: * addresses available).
166: * @param port
167: * The listening port.
168: * @param target
169: * The target Restlet.
170: */
171: public Server(List<Protocol> protocols, String address, int port,
172: Restlet target) {
173: this (null, protocols, address, port, target);
174: }
175:
176: /**
177: * Constructor.
178: *
179: * @param protocol
180: * The connector protocol.
181: * @param port
182: * The listening port.
183: * @param target
184: * The target Restlet.
185: */
186: public Server(Protocol protocol, int port, Restlet target) {
187: this (null, protocol, port, target);
188: }
189:
190: /**
191: * Constructor using the protocol's default port.
192: *
193: * @param protocol
194: * The connector protocol.
195: * @param target
196: * The target Restlet.
197: */
198: public Server(Protocol protocol, Restlet target) {
199: this (null, protocol, target);
200: }
201:
202: /**
203: * Constructor using the protocol's default port.
204: *
205: * @param protocol
206: * The connector protocol.
207: * @param address
208: * The listening IP address (useful if multiple IP addresses
209: * available).
210: * @param target
211: * The target Restlet.
212: */
213: public Server(Protocol protocol, String address, Restlet target) {
214: this (null, protocol, address, protocol.getDefaultPort(), target);
215: }
216:
217: /**
218: * Constructor.
219: *
220: * @param protocol
221: * The connector protocol.
222: * @param address
223: * The optional listening IP address (useful if multiple IP
224: * addresses available).
225: * @param port
226: * The listening port.
227: * @param target
228: * The target Restlet.
229: */
230: public Server(Protocol protocol, String address, int port,
231: Restlet target) {
232: this (null, protocol, address, port, target);
233: }
234:
235: /**
236: * Returns the optional listening IP address (local host used if null).
237: *
238: * @return The optional listening IP address (local host used if null).
239: */
240: public String getAddress() {
241: return this .address;
242: }
243:
244: /**
245: * Returns the internal server.
246: *
247: * @return The internal server.
248: */
249: private Helper getHelper() {
250: return this .helper;
251: }
252:
253: /**
254: * Returns the listening port if specified.
255: *
256: * @return The listening port if specified.
257: */
258: public int getPort() {
259: return this .port;
260: }
261:
262: /**
263: * Returns the target Restlet.
264: *
265: * @return The target Restlet.
266: */
267: public Restlet getTarget() {
268: return this .target;
269: }
270:
271: /**
272: * Handles a call.
273: *
274: * @param request
275: * The request to handle.
276: * @param response
277: * The response to update.
278: */
279: public void handle(Request request, Response response) {
280: init(request, response);
281:
282: if (getTarget() != null) {
283: getTarget().handle(request, response);
284: }
285: }
286:
287: /**
288: * Indicates if a target Restlet is set.
289: *
290: * @return True if a target Restlet is set.
291: */
292: public boolean hasTarget() {
293: return this .target != null;
294: }
295:
296: /**
297: * Sets the optional listening IP address (local host used if null).
298: *
299: * @param address
300: * The optional listening IP address (local host used if null).
301: */
302: protected void setAddress(String address) {
303: this .address = address;
304: }
305:
306: /**
307: * Sets the listening port if specified.
308: *
309: * @param port
310: * The listening port if specified.
311: */
312: protected void setPort(int port) {
313: this .port = port;
314: }
315:
316: /**
317: * Sets the target Restlet.
318: *
319: * @param target
320: * The target Restlet.
321: */
322: public void setTarget(Restlet target) {
323: this .target = target;
324: }
325:
326: @Override
327: public void start() throws Exception {
328: if (isStopped()) {
329: super .start();
330: if (getHelper() != null)
331: getHelper().start();
332: }
333: }
334:
335: @Override
336: public void stop() throws Exception {
337: if (isStarted()) {
338: getHelper().stop();
339: if (getHelper() != null)
340: super.stop();
341: }
342: }
343:
344: }
|