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 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.List;
29 import java.util.Locale;
30 import java.util.Map;
31
32 import org.apache.maven.doxia.siterenderer.DocumentRenderer;
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.annotations.Mojo;
37 import org.apache.maven.plugins.annotations.Parameter;
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.mortbay.jetty.Connector;
42 import org.mortbay.jetty.Handler;
43 import org.mortbay.jetty.Server;
44 import org.mortbay.jetty.handler.DefaultHandler;
45 import org.mortbay.jetty.nio.SelectChannelConnector;
46 import org.mortbay.jetty.webapp.WebAppContext;
47
48
49
50
51
52
53
54
55 @Mojo( name = "run", aggregator = true, requiresReports = true )
56 public class SiteRunMojo
57 extends AbstractSiteRenderingMojo
58 {
59
60
61
62 @Parameter( defaultValue = "${project.build.directory}/site-webapp" )
63 private File tempWebappDirectory;
64
65
66
67
68 @Parameter( property = "port", defaultValue = "8080" )
69 private int port;
70
71 private static final int MAX_IDLE_TIME = 30000;
72
73
74
75
76 public void execute()
77 throws MojoExecutionException, MojoFailureException
78 {
79 Server server = new Server();
80 server.setStopAtShutdown( true );
81
82 Connector defaultConnector = getDefaultConnector();
83 server.setConnectors( new Connector[] { defaultConnector } );
84
85 WebAppContext webapp = createWebApplication();
86 webapp.setServer( server );
87
88 DefaultHandler defaultHandler = new DefaultHandler();
89 defaultHandler.setServer( server );
90
91 Handler[] handlers = new Handler[2];
92 handlers[0] = webapp;
93 handlers[1] = defaultHandler;
94 server.setHandlers( handlers );
95
96 getLog().info( "Starting Jetty on http://localhost:" + port + "/" );
97 try
98 {
99 server.start();
100 }
101 catch ( Exception e )
102 {
103 throw new MojoExecutionException( "Error executing Jetty: " + e.getMessage(), e );
104 }
105
106
107 try
108 {
109 server.getThreadPool().join();
110 }
111 catch ( InterruptedException e )
112 {
113 getLog().warn( "Jetty was interrupted", e );
114 }
115 }
116
117 private WebAppContext createWebApplication()
118 throws MojoExecutionException
119 {
120 File webXml = new File( tempWebappDirectory, "WEB-INF/web.xml" );
121 webXml.getParentFile().mkdirs();
122
123 InputStream inStream = null;
124 FileOutputStream outStream = null;
125 try
126 {
127 inStream = getClass().getResourceAsStream( "/run/web.xml" );
128 outStream = new FileOutputStream( webXml );
129 IOUtil.copy( inStream, outStream );
130 }
131 catch ( FileNotFoundException e )
132 {
133 throw new MojoExecutionException( "Unable to construct temporary webapp for running site", e );
134 }
135 catch ( IOException e )
136 {
137 throw new MojoExecutionException( "Unable to construct temporary webapp for running site", e );
138 }
139 finally
140 {
141 IOUtil.close( outStream );
142 IOUtil.close( inStream );
143 }
144
145 WebAppContext webapp = new WebAppContext();
146 webapp.setContextPath( "/" );
147 webapp.setResourceBase( tempWebappDirectory.getAbsolutePath() );
148 webapp.setAttribute( DoxiaFilter.SITE_RENDERER_KEY, siteRenderer );
149 webapp.getInitParams().put( "org.mortbay.jetty.servlet.Default.useFileMappedBuffer", "false" );
150
151
152 project.getReporting().setOutputDirectory( tempWebappDirectory.getAbsolutePath() );
153 for ( MavenReportExecution mavenReportExecution : getReports() )
154 {
155 mavenReportExecution.getMavenReport().setReportOutputDirectory( tempWebappDirectory );
156 }
157
158 List<MavenReportExecution> reports = getReports();
159
160 List<Locale> localesList = siteTool.getAvailableLocales( locales );
161 webapp.setAttribute( DoxiaFilter.LOCALES_LIST_KEY, localesList );
162
163
164 Locale defaultLocale = localesList.get( 0 );
165 Locale.setDefault( defaultLocale );
166
167 try
168 {
169 Map<String, DoxiaBean> i18nDoxiaContexts = new HashMap<String, DoxiaBean>();
170
171 for ( Locale locale : localesList )
172 {
173 SiteRenderingContext i18nContext = createSiteRenderingContext( locale );
174 i18nContext.setInputEncoding( getInputEncoding() );
175 i18nContext.setOutputEncoding( getOutputEncoding() );
176
177 Map<String, DocumentRenderer> i18nDocuments = locateDocuments( i18nContext, reports, locale );
178 DoxiaBean doxiaBean;
179 if ( defaultLocale.equals( locale ) )
180 {
181 doxiaBean = new DoxiaBean( i18nContext, i18nDocuments, generatedSiteDirectory );
182 }
183 else
184 {
185 doxiaBean =
186 new DoxiaBean( i18nContext, i18nDocuments, new File( generatedSiteDirectory,
187 locale.getLanguage() ) );
188 }
189
190 i18nDoxiaContexts.put( locale.getLanguage(), doxiaBean );
191 if ( defaultLocale.equals( locale ) )
192 {
193 i18nDoxiaContexts.put( "default", doxiaBean );
194 }
195
196 if ( defaultLocale.equals( locale ) )
197 {
198 siteRenderer.copyResources( i18nContext, new File( siteDirectory, "resources" ),
199 tempWebappDirectory );
200 }
201 else
202 {
203 siteRenderer.copyResources( i18nContext, new File( siteDirectory, "resources" ),
204 new File( tempWebappDirectory, locale.getLanguage() ) );
205 }
206 }
207
208 webapp.setAttribute( DoxiaFilter.I18N_DOXIA_CONTEXTS_KEY, i18nDoxiaContexts );
209 }
210 catch ( Exception e )
211 {
212 throw new MojoExecutionException( "Unable to set up webapp", e );
213 }
214 return webapp;
215 }
216
217 private Connector getDefaultConnector()
218 {
219 Connector connector = new SelectChannelConnector();
220 connector.setPort( port );
221 connector.setMaxIdleTime( MAX_IDLE_TIME );
222 return connector;
223 }
224
225 public void setTempWebappDirectory( File tempWebappDirectory )
226 {
227 this.tempWebappDirectory = tempWebappDirectory;
228 }
229
230 public void setPort( int port )
231 {
232 this.port = port;
233 }
234 }