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      @Override
78      public void execute() throws MojoExecutionException, MojoFailureException {
79          checkInputEncoding();
80  
81          Server server = new Server(InetSocketAddress.createUnresolved(host, port));
82          server.setStopAtShutdown(true);
83  
84          WebAppContext webapp = createWebApplication();
85          webapp.setServer(server);
86  
87          server.setHandler(webapp);
88  
89          try {
90              server.start();
91          } catch (Exception e) {
92              throw new MojoExecutionException("Error executing Jetty", e);
93          }
94  
95          getLog().info(buffer().a("Started Jetty on ").strong(server.getURI()).build());
96  
97          // Watch it
98          try {
99              server.getThreadPool().join();
100         } catch (InterruptedException e) {
101             getLog().warn("Jetty was interrupted", e);
102         }
103     }
104 
105     private WebAppContext createWebApplication() throws MojoExecutionException {
106         File webXml = new File(tempWebappDirectory, "WEB-INF/web.xml");
107         webXml.getParentFile().mkdirs();
108 
109         try (InputStream inStream = getClass().getResourceAsStream("/run/web.xml"); //
110                 FileOutputStream outStream = new FileOutputStream(webXml)) {
111             IOUtil.copy(inStream, outStream);
112         } catch (IOException e) {
113             throw new MojoExecutionException("Unable to construct temporary webapp for running site", e);
114         }
115 
116         WebAppContext webapp = new WebAppContext();
117         webapp.setContextPath("/");
118         webapp.setResourceBase(tempWebappDirectory.getAbsolutePath());
119         webapp.setAttribute(DoxiaFilter.OUTPUT_DIRECTORY_KEY, tempWebappDirectory);
120         webapp.setAttribute(DoxiaFilter.SITE_RENDERER_KEY, siteRenderer);
121         webapp.getInitParams().put("org.mortbay.jetty.servlet.Default.useFileMappedBuffer", "false");
122 
123         // For external reports
124         project.getReporting().setOutputDirectory(tempWebappDirectory.getAbsolutePath());
125 
126         List<Locale> localesList = getLocales();
127         webapp.setAttribute(DoxiaFilter.LOCALES_LIST_KEY, localesList);
128 
129         try {
130             Map<String, DoxiaBean> i18nDoxiaContexts = new HashMap<>();
131 
132             for (Locale locale : localesList) {
133                 SiteRenderingContext i18nContext = createSiteRenderingContext(locale);
134                 i18nContext.setInputEncoding(getInputEncoding());
135                 i18nContext.setOutputEncoding(getOutputEncoding());
136 
137                 File outputDirectory = getOutputDirectory(locale);
138                 List<MavenReportExecution> reports = getReports(outputDirectory);
139 
140                 Map<String, DocumentRenderer> i18nDocuments = locateDocuments(i18nContext, reports, locale);
141                 DoxiaBean doxiaBean = new DoxiaBean(i18nContext, i18nDocuments);
142 
143                 if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
144                     i18nDoxiaContexts.put(locale.toString(), doxiaBean);
145                 } else {
146                     i18nDoxiaContexts.put("default", doxiaBean);
147                 }
148 
149                 if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
150                     siteRenderer.copyResources(i18nContext, new File(tempWebappDirectory, locale.toString()));
151                 } else {
152                     siteRenderer.copyResources(i18nContext, tempWebappDirectory);
153                 }
154             }
155 
156             webapp.setAttribute(DoxiaFilter.I18N_DOXIA_CONTEXTS_KEY, i18nDoxiaContexts);
157         } catch (Exception e) {
158             throw new MojoExecutionException("Unable to set up webapp", e);
159         }
160         return webapp;
161     }
162 
163     private File getOutputDirectory(Locale locale) {
164         File file;
165         if (!locale.equals(SiteTool.DEFAULT_LOCALE)) {
166             file = new File(tempWebappDirectory, locale.toString());
167         } else {
168             file = tempWebappDirectory;
169         }
170 
171         // Safety
172         if (!file.exists()) {
173             file.mkdirs();
174         }
175 
176         return file;
177     }
178 
179     public void setTempWebappDirectory(File tempWebappDirectory) {
180         this.tempWebappDirectory = tempWebappDirectory;
181     }
182 
183     public void setPort(int port) {
184         this.port = port;
185     }
186 }