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