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.SiteRenderingContext;
34  import org.apache.maven.plugin.MojoExecutionException;
35  import org.apache.maven.plugin.MojoFailureException;
36  import org.apache.maven.plugins.site.webapp.DoxiaBean;
37  import org.apache.maven.plugins.site.webapp.DoxiaFilter;
38  import org.apache.maven.reporting.MavenReport;
39  import org.codehaus.plexus.util.IOUtil;
40  import org.mortbay.jetty.Connector;
41  import org.mortbay.jetty.Handler;
42  import org.mortbay.jetty.Server;
43  import org.mortbay.jetty.handler.DefaultHandler;
44  import org.mortbay.jetty.nio.SelectChannelConnector;
45  import org.mortbay.jetty.webapp.WebAppContext;
46  
47  /**
48   * Starts the site up, rendering documents as requested for faster editing.
49   * It uses Jetty as the web server.
50   *
51   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
52   * @version $Id: SiteRunMojo.html 816552 2012-05-08 11:54:37Z hboutemy $
53   * @goal run
54   * @aggregator
55   */
56  public class SiteRunMojo
57      extends AbstractSiteRenderingMojo
58  {
59      /**
60       * Where to create the dummy web application.
61       *
62       * @parameter expression="${project.build.directory}/site-webapp"
63       */
64      private File tempWebappDirectory;
65  
66      /**
67       * The port to execute the HTTP server on.
68       *
69       * @parameter expression="${port}" default-value="8080"
70       */
71      private int port;
72  
73      private static final int MAX_IDLE_TIME = 30000;
74  
75      /**
76       * @see org.apache.maven.plugin.AbstractMojo#execute()
77       */
78      public void execute()
79          throws MojoExecutionException, MojoFailureException
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( "/webapp/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 ( Iterator i = reports.iterator(); i.hasNext(); )
156         {
157             MavenReport report = (MavenReport) i.next();
158             report.setReportOutputDirectory( tempWebappDirectory );
159         }
160 
161         List filteredReports = filterReports( reports );
162 
163         List localesList = siteTool.getAvailableLocales( locales );
164         webapp.setAttribute( DoxiaFilter.LOCALES_LIST_KEY, localesList );
165 
166         // Default is first in the list
167         Locale defaultLocale = (Locale) localesList.get( 0 );
168         Locale.setDefault( defaultLocale );
169 
170         try
171         {
172             Map i18nDoxiaContexts = new HashMap();
173 
174             for ( Iterator it = localesList.iterator(); it.hasNext(); )
175             {
176                 Locale locale = (Locale) it.next();
177 
178                 SiteRenderingContext i18nContext = createSiteRenderingContext( locale );
179                 i18nContext.setInputEncoding( getInputEncoding() );
180                 i18nContext.setOutputEncoding( getOutputEncoding() );
181 
182                 Map i18nDocuments = locateDocuments( i18nContext, filteredReports, locale );
183                 DoxiaBean doxiaBean;
184                 if ( defaultLocale.equals( locale ) )
185                 {
186                     doxiaBean = new DoxiaBean( i18nContext, i18nDocuments, generatedSiteDirectory );
187                 }
188                 else
189                 {
190                     doxiaBean = new DoxiaBean( i18nContext, i18nDocuments, new File( generatedSiteDirectory, locale
191                         .getLanguage() ) );
192                 }
193 
194                 i18nDoxiaContexts.put( locale.getLanguage(), doxiaBean );
195                 if ( defaultLocale.equals( locale ) )
196                 {
197                     i18nDoxiaContexts.put( "default", doxiaBean );
198                 }
199 
200                 if ( defaultLocale.equals( locale ) )
201                 {
202                     siteRenderer.copyResources( i18nContext, new File( siteDirectory, "resources" ),
203                                                 tempWebappDirectory );
204                 }
205                 else
206                 {
207                     siteRenderer.copyResources( i18nContext, new File( siteDirectory, "resources" ),
208                                                 new File( tempWebappDirectory, locale.getLanguage() ) );
209                 }
210             }
211 
212             webapp.setAttribute( DoxiaFilter.I18N_DOXIA_CONTEXTS_KEY, i18nDoxiaContexts );
213         }
214         catch ( Exception e )
215         {
216             throw new MojoExecutionException( "Unable to set up webapp", e );
217         }
218         return webapp;
219     }
220 
221     private Connector getDefaultConnector()
222     {
223         Connector connector = new SelectChannelConnector();
224         connector.setPort( port );
225         connector.setMaxIdleTime( MAX_IDLE_TIME );
226         return connector;
227     }
228 
229     public void setTempWebappDirectory( File tempWebappDirectory )
230     {
231         this.tempWebappDirectory = tempWebappDirectory;
232     }
233 
234     public void setPort( int port )
235     {
236         this.port = port;
237     }
238 }