View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.site.run;
20  
21  import java.io.File;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.net.InetSocketAddress;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Locale;
29  import java.util.Map;
30  
31  import org.apache.maven.doxia.siterenderer.DocumentRenderer;
32  import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
33  import org.apache.maven.plugin.MojoExecutionException;
34  import org.apache.maven.plugin.MojoFailureException;
35  import org.apache.maven.plugins.annotations.Mojo;
36  import org.apache.maven.plugins.annotations.Parameter;
37  import org.apache.maven.plugins.annotations.ResolutionScope;
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.eclipse.jetty.server.Server;
42  import org.eclipse.jetty.webapp.WebAppContext;
43  
44  import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
45  
46  /**
47   * Starts the site up, rendering documents as requested for faster editing.
48   * It uses Jetty as the web server.
49   *
50   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
51   *
52   */
53  @Mojo(name = "run", requiresDependencyResolution = ResolutionScope.TEST, requiresReports = true)
54  public class SiteRunMojo extends AbstractSiteRenderingMojo {
55      /**
56       * Where to create the dummy web application.
57       */
58      @Parameter(defaultValue = "${project.build.directory}/site-webapp")
59      private File tempWebappDirectory;
60  
61      /**
62       * The host to execute the HTTP server on.
63       */
64      @Parameter(property = "host", defaultValue = "localhost")
65      private String host;
66  
67      /**
68       * The port to execute the HTTP server on.
69       */
70      @Parameter(property = "port", defaultValue = "8080")
71      private int port;
72  
73      /**
74       * @see org.apache.maven.plugin.AbstractMojo#execute()
75       */
76      public void execute() throws MojoExecutionException, MojoFailureException {
77          checkInputEncoding();
78  
79          Server server = new Server(InetSocketAddress.createUnresolved(host, port));
80          server.setStopAtShutdown(true);
81  
82          WebAppContext webapp = createWebApplication();
83          webapp.setServer(server);
84  
85          server.setHandler(webapp);
86  
87          try {
88              server.start();
89          } catch (Exception e) {
90              throw new MojoExecutionException("Error executing Jetty", e);
91          }
92  
93          getLog().info(buffer().a("Started Jetty on ").strong(server.getURI()).toString());
94  
95          // Watch it
96          try {
97              server.getThreadPool().join();
98          } catch (InterruptedException e) {
99              getLog().warn("Jetty was interrupted", e);
100         }
101     }
102 
103     private WebAppContext createWebApplication() throws MojoExecutionException {
104         File webXml = new File(tempWebappDirectory, "WEB-INF/web.xml");
105         webXml.getParentFile().mkdirs();
106 
107         try (InputStream inStream = getClass().getResourceAsStream("/run/web.xml"); //
108                 FileOutputStream outStream = new FileOutputStream(webXml)) {
109             IOUtil.copy(inStream, outStream);
110         } catch (IOException e) {
111             throw new MojoExecutionException("Unable to construct temporary webapp for running site", e);
112         }
113 
114         WebAppContext webapp = new WebAppContext();
115         webapp.setContextPath("/");
116         webapp.setResourceBase(tempWebappDirectory.getAbsolutePath());
117         webapp.setAttribute(DoxiaFilter.OUTPUT_DIRECTORY_KEY, tempWebappDirectory);
118         webapp.setAttribute(DoxiaFilter.SITE_RENDERER_KEY, siteRenderer);
119         webapp.getInitParams().put("org.mortbay.jetty.servlet.Default.useFileMappedBuffer", "false");
120 
121         // For external reports
122         project.getReporting().setOutputDirectory(tempWebappDirectory.getAbsolutePath());
123         for (MavenReportExecution mavenReportExecution : getReports()) {
124             mavenReportExecution.getMavenReport().setReportOutputDirectory(tempWebappDirectory);
125         }
126 
127         List<MavenReportExecution> reports =
128                 getReports(); // TODO: is it sane to call getReports() method a second time?
129 
130         List<Locale> localesList = getLocales();
131         webapp.setAttribute(DoxiaFilter.LOCALES_LIST_KEY, localesList);
132 
133         // Default is first in the list
134         Locale defaultLocale = localesList.get(0);
135 
136         try {
137             Map<String, DoxiaBean> i18nDoxiaContexts = new HashMap<>();
138 
139             for (Locale locale : localesList) {
140                 SiteRenderingContext i18nContext = createSiteRenderingContext(locale);
141                 i18nContext.setInputEncoding(getInputEncoding());
142                 i18nContext.setOutputEncoding(getOutputEncoding());
143 
144                 SiteRenderingContext i18nGeneratedSiteContext = createSiteRenderingContext(locale);
145                 i18nGeneratedSiteContext.setInputEncoding(getInputEncoding());
146                 i18nGeneratedSiteContext.setOutputEncoding(getOutputEncoding());
147                 i18nGeneratedSiteContext.getSiteDirectories().clear();
148 
149                 Map<String, DocumentRenderer> i18nDocuments = locateDocuments(i18nContext, reports, locale);
150                 DoxiaBean doxiaBean;
151                 if (defaultLocale.equals(locale)) {
152                     i18nGeneratedSiteContext.addSiteDirectory(generatedSiteDirectory);
153                     doxiaBean = new DoxiaBean(i18nContext, i18nDocuments, i18nGeneratedSiteContext);
154                 } else {
155                     i18nGeneratedSiteContext.addSiteDirectory(new File(generatedSiteDirectory, locale.toString()));
156                     doxiaBean = new DoxiaBean(i18nContext, i18nDocuments, i18nGeneratedSiteContext);
157                 }
158 
159                 i18nDoxiaContexts.put(locale.toString(), doxiaBean);
160                 if (defaultLocale.equals(locale)) {
161                     i18nDoxiaContexts.put("default", doxiaBean);
162                 }
163 
164                 if (defaultLocale.equals(locale)) {
165                     siteRenderer.copyResources(i18nContext, tempWebappDirectory);
166                 } else {
167                     siteRenderer.copyResources(i18nContext, new File(tempWebappDirectory, locale.toString()));
168                 }
169             }
170 
171             webapp.setAttribute(DoxiaFilter.I18N_DOXIA_CONTEXTS_KEY, i18nDoxiaContexts);
172         } catch (Exception e) {
173             throw new MojoExecutionException("Unable to set up webapp", e);
174         }
175         return webapp;
176     }
177 
178     public void setTempWebappDirectory(File tempWebappDirectory) {
179         this.tempWebappDirectory = tempWebappDirectory;
180     }
181 
182     public void setPort(int port) {
183         this.port = port;
184     }
185 }