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