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 DefaultFileMarkerHandler implements MarkerHandler {
31      /**
32       * The artifact.
33       */
34      protected Artifact artifact;
35  
36      /**
37       * The marker directory.
38       */
39      protected File markerFilesDirectory;
40  
41      /**
42       * @param theMarkerFilesDirectory The marker directory.
43       */
44      public DefaultFileMarkerHandler(File theMarkerFilesDirectory) {
45          this.markerFilesDirectory = theMarkerFilesDirectory;
46      }
47  
48      /**
49       * @param theArtifact {@link Artifact}
50       * @param theMarkerFilesDirectory The marker directory.
51       */
52      public DefaultFileMarkerHandler(Artifact theArtifact, File theMarkerFilesDirectory) {
53          this.artifact = theArtifact;
54          this.markerFilesDirectory = theMarkerFilesDirectory;
55      }
56  
57      /**
58       * Returns properly formatted File
59       *
60       * @return File object for marker. The file is not guaranteed to exist.
61       */
62      protected File getMarkerFile() {
63          return new File(this.markerFilesDirectory, this.artifact.getId().replace(':', '-') + ".marker");
64      }
65  
66      /**
67       * Tests whether the file or directory denoted by this abstract pathname exists.
68       *
69       * @return <code>true</code> if and only if the file or directory denoted by this abstract pathname exists;
70       *         <code>false</code> otherwise
71       * @throws SecurityException If a security manager exists and its <code>{@link
72       *          java.lang.SecurityManager#checkRead(java.lang.String)}</code> method denies read access to the file or
73       *             directory
74       */
75      @Override
76      public boolean isMarkerSet() throws MojoExecutionException {
77          File marker = getMarkerFile();
78          return marker.exists();
79      }
80  
81      @Override
82      public boolean isMarkerOlder(Artifact artifact1) throws MojoExecutionException {
83          File marker = getMarkerFile();
84          if (marker.exists()) {
85              return artifact1.getFile().lastModified() > marker.lastModified();
86          } else {
87              // if the marker doesn't exist, we want to copy so assume it is
88              // infinitely older
89              return true;
90          }
91      }
92  
93      @Override
94      public void setMarker() throws MojoExecutionException {
95          File marker = getMarkerFile();
96          // create marker file
97          try {
98              marker.getParentFile().mkdirs();
99          } catch (NullPointerException e) {
100             // parent is null, ignore it.
101         }
102         try {
103             marker.createNewFile();
104         } catch (IOException e) {
105             throw new MojoExecutionException("Unable to create Marker: " + marker.getAbsolutePath(), e);
106         }
107 
108         // update marker file timestamp
109         try {
110             long ts;
111             if (this.artifact != null && this.artifact.getFile() != null) {
112                 ts = this.artifact.getFile().lastModified();
113             } else {
114                 ts = System.currentTimeMillis();
115             }
116             if (!marker.setLastModified(ts)) {
117                 throw new MojoExecutionException(
118                         "Unable to update last modified timestamp on marker file " + marker.getAbsolutePath());
119             }
120         } catch (Exception e) {
121             throw new MojoExecutionException("Unable to update Marker timestamp: " + marker.getAbsolutePath(), e);
122         }
123     }
124 
125     /**
126      * Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the
127      * directory must be empty in order to be deleted.
128      *
129      * @return <code>true</code> if and only if the file or directory is successfully deleted; <code>false</code>
130      *         otherwise
131      * @throws SecurityException If a security manager exists and its <code>{@link
132      *          java.lang.SecurityManager#checkDelete}</code> method denies delete access to the file
133      */
134     @Override
135     public boolean clearMarker() throws MojoExecutionException {
136         File marker = getMarkerFile();
137         return marker.delete();
138     }
139 
140     /**
141      * @return Returns the artifact.
142      */
143     public Artifact getArtifact() {
144         return this.artifact;
145     }
146 
147     /**
148      * @param artifact The artifact to set.
149      */
150     @Override
151     public void setArtifact(Artifact artifact) {
152         this.artifact = artifact;
153     }
154 
155     /**
156      * @return Returns the markerFilesDirectory.
157      */
158     public File getMarkerFilesDirectory() {
159         return this.markerFilesDirectory;
160     }
161 
162     /**
163      * @param markerFilesDirectory The markerFilesDirectory to set.
164      */
165     public void setMarkerFilesDirectory(File markerFilesDirectory) {
166         this.markerFilesDirectory = markerFilesDirectory;
167     }
168 }