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 static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
35  
36  import java.io.File;
37  import java.io.FileOutputStream;
38  import java.io.IOException;
39  import java.io.InputStream;
40  import java.util.HashMap;
41  import java.util.List;
42  import java.util.Locale;
43  import java.util.Map;
44  
45  /**
46   * Starts the site up, rendering documents as requested for faster editing.
47   * It uses Jetty as the web server.
48   *
49   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
50   *
51   */
52  @Mojo( name = "run", aggregator = true, requiresReports = true )
53  public class SiteRunMojo
54      extends AbstractSiteRenderingMojo
55  {
56      /**
57       * Where to create the dummy web application.
58       */
59      @Parameter( defaultValue = "${project.build.directory}/site-webapp" )
60      private File tempWebappDirectory;
61  
62      /**
63       * The port to execute the HTTP server on.
64       */
65      @Parameter( property = "port", defaultValue = "8080" )
66      private int port;
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( buffer().a( "Starting Jetty on " ).strong( "http://localhost:" + port + "/" ).toString() );
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 
143         try
144         {
145             Map<String, DoxiaBean> i18nDoxiaContexts = new HashMap<>();
146 
147             for ( Locale locale : localesList )
148             {
149                 SiteRenderingContext i18nContext = createSiteRenderingContext( locale );
150                 i18nContext.setInputEncoding( getInputEncoding() );
151                 i18nContext.setOutputEncoding( getOutputEncoding() );
152 
153                 SiteRenderingContext i18nGeneratedSiteContext = createSiteRenderingContext( locale );
154                 i18nGeneratedSiteContext.setInputEncoding( getInputEncoding() );
155                 i18nGeneratedSiteContext.setOutputEncoding( getOutputEncoding() );
156                 i18nGeneratedSiteContext.getSiteDirectories().clear();
157 
158                 Map<String, DocumentRenderer> i18nDocuments = locateDocuments( i18nContext, reports, locale );
159                 DoxiaBean doxiaBean;
160                 if ( defaultLocale.equals( locale ) )
161                 {
162                     i18nGeneratedSiteContext.addSiteDirectory( generatedSiteDirectory );
163                     doxiaBean = new DoxiaBean( i18nContext, i18nDocuments, i18nGeneratedSiteContext );
164                 }
165                 else
166                 {
167                     i18nGeneratedSiteContext.addSiteDirectory( new File( generatedSiteDirectory,
168                                                                          locale.toString() ) );
169                     doxiaBean = new DoxiaBean( i18nContext, i18nDocuments, i18nGeneratedSiteContext );
170                 }
171 
172                 i18nDoxiaContexts.put( locale.toString(), doxiaBean );
173                 if ( defaultLocale.equals( locale ) )
174                 {
175                     i18nDoxiaContexts.put( "default", doxiaBean );
176                 }
177 
178                 if ( defaultLocale.equals( locale ) )
179                 {
180                     siteRenderer.copyResources( i18nContext, tempWebappDirectory );
181                 }
182                 else
183                 {
184                     siteRenderer.copyResources( i18nContext, new File( tempWebappDirectory, locale.toString() ) );
185                 }
186             }
187 
188             webapp.setAttribute( DoxiaFilter.I18N_DOXIA_CONTEXTS_KEY, i18nDoxiaContexts );
189         }
190         catch ( Exception e )
191         {
192             throw new MojoExecutionException( "Unable to set up webapp", e );
193         }
194         return webapp;
195     }
196 
197     public void setTempWebappDirectory( File tempWebappDirectory )
198     {
199         this.tempWebappDirectory = tempWebappDirectory;
200     }
201 
202     public void setPort( int port )
203     {
204         this.port = port;
205     }
206 }