package org.jenkinsci.plugins.p4_client.client; import hudson.model.TaskListener; import hudson.security.ACL; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.logging.Logger; import jenkins.model.Jenkins; import org.acegisecurity.Authentication; import org.jenkinsci.plugins.p4_client.credentials.P4StandardCredentials; import org.jenkinsci.plugins.p4_client.workspace.Workspace; import com.cloudbees.plugins.credentials.CredentialsProvider; import com.cloudbees.plugins.credentials.domains.DomainRequirement; import com.perforce.p4java.core.file.FileSpecOpStatus; import com.perforce.p4java.core.file.IFileSpec; import com.perforce.p4java.server.IOptionsServer; public class ConnectionHelper { private static Logger logger = Logger.getLogger(ConnectionHelper.class .getName()); protected final ConnectionConfig connectionConfig; protected final AuthorisationConfig authorisationConfig; protected final IOptionsServer connection; private final TaskListener listener; public ConnectionHelper(String credentialID, TaskListener listener) throws Exception { this.listener = listener; P4StandardCredentials credential = findCredential(credentialID); this.connectionConfig = new ConnectionConfig(credential); this.authorisationConfig = new AuthorisationConfig(credential); this.connection = ConnectionFactory.getConnection(connectionConfig); } public ConnectionHelper(P4StandardCredentials credential, TaskListener listener) throws Exception { this.listener = listener; this.connectionConfig = new ConnectionConfig(credential); this.authorisationConfig = new AuthorisationConfig(credential); this.connection = ConnectionFactory.getConnection(connectionConfig); } public ConnectionHelper(P4StandardCredentials credential) throws Exception { this.listener = null; this.connectionConfig = new ConnectionConfig(credential); this.authorisationConfig = new AuthorisationConfig(credential); this.connection = ConnectionFactory.getConnection(connectionConfig); } public boolean isConnected() { return connection.isConnected(); } public boolean isUnicode() { try { return connection.getServerInfo().isUnicodeEnabled(); } catch (Exception e) { return false; } } /** * Checks the Perforce server version number and returns true if greater * than or equal to the min version. The value of min must be of the form * 20092 or 20073 (corresponding to 2009.2 and 2007.3 respectively). * * @param min * @return */ public boolean checkVersion(int min) { int ver = connection.getServerVersionNumber(); return (ver >= min); } public boolean login() throws Exception { connection.setUserName(authorisationConfig.getUsername()); // CHARSET is not defined (only for client access) if (connection.getServerInfo().isUnicodeEnabled()) { connection.setCharsetName("utf8"); } switch (authorisationConfig.getType()) { case PASSWORD: String status = connection.getLoginStatus(); if (!status.contains("not necessary")) { String pass = authorisationConfig.getPassword(); connection.login(pass); } break; case TICKET: String ticket = authorisationConfig.getTicketValue(); connection.setAuthTicket(ticket); break; case TICKETPATH: String path = authorisationConfig.getTicketPath(); connection.setTicketsFilePath(path); break; default: throw new Exception("Unknown Authorisation type: " + authorisationConfig.getType()); } return isLogin(); } public void logout() throws Exception { if (isLogin()) { connection.logout(); } } private boolean isLogin() throws Exception { String status = connection.getLoginStatus(); if (status.contains("not necessary")) { return true; } if (status.contains("ticket expires in")) { return true; } // If there is a broker or something else that swallows the message if (status.isEmpty()) { return true; } logger.info("P4:login failed '" + status + "'"); return false; } public void deleteClient(Workspace workspace) throws Exception { if (connectionConfig.isUnicode()) { String charset = "utf8"; connection.setCharsetName(charset); } String name = workspace.getName(); connection.deleteClient(name, true); } /** * Disconnect from the Perforce Server. * * @throws Exception */ public void disconnect() throws Exception { connection.disconnect(); } /** * Look for a message in the returned FileSpec from operation. * * @param fileSpecs * @param ignore * @return * @throws ConverterException */ public boolean validateFileSpecs(List<IFileSpec> fileSpecs, String... ignore) throws Exception { return validateFileSpecs(fileSpecs, false, ignore); } public boolean validateFileSpecs(List<IFileSpec> fileSpecs, boolean quiet, String... ignore) throws Exception { for (IFileSpec fileSpec : fileSpecs) { if (fileSpec.getOpStatus() != FileSpecOpStatus.VALID) { String msg = fileSpec.getStatusMessage(); // superfluous p4java message boolean unknownMsg = true; ArrayList<String> ignoreList = new ArrayList<String>(); ignoreList.addAll(Arrays.asList(ignore)); for (String istring : ignoreList) { if (msg.contains(istring)) { // its a known message unknownMsg = false; } } // check and report unknown message if (unknownMsg) { if (!quiet) { log("P4JAVA: " + msg); logger.warning("p4java: " + msg); } return false; } } } return true; } /** * Finds a Perforce Credential based on the String id. * * @return a P4StandardCredentials credential or null if not found. */ public static P4StandardCredentials findCredential(String id) { Class<P4StandardCredentials> type = P4StandardCredentials.class; Jenkins scope = Jenkins.getInstance(); Authentication acl = ACL.SYSTEM; DomainRequirement domain = new DomainRequirement(); List<P4StandardCredentials> list; list = CredentialsProvider.lookupCredentials(type, scope, acl, domain); for (P4StandardCredentials c : list) { if (c.getId().equals(id)) { return c; } } return null; } protected void log(String msg) { if (listener == null) { return; } listener.getLogger().println(msg); } public void stop() throws Exception { connection.execMapCmd("admin", new String[] { "stop" }, null); } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 9554 | gronk | mege in Paul's changes | ||
#1 | 8783 | gronk | Populate //guest/gronk/p4-jenkins/.... | ||
//guest/paul_allen/dev/p4-jenkins/p4-client/src/main/java/org/jenkinsci/plugins/p4_client/client/ConnectionHelper.java | |||||
#19 | 8771 | Paul Allen |
Perforce Server 12.1 min check for: Build configuration and password/ticket credentials. Includes: - Added logging for Perforce connections (fine) and set connection pool to 2. - Add 'none' to empty charset list (connection bug?) - Supress P4Java errors for syncing ubinary, xtext, unicode |
||
#18 | 8770 | Paul Allen |
Implemented SCM hook for when Jenkins deletes a workspace. The PerforceScm plugin deletes the workspace, but leaves Jenkins to clean up the local files and directories. (includes dummy 'p4 cstat' code for change reporting) |
||
#17 | 8766 | Paul Allen |
Bug fix: Client Owner not set when creating workspace. Now jenkins will set the owner for new workspaces and take ownership of other manual workspaces it uses for build. |
||
#16 | 8764 | Paul Allen | Supress P4Java warnings, to prevent SCM tasks from failing. | ||
#15 | 8763 | Paul Allen | More Console Output logging | ||
#14 | 8762 | Paul Allen |
Console Ouptut logging for SCM build steps. - Removed SLF4J and used old style logger (matching Jenkins) - Set Client Host filed to null, Jenkins sometimes gives an IP address. - Test p4java setps in SCM tasks for success(true/false) |
||
#13 | 8756 | Paul Allen | Added Stream field to Manual Workspace config. | ||
#12 | 8749 | Paul Allen |
Split off Worksapce Spec from Manual Workspace configuration into a Property Jelly item. Plan to autoload values based on selected client. |
||
#11 | 8744 | Paul Allen |
Support basic SCM Polling. When Jenkins has "Poll SCM" checked the Perforce SCM provider just runs a 'p4 sync -n' on the Workspace. Simple and Fast. |
||
#10 | 8741 | Paul Allen |
Added "Test Connection" to Perforce Password and Ticket Credentials. Does not support SSL connections...yet |
||
#9 | 8738 | Paul Allen |
Workspace Name Formatter. For Template and Stream workspaces it allows the substitution of the following tags: ${node} The name given to the slave Jenkins node. ${hostname} The hostname for the slave Jenkins node. ${project} The name of the Jenkins build Job. ${hash} Unique hash code of the Jenkins node. |
||
#8 | 8725 | Paul Allen |
Minor fixes: - Improved tidy workspace method - Fix for template clients - After an unshelf revert -k files - Minor bug in CheckoutTask - Clear callback urls to prevet reuse |
||
#7 | 8697 | Paul Allen |
Added Manual workspace option for user to define Options/LineEnd/View etc... in Jenkins. |
||
#6 | 8694 | Paul Allen | Added support for unshelve and revert -w behaviour for builds. | ||
#5 | 8664 | Paul Allen | Simplified connection to Perforce to get around the SCM initilisation (or lack of). | ||
#4 | 8662 | Paul Allen | Added auto fill and checks for streams and templates. | ||
#3 | 8661 | Paul Allen | Workspace auto fill | ||
#2 | 8641 | Paul Allen | Added workspace helper (setClient) and template/stream types. | ||
#1 | 8629 | Paul Allen |
Added p4java with connection/authorisation helper classes. Included SSL support and detection of Unicode servers. |