forked from restlet/restlet-framework-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponent.java
More file actions
765 lines (693 loc) · 23 KB
/
Component.java
File metadata and controls
765 lines (693 loc) · 23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
/**
* Copyright 2005-2013 Restlet S.A.S.
*
* The contents of this file are subject to the terms of one of the following
* open source licenses: Apache 2.0 or LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL
* 1.0 (the "Licenses"). You can select the license that you prefer but you may
* not use this file except in compliance with one of these Licenses.
*
* You can obtain a copy of the Apache 2.0 license at
* http://www.opensource.org/licenses/apache-2.0
*
* You can obtain a copy of the LGPL 3.0 license at
* http://www.opensource.org/licenses/lgpl-3.0
*
* You can obtain a copy of the LGPL 2.1 license at
* http://www.opensource.org/licenses/lgpl-2.1
*
* You can obtain a copy of the CDDL 1.0 license at
* http://www.opensource.org/licenses/cddl1
*
* You can obtain a copy of the EPL 1.0 license at
* http://www.opensource.org/licenses/eclipse-1.0
*
* See the Licenses for the specific language governing permissions and
* limitations under the Licenses.
*
* Alternatively, you can obtain a royalty free commercial license with less
* limitations, transferable or non-transferable, directly at
* http://www.restlet.com/products/restlet-framework
*
* Restlet is a registered trademark of Restlet S.A.S.
*/
package org.restlet;
import java.io.File;
import java.net.URI;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import org.restlet.data.Reference;
import org.restlet.engine.Engine;
import org.restlet.engine.component.ComponentHelper;
import org.restlet.engine.component.ComponentXmlParser;
import org.restlet.engine.component.InternalRouter;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;
import org.restlet.routing.Router;
import org.restlet.routing.VirtualHost;
import org.restlet.security.Realm;
import org.restlet.service.LogService;
import org.restlet.service.Service;
import org.restlet.service.StatusService;
import org.restlet.util.ClientList;
import org.restlet.util.ServerList;
import org.restlet.util.ServiceList;
/**
* Restlet managing a set of {@link Connector}s, {@link VirtualHost}s,
* {@link Service}s and {@link Application}s. Applications are expected to be
* directly attached to virtual hosts or to the internal router (see RIAP
* pseudo-protocol for usage). Components also expose several services: access
* logging and status setting. <br>
* <br>
* From an architectural point of view, here is the REST definition: "A
* component is an abstract unit of software instructions and internal state
* that provides a transformation of data via its interface." Roy T. Fielding<br>
* <br>
* The configuration of a Component can be done programmatically or by using a
* XML document. There are dedicated constructors that accept either an URI
* reference to such XML document or a representation of such XML document,
* allowing easy configuration of the list of supported client and server
* connectors as well as services. In addition, you can add and configure
* virtual hosts (including the default one). Finally, you can attach
* applications either using their fully qualified class name or by pointing to
* a descriptor document (at this time only WADL description are supported, see
* the WADL Restlet extension for details).<br>
* <br>
* The XML Schema of the configuration files is available both <a
* href="http://www.restlet.org/schemas/2.0/Component">online</a> and inside the
* API JAR under the "org.restlet.Component.xsd" name. Here is a sample of XML
* configuration:
*
* <pre>
* <?xml version="1.0"?>
* <component xmlns="http://www.restlet.org/schemas/2.0/Component">
* <client protocol="CLAP" />
* <client protocol="FILE" />
* <client protocols="HTTP HTTPS" />
* <server protocols="HTTP HTTPS" />
*
* <defaultHost>
* <attach uriPattern="/abcd/{xyz}"
* targetClass="org.restlet.test.MyApplication" />
* <attach uriPattern="/efgh/{xyz}"
* targetDescriptor="clap://class/org/restlet/test/MyApplication.wadl" />
* </defaultHost>
* </component>
* </pre>
*
* <br>
* Components also have useful services associated. They are all enabled by
* default and are available as properties that can be eventually overridden:
* <ul>
* <li>"logService" to configure access logging.</li>
* <li>"statusService" to provide common representations for exception status.</li>
* <li>"taskService" to run tasks asynchronously.</li>
* </ul>
*
* Concurrency note: instances of this class or its subclasses can be invoked by
* several threads at the same time and therefore must be thread-safe. You
* should be especially careful when storing state in member variables.
*
* @see <a
* href="http://roy.gbiv.com/pubs/dissertation/software_arch.htm#sec_1_2_1">Source
* dissertation</a>
*
* @author Jerome Louvel
*/
public class Component extends Restlet {
/**
* Used as bootstrap for configuring and running a component in command
* line. Just provide as first and unique parameter the URI to the XML file.
* Note that relative paths are accepted.
*
* @param args
* The list of in-line parameters.
*/
public static void main(String[] args) throws Exception {
try {
if ((args == null) || (args.length != 1)) {
// Display program arguments
System.err
.println("Can't launch the component. Requires the path to an XML configuration file.\n");
} else {
// Create and start the component
URI currentDirURI = (new File(".")).toURI();
URI confURI = currentDirURI.resolve(args[0]);
new Component(confURI.toString()).start();
}
} catch (Exception e) {
System.err
.println("Can't launch the component.\nAn unexpected exception occurred:");
e.printStackTrace(System.err);
}
}
/** The modifiable list of client connectors. */
private final ClientList clients;
/** The default host. */
private volatile VirtualHost defaultHost;
/** The helper provided by the implementation. */
private volatile ComponentHelper helper;
/** The modifiable list of virtual hosts. */
private final List<VirtualHost> hosts;
/**
* The private internal router that can be addressed via the RIAP client
* connector.
*/
private volatile Router internalRouter;
/** The modifiable list of security realms. */
private final List<Realm> realms;
/** The modifiable list of server connectors. */
private final ServerList servers;
/** The list of services. */
private final ServiceList services;
/**
* Constructor.
*/
public Component() {
super();
this.hosts = new CopyOnWriteArrayList<VirtualHost>();
this.clients = new ClientList(null);
this.servers = new ServerList(null, this);
this.realms = new CopyOnWriteArrayList<Realm>();
this.services = new ServiceList(getContext());
if (Engine.getInstance() != null) {
// [ifndef gae] instruction
// To be done before setting the helper...
this.services.add(new org.restlet.service.TaskService());
this.helper = new ComponentHelper(this);
Context childContext = getContext().createChildContext();
this.defaultHost = new VirtualHost(childContext);
this.internalRouter = new InternalRouter(childContext);
this.services.add(new LogService());
getLogService().setContext(childContext);
this.services.add(new StatusService());
getStatusService().setContext(childContext);
this.clients.setContext(childContext);
this.servers.setContext(childContext);
}
}
/**
* Constructor with the reference to the XML configuration file.
*
* @param xmlConfigRef
* The URI reference to the XML configuration file.
*/
public Component(Reference xmlConfigRef) {
this();
// Get the representation of the configuration file.
Representation xmlConfigRepresentation = null;
if (xmlConfigRef != null) {
ClientResource cr = new ClientResource(xmlConfigRef);
xmlConfigRepresentation = cr.get();
if (xmlConfigRepresentation != null) {
new ComponentXmlParser(this, xmlConfigRepresentation).parse();
} else {
getLogger().log(
Level.WARNING,
"Unable to get the Component XML configuration located at this URI: "
+ xmlConfigRef);
}
}
}
/**
* Constructor with the representation of the XML configuration file.
*
* @param xmlConfigRepresentation
* The representation of the XML configuration file.
*/
public Component(Representation xmlConfigRepresentation) {
this();
if (xmlConfigRepresentation != null) {
new ComponentXmlParser(this, xmlConfigRepresentation).parse();
} else {
getLogger().log(Level.WARNING,
"Unable to parse the Component XML configuration.");
}
}
/**
* Constructor with the URI reference to the XML configuration file.
*
* @param xmlConfigurationRef
* The URI reference to the XML configuration file.
*/
public Component(String xmlConfigurationRef) {
this((xmlConfigurationRef == null) ? null : new Reference(
xmlConfigurationRef));
}
/**
* Returns a modifiable list of client connectors.
*
* @return A modifiable list of client connectors.
*/
public ClientList getClients() {
return this.clients;
}
/**
* Returns the default virtual host.
*
* @return The default virtual host.
*/
public VirtualHost getDefaultHost() {
return this.defaultHost;
}
/**
* Returns the helper provided by the implementation.
*
* @return The helper provided by the implementation.
*/
private ComponentHelper getHelper() {
return this.helper;
}
/**
* Returns the modifiable list of virtual hosts. Note that the order of
* virtual hosts in this list will be used to check the first one that
* matches.
*
* @return The modifiable list of virtual hosts.
*/
public List<VirtualHost> getHosts() {
return this.hosts;
}
/**
* Returns the private internal router where Restlets like Applications can
* be attached. Those Restlets can be addressed via the
* {@link org.restlet.data.Protocol#RIAP} (Restlet Internal Access Protocol)
* client connector. This is used to manage private, internal and optimized
* access to local applications.<br>
* <br>
* The first use case is the modularization of a large application into
* modules or layers. This can also be achieved using the
* {@link Context#getServerDispatcher()} method, but the internal router is
* easily addressable via an URI scheme and can be fully private to the
* current Component.<br>
* <br>
* The second use case is the composition/mash-up of several representations
* via the org.restlet.ext.xml.Transformer class for example. For this you
* can leverage the XPath's document() function or the XSLT's include and
* import elements with RIAP URIs.
*
* @return The private internal router.
*/
public Router getInternalRouter() {
return this.internalRouter;
}
/**
* Returns the global log service. On the first call, if no log service was
* defined via the {@link #setLogService(LogService)} method, then a default
* logger service is created. This service will be enabled by default and
* has a logger name composed the "org.restlet." prefix followed by the
* simple component class name (without packages), followed by the
* ".LogService" suffix.
*
* @return The global log service.
*/
public LogService getLogService() {
return getServices().get(LogService.class);
}
/**
* Finds the realm with the given name.
*
* @param name
* The name.
* @return The realm found or null.
*/
public Realm getRealm(String name) {
if (name != null) {
for (Realm realm : getRealms()) {
if (name.equals(realm.getName())) {
return realm;
}
}
}
return null;
}
/**
* Returns the modifiable list of security realms.
*
* @return The modifiable list of security realms.
*/
public List<Realm> getRealms() {
return realms;
}
/**
* Returns the modifiable list of server connectors.
*
* @return The modifiable list of server connectors.
*/
public ServerList getServers() {
return this.servers;
}
/**
* Returns the modifiable list of services.
*
* @return The modifiable list of services.
*/
public ServiceList getServices() {
return services;
}
/**
* Returns the status service, enabled by default.
*
* @return The status service.
*/
public StatusService getStatusService() {
return getServices().get(StatusService.class);
}
/**
* Returns a task service to run concurrent tasks. The service is enabled by
* default.
*
* @return A task service.
*/
// [ifndef gae] method
public org.restlet.service.TaskService getTaskService() {
return getServices().get(org.restlet.service.TaskService.class);
}
@Override
public void handle(Request request, Response response) {
super.handle(request, response);
if (getHelper() != null) {
getHelper().handle(request, response);
}
}
/**
* Sets the modifiable list of client connectors. This method clears the
* current list and adds all entries in the parameter list.
*
* @param clients
* A list of client connectors.
*/
public void setClients(ClientList clients) {
synchronized (getClients()) {
if (clients != getClients()) {
getClients().clear();
if (clients != null) {
getClients().addAll(clients);
}
}
}
}
@Override
public void setContext(Context context) {
super.setContext(context);
getServices().setContext(context);
}
/**
* Sets the default virtual host.
*
* @param defaultHost
* The default virtual host.
*/
public void setDefaultHost(VirtualHost defaultHost) {
this.defaultHost = defaultHost;
}
/**
* Sets the modifiable list of virtual hosts. Note that the order of virtual
* hosts in this list will be used to check the first one that matches. This
* method clears the current list and adds all entries in the parameter
* list.
*
* @param hosts
* A list of virtual hosts.
*/
public void setHosts(List<VirtualHost> hosts) {
synchronized (getHosts()) {
if (hosts != getHosts()) {
getHosts().clear();
if (hosts != null) {
getHosts().addAll(hosts);
}
}
}
}
/**
* Sets the private internal router were Restlets like Applications can be
* attached.
*
* @param internalRouter
* The private internal router.
* @see #getInternalRouter()
*/
public void setInternalRouter(Router internalRouter) {
this.internalRouter = internalRouter;
}
/**
* Sets the global log service.
*
* @param logService
* The global log service.
*/
public void setLogService(LogService logService) {
getServices().set(logService);
}
/**
* Sets the list of realms. This method clears the current list and adds all
* entries in the parameter list.
*
* @param realms
* A list of realms.
*/
public void setRealms(List<Realm> realms) {
synchronized (getRealms()) {
if (realms != getRealms()) {
getRealms().clear();
if (realms != null) {
getRealms().addAll(realms);
}
}
}
}
/**
* Sets the modifiable list of server connectors. This method clears the
* current list and adds all entries in the parameter list.
*
* @param servers
* A list of server connectors.
*/
public void setServers(ServerList servers) {
synchronized (getServers()) {
if (servers != getServers()) {
getServers().clear();
if (servers != null) {
getServers().addAll(servers);
}
}
}
}
/**
* Sets the status service.
*
* @param statusService
* The status service.
*/
public void setStatusService(StatusService statusService) {
getServices().set(statusService);
}
/**
* Sets the task service.
*
* @param taskService
* The task service.
*/
// [ifndef gae] method
public void setTaskService(org.restlet.service.TaskService taskService) {
getServices().set(taskService);
}
/**
* Starts the component. First it starts all the connectors (clients then
* servers), the routers, the services, the realms and then the component's
* internal helper. Finally it calls the start method of the super class.
*
* @see #startClients()
* @see #startServers()
* @see #startRouters()
* @see #startServices()
* @see #startRealms()
* @see #startHelper()
*/
@Override
public synchronized void start() throws Exception {
if (isStopped()) {
startClients();
startServers();
startRouters();
startServices();
startRealms();
startHelper();
// Must be invoked as a last step
super.start();
}
}
/**
* Starts the client connectors.
*
* @throws Exception
*/
protected synchronized void startClients() throws Exception {
if (this.clients != null) {
for (final Client client : this.clients) {
client.start();
}
}
}
/**
* Starts the internal helper allowing incoming requests to be served.
*
* @throws Exception
*/
protected synchronized void startHelper() throws Exception {
if (getHelper() != null) {
getHelper().start();
}
}
/**
* Starts the realms.
*
* @throws Exception
*/
protected synchronized void startRealms() throws Exception {
if (this.realms != null) {
for (Realm realm : this.realms) {
realm.start();
}
}
}
/**
* Starts the virtual hosts and the internal router.
*
* @throws Exception
*/
protected synchronized void startRouters() throws Exception {
if (this.internalRouter != null) {
this.internalRouter.start();
}
if (this.defaultHost != null) {
this.defaultHost.start();
}
for (VirtualHost host : getHosts()) {
host.start();
}
}
/**
* Starts the server connectors.
*
* @throws Exception
*/
protected synchronized void startServers() throws Exception {
if (this.servers != null) {
for (final Server server : this.servers) {
server.start();
}
}
}
/**
* Starts the associated services.
*
* @throws Exception
*/
protected synchronized void startServices() throws Exception {
getServices().start();
}
/**
* Stops the component. First it stops the component's internal helper, the
* realms, the services, the routers and then stops all the connectors
* (servers then clients) Finally it calls the stop method of the super
* class.
*
* @see #stopHelper()
* @see #stopRealms()
* @see #stopServices()
* @see #stopRouters()
* @see #stopServers()
* @see #stopClients()
*/
@Override
public synchronized void stop() throws Exception {
stopHelper();
stopRealms();
stopServices();
stopRouters();
stopServers();
stopClients();
super.stop();
}
/**
* Stops the client connectors.
*
* @throws Exception
*/
protected synchronized void stopClients() throws Exception {
if (this.clients != null) {
for (final Client client : this.clients) {
client.stop();
}
}
}
/**
* Stops the internal helper allowing incoming requests to be served.
*
* @throws Exception
*/
protected synchronized void stopHelper() throws Exception {
if (getHelper() != null) {
getHelper().stop();
}
}
/**
* Stops the realms.
*
* @throws Exception
*/
protected synchronized void stopRealms() throws Exception {
if (this.realms != null) {
for (Realm realm : this.realms) {
realm.stop();
}
}
}
/**
* Stops the virtual hosts and the internal router.
*
* @throws Exception
*/
protected synchronized void stopRouters() throws Exception {
for (VirtualHost host : getHosts()) {
host.stop();
}
if (this.defaultHost != null) {
this.defaultHost.stop();
}
if (this.internalRouter != null) {
this.internalRouter.stop();
}
}
/**
* Stops the server connectors.
*
* @throws Exception
*/
protected synchronized void stopServers() throws Exception {
if (this.servers != null) {
for (final Server server : this.servers) {
server.stop();
}
}
}
/**
* Stops the associated services.
*
* @throws Exception
*/
protected synchronized void stopServices() throws Exception {
getServices().stop();
}
/**
* Updates the component to take into account changes to the virtual hosts.
* This method doesn't stop the connectors or the applications or Restlets
* attached to the virtual hosts. It just updates the internal routes
* between the virtual hosts and the attached Restlets or applications.<br>
*/
public synchronized void updateHosts() throws Exception {
getHelper().update();
}
}