View Javadoc

1   /*
2    * $Id: RemoteDispatcherAgent.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.client.remoting;
12  
13  import org.mule.AbstractAgent;
14  import org.mule.api.MuleException;
15  import org.mule.api.endpoint.InboundEndpoint;
16  import org.mule.api.lifecycle.InitialisationException;
17  import org.mule.api.service.Service;
18  import org.mule.api.transformer.wire.WireFormat;
19  import org.mule.config.i18n.CoreMessages;
20  import org.mule.module.client.remoting.notification.RemoteDispatcherNotification;
21  import org.mule.module.client.remoting.notification.RemoteDispatcherNotificationListener;
22  import org.mule.transformer.wire.SerializedMuleMessageWireFormat;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  /**
28   * <code>RemoteDispatcherAgent</code> manages the server endpoint that receives Admin and
29   * remote client requests
30   */
31  public class RemoteDispatcherAgent extends AbstractAgent
32  {
33      public static final String AGENT_NAME = "RemoteDispatcherServer";
34  
35      /**
36       * logger used by this class
37       */
38      protected static final Log logger = LogFactory.getLog(RemoteDispatcherAgent.class);
39  
40      private WireFormat wireFormat;
41  
42      private InboundEndpoint endpoint;
43  
44  
45      public RemoteDispatcherAgent()
46      {
47          super(AGENT_NAME);
48      }
49  
50      /**
51       * Should be a 1 line description of the agent
52       */
53      public String getDescription()
54      {
55          return getName() + ": accepting connections on " + endpoint.getEndpointURI().getAddress();
56      }
57  
58      public void start() throws MuleException
59      {
60          // nothing to do
61      }
62  
63      public void stop() throws MuleException
64      {
65          // nothing to do
66      }
67  
68      public void dispose()
69      {
70          // nothing to do
71      }
72  
73  
74      public void initialise() throws InitialisationException
75      {
76          if (endpoint == null)
77          {
78              throw new IllegalArgumentException(CoreMessages.objectIsNull("remote-endpoint").getMessage());
79          }
80  
81          if (wireFormat == null)
82          {
83              wireFormat = new SerializedMuleMessageWireFormat();
84          }
85          wireFormat.setMuleContext(muleContext);
86  
87          
88          //Register the Remote Dispatcher Notification support
89          muleContext.getNotificationManager().addInterfaceToType(
90                  RemoteDispatcherNotificationListener.class,
91                  RemoteDispatcherNotification.class);
92  
93          try
94          {
95              // Check for override
96              if (muleContext.getRegistry().lookupService(RemoteDispatcherComponent.MANAGER_COMPONENT_NAME) != null)
97              {
98                  logger.info("Mule manager component has already been initialised, ignoring server url");
99              }
100             else
101             {
102 
103                 Service component = RemoteDispatcherComponent.getSerivce(endpoint, wireFormat,
104                     muleContext.getConfiguration().getDefaultEncoding(), muleContext.getConfiguration()
105                         .getDefaultResponseTimeout(), muleContext);
106                 muleContext.getRegistry().registerService(component);
107             }
108         }
109         catch (MuleException e)
110         {
111             throw new InitialisationException(e, this);
112         }
113     }
114 
115     public String toString()
116     {
117         String address = "not set";
118         if(endpoint!=null)
119         {
120              address = endpoint.getEndpointURI().getAddress();
121         }
122         return "RemoteDispatcherAgent{" + "remote-endpoint='" + address + "'" + "}";
123     }
124 
125     public WireFormat getWireFormat()
126     {
127         return wireFormat;
128     }
129 
130     public void setWireFormat(WireFormat wireFormat)
131     {
132         this.wireFormat = wireFormat;
133     }
134 
135     public InboundEndpoint getEndpoint()
136     {
137         return endpoint;
138     }
139 
140     public void setEndpoint(InboundEndpoint endpoint)
141     {
142         this.endpoint = endpoint;
143     }
144 }