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