View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.maven.plugin;
21  
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.plugin.MojoFailureException;
24  import org.codehaus.plexus.util.DirectoryScanner;
25  import org.codehaus.plexus.util.FileUtils;
26  import org.codehaus.plexus.util.IOUtil;
27  
28  import java.io.BufferedWriter;
29  import java.io.File;
30  import java.io.IOException;
31  import java.io.StringWriter;
32  
33  /**
34   * @goal index
35   * @phase prepare-package
36   */
37  public class IndexThemeMojo extends AbstractThemeMojo {
38  
39    private static final char FILE_SEPARATOR = System.getProperty("file.separator").charAt(0);
40    private static final boolean FILE_SEPARATOR_IS_SLASH = (FILE_SEPARATOR == '/');
41  
42    private static final String[] EXCLUDES = new String[]{
43        "META-INF/**/*",
44        "**/*.class"
45    };
46  
47    /**
48     * Directory containing the resource files that should be listed into the tobago-resources.properties.
49     *
50     * @parameter default-value="${project.build.outputDirectory}"
51     * @required
52     */
53    private File outputDirectory;
54  
55    /**
56     * @parameter default-value="${project.build.outputDirectory}/META-INF/tobago-resource-index.txt"
57     * @required
58     */
59    private File tobagoResourcesFile;
60  
61    public void execute() throws MojoExecutionException, MojoFailureException {
62      if ("pom".equalsIgnoreCase(getProject().getPackaging())) {
63        getLog().info("Not creating " + tobagoResourcesFile.getName() + " as the project is a pom package");
64        return;
65      }
66      if (getLog().isDebugEnabled()) {
67        getLog().debug("Scanning resource: " + outputDirectory.getName());
68      }
69  
70      if (!outputDirectory.isAbsolute()) {
71        outputDirectory = new File(getProject().getBasedir(), outputDirectory.getPath());
72      }
73      if (!outputDirectory.exists()) {
74        getLog().info("Not creating " + tobagoResourcesFile.getName() + " as the project has no outputDirectory");
75        return;
76      }
77      final StaleCheckDirectoryScanner scanner = new StaleCheckDirectoryScanner(tobagoResourcesFile.lastModified());
78      scanner.setBasedir(outputDirectory);
79      scanner.setIncludes(getIncludes());
80      scanner.setExcludes(getExcludes());
81      scanner.scan();
82  
83      final String[] fileNames = scanner.getIncludedFiles();
84      if (fileNames == null || fileNames.length == 0) {
85        getLog().info("Skipping create resource file " + tobagoResourcesFile.getName() + ". No resources found");
86        return;
87      }
88  
89      if (!scanner.isUp2date) {
90        final File metaInf = tobagoResourcesFile.getParentFile();
91        if (!metaInf.exists()) {
92          if (!metaInf.mkdirs()) {
93            getLog().error("Error creating directory " + metaInf.getName());
94          }
95        }
96        BufferedWriter bufferedWriter = null;
97        try {
98          final StringWriter stringWriter = new StringWriter();
99          bufferedWriter = new BufferedWriter(stringWriter);
100         for (final String file : fileNames) {
101           bufferedWriter.append('/');
102           if (FILE_SEPARATOR_IS_SLASH) {
103             bufferedWriter.append(file);
104           } else {
105             bufferedWriter.append(convertFileSeparatorToSlash(file));
106           }
107           bufferedWriter.newLine();
108         }
109         bufferedWriter.flush();
110         FileUtils.fileWrite(tobagoResourcesFile, "utf-8", stringWriter.toString());
111       } catch (final IOException e) {
112         getLog().error("Error creating resource file " + tobagoResourcesFile.getName(), e);
113       } finally {
114         IOUtil.close(bufferedWriter);
115       }
116     } else {
117       getLog().info("Skipping create resource file " + tobagoResourcesFile.getName());
118     }
119   }
120 
121   private String convertFileSeparatorToSlash(String file) {
122     return file.replace(FILE_SEPARATOR, '/');
123   }
124 
125   public String[] getExcludes() {
126     return EXCLUDES;
127   }
128 
129   private static class StaleCheckDirectoryScanner extends DirectoryScanner {
130     private long lastModified;
131     private boolean isUp2date = true;
132 
133     private StaleCheckDirectoryScanner(final long lastModified) {
134       this.lastModified = lastModified;
135     }
136 
137     @Override
138     protected boolean isSelected(final String name, final File file) {
139       final long lastModified = file.lastModified();
140       if (lastModified > this.lastModified) {
141         isUp2date = false;
142       }
143       return super.isSelected(name, file);
144     }
145   }
146 }