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  package org.apache.maven.plugins.dependency.utils.markers;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.plugin.MojoExecutionException;
26  
27  /**
28   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
29   */
30  public class SourcesFileMarkerHandler extends DefaultFileMarkerHandler {
31  
32      boolean resolved;
33  
34      /**
35       * @param markerFilesDirectory the marker files directory.
36       */
37      public SourcesFileMarkerHandler(File markerFilesDirectory) {
38          super(markerFilesDirectory);
39      }
40  
41      /**
42       * @param artifact {@link Artifact}
43       * @param markerFilesDirectory marker files directory.
44       * @param isResolved true/false.
45       */
46      public SourcesFileMarkerHandler(Artifact artifact, File markerFilesDirectory, boolean isResolved) {
47          super(artifact, markerFilesDirectory);
48          this.resolved = isResolved;
49      }
50  
51      /**
52       * Returns properly formatted File
53       *
54       * @return File object for marker. The file is not guaranteed to exist.
55       */
56      @Override
57      public File getMarkerFile() {
58          return getMarkerFile(this.resolved);
59      }
60  
61      /**
62       * Get MarkerFile, exposed for unit testing purposes
63       *
64       * @param res resolved or not.
65       * @return marker file for this artifact.
66       */
67      protected File getMarkerFile(boolean res) {
68          String suffix;
69          if (res) {
70              suffix = ".resolved";
71          } else {
72              suffix = ".unresolved";
73          }
74  
75          return new File(this.markerFilesDirectory, this.artifact.getId().replace(':', '-') + suffix);
76      }
77  
78      /**
79       * Tests whether the file or directory denoted by this abstract pathname exists.
80       *
81       * @return <code>true</code> if and only if the file or directory denoted by this abstract pathname exists;
82       *         <code>false</code> otherwise
83       * @throws MojoExecutionException If a security manager exists and its <code>{@link
84       *          java.lang.SecurityManager#checkRead(java.lang.String)}</code> method denies read access to the file or
85       *             directory
86       */
87      @Override
88      public boolean isMarkerSet() throws MojoExecutionException {
89          File marker = getMarkerFile();
90  
91          File marker2 = getMarkerFile(!this.resolved);
92  
93          return marker.exists() || marker2.exists();
94      }
95  
96      @Override
97      public boolean isMarkerOlder(Artifact theArtifact) throws MojoExecutionException {
98          File marker = getMarkerFile();
99          if (marker.exists()) {
100             return theArtifact.getFile().lastModified() > marker.lastModified();
101         } else {
102             marker = getMarkerFile(!this.resolved);
103             if (marker.exists()) {
104                 return theArtifact.getFile().lastModified() > marker.lastModified();
105             } else {
106                 // if the marker doesn't exist, we want to copy so assume it is
107                 // infinitely older
108                 return true;
109             }
110         }
111     }
112 
113     @Override
114     public void setMarker() throws MojoExecutionException {
115         File marker = getMarkerFile();
116 
117         // get the other file if it exists.
118         File clearMarker = getMarkerFile(!this.resolved);
119         // create marker file
120         try {
121             marker.getParentFile().mkdirs();
122         } catch (NullPointerException e) {
123             // parent is null, ignore it.
124         }
125 
126         try {
127             marker.createNewFile();
128             // clear the other file if it exists.
129             if (clearMarker.exists()) {
130                 if (!clearMarker.delete()) {
131                     clearMarker.deleteOnExit();
132                 }
133             }
134         } catch (IOException e) {
135             throw new MojoExecutionException("Unable to create Marker: " + marker.getAbsolutePath(), e);
136         }
137     }
138 
139     /**
140      * Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the
141      * directory must be empty in order to be deleted.
142      *
143      * @return <code>true</code> if and only if the file or directory is successfully deleted; <code>false</code>
144      *         otherwise
145      * @throws SecurityException If a security manager exists and its <code>{@link
146      *          java.lang.SecurityManager#checkDelete}</code> method denies delete access to the file
147      */
148     @Override
149     public boolean clearMarker() throws MojoExecutionException {
150         File marker = getMarkerFile();
151         File marker2 = getMarkerFile(!this.resolved);
152         boolean markResult = marker.delete();
153         boolean mark2Result = marker2.delete();
154         return markResult || mark2Result;
155     }
156 
157     /**
158      * @return Returns the resolved.
159      */
160     public boolean isResolved() {
161         return this.resolved;
162     }
163 
164     /**
165      * @param isResolved The resolved to set.
166      */
167     public void setResolved(boolean isResolved) {
168         this.resolved = isResolved;
169     }
170 }