View Javadoc

1   /*
2    * $Id: JettyWebappServerAgent.java 21048 2011-01-20 16:37:13Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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   * A 'proxy' agent that displays info about any webapps deployed together
28   * with their entry-point url. Intended as a helper for splash screen, no other functionality.
29   * Cooperates with the {@link JettyHttpConnector}.
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              // no target web servers configured, nothing to do.
62              unregisterMeQuietly();
63          }
64  
65          // special handling for ajax connector, it inherits jetty one, but is not hosting full webapps
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       * A callback for the connector, as it has a 'lazy-start' policy.
91       */
92      public void onJettyConnectorStarted(JettyHttpConnector jetty)
93      {
94          // include every connector, just in case
95          final Handler[] handlers = jetty.getHttpServer().getChildHandlersByClass(WebAppContext.class);
96          for (Handler handler : handlers)
97          {
98              // so much for generics :(
99              WebAppContext webapp = (WebAppContext) handler;
100             // build the full webapp url
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 }