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