KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > soap > glue > GlueMessageReceiver


1 /*
2  * $Id: GlueMessageReceiver.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.soap.glue;
12
13 import electric.glue.context.ApplicationContext;
14 import electric.glue.context.ServiceContext;
15 import electric.registry.Registry;
16 import electric.registry.RegistryException;
17 import electric.server.http.HTTP;
18 import electric.service.virtual.VirtualService;
19 import electric.util.Context;
20 import electric.util.interceptor.ReceiveThreadContext;
21 import electric.util.interceptor.SendThreadContext;
22
23 import org.mule.config.MuleProperties;
24 import org.mule.config.i18n.Message;
25 import org.mule.config.i18n.Messages;
26 import org.mule.impl.MuleDescriptor;
27 import org.mule.providers.AbstractMessageReceiver;
28 import org.mule.providers.ConnectException;
29 import org.mule.providers.soap.ServiceProxy;
30 import org.mule.umo.UMOComponent;
31 import org.mule.umo.UMOException;
32 import org.mule.umo.endpoint.UMOEndpoint;
33 import org.mule.umo.lifecycle.InitialisationException;
34 import org.mule.umo.provider.UMOConnector;
35
36 import java.io.IOException JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Map JavaDoc;
39
40 /**
41  * <code>GlueMessageReceiver</code> is used to receive Glue bounded services for
42  * Mule components. services are bound in the Glue Registry using the Virtualservice
43  * implementation
44  *
45  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
46  * @version $Revision: 3798 $
47  */

48
49 public class GlueMessageReceiver extends AbstractMessageReceiver
50 {
51     private boolean createServer = false;
52
53     public GlueMessageReceiver(UMOConnector connector,
54                                UMOComponent component,
55                                UMOEndpoint endpoint,
56                                Boolean JavaDoc createServer) throws InitialisationException
57     {
58         super(connector, component, endpoint);
59         this.createServer = createServer.booleanValue();
60     }
61
62     public void doConnect() throws Exception JavaDoc
63     {
64         try
65         {
66             Class JavaDoc[] interfaces = ServiceProxy.getInterfacesForComponent(component);
67             if (interfaces.length == 0)
68             {
69                 throw new InitialisationException(
70                     new Message("soap", 2, component.getDescriptor().getName()), this);
71             }
72
73             // this is always initialisaed as synchronous as ws invocations
74
// should
75
// always execute in a single thread unless the endpont has
76
// explicitly
77
// been set to run asynchronously
78
if (!endpoint.isSynchronousSet() && !endpoint.isSynchronous())
79             {
80                 logger.debug("overriding endpoint synchronicity and setting it to true. Web service requests are executed in a single thread");
81                 endpoint.setSynchronous(true);
82             }
83
84             if (createServer)
85             {
86                 HTTP.startup(getEndpointURI().getScheme() + "://" + getEndpointURI().getHost() + ":"
87                              + getEndpointURI().getPort());
88                 registerContextHeaders();
89             }
90
91             VirtualService.enable();
92             VirtualService vService = new VirtualService(interfaces, GlueServiceProxy.createServiceHandler(
93                 this, endpoint.isSynchronous()));
94
95             // Add initialisation callback for the Glue service
96
// The callback will actually register the service
97
MuleDescriptor desc = (MuleDescriptor)component.getDescriptor();
98             String JavaDoc serviceName = getEndpointURI().getPath();
99             if (!serviceName.endsWith("/"))
100             {
101                 serviceName += "/";
102             }
103             serviceName += component.getDescriptor().getName();
104             desc.addInitialisationCallback(new GlueInitialisationCallback(vService, serviceName,
105                 new ServiceContext()));
106
107         }
108         catch (ClassNotFoundException JavaDoc e)
109         {
110             throw new InitialisationException(new Message(Messages.CLASS_X_NOT_FOUND, e.getMessage()), e,
111                 this);
112         }
113         catch (UMOException e)
114         {
115             throw new InitialisationException(new Message("soap", 3, component.getDescriptor().getName()), e,
116                 this);
117         }
118         catch (Exception JavaDoc e)
119         {
120             throw new InitialisationException(new Message(Messages.FAILED_TO_START_X, "Soap Server"), e, this);
121         }
122     }
123
124     public void doDisconnect() throws Exception JavaDoc
125     {
126         if (createServer)
127         {
128             try
129             {
130                 HTTP.shutdown(getEndpointURI().getScheme() + "://" + getEndpointURI().getHost() + ":"
131                               + getEndpointURI().getPort());
132             }
133             catch (IOException JavaDoc e)
134             {
135                 throw new ConnectException(e, this);
136             }
137         }
138     }
139
140     protected void registerContextHeaders()
141     {
142         ApplicationContext.addOutboundSoapRequestInterceptor(new SendThreadContext(
143             MuleProperties.MULE_CORRELATION_ID_PROPERTY));
144         ApplicationContext.addOutboundSoapRequestInterceptor(new SendThreadContext(
145             MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY));
146         ApplicationContext.addOutboundSoapRequestInterceptor(new SendThreadContext(
147             MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY));
148         ApplicationContext.addOutboundSoapRequestInterceptor(new SendThreadContext(
149             MuleProperties.MULE_REPLY_TO_PROPERTY, true));
150
151         ApplicationContext.addInboundSoapRequestInterceptor(new ReceiveThreadContext(
152             MuleProperties.MULE_CORRELATION_ID_PROPERTY));
153         ApplicationContext.addInboundSoapRequestInterceptor(new ReceiveThreadContext(
154             MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY));
155         ApplicationContext.addInboundSoapRequestInterceptor(new ReceiveThreadContext(
156             MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY));
157         ApplicationContext.addInboundSoapRequestInterceptor(new ReceiveThreadContext(
158             MuleProperties.MULE_REPLY_TO_PROPERTY, true));
159     }
160
161     /**
162      * Template method to dispose any resources associated with this receiver. There
163      * is not need to dispose the connector as this is already done by the framework
164      */

165     protected void doDispose()
166     {
167         try
168         {
169             Registry.unpublish(component.getDescriptor().getName());
170         }
171         catch (RegistryException e)
172         {
173             logger.error(new Message(Messages.FAILED_TO_UNREGISTER_X_ON_ENDPOINT_X, component.getDescriptor()
174                 .getName(), endpoint.getEndpointURI()), e);
175         }
176     }
177
178     protected Context getContext()
179     {
180         Context c = null;
181         if (endpoint.getProperties() != null)
182         {
183             c = (Context)endpoint.getProperties().get("glueContext");
184             if (c == null && endpoint.getProperties().size() > 0)
185             {
186                 c = new Context();
187                 for (Iterator JavaDoc iterator = endpoint.getProperties().entrySet().iterator(); iterator.hasNext();)
188                 {
189                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iterator.next();
190                     c.addProperty(entry.getKey().toString(), entry.getValue());
191                 }
192             }
193         }
194         return c;
195     }
196 }
197
Popular Tags