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