Coverage Report - org.apache.maven.plugins.site.SiteRunMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
SiteRunMojo
0%
0/77
0%
0/10
3,8
 
 1  
 package org.apache.maven.plugins.site;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.File;
 23  
 import java.io.FileNotFoundException;
 24  
 import java.io.FileOutputStream;
 25  
 import java.io.IOException;
 26  
 import java.io.InputStream;
 27  
 import java.util.HashMap;
 28  
 import java.util.Iterator;
 29  
 import java.util.List;
 30  
 import java.util.Locale;
 31  
 import java.util.Map;
 32  
 
 33  
 import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
 34  
 import org.apache.maven.plugin.MojoExecutionException;
 35  
 import org.apache.maven.plugin.MojoFailureException;
 36  
 import org.apache.maven.plugins.site.webapp.DoxiaBean;
 37  
 import org.apache.maven.plugins.site.webapp.DoxiaFilter;
 38  
 import org.apache.maven.reporting.MavenReport;
 39  
 import org.codehaus.plexus.util.IOUtil;
 40  
 import org.mortbay.jetty.Connector;
 41  
 import org.mortbay.jetty.Handler;
 42  
 import org.mortbay.jetty.Server;
 43  
 import org.mortbay.jetty.handler.DefaultHandler;
 44  
 import org.mortbay.jetty.nio.SelectChannelConnector;
 45  
 import org.mortbay.jetty.webapp.WebAppContext;
 46  
 
 47  
 /**
 48  
  * Starts the site up, rendering documents as requested for faster editing.
 49  
  * It uses Jetty as the web server.
 50  
  *
 51  
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 52  
  * @version $Id: org.apache.maven.plugins.site.SiteRunMojo.html 816550 2012-05-08 11:52:49Z hboutemy $
 53  
  * @goal run
 54  
  * @aggregator
 55  
  */
 56  0
 public class SiteRunMojo
 57  
     extends AbstractSiteRenderingMojo
 58  
 {
 59  
     /**
 60  
      * Where to create the dummy web application.
 61  
      *
 62  
      * @parameter expression="${project.build.directory}/site-webapp"
 63  
      */
 64  
     private File tempWebappDirectory;
 65  
 
 66  
     /**
 67  
      * The port to execute the HTTP server on.
 68  
      *
 69  
      * @parameter expression="${port}" default-value="8080"
 70  
      */
 71  
     private int port;
 72  
 
 73  
     private static final int MAX_IDLE_TIME = 30000;
 74  
 
 75  
     /**
 76  
      * @see org.apache.maven.plugin.AbstractMojo#execute()
 77  
      */
 78  
     public void execute()
 79  
         throws MojoExecutionException, MojoFailureException
 80  
     {
 81  0
         Server server = new Server();
 82  0
         server.setStopAtShutdown( true );
 83  
 
 84  0
         Connector defaultConnector = getDefaultConnector();
 85  0
         server.setConnectors( new Connector[] { defaultConnector } );
 86  
 
 87  0
         WebAppContext webapp = createWebApplication();
 88  0
         webapp.setServer( server );
 89  
 
 90  0
         DefaultHandler defaultHandler = new DefaultHandler();
 91  0
         defaultHandler.setServer( server );
 92  
 
 93  0
         Handler[] handlers = new Handler[2];
 94  0
         handlers[0] = webapp;
 95  0
         handlers[1] = defaultHandler;
 96  0
         server.setHandlers( handlers );
 97  
 
 98  0
         getLog().info( "Starting Jetty on http://localhost:" + port + "/" );
 99  
         try
 100  
         {
 101  0
             server.start();
 102  
         }
 103  0
         catch ( Exception e )
 104  
         {
 105  0
             throw new MojoExecutionException( "Error executing Jetty: " + e.getMessage(), e );
 106  0
         }
 107  
 
 108  
         // Watch it
 109  
         try
 110  
         {
 111  0
             server.getThreadPool().join();
 112  
         }
 113  0
         catch ( InterruptedException e )
 114  
         {
 115  0
             getLog().warn( "Jetty was interrupted", e );
 116  0
         }
 117  0
     }
 118  
 
 119  
     private WebAppContext createWebApplication()
 120  
         throws MojoExecutionException
 121  
     {
 122  0
         File webXml = new File( tempWebappDirectory, "WEB-INF/web.xml" );
 123  0
         webXml.getParentFile().mkdirs();
 124  
 
 125  0
         InputStream inStream = null;
 126  0
         FileOutputStream outStream = null;
 127  
         try
 128  
         {
 129  0
             inStream = getClass().getResourceAsStream( "/webapp/web.xml" );
 130  0
             outStream = new FileOutputStream( webXml );
 131  0
             IOUtil.copy( inStream, outStream );
 132  
         }
 133  0
         catch ( FileNotFoundException e )
 134  
         {
 135  0
             throw new MojoExecutionException( "Unable to construct temporary webapp for running site", e );
 136  
         }
 137  0
         catch ( IOException e )
 138  
         {
 139  0
             throw new MojoExecutionException( "Unable to construct temporary webapp for running site", e );
 140  
         }
 141  
         finally
 142  
         {
 143  0
             IOUtil.close( outStream );
 144  0
             IOUtil.close( inStream );
 145  0
         }
 146  
 
 147  0
         WebAppContext webapp = new WebAppContext();
 148  0
         webapp.setContextPath( "/" );
 149  0
         webapp.setResourceBase( tempWebappDirectory.getAbsolutePath() );
 150  0
         webapp.setAttribute( DoxiaFilter.SITE_RENDERER_KEY, siteRenderer );
 151  
 
 152  
         // For external reports
 153  0
         project.getReporting().setOutputDirectory( tempWebappDirectory.getAbsolutePath() );
 154  0
         for ( Iterator i = reports.iterator(); i.hasNext(); )
 155  
         {
 156  0
             MavenReport report = (MavenReport) i.next();
 157  0
             report.setReportOutputDirectory( tempWebappDirectory );
 158  
         }
 159  
 
 160  0
         List filteredReports = filterReports( reports );
 161  
 
 162  0
         List localesList = siteTool.getAvailableLocales( locales );
 163  0
         webapp.setAttribute( DoxiaFilter.LOCALES_LIST_KEY, localesList );
 164  
 
 165  
         // Default is first in the list
 166  0
         Locale defaultLocale = (Locale) localesList.get( 0 );
 167  0
         Locale.setDefault( defaultLocale );
 168  
 
 169  
         try
 170  
         {
 171  0
             Map i18nDoxiaContexts = new HashMap();
 172  
 
 173  0
             for ( Iterator it = localesList.iterator(); it.hasNext(); )
 174  
             {
 175  0
                 Locale locale = (Locale) it.next();
 176  
 
 177  0
                 SiteRenderingContext i18nContext = createSiteRenderingContext( locale );
 178  0
                 Map i18nDocuments = locateDocuments( i18nContext, filteredReports, locale );
 179  
                 DoxiaBean doxiaBean;
 180  0
                 if ( defaultLocale.equals( locale ) )
 181  
                 {
 182  0
                     doxiaBean = new DoxiaBean( i18nContext, i18nDocuments, generatedSiteDirectory );
 183  
                 }
 184  
                 else
 185  
                 {
 186  0
                     doxiaBean = new DoxiaBean( i18nContext, i18nDocuments, new File( generatedSiteDirectory, locale
 187  
                         .getLanguage() ) );
 188  
                 }
 189  
 
 190  0
                 i18nDoxiaContexts.put( locale.getLanguage(), doxiaBean );
 191  0
                 if ( defaultLocale.equals( locale ) )
 192  
                 {
 193  0
                     i18nDoxiaContexts.put( "default", doxiaBean );
 194  
                 }
 195  
 
 196  0
                 if ( defaultLocale.equals( locale ) )
 197  
                 {
 198  0
                     siteRenderer.copyResources( i18nContext, new File( siteDirectory, "resources" ),
 199  
                                                 tempWebappDirectory );
 200  
                 }
 201  
                 else
 202  
                 {
 203  0
                     siteRenderer.copyResources( i18nContext, new File( siteDirectory, "resources" ),
 204  
                                                 new File( tempWebappDirectory, locale.getLanguage() ) );
 205  
                 }
 206  
             }
 207  
 
 208  0
             webapp.setAttribute( DoxiaFilter.I18N_DOXIA_CONTEXTS_KEY, i18nDoxiaContexts );
 209  
         }
 210  0
         catch ( Exception e )
 211  
         {
 212  0
             throw new MojoExecutionException( "Unable to set up webapp", e );
 213  0
         }
 214  0
         return webapp;
 215  
     }
 216  
 
 217  
     private Connector getDefaultConnector()
 218  
     {
 219  0
         Connector connector = new SelectChannelConnector();
 220  0
         connector.setPort( port );
 221  0
         connector.setMaxIdleTime( MAX_IDLE_TIME );
 222  0
         return connector;
 223  
     }
 224  
 
 225  
     public void setTempWebappDirectory( File tempWebappDirectory )
 226  
     {
 227  0
         this.tempWebappDirectory = tempWebappDirectory;
 228  0
     }
 229  
 
 230  
     public void setPort( int port )
 231  
     {
 232  0
         this.port = port;
 233  0
     }
 234  
 }