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: SourcesFileMarkerHandler.java 1367274 2012-07-30 20:32:05Z hboutemy $
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      public File getMarkerFile()
55      {
56          return getMarkerFile( this.resolved );
57      }
58  
59      /**
60       * Get MarkerFile, exposed for unit testing purposes
61       * 
62       * @param res
63       *            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
83       * exists.
84       * 
85       * @return <code>true</code> if and only if the file or directory denoted
86       *         by this abstract pathname exists; <code>false</code> otherwise
87       * 
88       * @throws MojoExecutionException
89       *             If a security manager exists and its <code>{@link
90       *          java.lang.SecurityManager#checkRead(java.lang.String)}</code>
91       *             method denies read access to the file or directory
92       */
93      public boolean isMarkerSet()
94          throws MojoExecutionException
95      {
96          File marker = getMarkerFile();
97  
98          File marker2 = getMarkerFile( !this.resolved );
99  
100         return marker.exists() || marker2.exists();
101     }
102 
103     public boolean isMarkerOlder( Artifact theArtifact )
104         throws MojoExecutionException
105     {
106         File marker = getMarkerFile();
107         if ( marker.exists() )
108         {
109             return theArtifact.getFile().lastModified() > marker.lastModified();
110         }
111         else
112         {
113             marker = getMarkerFile( !this.resolved );
114             if ( marker.exists() )
115             {
116                 return theArtifact.getFile().lastModified() > marker.lastModified();
117             }
118             else
119             {
120                 // if the marker doesn't exist, we want to copy so assume it is
121                 // infinitely older
122                 return true;
123             }
124         }
125     }
126 
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                 clearMarker.delete();
151             }
152         }
153         catch ( IOException e )
154         {
155             throw new MojoExecutionException( "Unable to create Marker: " + marker.getAbsolutePath(), e );
156         }
157     }
158 
159     /**
160      * Deletes the file or directory denoted by this abstract pathname. If this
161      * pathname denotes a directory, then the directory must be empty in order
162      * to be deleted.
163      * 
164      * @return <code>true</code> if and only if the file or directory is
165      *         successfully deleted; <code>false</code> otherwise
166      * 
167      * @throws SecurityException
168      *             If a security manager exists and its <code>{@link
169      *          java.lang.SecurityManager#checkDelete}</code>
170      *             method denies delete access to the file
171      */
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
192      *            The resolved to set.
193      */
194     public void setResolved( boolean isResolved )
195     {
196         this.resolved = isResolved;
197     }
198 }