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