1
2
3
4
5
6
7 package org.mule.transport.servlet.jetty;
8
9 import org.mule.AbstractAgent;
10 import org.mule.api.MuleException;
11 import org.mule.api.lifecycle.InitialisationException;
12 import org.mule.util.StringUtils;
13
14 import java.util.Map;
15 import java.util.SortedSet;
16 import java.util.TreeSet;
17
18 import org.mortbay.jetty.Connector;
19 import org.mortbay.jetty.Handler;
20 import org.mortbay.jetty.webapp.WebAppContext;
21
22
23
24
25
26
27 public class JettyWebappServerAgent extends AbstractAgent
28 {
29 public static final String NAME = "zzz_last_jetty-webapp-agent";
30
31 protected SortedSet<String> webapps = new TreeSet<String>();
32
33 public JettyWebappServerAgent()
34 {
35 super(NAME);
36 }
37
38 protected JettyWebappServerAgent(String name)
39 {
40 super(name);
41 }
42
43 public void dispose()
44 {
45 webapps.clear();
46 }
47
48 public void initialise() throws InitialisationException
49 {
50 }
51
52 public void start() throws MuleException
53 {
54 final Map<String,JettyHttpConnector> connectorMap = muleContext.getRegistry().lookupByType(JettyHttpConnector.class);
55 if (connectorMap.isEmpty())
56 {
57
58 unregisterMeQuietly();
59 }
60
61
62 for (JettyHttpConnector c : connectorMap.values())
63 {
64 if (!c.canHostFullWars())
65 {
66 unregisterMeQuietly();
67 break;
68 }
69 }
70 }
71
72 public void stop() throws MuleException
73 {
74 }
75
76 @Override
77 public String getDescription()
78 {
79 StringBuilder sb = new StringBuilder(String.format("'''Embedded server hosting webapps at:%n "));
80 sb.append(StringUtils.join(webapps.iterator(), String.format("%n ")));
81
82 return sb.toString();
83 }
84
85
86
87
88 public void onJettyConnectorStarted(JettyHttpConnector jetty)
89 {
90
91 final Handler[] handlers = jetty.getHttpServer().getChildHandlersByClass(WebAppContext.class);
92 for (Handler handler : handlers)
93 {
94
95 WebAppContext webapp = (WebAppContext) handler;
96
97 final Connector c = jetty.getHttpServer().getConnectors()[0];
98 final String url = String.format("http://%s%s%s",
99 c.getHost(),
100 c.getPort() == 80 ? StringUtils.EMPTY : ":" + c.getPort(),
101 webapp.getContextPath());
102 webapps.add(url);
103 }
104 }
105 }