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