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 | 0 | protected SortedSet<String> webapps = new TreeSet<String>(); |
32 | |
|
33 | |
public JettyWebappServerAgent() |
34 | |
{ |
35 | 0 | super(NAME); |
36 | 0 | } |
37 | |
|
38 | |
protected JettyWebappServerAgent(String name) |
39 | |
{ |
40 | 0 | super(name); |
41 | 0 | } |
42 | |
|
43 | |
public void dispose() |
44 | |
{ |
45 | 0 | webapps.clear(); |
46 | 0 | } |
47 | |
|
48 | |
public void initialise() throws InitialisationException |
49 | |
{ |
50 | 0 | } |
51 | |
|
52 | |
public void start() throws MuleException |
53 | |
{ |
54 | 0 | final Map<String,JettyHttpConnector> connectorMap = muleContext.getRegistry().lookupByType(JettyHttpConnector.class); |
55 | 0 | if (connectorMap.isEmpty()) |
56 | |
{ |
57 | |
|
58 | 0 | unregisterMeQuietly(); |
59 | |
} |
60 | |
|
61 | |
|
62 | 0 | for (JettyHttpConnector c : connectorMap.values()) |
63 | |
{ |
64 | 0 | if (!c.canHostFullWars()) |
65 | |
{ |
66 | 0 | unregisterMeQuietly(); |
67 | 0 | break; |
68 | |
} |
69 | |
} |
70 | 0 | } |
71 | |
|
72 | |
public void stop() throws MuleException |
73 | |
{ |
74 | 0 | } |
75 | |
|
76 | |
@Override |
77 | |
public String getDescription() |
78 | |
{ |
79 | 0 | StringBuilder sb = new StringBuilder(String.format("'''Embedded server hosting webapps at:%n ")); |
80 | 0 | sb.append(StringUtils.join(webapps.iterator(), String.format("%n "))); |
81 | |
|
82 | 0 | return sb.toString(); |
83 | |
} |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
public void onJettyConnectorStarted(JettyHttpConnector jetty) |
89 | |
{ |
90 | |
|
91 | 0 | final Handler[] handlers = jetty.getHttpServer().getChildHandlersByClass(WebAppContext.class); |
92 | 0 | for (Handler handler : handlers) |
93 | |
{ |
94 | |
|
95 | 0 | WebAppContext webapp = (WebAppContext) handler; |
96 | |
|
97 | 0 | final Connector c = jetty.getHttpServer().getConnectors()[0]; |
98 | 0 | final String url = String.format("http://%s%s%s", |
99 | |
c.getHost(), |
100 | |
c.getPort() == 80 ? StringUtils.EMPTY : ":" + c.getPort(), |
101 | |
webapp.getContextPath()); |
102 | 0 | webapps.add(url); |
103 | |
} |
104 | 0 | } |
105 | |
} |