KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > internal > admin > MuleAdminAgent


1 /*
2  * $Id: MuleAdminAgent.java 4259 2006-12-14 03:12:07Z 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.impl.internal.admin;
12
13 import org.apache.commons.lang.StringUtils;
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.mule.MuleManager;
17 import org.mule.transformers.wire.WireFormat;
18 import org.mule.transformers.wire.SerializationWireFormat;
19 import org.mule.impl.AlreadyInitialisedException;
20 import org.mule.impl.endpoint.MuleEndpointURI;
21 import org.mule.providers.service.ConnectorFactory;
22 import org.mule.umo.UMODescriptor;
23 import org.mule.umo.UMOException;
24 import org.mule.umo.endpoint.UMOEndpointURI;
25 import org.mule.umo.lifecycle.InitialisationException;
26 import org.mule.umo.manager.UMOAgent;
27 import org.mule.umo.manager.UMOManager;
28 import org.mule.umo.provider.UMOConnector;
29
30 /**
31  * <code>MuleAdminAgent</code> manages the server endpoint that receives Admin and
32  * remote client requests
33  */

34 public class MuleAdminAgent implements UMOAgent
35 {
36     public static final String JavaDoc DEFAULT_MANAGER_ENDPOINT = "_muleManagerEndpoint";
37
38     public static final String JavaDoc AGENT_NAME = "Mule Admin";
39
40     /**
41      * logger used by this class
42      */

43     protected static final Log logger = LogFactory.getLog(MuleAdminAgent.class);
44
45     private String JavaDoc serverEndpoint;
46
47     private WireFormat wireFormat;
48
49     /**
50      * Gets the name of this agent
51      *
52      * @return the agent name
53      */

54     public String JavaDoc getName()
55     {
56         return AGENT_NAME;
57     }
58
59     /**
60      * Sets the name of this agent
61      *
62      * @param name the name of the agent
63      */

64     public void setName(String JavaDoc name)
65     {
66         // not allowed
67
}
68
69     /**
70      * Should be a 1 line description of the agent
71      *
72      * @return
73      */

74     public String JavaDoc getDescription()
75     {
76         return getName() + ": accepting connections on " + serverEndpoint;
77     }
78
79     public void start() throws UMOException
80     {
81         // nothing to do (yet?)
82
}
83
84     public void stop() throws UMOException
85     {
86         // nothing to do (yet?)
87
}
88
89     public void dispose()
90     {
91         // nothing to do (yet?)
92
}
93
94     public void registered()
95     {
96         // nothing to do (yet?)
97
}
98
99     public void unregistered()
100     {
101         // nothing to do (yet?)
102
}
103
104     public void initialise() throws InitialisationException
105     {
106         if (wireFormat == null)
107         {
108             wireFormat = new SerializationWireFormat();
109         }
110         serverEndpoint = MuleManager.getConfiguration().getServerUrl();
111         UMOManager manager = MuleManager.getInstance();
112
113         try
114         {
115             if (StringUtils.isEmpty(serverEndpoint))
116             {
117                 // no serverUrl specified, warn a user
118
logger.warn("No serverEndpointUrl specified, MuleAdminAgent will not start. E.g. use "
119                             + "<mule-environment-properties serverUrl=\"tcp://example.com:60504\"/> ");
120
121                 // abort the agent registration process
122
manager.unregisterAgent(this.getName());
123
124                 return;
125             }
126
127             // Check for override
128
if (manager.getModel().isComponentRegistered(MuleManagerComponent.MANAGER_COMPONENT_NAME))
129             {
130                 logger.info("Mule manager component has already been initialised, ignoring server url");
131             }
132             else
133             {
134                 if (manager.lookupConnector(DEFAULT_MANAGER_ENDPOINT) != null)
135                 {
136                     throw new AlreadyInitialisedException("Server Components", this);
137                 }
138
139                 // Check to see if we have an endpoint identifier
140
serverEndpoint = MuleManager.getInstance().lookupEndpointIdentifier(serverEndpoint,
141                     serverEndpoint);
142                 UMOEndpointURI endpointUri = new MuleEndpointURI(serverEndpoint);
143                 UMOConnector connector = ConnectorFactory.getOrCreateConnectorByProtocol(endpointUri);
144                 // If this connector has already been initialised i.e. it's a
145
// pre-existing connector not not reinit
146
if (manager.lookupConnector(connector.getName()) == null)
147                 {
148                     connector.setName(DEFAULT_MANAGER_ENDPOINT);
149                     connector.initialise();
150                     manager.registerConnector(connector);
151                 }
152
153                 logger.info("Registering Admin listener on: " + serverEndpoint);
154                 UMODescriptor descriptor = MuleManagerComponent.getDescriptor(connector, endpointUri,
155                     wireFormat);
156                 manager.getModel().registerComponent(descriptor);
157             }
158         }
159         catch (UMOException e)
160         {
161             throw new InitialisationException(e, this);
162         }
163     }
164
165     public String JavaDoc toString()
166     {
167         return "MuleAdminAgent{" + "serverEndpoint='" + serverEndpoint + "'" + "}";
168     }
169
170     public WireFormat getWireFormat()
171     {
172         return wireFormat;
173     }
174
175     public void setWireFormat(WireFormat wireFormat)
176     {
177         this.wireFormat = wireFormat;
178     }
179 }
180
Popular Tags