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