View Javadoc

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