Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2012 JFrog Ltd.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *  http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.jfrog.hudson.release.scm.perforce;
 
 import  hudson.plugins.perforce.PerforceSCM;
 import  org.jfrog.build.vcs.perforce.PerforceClient;
 
 import java.io.File;
Performs Perforce actions for the release management.

Author(s):
Yossi Shaul
 
 public class PerforceManager extends AbstractScmManager<PerforceSCM> {

    
This builder is passed to EditFilesCallable for creating a new perforce connection since the operation may occur on remote agents.
 
     private PerforceClient.Builder builder;
     private PerforceClient perforce;
 
     public PerforceManager(AbstractBuild<?, ?> buildTaskListener buildListener) {
         super(buildbuildListener);
     }
 
     public void prepare() throws IOExceptionInterruptedException {
          = new PerforceClient.Builder();
         PerforceSCM jenkinsScm = getJenkinsScm();
         String hostAddress = jenkinsScm.getP4Port();
         if (!hostAddress.contains(":")) {
             hostAddress = "localhost:" + hostAddress;
         }
         .hostAddress(hostAddress).client(.getEnvironment().get("P4CLIENT"));
         .username(jenkinsScm.getP4User()).password(jenkinsScm.getDecryptedP4Passwd());
         .charset(jenkinsScm.getP4Charset());
          = .build();
     }
 
     public void commitWorkingCopy(int changeListIdString commitMessagethrows IOExceptionInterruptedException {
         FilePath workspace = .getWorkspace();
         if (workspace == null) {
             throw new IOException("Workspace is null, cannot commit changes");
         }
         workspace.act(new CommitFilesCallable(changeListIdcommitMessage));
     }
 
     public void createTag(String labelString commitMessageString changeListIdthrows IOException {
         .createLabel(labelcommitMessagechangeListId);
     }
 
     public void revertWorkingCopy(int changeListIdthrows IOException {
         .revertWorkingCopy(changeListId);
     }
 
     public void deleteLabel(String tagUrlthrows IOException {
         .deleteLabel(tagUrl);
     }

    
Creates a new changelist and returns its id number

Returns:
The id of the newly created changelist
Throws:
IOException In case of errors communicating with perforce server
 
     public int createNewChangeList() throws IOException {
         return .createNewChangeList();
     }
 
     public void deleteChangeList(int changeListIdthrows IOException {
         .deleteChangeList(changeListId);
     }
 
     public int getDefaultChangeListId() throws IOException {
         return .getDefaultChangeListId();
     }
 
     public void commitWorkingCopy(String commitMessagethrows IOExceptionInterruptedException {
        throw new UnsupportedOperationException("Use the overloaded method");
    }
    public void createTag(String tagUrlString commitMessagethrows IOExceptionInterruptedException {
        throw new UnsupportedOperationException("Use the overloaded method");
    }
    public String getRemoteUrl() {
        throw new UnsupportedOperationException("Remote URL not supported");
    }

    
Opens file for editing, this method uses EditFilesCallable which opens new connection to perforce server since it may invoke on remote agents.

Parameters:
currentChangeListId The current change list id to open the file for editing at
filePath The filePath which contains the file we need to edit
Throws:
IOException Thrown in case of perforce communication errors
InterruptedException
    public void edit(int currentChangeListIdFilePath filePaththrows IOExceptionInterruptedException {
        filePath.act(new EditFilesCallable(currentChangeListId));
    }
    public void closeConnection() throws IOException {
        .closeConnection();
    }
    private static class EditFilesCallable implements FilePath.FileCallable<String> {
        private PerforceClient.Builder builder;
        private int currentChangeListId;
        private TaskListener listener;
        public EditFilesCallable(PerforceClient.Builder builderTaskListener buildListenerint currentChangeListId) {
            this. = builder;
            this. = buildListener;
            this. = currentChangeListId;
        }
        public String invoke(File fileVirtualChannel channelthrows IOExceptionInterruptedException {
            log("Opening file: '" + file.getAbsolutePath() + "' for editing");
            PerforceClient perforce = .build();
            perforce.editFile(file);
            perforce.closeConnection();
            return null;
        }
    }
    private static class CommitFilesCallable implements FilePath.FileCallable<String> {
        private PerforceClient.Builder builder;
        private int changeListId;
        private String commitMessage;
        public CommitFilesCallable(PerforceClient.Builder builderint changeListIdString commitMessage) {
            this. = builder;
            this. = changeListId;
            this. = commitMessage;
        }
        public String invoke(File fVirtualChannel channelthrows IOExceptionInterruptedException {
            PerforceClient perforce = .build();
            perforce.commitWorkingCopy();
            perforce.closeConnection();
            return null;
        }
    }
New to GrepCode? Check out our FAQ X