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