Coverage Report - org.apache.maven.plugins.site.SiteRunMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
SiteRunMojo
0%
0/79
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  
 
 28  
 import java.util.HashMap;
 29  
 import java.util.List;
 30  
 import java.util.Locale;
 31  
 import java.util.Map;
 32  
 
 33  
 import org.apache.maven.doxia.siterenderer.DocumentRenderer;
 34  
 import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
 35  
 import org.apache.maven.plugin.MojoExecutionException;
 36  
 import org.apache.maven.plugin.MojoFailureException;
 37  
 import org.apache.maven.plugins.annotations.Mojo;
 38  
 import org.apache.maven.plugins.annotations.Parameter;
 39  
 import org.apache.maven.plugins.site.webapp.DoxiaBean;
 40  
 import org.apache.maven.plugins.site.webapp.DoxiaFilter;
 41  
 import org.apache.maven.reporting.exec.MavenReportExecution;
 42  
 
 43  
 import org.codehaus.plexus.util.IOUtil;
 44  
 
 45  
 import org.mortbay.jetty.Connector;
 46  
 import org.mortbay.jetty.Handler;
 47  
 import org.mortbay.jetty.Server;
 48  
 import org.mortbay.jetty.handler.DefaultHandler;
 49  
 import org.mortbay.jetty.nio.SelectChannelConnector;
 50  
 import org.mortbay.jetty.webapp.WebAppContext;
 51  
 
 52  
 /**
 53  
  * Starts the site up, rendering documents as requested for faster editing.
 54  
  * It uses Jetty as the web server.
 55  
  *
 56  
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 57  
  * @version $Id: SiteRunMojo.java 1365758 2012-07-25 21:20:39Z hboutemy $
 58  
  */
 59  
 @Mojo( name = "run", aggregator = true, requiresReports = true )
 60  0
 public class SiteRunMojo
 61  
     extends AbstractSiteRenderingMojo
 62  
 {
 63  
     /**
 64  
      * Where to create the dummy web application.
 65  
      */
 66  
     @Parameter( defaultValue = "${project.build.directory}/site-webapp" )
 67  
     private File tempWebappDirectory;
 68  
 
 69  
     /**
 70  
      * The port to execute the HTTP server on.
 71  
      *
 72  
      * @parameter expression="${port}" default-value="8080"
 73  
      */
 74  
     private int port;
 75  
 
 76  
     private static final int MAX_IDLE_TIME = 30000;
 77  
 
 78  
     /**
 79  
      * @see org.apache.maven.plugin.AbstractMojo#execute()
 80  
      */
 81  
     public void execute()
 82  
         throws MojoExecutionException, MojoFailureException
 83  
     {
 84  0
         Server server = new Server();
 85  0
         server.setStopAtShutdown( true );
 86  
 
 87  0
         Connector defaultConnector = getDefaultConnector();
 88  0
         server.setConnectors( new Connector[] { defaultConnector } );
 89  
 
 90  0
         WebAppContext webapp = createWebApplication();
 91  0
         webapp.setServer( server );
 92  
 
 93  0
         DefaultHandler defaultHandler = new DefaultHandler();
 94  0
         defaultHandler.setServer( server );
 95  
 
 96  0
         Handler[] handlers = new Handler[2];
 97  0
         handlers[0] = webapp;
 98  0
         handlers[1] = defaultHandler;
 99  0
         server.setHandlers( handlers );
 100  
 
 101  0
         getLog().info( "Starting Jetty on http://localhost:" + port + "/" );
 102  
         try
 103  
         {
 104  0
             server.start();
 105  
         }
 106  0
         catch ( Exception e )
 107  
         {
 108  0
             throw new MojoExecutionException( "Error executing Jetty: " + e.getMessage(), e );
 109  0
         }
 110  
 
 111  
         // Watch it
 112  
         try
 113  
         {
 114  0
             server.getThreadPool().join();
 115  
         }
 116  0
         catch ( InterruptedException e )
 117  
         {
 118  0
             getLog().warn( "Jetty was interrupted", e );
 119  0
         }
 120  0
     }
 121  
 
 122  
     private WebAppContext createWebApplication()
 123  
         throws MojoExecutionException
 124  
     {
 125  0
         File webXml = new File( tempWebappDirectory, "WEB-INF/web.xml" );
 126  0
         webXml.getParentFile().mkdirs();
 127  
 
 128  0
         InputStream inStream = null;
 129  0
         FileOutputStream outStream = null;
 130  
         try
 131  
         {
 132  0
             inStream = getClass().getResourceAsStream( "/webapp/web.xml" );
 133  0
             outStream = new FileOutputStream( webXml );
 134  0
             IOUtil.copy( inStream, outStream );
 135  
         }
 136  0
         catch ( FileNotFoundException e )
 137  
         {
 138  0
             throw new MojoExecutionException( "Unable to construct temporary webapp for running site", e );
 139  
         }
 140  0
         catch ( IOException e )
 141  
         {
 142  0
             throw new MojoExecutionException( "Unable to construct temporary webapp for running site", e );
 143  
         }
 144  
         finally
 145  
         {
 146  0
             IOUtil.close( outStream );
 147  0
             IOUtil.close( inStream );
 148  0
         }
 149  
 
 150  0
         WebAppContext webapp = new WebAppContext();
 151  0
         webapp.setContextPath( "/" );
 152  0
         webapp.setResourceBase( tempWebappDirectory.getAbsolutePath() );
 153  0
         webapp.setAttribute( DoxiaFilter.SITE_RENDERER_KEY, siteRenderer );
 154  0
         webapp.getInitParams().put( "org.mortbay.jetty.servlet.Default.useFileMappedBuffer", "false" );
 155  
 
 156  
         // For external reports
 157  0
         project.getReporting().setOutputDirectory( tempWebappDirectory.getAbsolutePath() );
 158  0
         for ( MavenReportExecution mavenReportExecution : getReports() )
 159  
         {
 160  0
             mavenReportExecution.getMavenReport().setReportOutputDirectory( tempWebappDirectory );
 161  
         }
 162  
 
 163  0
         List<MavenReportExecution> reports = getReports(); // TODO: is it sane to call getReports() method a second time?
 164  
 
 165  0
         List<Locale> localesList = siteTool.getAvailableLocales( locales );
 166  0
         webapp.setAttribute( DoxiaFilter.LOCALES_LIST_KEY, localesList );
 167  
 
 168  
         // Default is first in the list
 169  0
         Locale defaultLocale = localesList.get( 0 );
 170  0
         Locale.setDefault( defaultLocale );
 171  
 
 172  
         try
 173  
         {
 174  0
             Map<String, DoxiaBean> i18nDoxiaContexts = new HashMap<String, DoxiaBean>();
 175  
 
 176  0
             for ( Locale locale : localesList )
 177  
             {
 178  0
                 SiteRenderingContext i18nContext = createSiteRenderingContext( locale );
 179  0
                 i18nContext.setInputEncoding( getInputEncoding() );
 180  0
                 i18nContext.setOutputEncoding( getOutputEncoding() );
 181  
 
 182  0
                 Map<String, DocumentRenderer> i18nDocuments = locateDocuments( i18nContext, reports, locale );
 183  
                 DoxiaBean doxiaBean;
 184  0
                 if ( defaultLocale.equals( locale ) )
 185  
                 {
 186  0
                     doxiaBean = new DoxiaBean( i18nContext, i18nDocuments, generatedSiteDirectory );
 187  
                 }
 188  
                 else
 189  
                 {
 190  0
                     doxiaBean =
 191  
                         new DoxiaBean( i18nContext, i18nDocuments, new File( generatedSiteDirectory,
 192  
                                                                              locale.getLanguage() ) );
 193  
                 }
 194  
 
 195  0
                 i18nDoxiaContexts.put( locale.getLanguage(), doxiaBean );
 196  0
                 if ( defaultLocale.equals( locale ) )
 197  
                 {
 198  0
                     i18nDoxiaContexts.put( "default", doxiaBean );
 199  
                 }
 200  
 
 201  0
                 if ( defaultLocale.equals( locale ) )
 202  
                 {
 203  0
                     siteRenderer.copyResources( i18nContext, new File( siteDirectory, "resources" ),
 204  
                                                 tempWebappDirectory );
 205  
                 }
 206  
                 else
 207  
                 {
 208  0
                     siteRenderer.copyResources( i18nContext, new File( siteDirectory, "resources" ),
 209  
                                                 new File( tempWebappDirectory, locale.getLanguage() ) );
 210  
                 }
 211  0
             }
 212  
 
 213  0
             webapp.setAttribute( DoxiaFilter.I18N_DOXIA_CONTEXTS_KEY, i18nDoxiaContexts );
 214  
         }
 215  0
         catch ( Exception e )
 216  
         {
 217  0
             throw new MojoExecutionException( "Unable to set up webapp", e );
 218  0
         }
 219  0
         return webapp;
 220  
     }
 221  
 
 222  
     private Connector getDefaultConnector()
 223  
     {
 224  0
         Connector connector = new SelectChannelConnector();
 225  0
         connector.setPort( port );
 226  0
         connector.setMaxIdleTime( MAX_IDLE_TIME );
 227  0
         return connector;
 228  
     }
 229  
 
 230  
     public void setTempWebappDirectory( File tempWebappDirectory )
 231  
     {
 232  0
         this.tempWebappDirectory = tempWebappDirectory;
 233  0
     }
 234  
 
 235  
     public void setPort( int port )
 236  
     {
 237  0
         this.port = port;
 238  0
     }
 239  
 }