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