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$
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             outStream.close();
133             outStream = null;
134             inStream.close();
135             inStream = null;
136         }
137         catch ( FileNotFoundException e )
138         {
139             throw new MojoExecutionException( "Unable to construct temporary webapp for running site", e );
140         }
141         catch ( IOException e )
142         {
143             throw new MojoExecutionException( "Unable to construct temporary webapp for running site", e );
144         }
145         finally
146         {
147             IOUtil.close( outStream );
148             IOUtil.close( inStream );
149         }
150 
151         WebAppContext webapp = new WebAppContext();
152         webapp.setContextPath( "/" );
153         webapp.setResourceBase( tempWebappDirectory.getAbsolutePath() );
154         webapp.setAttribute( DoxiaFilter.SITE_RENDERER_KEY, siteRenderer );
155         webapp.getInitParams().put( "org.mortbay.jetty.servlet.Default.useFileMappedBuffer", "false" );
156 
157         // For external reports
158         project.getReporting().setOutputDirectory( tempWebappDirectory.getAbsolutePath() );
159         for ( MavenReportExecution mavenReportExecution : getReports() )
160         {
161             mavenReportExecution.getMavenReport().setReportOutputDirectory( tempWebappDirectory );
162         }
163 
164         List<MavenReportExecution> reports = getReports(); // TODO: is it sane to call getReports() method a second time?
165 
166         List<Locale> localesList = getLocales();
167         webapp.setAttribute( DoxiaFilter.LOCALES_LIST_KEY, localesList );
168 
169         // Default is first in the list
170         Locale defaultLocale = localesList.get( 0 );
171         Locale.setDefault( defaultLocale );
172 
173         try
174         {
175             Map<String, DoxiaBean> i18nDoxiaContexts = new HashMap<String, DoxiaBean>();
176 
177             for ( Locale locale : localesList )
178             {
179                 SiteRenderingContext i18nContext = createSiteRenderingContext( locale );
180                 i18nContext.setInputEncoding( getInputEncoding() );
181                 i18nContext.setOutputEncoding( getOutputEncoding() );
182 
183                 SiteRenderingContext i18nGeneratedSiteContext = createSiteRenderingContext( locale );
184                 i18nGeneratedSiteContext.setInputEncoding( getInputEncoding() );
185                 i18nGeneratedSiteContext.setOutputEncoding( getOutputEncoding() );
186                 i18nGeneratedSiteContext.getSiteDirectories().clear();
187 
188                 Map<String, DocumentRenderer> i18nDocuments = locateDocuments( i18nContext, reports, locale );
189                 DoxiaBean doxiaBean;
190                 if ( defaultLocale.equals( locale ) )
191                 {
192                     i18nGeneratedSiteContext.addSiteDirectory( generatedSiteDirectory );
193                     doxiaBean = new DoxiaBean( i18nContext, i18nDocuments, i18nGeneratedSiteContext );
194                 }
195                 else
196                 {
197                     i18nGeneratedSiteContext.addSiteDirectory( new File( generatedSiteDirectory,
198                                                                          locale.getLanguage() ) );
199                     doxiaBean = new DoxiaBean( i18nContext, i18nDocuments, i18nGeneratedSiteContext );
200                 }
201 
202                 i18nDoxiaContexts.put( locale.getLanguage(), doxiaBean );
203                 if ( defaultLocale.equals( locale ) )
204                 {
205                     i18nDoxiaContexts.put( "default", doxiaBean );
206                 }
207 
208                 if ( defaultLocale.equals( locale ) )
209                 {
210                     siteRenderer.copyResources( i18nContext, tempWebappDirectory );
211                 }
212                 else
213                 {
214                     siteRenderer.copyResources( i18nContext, new File( tempWebappDirectory, locale.getLanguage() ) );
215                 }
216             }
217 
218             webapp.setAttribute( DoxiaFilter.I18N_DOXIA_CONTEXTS_KEY, i18nDoxiaContexts );
219         }
220         catch ( Exception e )
221         {
222             throw new MojoExecutionException( "Unable to set up webapp", e );
223         }
224         return webapp;
225     }
226 
227     private Connector getDefaultConnector()
228     {
229         Connector connector = new SelectChannelConnector();
230         connector.setPort( port );
231         connector.setMaxIdleTime( MAX_IDLE_TIME );
232         return connector;
233     }
234 
235     public void setTempWebappDirectory( File tempWebappDirectory )
236     {
237         this.tempWebappDirectory = tempWebappDirectory;
238     }
239 
240     public void setPort( int port )
241     {
242         this.port = port;
243     }
244 }