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