Coverage Report - org.mule.transport.servlet.jetty.JettyWebappServerAgent
 
Classes in this File Line Coverage Branch Coverage Complexity
JettyWebappServerAgent
0%
0/27
0%
0/10
0
 
 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  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  
             // no target web servers configured, nothing to do.
 58  0
             unregisterMeQuietly();
 59  
         }
 60  
 
 61  
         // special handling for ajax connector, it inherits jetty one, but is not hosting full webapps
 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  
      * 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  0
         final Handler[] handlers = jetty.getHttpServer().getChildHandlersByClass(WebAppContext.class);
 92  0
         for (Handler handler : handlers)
 93  
         {
 94  
             // so much for generics :(
 95  0
             WebAppContext webapp = (WebAppContext) handler;
 96  
             // build the full webapp url
 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  
 }