View Javadoc

1   /*
2    * $Id: MuleAdminAgent.java 7963 2007-08-21 08:53:15Z dirk.olmes $
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 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.impl.internal.admin;
12  
13  import org.mule.MuleManager;
14  import org.mule.impl.AlreadyInitialisedException;
15  import org.mule.impl.endpoint.MuleEndpointURI;
16  import org.mule.impl.model.ModelHelper;
17  import org.mule.providers.service.TransportFactory;
18  import org.mule.transformers.wire.SerializationWireFormat;
19  import org.mule.transformers.wire.WireFormat;
20  import org.mule.umo.UMODescriptor;
21  import org.mule.umo.UMOException;
22  import org.mule.umo.endpoint.UMOEndpointURI;
23  import org.mule.umo.lifecycle.InitialisationException;
24  import org.mule.umo.manager.UMOAgent;
25  import org.mule.umo.manager.UMOManager;
26  import org.mule.umo.provider.UMOConnector;
27  import org.mule.util.StringUtils;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /**
33   * <code>MuleAdminAgent</code> manages the server endpoint that receives Admin and
34   * remote client requests
35   */
36  public class MuleAdminAgent implements UMOAgent
37  {
38      public static final String DEFAULT_MANAGER_ENDPOINT = "_muleManagerEndpoint";
39  
40      public static final String AGENT_NAME = "Mule Admin";
41  
42      /**
43       * logger used by this class
44       */
45      protected static final Log logger = LogFactory.getLog(MuleAdminAgent.class);
46  
47      private String serverEndpoint;
48  
49      private WireFormat wireFormat;
50  
51      /**
52       * Gets the name of this agent
53       * 
54       * @return the agent name
55       */
56      public String getName()
57      {
58          return AGENT_NAME;
59      }
60  
61      /**
62       * Sets the name of this agent
63       * 
64       * @param name the name of the agent
65       */
66      public void setName(String name)
67      {
68          // not allowed
69      }
70  
71      /**
72       * Should be a 1 line description of the agent
73       * 
74       * @return
75       */
76      public String getDescription()
77      {
78          return getName() + ": accepting connections on " + serverEndpoint;
79      }
80  
81      public void start() throws UMOException
82      {
83          // nothing to do (yet?)
84      }
85  
86      public void stop() throws UMOException
87      {
88          // nothing to do (yet?)
89      }
90  
91      public void dispose()
92      {
93          // nothing to do (yet?)
94      }
95  
96      public void registered()
97      {
98          // nothing to do (yet?)
99      }
100 
101     public void unregistered()
102     {
103         // nothing to do (yet?)
104     }
105 
106     public void initialise() throws InitialisationException
107     {
108         if (wireFormat == null)
109         {
110             wireFormat = new SerializationWireFormat();
111         }
112         serverEndpoint = MuleManager.getConfiguration().getServerUrl();
113         UMOManager manager = MuleManager.getInstance();
114 
115         try
116         {
117             if (StringUtils.isEmpty(serverEndpoint))
118             {
119                 // no serverUrl specified, warn a user
120                 logger.warn("No serverEndpointUrl specified, MuleAdminAgent will not start. E.g. use "
121                             + "<mule-environment-properties serverUrl=\"tcp://example.com:60504\"/> ");
122 
123                 // abort the agent registration process
124                 manager.unregisterAgent(this.getName());
125 
126                 return;
127             }
128 
129             // Check for override
130             if (ModelHelper.isComponentRegistered(MuleManagerComponent.MANAGER_COMPONENT_NAME))
131             {
132                 logger.info("Mule manager component has already been initialised, ignoring server url");
133             }
134             else
135             {
136                 if (manager.lookupConnector(DEFAULT_MANAGER_ENDPOINT) != null)
137                 {
138                     throw new AlreadyInitialisedException("Server Components", this);
139                 }
140 
141                 // Check to see if we have an endpoint identifier
142                 serverEndpoint = MuleManager.getInstance().lookupEndpointIdentifier(serverEndpoint,
143                     serverEndpoint);
144                 UMOEndpointURI endpointUri = new MuleEndpointURI(serverEndpoint);
145                 UMOConnector connector = TransportFactory.getOrCreateConnectorByProtocol(endpointUri);
146                 // If this connector has already been initialised i.e. it's a
147                 // pre-existing connector don't reinit
148                 if (manager.lookupConnector(connector.getName()) == null)
149                 {
150                     connector.setName(DEFAULT_MANAGER_ENDPOINT);
151                     connector.initialise();
152                     manager.registerConnector(connector);
153                 }
154 
155                 logger.info("Registering Admin listener on: " + serverEndpoint);
156                 UMODescriptor descriptor = MuleManagerComponent.getDescriptor(connector, endpointUri,
157                     wireFormat);
158                 ModelHelper.registerSystemComponent(descriptor);
159             }
160         }
161         catch (UMOException e)
162         {
163             throw new InitialisationException(e, this);
164         }
165     }
166 
167     public String toString()
168     {
169         return "MuleAdminAgent{" + "serverEndpoint='" + serverEndpoint + "'" + "}";
170     }
171 
172     public WireFormat getWireFormat()
173     {
174         return wireFormat;
175     }
176 
177     public void setWireFormat(WireFormat wireFormat)
178     {
179         this.wireFormat = wireFormat;
180     }
181 }