1 package org.apache.maven.plugins.dependency.utils.markers;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.File;
23 import java.io.IOException;
24
25 import org.apache.maven.artifact.Artifact;
26 import org.apache.maven.plugin.MojoExecutionException;
27
28 /**
29 * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
30 * @version $Id: DefaultFileMarkerHandler.java 1807877 2017-09-09 10:35:59Z khmarbaise $
31 */
32 public class DefaultFileMarkerHandler
33 implements MarkerHandler
34 {
35 protected Artifact artifact;
36
37 protected File markerFilesDirectory;
38
39 public DefaultFileMarkerHandler( File theMarkerFilesDirectory )
40 {
41 this.markerFilesDirectory = theMarkerFilesDirectory;
42 }
43
44 public DefaultFileMarkerHandler( Artifact theArtifact, File theMarkerFilesDirectory )
45 {
46 this.artifact = theArtifact;
47 this.markerFilesDirectory = theMarkerFilesDirectory;
48 }
49
50 /**
51 * Returns properly formatted File
52 *
53 * @return File object for marker. The file is not guaranteed to exist.
54 */
55 protected File getMarkerFile()
56 {
57 return new File( this.markerFilesDirectory, this.artifact.getId().replace( ':', '-' ) + ".marker" );
58 }
59
60 /**
61 * Tests whether the file or directory denoted by this abstract pathname exists.
62 *
63 * @return <code>true</code> if and only if the file or directory denoted by this abstract pathname exists;
64 * <code>false</code> otherwise
65 * @throws SecurityException If a security manager exists and its <code>{@link
66 * java.lang.SecurityManager#checkRead(java.lang.String)}</code> method denies read access to the file or
67 * directory
68 */
69 @Override
70 public boolean isMarkerSet()
71 throws MojoExecutionException
72 {
73 File marker = getMarkerFile();
74 return marker.exists();
75 }
76
77 @Override
78 public boolean isMarkerOlder( Artifact artifact1 )
79 throws MojoExecutionException
80 {
81 File marker = getMarkerFile();
82 if ( marker.exists() )
83 {
84 return artifact1.getFile().lastModified() > marker.lastModified();
85 }
86 else
87 {
88 // if the marker doesn't exist, we want to copy so assume it is
89 // infinitely older
90 return true;
91 }
92 }
93
94 @Override
95 public void setMarker()
96 throws MojoExecutionException
97 {
98 File marker = getMarkerFile();
99 // create marker file
100 try
101 {
102 marker.getParentFile().mkdirs();
103 }
104 catch ( NullPointerException e )
105 {
106 // parent is null, ignore it.
107 }
108 try
109 {
110 marker.createNewFile();
111 }
112 catch ( IOException e )
113 {
114 throw new MojoExecutionException( "Unable to create Marker: " + marker.getAbsolutePath(), e );
115 }
116
117 // update marker file timestamp
118 try
119 {
120 long ts;
121 if ( this.artifact != null && this.artifact.getFile() != null )
122 {
123 ts = this.artifact.getFile().lastModified();
124 }
125 else
126 {
127 ts = System.currentTimeMillis();
128 }
129 if ( !marker.setLastModified( ts ) )
130 {
131 throw new MojoExecutionException( "Unable to update last modified timestamp on marker file "
132 + marker.getAbsolutePath() );
133
134 }
135 }
136 catch ( Exception e )
137 {
138 throw new MojoExecutionException( "Unable to update Marker timestamp: " + marker.getAbsolutePath(), e );
139 }
140 }
141
142 /**
143 * Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the
144 * directory must be empty in order to be deleted.
145 *
146 * @return <code>true</code> if and only if the file or directory is successfully deleted; <code>false</code>
147 * otherwise
148 * @throws SecurityException If a security manager exists and its <code>{@link
149 * java.lang.SecurityManager#checkDelete}</code> method denies delete access to the file
150 */
151 @Override
152 public boolean clearMarker()
153 throws MojoExecutionException
154 {
155 File marker = getMarkerFile();
156 return marker.delete();
157 }
158
159 /**
160 * @return Returns the artifact.
161 */
162 public Artifact getArtifact()
163 {
164 return this.artifact;
165 }
166
167 /**
168 * @param artifact The artifact to set.
169 */
170 @Override
171 public void setArtifact( Artifact artifact )
172 {
173 this.artifact = artifact;
174 }
175
176 /**
177 * @return Returns the markerFilesDirectory.
178 */
179 public File getMarkerFilesDirectory()
180 {
181 return this.markerFilesDirectory;
182 }
183
184 /**
185 * @param markerFilesDirectory The markerFilesDirectory to set.
186 */
187 public void setMarkerFilesDirectory( File markerFilesDirectory )
188 {
189 this.markerFilesDirectory = markerFilesDirectory;
190 }
191 }