View Javadoc

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  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 1085975 2011-03-27 16:03:41Z hboutemy $
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
62       * exists.
63       * 
64       * @return <code>true</code> if and only if the file or directory denoted
65       *         by this abstract pathname exists; <code>false</code> otherwise
66       * 
67       * @throws SecurityException
68       *             If a security manager exists and its <code>{@link
69       *          java.lang.SecurityManager#checkRead(java.lang.String)}</code>
70       *             method denies read access to the file or directory
71       */
72      public boolean isMarkerSet()
73          throws MojoExecutionException
74      {
75          File marker = getMarkerFile();
76          return marker.exists();
77      }
78  
79      public boolean isMarkerOlder( Artifact artifact1 )
80          throws MojoExecutionException
81      {
82          // temporary debug to find out why this intermittent failure on Sonatype grid Windows
83          System.out.println( "> isMarkerOlder:" );
84          File marker = getMarkerFile();
85          System.out.println( "  artifact1 = " + artifact1.getFile().getPath() );
86          System.out.println( "  marker    = " + marker.getPath() );
87          if ( marker.exists() )
88          {
89              System.out.println( "    artifact1 lastModified: " + artifact1.getFile().lastModified() );
90              System.out.println( "    marker lastModified: " + marker.lastModified() );
91              System.out.println( "< " + ( artifact1.getFile().lastModified() > marker.lastModified() ) + " = marker older than artifact?"  );
92              return artifact1.getFile().lastModified() > marker.lastModified();
93          }
94          else
95          {
96              System.out.println( "< true : marker does not exist" );
97              // if the marker doesn't exist, we want to copy so assume it is
98              // infinitely older
99              return true;
100         }
101     }
102 
103     public void setMarker()
104         throws MojoExecutionException
105     {
106         File marker = getMarkerFile();
107         // create marker file
108         try
109         {
110             marker.getParentFile().mkdirs();
111         }
112         catch ( NullPointerException e )
113         {
114             // parent is null, ignore it.
115         }
116         try
117         {
118             marker.createNewFile();
119         }
120         catch ( IOException e )
121         {
122             throw new MojoExecutionException( "Unable to create Marker: " + marker.getAbsolutePath(), e );
123         }
124         
125         // update marker file timestamp
126         try
127         {
128             long ts;
129             if ( this.artifact != null && this.artifact.getFile() != null )
130             {
131                 ts = this.artifact.getFile().lastModified();
132             }
133             else
134             {
135                 ts = System.currentTimeMillis();
136             }
137             marker.setLastModified( ts );
138         }
139         catch ( Exception e )
140         {
141             throw new MojoExecutionException( "Unable to update Marker timestamp: " + marker.getAbsolutePath(), e );
142         }
143     }
144 
145     /**
146      * Deletes the file or directory denoted by this abstract pathname. If this
147      * pathname denotes a directory, then the directory must be empty in order
148      * to be deleted.
149      * 
150      * @return <code>true</code> if and only if the file or directory is
151      *         successfully deleted; <code>false</code> otherwise
152      * 
153      * @throws SecurityException
154      *             If a security manager exists and its <code>{@link
155      *          java.lang.SecurityManager#checkDelete}</code>
156      *             method denies delete access to the file
157      */
158     public boolean clearMarker()
159         throws MojoExecutionException
160     {
161         File marker = getMarkerFile();
162         return marker.delete();
163     }
164 
165     /**
166      * @return Returns the artifact.
167      */
168     public Artifact getArtifact()
169     {
170         return this.artifact;
171     }
172 
173     /**
174      * @param artifact
175      *            The artifact to set.
176      */
177     public void setArtifact( Artifact artifact )
178     {
179         this.artifact = artifact;
180     }
181 
182     /**
183      * @return Returns the markerFilesDirectory.
184      */
185     public File getMarkerFilesDirectory()
186     {
187         return this.markerFilesDirectory;
188     }
189 
190     /**
191      * @param markerFilesDirectory
192      *            The markerFilesDirectory to set.
193      */
194     public void setMarkerFilesDirectory( File markerFilesDirectory )
195     {
196         this.markerFilesDirectory = markerFilesDirectory;
197     }
198 }