Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.apache.felix.obr.plugin; |
| 20 | |
| 21 | |
| 22 | import java.io.BufferedWriter; |
| 23 | import java.io.File; |
| 24 | import java.io.FileWriter; |
| 25 | import java.io.IOException; |
| 26 | import java.io.Writer; |
Stuart McCulloch | 473e994 | 2008-01-28 05:23:50 +0000 | [diff] [blame^] | 27 | import java.net.URI; |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 28 | |
| 29 | import org.apache.maven.artifact.manager.WagonManager; |
| 30 | import org.apache.maven.artifact.repository.ArtifactRepository; |
| 31 | import org.apache.maven.plugin.AbstractMojo; |
| 32 | import org.apache.maven.plugin.MojoExecutionException; |
| 33 | import org.apache.maven.plugin.MojoFailureException; |
| 34 | import org.apache.maven.project.MavenProject; |
| 35 | import org.apache.maven.settings.Settings; |
| 36 | import org.apache.maven.wagon.ResourceDoesNotExistException; |
| 37 | import org.apache.maven.wagon.TransferFailedException; |
| 38 | import org.apache.maven.wagon.authorization.AuthorizationException; |
| 39 | |
| 40 | |
| 41 | /** |
| 42 | * deploy the bundle to a ftp site. |
| 43 | * this goal is used when you upload a jar file (in command line) |
| 44 | * @goal deploy-file |
| 45 | * @phase deploy |
| 46 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 47 | */ |
| 48 | public class ObrDeployFile extends AbstractMojo |
| 49 | { |
| 50 | |
| 51 | /** |
| 52 | * setting of maven. |
| 53 | * |
| 54 | * @parameter expression="${settings}" |
| 55 | * @require |
| 56 | */ |
| 57 | |
| 58 | private Settings m_settings; |
| 59 | |
| 60 | /** |
| 61 | * name of the repository xml descriptor file. |
| 62 | * |
| 63 | * @parameter expression="${repository-name}" default-value="repository.xml" |
| 64 | */ |
| 65 | private String m_repositoryName; |
| 66 | |
| 67 | /** |
| 68 | * The local Maven repository. |
| 69 | * |
| 70 | * @parameter expression="${localRepository}" |
| 71 | * @required |
| 72 | */ |
| 73 | private ArtifactRepository m_localRepo; |
| 74 | |
| 75 | /** |
| 76 | * Project in use. |
| 77 | * |
| 78 | * @parameter expression="${project}" |
| 79 | * @require |
| 80 | */ |
| 81 | private MavenProject m_project; |
| 82 | |
| 83 | /** |
| 84 | * Wagon Manager. |
| 85 | * @component |
| 86 | */ |
| 87 | private WagonManager m_wagonManager; |
| 88 | |
| 89 | /** |
| 90 | * obr file define by the user. |
| 91 | * |
| 92 | * @parameter expression="${obr-file}" |
| 93 | * |
| 94 | */ |
| 95 | private String m_obrFile; |
| 96 | |
| 97 | /** |
| 98 | * obr file define by the user. |
| 99 | * |
| 100 | * @parameter expression="${ignore-lock}" |
| 101 | * |
| 102 | */ |
| 103 | private boolean m_ignoreLock; |
| 104 | |
| 105 | /** |
| 106 | * used to store pathfile in local repo. |
| 107 | */ |
| 108 | private String m_fileInLocalRepo; |
| 109 | |
| 110 | |
| 111 | /** |
| 112 | * main method for this goal. |
| 113 | * @implements org.apache.maven.plugin.Mojo.execute |
| 114 | * @throws MojoExecutionException if the plugin failed |
| 115 | * @throws MojoFailureException if the plugin failed |
| 116 | */ |
| 117 | public void execute() throws MojoExecutionException, MojoFailureException |
| 118 | { |
| 119 | getLog().info( "Obr-deploy-file start:" ); |
| 120 | |
| 121 | ArtifactRepository ar = m_project.getDistributionManagementArtifactRepository(); |
| 122 | |
| 123 | // locate the obr.xml file |
Stuart McCulloch | 473e994 | 2008-01-28 05:23:50 +0000 | [diff] [blame^] | 124 | URI obrXml = ObrUtils.toFileURI( m_obrFile ); |
| 125 | if ( null == obrXml ) |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 126 | { |
Stuart McCulloch | 473e994 | 2008-01-28 05:23:50 +0000 | [diff] [blame^] | 127 | getLog().info( "obr.xml is not present, use default" ); |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | File repoDescriptorFile = null; |
| 131 | |
| 132 | RemoteFileManager remoteFile = new RemoteFileManager( ar, m_wagonManager, m_settings, getLog() ); |
| 133 | |
| 134 | remoteFile.connect(); |
| 135 | |
| 136 | // create a non-empty file used to lock the repository descriptor file |
| 137 | File lockFile = null; |
| 138 | Writer output = null; |
| 139 | try |
| 140 | { |
| 141 | lockFile = File.createTempFile( String.valueOf( System.currentTimeMillis() ), null ); |
| 142 | output = new BufferedWriter( new FileWriter( lockFile ) ); |
| 143 | output.write( "locked" ); |
| 144 | output.close(); |
| 145 | } |
| 146 | catch ( IOException e ) |
| 147 | { |
| 148 | getLog().error( "Unable to create temporary file" ); |
| 149 | throw new MojoFailureException( "IOException" ); |
| 150 | } |
| 151 | |
| 152 | if ( m_ignoreLock ) |
| 153 | { |
| 154 | try |
| 155 | { |
| 156 | remoteFile.put( lockFile, m_repositoryName + ".lock" ); |
| 157 | } |
| 158 | catch ( TransferFailedException e ) |
| 159 | { |
| 160 | getLog().error( "Transfer failed" ); |
| 161 | e.printStackTrace(); |
| 162 | throw new MojoFailureException( "TransferFailedException" ); |
| 163 | |
| 164 | } |
| 165 | catch ( ResourceDoesNotExistException e ) |
| 166 | { |
| 167 | throw new MojoFailureException( "ResourceDoesNotExistException" ); |
| 168 | } |
| 169 | catch ( AuthorizationException e ) |
| 170 | { |
| 171 | getLog().error( "Authorization failed" ); |
| 172 | e.printStackTrace(); |
| 173 | throw new MojoFailureException( "AuthorizationException" ); |
| 174 | } |
| 175 | |
| 176 | } |
| 177 | else |
| 178 | { |
| 179 | int countError = 0; |
| 180 | while ( remoteFile.isLockedFile( remoteFile, m_repositoryName ) && countError < 2 ) |
| 181 | { |
| 182 | countError++; |
| 183 | getLog().warn( "File is locked, retry in 10s" ); |
| 184 | try |
| 185 | { |
| 186 | Thread.sleep( 10000 ); |
| 187 | } |
| 188 | catch ( InterruptedException e ) |
| 189 | { |
| 190 | getLog().warn( "Sleep Interupted" ); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | if ( countError == 2 ) |
| 195 | { |
| 196 | getLog().error( |
| 197 | "File: " + m_repositoryName + " is locked. Try -Dignore-lock=true if you want to force uploading" ); |
| 198 | throw new MojoFailureException( "fileLocked" ); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // file is not locked, so we lock it now |
| 203 | try |
| 204 | { |
| 205 | remoteFile.put( lockFile, m_repositoryName + ".lock" ); |
| 206 | } |
| 207 | catch ( TransferFailedException e ) |
| 208 | { |
| 209 | getLog().error( "Transfer failed" ); |
| 210 | e.printStackTrace(); |
| 211 | throw new MojoFailureException( "TransferFailedException" ); |
| 212 | |
| 213 | } |
| 214 | catch ( ResourceDoesNotExistException e ) |
| 215 | { |
| 216 | throw new MojoFailureException( "ResourceDoesNotExistException" ); |
| 217 | } |
| 218 | catch ( AuthorizationException e ) |
| 219 | { |
| 220 | getLog().error( "Authorization failed" ); |
| 221 | e.printStackTrace(); |
| 222 | throw new MojoFailureException( "AuthorizationException" ); |
| 223 | } |
| 224 | |
| 225 | try |
| 226 | { |
| 227 | repoDescriptorFile = remoteFile.get( m_repositoryName ); |
| 228 | } |
| 229 | catch ( TransferFailedException e ) |
| 230 | { |
| 231 | getLog().error( "Transfer failed" ); |
| 232 | e.printStackTrace(); |
| 233 | throw new MojoFailureException( "TransferFailedException" ); |
| 234 | |
| 235 | } |
| 236 | catch ( ResourceDoesNotExistException e ) |
| 237 | { |
| 238 | // file doesn't exist! create a new one |
| 239 | getLog().warn( "file specified does not exist: " + m_repositoryName ); |
| 240 | getLog().warn( "Create a new repository descriptor file " + m_repositoryName ); |
| 241 | try |
| 242 | { |
| 243 | File f = File.createTempFile( String.valueOf( System.currentTimeMillis() ), null ); |
| 244 | repoDescriptorFile = new File( f.getParent() + File.separator |
| 245 | + String.valueOf( System.currentTimeMillis() ) + ".xml" ); |
| 246 | } |
| 247 | catch ( IOException e1 ) |
| 248 | { |
| 249 | getLog().error( "canno't create temporary file" ); |
| 250 | e1.printStackTrace(); |
| 251 | return; |
| 252 | } |
| 253 | } |
| 254 | catch ( AuthorizationException e ) |
| 255 | { |
| 256 | getLog().error( "Authorization failed" ); |
| 257 | e.printStackTrace(); |
| 258 | throw new MojoFailureException( "AuthorizationException" ); |
| 259 | } |
| 260 | catch ( IOException e ) |
| 261 | { |
| 262 | e.printStackTrace(); |
| 263 | throw new MojoFailureException( "IOException" ); |
| 264 | } |
| 265 | |
| 266 | Config userConfig = new Config(); |
| 267 | userConfig.setPathRelative( true ); |
| 268 | userConfig.setRemotely( true ); |
| 269 | |
| 270 | PathFile file = null; |
| 271 | |
| 272 | // get the path to local maven repository |
| 273 | file = new PathFile( PathFile.uniformSeparator( m_settings.getLocalRepository() ) + File.separator |
| 274 | + PathFile.uniformSeparator( m_localRepo.pathOf( m_project.getArtifact() ) ) ); |
| 275 | if ( file.isExists() ) |
| 276 | { |
| 277 | m_fileInLocalRepo = file.getOnlyAbsoluteFilename(); |
| 278 | } |
| 279 | else |
| 280 | { |
| 281 | getLog().error( |
| 282 | "file not found in local repository: " + m_settings.getLocalRepository() + File.separator |
| 283 | + m_localRepo.pathOf( m_project.getArtifact() ) ); |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | file = new PathFile( "file:/" + repoDescriptorFile.getAbsolutePath() ); |
| 288 | |
Stuart McCulloch | 473e994 | 2008-01-28 05:23:50 +0000 | [diff] [blame^] | 289 | ObrUpdate obrUpdate = new ObrUpdate( file, obrXml, m_project, m_fileInLocalRepo, PathFile |
| 290 | .uniformSeparator( m_settings.getLocalRepository() ), userConfig, getLog() ); |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 291 | |
| 292 | obrUpdate.updateRepository(); |
| 293 | |
| 294 | // the reposiroty descriptor file is modified, we upload it on the remote repository |
| 295 | try |
| 296 | { |
| 297 | remoteFile.put( repoDescriptorFile, m_repositoryName ); |
| 298 | } |
| 299 | catch ( TransferFailedException e ) |
| 300 | { |
| 301 | getLog().error( "Transfer failed" ); |
| 302 | e.printStackTrace(); |
| 303 | throw new MojoFailureException( "TransferFailedException" ); |
| 304 | } |
| 305 | catch ( ResourceDoesNotExistException e ) |
| 306 | { |
| 307 | getLog().error( "Resource does not exist:" + repoDescriptorFile.getName() ); |
| 308 | e.printStackTrace(); |
| 309 | throw new MojoFailureException( "ResourceDoesNotExistException" ); |
| 310 | } |
| 311 | catch ( AuthorizationException e ) |
| 312 | { |
| 313 | getLog().error( "Authorization failed" ); |
| 314 | e.printStackTrace(); |
| 315 | throw new MojoFailureException( "AuthorizationException" ); |
| 316 | } |
| 317 | repoDescriptorFile.delete(); |
| 318 | |
| 319 | // we remove lockFile activation |
| 320 | lockFile = null; |
| 321 | try |
| 322 | { |
| 323 | lockFile = File.createTempFile( String.valueOf( System.currentTimeMillis() ), null ); |
| 324 | } |
| 325 | catch ( IOException e ) |
| 326 | { |
| 327 | e.printStackTrace(); |
| 328 | throw new MojoFailureException( "IOException" ); |
| 329 | } |
| 330 | try |
| 331 | { |
| 332 | remoteFile.put( lockFile, m_repositoryName + ".lock" ); |
| 333 | } |
| 334 | catch ( TransferFailedException e ) |
| 335 | { |
| 336 | getLog().error( "Transfer failed" ); |
| 337 | e.printStackTrace(); |
| 338 | throw new MojoFailureException( "TransferFailedException" ); |
| 339 | } |
| 340 | catch ( ResourceDoesNotExistException e ) |
| 341 | { |
| 342 | e.printStackTrace(); |
| 343 | throw new MojoFailureException( "ResourceDoesNotExistException" ); |
| 344 | } |
| 345 | catch ( AuthorizationException e ) |
| 346 | { |
| 347 | getLog().error( "Authorization failed" ); |
| 348 | e.printStackTrace(); |
| 349 | throw new MojoFailureException( "AuthorizationException" ); |
| 350 | } |
| 351 | remoteFile.disconnect(); |
| 352 | } |
| 353 | } |