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