View Javadoc
1   package org.codehaus.mojo.webstart;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.doxia.siterenderer.Renderer;
23  import org.apache.maven.plugins.annotations.Component;
24  import org.apache.maven.plugins.annotations.LifecyclePhase;
25  import org.apache.maven.plugins.annotations.Mojo;
26  import org.apache.maven.plugins.annotations.Parameter;
27  import org.apache.maven.project.MavenProject;
28  import org.apache.maven.reporting.AbstractMavenReport;
29  import org.apache.maven.reporting.MavenReportException;
30  import org.codehaus.plexus.util.FileUtils;
31  
32  import java.io.File;
33  import java.io.IOException;
34  import java.util.List;
35  import java.util.Locale;
36  import java.util.ResourceBundle;
37  
38  /**
39   * Creates a JNLP report.
40   *
41   * @author Geoffrey De Smet
42   */
43  @Mojo( name = "report", defaultPhase = LifecyclePhase.SITE, requiresReports = true )
44  public class JnlpReportMojo
45      extends AbstractMavenReport
46  {
47      // ----------------------------------------------------------------------
48      // Mojo Parameters
49      // ----------------------------------------------------------------------
50  
51      /**
52       * Location where the site is generated.
53       */
54      @Parameter( property = "jnlp.outputDirectory", defaultValue = "${project.reporting.outputDirectory}" )
55      private File outputDirectory;
56  
57      /**
58       * Directory where the jnlp artifacts and jnlp sources files reside.
59       */
60      @Parameter( property = "jnlp.jnlpSourceDirectory", defaultValue = "${project.build.directory}/jnlp", required = true )
61      private File jnlpSourceDirectory;
62  
63      /**
64       * Directory in the site directory where the jnlp artifacts and jnlp sources files reside.
65       */
66      @Parameter( property = "jnlp.siteJnlpDirectory", defaultValue = "jnlp", required = true )
67      private String siteJnlpDirectory;
68  
69      /**
70       * Name of the main jnlp file of the project.
71       */
72      @Parameter( property = "jnlp.siteJnlpFile", defaultValue = "launch.jnlp", required = true )
73      private String siteJnlpFile;
74  
75      /**
76       * The default filename to use for the report.
77       */
78      @Parameter( property = "outputName", defaultValue = "jnlp-report", required = true )
79      private String outputName;
80  
81      /**
82       * The code base to use on the generated jnlp files.
83       *
84       * @since 1.0-beta-2
85       */
86      @Parameter( property = "jnlp.codebase", defaultValue = "${project.url}/jnlp" )
87      private String codebase;
88  
89      // ----------------------------------------------------------------------
90      // Components
91      // ----------------------------------------------------------------------
92  
93      /**
94       * Maven Project
95       */
96      @Component
97      private MavenProject project;
98  
99      /**
100      * <i>Maven Internal</i>: The Doxia Site Renderer.
101      */
102     @Component
103     private Renderer siteRenderer;
104 
105     // ----------------------------------------------------------------------
106     // MavenReport implementatio
107     // ----------------------------------------------------------------------
108 
109     /**
110      * {@inheritDoc}
111      */
112     public void executeReport( Locale locale )
113         throws MavenReportException
114     {
115         copyJnlpFiles();
116         fillReport( locale );
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     public String getName( Locale locale )
123     {
124         return getBundle( locale ).getString( "report.jnlp-report.name" );
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     public String getDescription( Locale locale )
131     {
132         return getBundle( locale ).getString( "report.jnlp-report.description" );
133     }
134 
135     // ----------------------------------------------------------------------
136     // AbstractMavenReport implementatio
137     // ----------------------------------------------------------------------
138 
139     /**
140      * {@inheritDoc}
141      */
142     protected Renderer getSiteRenderer()
143     {
144         return siteRenderer;
145     }
146 
147     /**
148      * {@inheritDoc}
149      */
150     protected MavenProject getProject()
151     {
152         return project;
153     }
154 
155     /**
156      * {@inheritDoc}
157      */
158     public String getOutputName()
159     {
160         return outputName;
161     }
162 
163     /**
164      * {@inheritDoc}
165      */
166     protected String getOutputDirectory()
167     {
168         return outputDirectory.getPath();
169     }
170 
171     // ----------------------------------------------------------------------
172     // Private methods
173     // ----------------------------------------------------------------------
174 
175     private void copyJnlpFiles()
176         throws MavenReportException
177     {
178         if ( !jnlpSourceDirectory.exists() )
179         {
180             throw new MavenReportException( "jnlpSourceDirectory does not exist" );
181         }
182         try
183         {
184             File destinationDirectory = new File( outputDirectory, siteJnlpDirectory );
185             List<File> files = FileUtils.getFiles( jnlpSourceDirectory, "**/*", "" );
186             for ( File file : files )
187             {
188                 getLog().debug( "Copying " + file + " to " + destinationDirectory );
189                 String path = file.getAbsolutePath().substring( jnlpSourceDirectory.getAbsolutePath().length() + 1 );
190                 File destDir = new File( destinationDirectory, path );
191                 getLog().debug( "Copying " + file + " to " + destDir );
192                 if ( file.isDirectory() )
193                 {
194                     destDir.mkdirs();
195                 }
196                 else
197                 {
198                     FileUtils.copyFileToDirectory( file, destDir.getParentFile() );
199                 }
200             }
201         }
202         catch ( IOException e )
203         {
204             throw new MavenReportException( "Failed to copy jnlp files", e );
205         }
206     }
207 
208     private void fillReport( Locale locale )
209     {
210         ResourceBundle bundle = getBundle( locale );
211         getSink().head();
212         getSink().text( bundle.getString( "report.jnlp-report.description" ) );
213         getSink().head_();
214         getSink().body();
215         getSink().sectionTitle1();
216         getSink().text( bundle.getString( "report.jnlp-report.label.installation.header" ) );
217         getSink().sectionTitle1_();
218         getSink().paragraph();
219         getSink().text( bundle.getString( "report.jnlp-report.label.installation.description" ) );
220         getSink().paragraph_();
221         getSink().paragraph();
222         if ( codebase.startsWith( "file://" ) )
223         {
224             if ( !codebase.endsWith( File.separator ) )
225             {
226                 codebase += File.separator;
227             }
228         }
229         else
230         {
231             if ( !codebase.endsWith( "/" ) )
232             {
233                 codebase += "/";
234             }
235         }
236         getSink().link( codebase + siteJnlpFile );
237 
238         getSink().text( bundle.getString( "report.jnlp-report.label.installation.webStartMeNow" ) );
239         getSink().link_();
240         getSink().paragraph_();
241         getSink().paragraph();
242         getSink().text( bundle.getString( "report.jnlp-report.label.installation.getJava" ) + " " );
243         getSink().link( "http://java.com" );
244         getSink().text( "http://java.com" );
245         getSink().link_();
246         getSink().paragraph_();
247         getSink().sectionTitle1();
248         getSink().text( bundle.getString( "report.jnlp-report.label.uninstallation.header" ) );
249         getSink().sectionTitle1_();
250         getSink().paragraph();
251         getSink().text( bundle.getString( "report.jnlp-report.label.uninstallation.description" ) );
252         getSink().paragraph_();
253         getSink().body_();
254         getSink().flush();
255         getSink().close();
256     }
257 
258     private ResourceBundle getBundle( Locale locale )
259     {
260         return ResourceBundle.getBundle( "jnlp-report", locale, this.getClass().getClassLoader() );
261     }
262 
263 }