001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *   http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.myfaces.tobago.maven.plugin;
021    
022    import org.apache.maven.artifact.Artifact;
023    import org.apache.maven.plugin.MojoExecutionException;
024    import org.codehaus.plexus.archiver.ArchiverException;
025    import org.codehaus.plexus.archiver.UnArchiver;
026    import org.codehaus.plexus.archiver.manager.ArchiverManager;
027    import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
028    import org.codehaus.plexus.util.FileUtils;
029    
030    import java.io.File;
031    import java.io.IOException;
032    import java.io.FileInputStream;
033    import java.util.Iterator;
034    import java.util.List;
035    import java.util.Locale;
036    import java.util.zip.ZipInputStream;
037    import java.util.zip.ZipEntry;
038    
039    /**
040     *
041     * @version $Id: UnPackThemeMojo.java 1368577 2012-08-02 16:20:31Z lofwyr $
042     * @goal resources
043     * @phase process-resources
044     * @requiresDependencyResolution compile
045     */
046    public class UnPackThemeMojo extends AbstractThemeMojo {
047      /**
048       * To look up Archiver/UnArchiver implementations
049       *
050       * @parameter expression="${component.org.codehaus.plexus.archiver.manager.ArchiverManager}"
051       * @required
052       */
053      private ArchiverManager archiverManager;
054    
055      /**
056       * Directory to unpack JARs into if needed
057       *
058       * @parameter expression="${project.build.directory}/theme/work"
059       * @required
060       */
061      private File workDirectory;
062    
063      /**
064       * The directory where the webapp is built.
065       *
066       * @parameter expression="${project.build.directory}/${project.build.finalName}"
067       * @required
068       */
069      private File webappDirectory;
070    
071      /**
072       * @parameter expression="${plugin.artifacts}"
073       * @required
074       */
075      private List pluginArtifacts;
076    
077      private boolean findThemeDescriptor(File jarFile) throws MojoExecutionException {
078        ZipInputStream zip = null;
079        try {
080          zip = new ZipInputStream(new FileInputStream(jarFile));
081          while (zip.available() > 0) {
082            ZipEntry nextEntry = zip.getNextEntry();
083            if (nextEntry == null || nextEntry.isDirectory()) {
084              continue;
085            }
086            String name = nextEntry.getName();
087            if (name.equals("META-INF/tobago-theme.xml")) {
088              return true;
089            }
090          }
091        } catch (IOException e) {
092          throw new MojoExecutionException("Error find ThemeDescriptor in " + jarFile, e);
093        } finally {
094          if (zip != null) {
095            try {
096              zip.close();
097            } catch (IOException e) {
098              // ignore
099            }
100          }
101        }
102    
103        return false;
104      }
105    
106      public void execute() throws MojoExecutionException {
107    
108        Iterator artifacts =  getProject().getDependencyArtifacts().iterator();
109        while (artifacts.hasNext()) {
110          if (!workDirectory.exists()) {
111            workDirectory.mkdirs();
112          }
113          Artifact artifact = (Artifact) artifacts.next();
114          getLog().debug("Expanding theme "+ artifact);
115          File file = artifact.getFile();
116          if (Artifact.SCOPE_COMPILE.equals(artifact.getScope()) && file.isFile()
117              && "jar".equals(artifact.getType()) && findThemeDescriptor(file)) {
118    
119            String name = file.getName();
120            getLog().debug("Expanding theme "+ name);
121            File tempLocation = new File(workDirectory, name.substring(0, name.length() - 4));
122            boolean process = false;
123            if (!tempLocation.exists()) {
124              tempLocation.mkdirs();
125              process = true;
126            } else if (artifact.getFile().lastModified() > tempLocation.lastModified()) {
127              process = true;
128            }
129            if (process) {
130              try {
131                unpack(file, tempLocation);
132                String[] fileNames = getThemeFiles(tempLocation);
133                for (String fileName : fileNames) {
134    
135                  File fromFile = new File(tempLocation, fileName);
136                  File toFile = new File(webappDirectory, fileName);
137                  try {
138                    FileUtils.copyFile(fromFile, toFile);
139                  } catch (IOException e) {
140                    throw new MojoExecutionException("Error copy file: " + fromFile + "to: " + toFile, e);
141                  }
142                }
143              } catch (NoSuchArchiverException e) {
144                getLog().info("Skip unpacking dependency file with unknown extension: " + file.getPath());
145              }
146            }
147          }
148        }
149      }
150    
151      private void unpack(File file, File location)
152          throws MojoExecutionException, NoSuchArchiverException {
153        String archiveExt = FileUtils.getExtension(file.getAbsolutePath()).toLowerCase(Locale.ENGLISH);
154        try {
155          UnArchiver unArchiver = archiverManager.getUnArchiver(archiveExt);
156          unArchiver.setSourceFile(file);
157          unArchiver.setDestDirectory(location);
158          unArchiver.extract();
159        } catch (IOException e) {
160          throw new MojoExecutionException("Error unpacking file: " + file + "to: " + location, e);
161        } catch (ArchiverException e) {
162          throw new MojoExecutionException("Error unpacking file: " + file + "to: " + location, e);
163        }
164      }
165    }
166    
167