Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package hudson.plugins.collabnet.util;
  
  import  com.collabnet.ce.soap50.webservices.scm.Repository2SoapDO;
  import  com.collabnet.ce.soap50.webservices.tracker.ArtifactSoapDO;
 
Class for methods that are useful across Hudson plugins.
 
 public class CNHudsonUtil {
     private static Logger log = Logger.getLogger("CNHudsonUtil");

    
As a utility class, CNHudsonUtil should never be instantiated.
 
     private CNHudsonUtil() {}

    

Parameters:
url
username
password
Returns:
collabnet app, if one can be created; null otherwise.
 
     public static CollabNetApp getCollabNetApp(String url
                                                String username
                                                String password) {
         if (CommonUtil.unset(url)) {
             return null;
         }
         try {
             return new CollabNetApp(urlusernamepassword);
         } catch (RemoteException re) {
             CommonUtil.logRE("getCollabNetApp"re);
             // more logging
             .log(...."getCollabNetApp failed"
                     re);
             return null;
         }
     }

    
Get a CollabNetApp, given a StaplerRequest with url, username, and password set. If login fails, null will be returned.
 
     public static CollabNetApp getCollabNetApp(StaplerRequest request) {
         String url = null;
         String username = null;
         String password = null;
         String override_auth = request.getParameter("override_auth");
         TeamForgeShare.TeamForgeShareDescriptor descriptor = 
             TeamForgeShare.getTeamForgeShareDescriptor();
         if (descriptor != null && descriptor.useGlobal() && 
             override_auth != null && override_auth.equals("false")) {
             url = descriptor.getCollabNetUrl();
             username = descriptor.getUsername();
             password = descriptor.getPassword();
         } else {
             url = request.getParameter("url");
             username = request.getParameter("username");
             password = request.getParameter("password");
         }
 
         if (CommonUtil.unset(url) || CommonUtil.unset(username
             || CommonUtil.unset(password)) {
             return null;
         }
         return getCollabNetApp(urlusernamepassword);
     }

    

Returns:
the username from the stapler request or the global value, if applicable.
 
     public static String getUsername(StaplerRequest request) {
         String username = null;
         String override_auth = request.getParameter("override_auth");
         TeamForgeShare.TeamForgeShareDescriptor descriptor = 
             TeamForgeShare.getTeamForgeShareDescriptor();
         if (descriptor != null && descriptor.useGlobal() && 
             override_auth != null && override_auth.equals("false")) {
             username = descriptor.getUsername();
         } else {
             username = request.getParameter("username");
         }
         return username;
     }

    
Logs off the CollabNetApp, if possible.

Parameters:
cna CollabNetApp
    public static void logoff(CollabNetApp cna) {
        if (cna != null) {
            try {
                cna.logoff();
            } catch (RemoteException re) {
                CommonUtil.logRE("logoff"re);
            }
        }
    }

    
Get the packageId for the package name.

Parameters:
cna the collab net app instance to use
rpackage name of the package.
projectId the project id.
Returns:
the package id if found, null otherwise.
    public static String getPackageId(CollabNetApp cnaString rpackage
                                String projectId) {
        String packageId = null;
        if (cna != null && projectId != null) {
            FrsApp fa = new FrsApp(cna);
            try {
                packageId = fa.findPackageId(rpackageprojectId);
            } catch (RemoteException re) {
                CommonUtil.logRE("getPackageId"re);
                return null;
            }
        }
        return packageId;
    }

    
Get the projectId for the project name.

Parameters:
cna for accessing the webservice methods.
project name.
Returns:
the id for the project or null, if no such project exist.
    public static String getProjectId(CollabNetApp cnaString project) {
        String projectId = null;
        if (cna != null) {
            try {
                projectId = cna.getProjectId(project);
            } catch (RemoteException re) {
                CommonUtil.logRE("getProjectId"re);
            }
        } 
        return projectId;
    }

    
Get the release id.

Parameters:
cna for accessing the webservice methods.
packageId the id of the package which contains this release.
release the name of the release
Returns:
the release id, or null if none is found.
    public static String getReleaseId(CollabNetApp cnaString packageId
                               String release) {
        if (cna == null || packageId == null) {
            return null;
        }
        String releaseId = null;
        FrsApp fa = new FrsApp(cna);
        try {
            releaseId = fa.findReleaseIdByPackage(releasepackageId);
        } catch (RemoteException re) {
            CommonUtil.logRE("getReleaseId"re);
        }
        return releaseId;
    }

    
Get the file id.

Parameters:
cna for accessing the webservice methods.
releaseId the id of the release.
file name
Returns:
the file id, or null if none is found.
    public static String getFileId(CollabNetApp cnaString releaseId
                                   String file) {
        if (cna == null || releaseId == null) {
            return null;
        }
        String fileId = null;
        FrsApp fa = new FrsApp(cna);
        try {
            fileId = fa.findFrsFile(filereleaseId);
        } catch (RemoteException re) {
            CommonUtil.logRE("getFileId"re);
        }
        return fileId;
    }

    
Get a releaseId, given a projectId and a release title.

Parameters:
cna for accessing the webservice methods.
projectId
release
Returns:
the releaseId in this project which matches the release name or null if none is found.
    public static String getProjectReleaseId (CollabNetApp cna
                                              String projectId,
                                              String release) {
        if (cna == null || projectId == null) {
            return null;
        }
        FrsApp fa = new FrsApp(cna);
        String releaseId = null;
        try {
            releaseId = fa.findReleaseId(releaseprojectId);
        } catch (RemoteException re) {
            CommonUtil.logRE("getProjectReleaseId"re);
        }
        return releaseId;
    }

    
Given a tracker title and a projectId, find the matching tracker id.

Parameters:
cna for accessing the webservice methods.
projectId
trackerName
Returns:
the tracker id for the tracker that matches this name, or null if no matching tracker is found.
    public static String getTrackerId(CollabNetApp cnaString projectId
                                      String trackerName) {
        if (cna == null || projectId == null) {
            return null;
        }
        TrackerApp ta = new TrackerApp(cna);
        String trackerId = null;
        try {
            trackerId = ta.getTrackerId(projectIdtrackerName);
        } catch (RemoteException re) {
            CommonUtil.logRE("getTrackerId"re);
            return null;
        }
        return trackerId;
    }

    
Given a project and issue title, find the most recent matching issue object, or null if none matches.
    public static ArtifactSoapDO getTrackerArtifact(CollabNetApp cna
                                                    String project
                                                    String tracker
                                                    String issueTitle) {
        ArtifactSoapDO artifact = null;
        if (cna != null) {
            String projectId = CNHudsonUtil.getProjectId(cnaproject);
            if (projectId != null) {
                String trackerId = CNHudsonUtil.getTrackerId(cnaprojectId
                                                             tracker);
                if (trackerId != null) {
                    TrackerApp ta = new TrackerApp(cna);
                    try {
                        artifact = ta.findLastTrackerArtifact(trackerId
                                                              issueTitle);
                    } catch (RemoteException re) {}
                }
            }
        }
        return artifact;
    }
    
    
Given project, package, release, and file name, find the fileId for a file in the system, or null if none is found.

Parameters:
cna for accessing webservice methods
project name
rpackage name
release name
file name
    public static String getFileId(CollabNetApp cnaString project
                                   String rpackageString release
                                   String file) {
        String fileId = null;
        String projectId = CNHudsonUtil.getProjectId(cnaproject);
        if (projectId != null) {
            String packageId = CNHudsonUtil.getPackageId(cnarpackage
                                                         projectId);
            if (packageId != null) {
                String releaseId = CNHudsonUtil.getReleaseId(cnapackageId
                                                             release);
                if (releaseId != null) {
                    fileId = CNHudsonUtil.getFileId(cnareleaseIdfile);
                }
            }
        }
        return fileId;
    }

    

Parameters:
cna for accessing the webservice methods.
username
Returns:
true if the user can be found.
    public static boolean isUserValid(CollabNetApp cnaString username) {
        boolean valid = false;
        try {
            valid = cna.isUsernameValid(username);
        } catch (RemoteException re) {
            CommonUtil.logRE("userExists"re);
        }
        return valid;
    }
    
    

Parameters:
cna for accessing the webservice methods.
username
projectId
Returns:
true if the user is a member of the project.
    public static boolean isUserMember(CollabNetApp cna,
                                       String username,
                                       String projectId) {
        boolean member = false;
        try {
            member = cna.isUserMemberOfProject(usernameprojectId);
        } catch (RemoteException re) {
            CommonUtil.logRE("userMember"re);
        }
        return member;
    }

    

Parameters:
cna for accessing the webservice methods.
collabnetUrl the collabnet url
projectName name of the project
repoName name of the repository
Returns:
the scm viewer url
    public static String getScmViewerUrl(CollabNetApp cnaString collabnetUrlString projectNameString repoName) {
        String url = null;
        Repository2SoapDO repoData = CNHudsonUtil.getRepoData(cnaprojectNamerepoName);
        if (repoData != null) {
            // normally, just use the defined scm viewer url
            url = repoData.getScmViewerUrl();
            if (cna != null) {
                int apiVersion[] = null;
                try {
                    apiVersion = getVersionArray(cna.getApiVersion());
                } catch (RemoteException re) {
                    CommonUtil.logRE("getScmViewerUrl"re);
                }
                if (isSupportedVersion(new int[] {5, 3, 0, 0}, new int[] {6, 0, 0, 0}, apiVersion)) {
                    // starting with CTF 5.3, you can use the new viewRepositorySource method that does auth for viewvc
                    url = collabnetUrl + "/sf/scm/do/viewRepositorySource/" + repoData.getPath();
                }
            }
        }
        return url;
    }

    
Turn version string into an array, where each version is in its own index/pos.

Parameters:
apiVersionStr
Returns:
    public static int[] getVersionArray(String apiVersionStr) {
        int[] versionNums = null;
        if (apiVersionStr != null) {
            String[] versionArr = apiVersionStr.split("\\.");
            versionNums = new int[versionArr.length];
            for (int i = 0; i < versionArr.lengthi++) {
                versionNums[i] = Integer.parseInt(versionArr[i]);
            }
        }
        return versionNums;
    }

    
Check if the actual version is within the range of the start/end support version

Parameters:
startSupportVersion the start version, inclusive. null to ignore check.
endSupportVersion the ending version, not inclusive. null to ignore check.
actualVersion the actual version
Returns:
true if actual version is between start version (inclusive) and end version (non inclusive)
    public static boolean isSupportedVersion(int[] startSupportVersionint[] endSupportVersionint[] actualVersion) {
        if (actualVersion == null || actualVersion.length != 4) {
            .warning("Unable to determine api version: isSupportedVersion returning false");
            return false;
        }
        if (startSupportVersion != null) {
            if (startSupportVersion.length != 4) {
                return false;
            }
            if (compareVersion(actualVersionstartSupportVersion) == -1) {
                // means actual version is before the start support version
                return false;
            }
        }
        if (endSupportVersion != null) {
            if (endSupportVersion.length != 4) {
                return false;
            }
            if (compareVersion(actualVersionendSupportVersion) != -1) {
                // means actual version is either after or the same as endSupport version
                return false;
            }
        }
        return true;
    }

    
Compare two equal length version array

Parameters:
version1 first version
version2 second version
Returns:
-1 if version1 is less than version2, 0 if they are the same, and 1 if version1 is greater than version2
    public static int compareVersion(int[] version1int[] version2) {
        for (int i=0; i < version1.lengthi++) {
            int v1 = version1[i];
            int v2 = version2[i];
            if (v1 > v2) {
                return 1;
            } else if (v1 < v2) {
                return -1;
            }
        }
        return 0;
    }

    

Parameters:
cna for accessing the webservice methods.
projectName
repoName
    public static String getSystemId(CollabNetApp cnaString projectName
                                     String repoName) {
        String systemId = null;
        Repository2SoapDO repoData = CNHudsonUtil.getRepoData(cnaprojectName,
                                                              repoName);
        if (repoData != null) {
            systemId = repoData.getSystemId();
        }
        return systemId;
    }
    
    

Parameters:
cna for accessing the webservice methods.
projectName
repoName
    private static Repository2SoapDO getRepoData(CollabNetApp cna
                                                 String projectName,
                                                 String repoName) {
        Repository2SoapDO repoData = null;
        String projectId = CNHudsonUtil.getProjectId(cnaprojectName);
        if (projectId == null) {
            return null;
        }
        String repoId = CNHudsonUtil.getRepoId(cnaprojectIdrepoName);
        if (repoId == null) {
            return null;
        }
        ScmApp sa = new ScmApp(cna);
        try {
            repoData = sa.getRepoData(repoId);
        } catch (RemoteException re) {
            CommonUtil.logRE("getScmViewerUrl"re);
        }
        return repoData;
    }

    

Parameters:
cna for accessing the webservice methods.
projectId
repoName
    public static String getRepoId(CollabNetApp cnaString projectId
                                   String repoName) {
        String repoId = null;
        ScmApp sa = new ScmApp(cna);
        try {
            repoId = sa.getRepoId(projectIdrepoName);
        } catch (RemoteException re) {
            CommonUtil.logRE("getRepoId"re);
        }
        return repoId;
    }

    
Sanitizes a CollabNet url and make it appropriate to be used by this plugin.

Parameters:
url original url
Returns:
sanitized collabnet url
    public static String sanitizeCollabNetUrl(String url) {
        // strip the trailing "/" from the collabnet url, as this causes browser to log off session (artf51846)
        if (url != null && url.endsWith("/")) {
            url = url.substring(0, url.length() - 1);
        }
        return url;
    }
New to GrepCode? Check out our FAQ X