1 package org.apache.maven.plugins.site.run;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.doxia.siterenderer.DocumentRenderer;
23 import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.maven.plugin.MojoFailureException;
26 import org.apache.maven.plugins.annotations.Mojo;
27 import org.apache.maven.plugins.annotations.Parameter;
28 import org.apache.maven.plugins.site.render.AbstractSiteRenderingMojo;
29 import org.apache.maven.reporting.exec.MavenReportExecution;
30 import org.codehaus.plexus.util.IOUtil;
31 import org.eclipse.jetty.server.Server;
32 import org.eclipse.jetty.webapp.WebAppContext;
33
34 import java.io.File;
35 import java.io.FileOutputStream;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Locale;
41 import java.util.Map;
42
43
44
45
46
47
48
49
50 @Mojo( name = "run", aggregator = true, requiresReports = true )
51 public class SiteRunMojo
52 extends AbstractSiteRenderingMojo
53 {
54
55
56
57 @Parameter( defaultValue = "${project.build.directory}/site-webapp" )
58 private File tempWebappDirectory;
59
60
61
62
63 @Parameter( property = "port", defaultValue = "8080" )
64 private int port;
65
66 private static final int MAX_IDLE_TIME = 30000;
67
68
69
70
71 public void execute()
72 throws MojoExecutionException, MojoFailureException
73 {
74 checkInputEncoding();
75
76 Server server = new Server( port );
77 server.setStopAtShutdown( true );
78
79 WebAppContext webapp = createWebApplication();
80 webapp.setServer( server );
81
82 server.setHandler( webapp );
83
84 getLog().info( "Starting Jetty on http://localhost:" + port + "/" );
85 try
86 {
87 server.start();
88 }
89 catch ( Exception e )
90 {
91 throw new MojoExecutionException( "Error executing Jetty: " + e.getMessage(), e );
92 }
93
94
95 try
96 {
97 server.getThreadPool().join();
98 }
99 catch ( InterruptedException e )
100 {
101 getLog().warn( "Jetty was interrupted", e );
102 }
103 }
104
105 private WebAppContext createWebApplication()
106 throws MojoExecutionException
107 {
108 File webXml = new File( tempWebappDirectory, "WEB-INF/web.xml" );
109 webXml.getParentFile().mkdirs();
110
111
112 try ( InputStream inStream = getClass().getResourceAsStream( "/run/web.xml" );
113 FileOutputStream outStream = new FileOutputStream( webXml ) )
114 {
115 IOUtil.copy( inStream, outStream );
116 }
117 catch ( IOException e )
118 {
119 throw new MojoExecutionException( "Unable to construct temporary webapp for running site", e );
120 }
121
122 WebAppContext webapp = new WebAppContext();
123 webapp.setContextPath( "/" );
124 webapp.setResourceBase( tempWebappDirectory.getAbsolutePath() );
125 webapp.setAttribute( DoxiaFilter.SITE_RENDERER_KEY, siteRenderer );
126 webapp.getInitParams().put( "org.mortbay.jetty.servlet.Default.useFileMappedBuffer", "false" );
127
128
129 project.getReporting().setOutputDirectory( tempWebappDirectory.getAbsolutePath() );
130 for ( MavenReportExecution mavenReportExecution : getReports() )
131 {
132 mavenReportExecution.getMavenReport().setReportOutputDirectory( tempWebappDirectory );
133 }
134
135 List<MavenReportExecution> reports = getReports();
136
137 List<Locale> localesList = getLocales();
138 webapp.setAttribute( DoxiaFilter.LOCALES_LIST_KEY, localesList );
139
140
141 Locale defaultLocale = localesList.get( 0 );
142 Locale.setDefault( defaultLocale );
143
144 try
145 {
146 Map<String, DoxiaBean> i18nDoxiaContexts = new HashMap<>();
147
148 for ( Locale locale : localesList )
149 {
150 SiteRenderingContext i18nContext = createSiteRenderingContext( locale );
151 i18nContext.setInputEncoding( getInputEncoding() );
152 i18nContext.setOutputEncoding( getOutputEncoding() );
153
154 SiteRenderingContext i18nGeneratedSiteContext = createSiteRenderingContext( locale );
155 i18nGeneratedSiteContext.setInputEncoding( getInputEncoding() );
156 i18nGeneratedSiteContext.setOutputEncoding( getOutputEncoding() );
157 i18nGeneratedSiteContext.getSiteDirectories().clear();
158
159 Map<String, DocumentRenderer> i18nDocuments = locateDocuments( i18nContext, reports, locale );
160 DoxiaBean doxiaBean;
161 if ( defaultLocale.equals( locale ) )
162 {
163 i18nGeneratedSiteContext.addSiteDirectory( generatedSiteDirectory );
164 doxiaBean = new DoxiaBean( i18nContext, i18nDocuments, i18nGeneratedSiteContext );
165 }
166 else
167 {
168 i18nGeneratedSiteContext.addSiteDirectory( new File( generatedSiteDirectory,
169 locale.getLanguage() ) );
170 doxiaBean = new DoxiaBean( i18nContext, i18nDocuments, i18nGeneratedSiteContext );
171 }
172
173 i18nDoxiaContexts.put( locale.getLanguage(), doxiaBean );
174 if ( defaultLocale.equals( locale ) )
175 {
176 i18nDoxiaContexts.put( "default", doxiaBean );
177 }
178
179 if ( defaultLocale.equals( locale ) )
180 {
181 siteRenderer.copyResources( i18nContext, tempWebappDirectory );
182 }
183 else
184 {
185 siteRenderer.copyResources( i18nContext, new File( tempWebappDirectory, locale.getLanguage() ) );
186 }
187 }
188
189 webapp.setAttribute( DoxiaFilter.I18N_DOXIA_CONTEXTS_KEY, i18nDoxiaContexts );
190 }
191 catch ( Exception e )
192 {
193 throw new MojoExecutionException( "Unable to set up webapp", e );
194 }
195 return webapp;
196 }
197
198 public void setTempWebappDirectory( File tempWebappDirectory )
199 {
200 this.tempWebappDirectory = tempWebappDirectory;
201 }
202
203 public void setPort( int port )
204 {
205 this.port = port;
206 }
207 }