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