View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
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   * A 'proxy' agent that displays info about any webapps deployed together
24   * with their entry-point url. Intended as a helper for splash screen, no other functionality.
25   * Cooperates with the {@link JettyHttpConnector}.
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              // no target web servers configured, nothing to do.
58              unregisterMeQuietly();
59          }
60  
61          // special handling for ajax connector, it inherits jetty one, but is not hosting full webapps
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       * A callback for the connector, as it has a 'lazy-start' policy.
87       */
88      public void onJettyConnectorStarted(JettyHttpConnector jetty)
89      {
90          // include every connector, just in case
91          final Handler[] handlers = jetty.getHttpServer().getChildHandlersByClass(WebAppContext.class);
92          for (Handler handler : handlers)
93          {
94              // so much for generics :(
95              WebAppContext webapp = (WebAppContext) handler;
96              // build the full webapp url
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 }