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.doxia.tools.SiteTool;
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.annotations.ResolutionScope;
39  import org.apache.maven.plugins.site.render.AbstractSiteRenderingMojo;
40  import org.apache.maven.reporting.exec.MavenReportExecution;
41  import org.codehaus.plexus.util.IOUtil;
42  import org.eclipse.jetty.server.Server;
43  import org.eclipse.jetty.webapp.WebAppContext;
44  
45  import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
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   *
53   */
54  @Mojo(name = "run", requiresDependencyResolution = ResolutionScope.TEST, requiresReports = true)
55  public class SiteRunMojo extends AbstractSiteRenderingMojo {
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 host to execute the HTTP server on.
64       */
65      @Parameter(property = "host", defaultValue = "localhost")
66      private String host;
67  
68      /**
69       * The port to execute the HTTP server on.
70       */
71      @Parameter(property = "port", defaultValue = "8080")
72      private int port;
73  
74      /**
75       * @see org.apache.maven.plugin.AbstractMojo#execute()
76       */
77      public void execute() throws MojoExecutionException, MojoFailureException {
78          checkInputEncoding();
79  
80          Server server = new Server(InetSocketAddress.createUnresolved(host, port));
81          server.setStopAtShutdown(true);
82  
83          WebAppContext webapp = createWebApplication();
84          webapp.setServer(server);
85  
86          server.setHandler(webapp);
87  
88          try {
89              server.start();
90          } catch (Exception e) {
91              throw new MojoExecutionException("Error executing Jetty", e);
92          }
93  
94          getLog().info(buffer().a("Started Jetty on ").strong(server.getURI()).toString());
95  
96          // Watch it
97          try {
98              server.getThreadPool().join();
99          } catch (InterruptedException e) {
100             getLog().warn("Jetty was interrupted", e);
101         }
102     }
103 
104     private WebAppContext createWebApplication() throws MojoExecutionException {
105         File webXml = new File(tempWebappDirectory, "WEB-INF/web.xml");
106         webXml.getParentFile().mkdirs();
107 
108         try (InputStream inStream = getClass().getResourceAsStream("/run/web.xml"); //
109                 FileOutputStream outStream = new FileOutputStream(webXml)) {
110             IOUtil.copy(inStream, outStream);
111         } catch (IOException e) {
112             throw new MojoExecutionException("Unable to construct temporary webapp for running site", e);
113         }
114 
115         WebAppContext webapp = new WebAppContext();
116         webapp.setContextPath("/");
117         webapp.setResourceBase(tempWebappDirectory.getAbsolutePath());
118         webapp.setAttribute(DoxiaFilter.OUTPUT_DIRECTORY_KEY, tempWebappDirectory);
119         webapp.setAttribute(DoxiaFilter.SITE_RENDERER_KEY, siteRenderer);
120         webapp.getInitParams().put("org.mortbay.jetty.servlet.Default.useFileMappedBuffer", "false");
121 
122         // For external reports
123         project.getReporting().setOutputDirectory(tempWebappDirectory.getAbsolutePath());
124 
125         List<Locale> localesList = getLocales();
126         webapp.setAttribute(DoxiaFilter.LOCALES_LIST_KEY, localesList);
127 
128         try {
129             Map<String, DoxiaBean> i18nDoxiaContexts = new HashMap<>();
130 
131             for (Locale locale : localesList) {
132                 SiteRenderingContext i18nContext = createSiteRenderingContext(locale);
133                 i18nContext.setInputEncoding(getInputEncoding());
134                 i18nContext.setOutputEncoding(getOutputEncoding());
135 
136                 SiteRenderingContext i18nGeneratedSiteContext = createSiteRenderingContext(locale);
137                 i18nGeneratedSiteContext.setInputEncoding(getInputEncoding());
138                 i18nGeneratedSiteContext.setOutputEncoding(getOutputEncoding());
139                 i18nGeneratedSiteContext.getSiteDirectories().clear();
140 
141                 File outputDirectory = getOutputDirectory(locale);
142                 List<MavenReportExecution> reports = getReports(outputDirectory);
143 
144                 Map<String, DocumentRenderer> i18nDocuments = locateDocuments(i18nContext, reports, locale);
145                 if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
146                     i18nGeneratedSiteContext.addSiteDirectory(new File(generatedSiteDirectory, locale.toString()));
147                 } else {
148                     i18nGeneratedSiteContext.addSiteDirectory(generatedSiteDirectory);
149                 }
150                 DoxiaBean doxiaBean = new DoxiaBean(i18nContext, i18nDocuments, i18nGeneratedSiteContext);
151 
152                 if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
153                     i18nDoxiaContexts.put(locale.toString(), doxiaBean);
154                 } else {
155                     i18nDoxiaContexts.put("default", doxiaBean);
156                 }
157 
158                 if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
159                     siteRenderer.copyResources(i18nContext, new File(tempWebappDirectory, locale.toString()));
160                 } else {
161                     siteRenderer.copyResources(i18nContext, tempWebappDirectory);
162                 }
163             }
164 
165             webapp.setAttribute(DoxiaFilter.I18N_DOXIA_CONTEXTS_KEY, i18nDoxiaContexts);
166         } catch (Exception e) {
167             throw new MojoExecutionException("Unable to set up webapp", e);
168         }
169         return webapp;
170     }
171 
172     private File getOutputDirectory(Locale locale) {
173         File file;
174         if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
175             file = new File(tempWebappDirectory, locale.toString());
176         } else {
177             file = tempWebappDirectory;
178         }
179 
180         // Safety
181         if (!file.exists()) {
182             file.mkdirs();
183         }
184 
185         return file;
186     }
187 
188     public void setTempWebappDirectory(File tempWebappDirectory) {
189         this.tempWebappDirectory = tempWebappDirectory;
190     }
191 
192     public void setPort(int port) {
193         this.port = port;
194     }
195 }