KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > routing > RouterImpl


1 /**
2  * PETALS: PETALS Services Platform
3  * Copyright (C) 2005 EBM WebSourcing
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): EBM WebSourcing
21  * --------------------------------------------------------------------------
22  * $Id: RouterImpl.java,v 1.2 2005/07/22 10:24:27 alouis Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.petals.jbi.routing;
27
28 import javax.jbi.JBIException;
29 import javax.jbi.messaging.MessageExchange;
30
31 import org.objectweb.fractal.fraclet.annotation.FractalComponent;
32 import org.objectweb.fractal.fraclet.annotation.Interface;
33 import org.objectweb.fractal.fraclet.annotation.LifeCycle;
34 import org.objectweb.fractal.fraclet.annotation.LifeCycleType;
35 import org.objectweb.fractal.fraclet.annotation.Monolog;
36 import org.objectweb.fractal.fraclet.annotation.Provides;
37 import org.objectweb.fractal.fraclet.annotation.Requires;
38 import org.objectweb.petals.jbi.component.context.ComponentContextImpl;
39 import org.objectweb.petals.jbi.component.lifecycle.ComponentLifeCycle;
40 import org.objectweb.petals.jbi.management.service.EndpointService;
41 import org.objectweb.petals.jbi.management.service.LifeCycleManagerService;
42 import org.objectweb.petals.jbi.messaging.MessageExchangeImpl;
43 import org.objectweb.petals.jbi.registry.AbstractEndpoint;
44 import org.objectweb.petals.jbi.registry.ConsumerEndpoint;
45 import org.objectweb.petals.jbi.registry.InternalEndpoint;
46 import org.objectweb.petals.jbi.transport.TransportException;
47 import org.objectweb.petals.jbi.transport.Transporter;
48 import org.objectweb.petals.util.LoggingUtil;
49 import org.objectweb.util.monolog.api.Logger;
50
51 /**
52  * Routes messages to their destinations. Performs an itinerary resolution,
53  * adding for example a transformatino service before delivering the message to
54  * its recipient, and perform an address resolution, finding the endpoint
55  * satisfying the the destination information (finding an endpoint for a
56  * specified service). Resolutions are based on defined rules.
57  *
58  * @author Adrien LOUIS - EBM WebSourcing
59  */

60 @FractalComponent
61 @Provides(interfaces=@Interface(name="service",signature=org.objectweb.petals.jbi.routing.Router.class))
62 public class RouterImpl implements Router {
63
64     @Requires(name="endpoint-service",signature=org.objectweb.petals.jbi.management.service.EndpointService.class)
65     protected EndpointService endpointService;
66
67     /**
68      * logger wrapper
69      */

70     protected LoggingUtil log;
71
72     @Monolog(name="logger")
73     protected Logger logger;
74
75     @Requires(name="lifecyclemanager",signature=org.objectweb.petals.jbi.management.service.LifeCycleManagerService.class)
76     protected LifeCycleManagerService manager;
77
78     @Requires(name="transporter",signature=org.objectweb.petals.jbi.transport.Transporter.class)
79     protected Transporter transporter;
80
81     protected AddressResolver addressResolver;
82
83     // ----------------------------------------------------------------------
84
// Fractal BindingController implementation
85
// ----------------------------------------------------------------------
86

87     /**
88      * Sends the specified message. Depending on the Role of the sender, send
89      * the message to the producer (request) or to the consumer (response)
90      *
91      * @param source
92      * @param exchange
93      * @param timeOut
94      * @throws RoutingException
95      */

96     public void send(ComponentContextImpl source, MessageExchangeImpl exchange,
97         long timeOut) throws RoutingException {
98         log.call();
99
100         if (MessageExchange.Role.CONSUMER.equals(exchange.getRole())) {
101             sendToProvider(source, exchange, timeOut);
102         } else {
103             sendToConsumer(source, exchange, timeOut);
104         }
105     }
106
107     // ----------------------------------------------------------------------
108
// Fractal LifecycleController implementation
109
// ----------------------------------------------------------------------
110

111     @LifeCycle(on=LifeCycleType.START)
112     protected void start() {
113         log = new LoggingUtil(logger);
114         log.start();
115         addressResolver = new AddressResolver(endpointService);
116         log.end();
117     }
118
119     @LifeCycle(on=LifeCycleType.STOP)
120     protected void stop() {
121         log.start();
122
123         logger = null;
124
125         log.end();
126     }
127
128     /**
129      * Sends the specified message, sent by the specified component. TODO send
130      * directly to the DeliveryChannel of the component related to the recipient
131      * endpoint.
132      *
133      * @param source
134      * @param exchange
135      * @param timeOut
136      * @throws RoutingException
137      */

138     protected void sendToConsumer(ComponentContextImpl source,
139         MessageExchangeImpl exchange, long timeOut) throws RoutingException {
140         log.start();
141
142         try {
143             AbstractEndpoint consumerEndpoint = exchange.getConsumerEndpoint();
144
145             String JavaDoc containerName = consumerEndpoint.getContainerName();
146
147             exchange.setRole(MessageExchange.Role.CONSUMER);
148
149             transporter.send(exchange, containerName, timeOut);
150         } catch (TransportException e) {
151             // restore the Role if the message has not been sent
152
exchange.setRole(MessageExchange.Role.PROVIDER);
153
154             throw new RoutingException(e);
155         }
156
157         log.end();
158     }
159
160     /**
161      * Sends the specified message to the provider of the service. TODO for the
162      * moment, send directly to the DeliveryChannel of the component related to
163      * the recipient endpoint.
164      *
165      * @param source
166      * @param exchange
167      * @throws RoutingException
168      */

169     protected void sendToProvider(ComponentContextImpl source,
170         MessageExchangeImpl exchange, long timeOut) throws RoutingException {
171         log.start();
172
173         // resolve the address (find endpoint)
174
addressResolver.resolveAddress(source, exchange);
175
176         try {
177             AbstractEndpoint providerEndpoint = (AbstractEndpoint) exchange
178                 .getEndpoint();
179
180             String JavaDoc containerName = providerEndpoint.getContainerName();
181
182             exchange.setRole(MessageExchange.Role.PROVIDER);
183
184             transporter.send(exchange, containerName, timeOut);
185         } catch (TransportException e) {
186             // restore the Role if the message has not been sent
187
exchange.setRole(MessageExchange.Role.CONSUMER);
188
189             // TODO set message to Error ?
190
throw new RoutingException(e);
191         }
192
193         log.end();
194     }
195
196     /**
197      * route the received exchange to the correct DeliveryChannel.
198      */

199     public void receive(MessageExchangeImpl exchange) throws RoutingException {
200         log.start();
201
202         // find the name of the component to deliver the Exchange.
203
// Depends on the Role (consumer or provider)
204
String JavaDoc componentname = null;
205
206         if (MessageExchange.Role.PROVIDER.equals(exchange.getRole())) {
207             componentname = ((InternalEndpoint) exchange.getEndpoint())
208                 .getComponentName();
209         } else {
210             componentname = ((ConsumerEndpoint) exchange.getConsumerEndpoint())
211                 .getComponentName();
212         }
213
214         // retrieve the ComponentLifeCycle
215
ComponentLifeCycle comp = manager.getComponentByName(componentname);
216
217         // push the exchange
218
try {
219             comp.getComponentContext().getDeliveryChannelImpl().push(exchange);
220         } catch (JBIException e) {
221             throw new RoutingException(e);
222         }
223         log.end();
224     }
225 }
226
Popular Tags