package com.perforce.maven.scm.provider.p4.util; import java.util.Arrays; import java.util.HashSet; import org.codehaus.plexus.util.StringUtils; public class P4ScmUrl { public static final String P4MAVEN_URL_PREFIX = "scm:p4:"; public static final String P4MAVEN_URL_FORMAT = P4MAVEN_URL_PREFIX + "[protocol:][[username[:password]@]hostname:port:]//depot/path/to/project"; private static final String P4MAVEN_URL_DELIMETER = ":"; private static final String PARSING_ERROR = "Invalid SCM URL: The url has to be on the form: " + P4MAVEN_URL_FORMAT; private String protocol; private String host; private int port = 1666; private String p4Port; private String username; private String password; private String path; public String getProtocol() { return protocol; } private void setProtocol( String protocol ) { this.protocol = protocol; } public String getHost() { return host; } public void setHost( String host ) { this.host = host; } public int getPort() { return port; } public void setPort( int port ) { if ( port < 0 ) { throw new IllegalArgumentException( PARSING_ERROR ); } if ( port == 0 ) { port = 1666; } this.port = port; } public String getUsername() { return username; } public void setUsername( String username ) { this.username = username; } public String getPassword() { return password; } public void setPassword( String password ) { this.password = password; } public String getPath() { return path; } public void setPath( String path ) { if ( !path.startsWith( "//" ) ) { throw new IllegalArgumentException( PARSING_ERROR ); } this.path = path; } public String getP4Port() { return p4Port; } public P4ScmUrl( String url ) { if ( url == null || !url.startsWith( P4MAVEN_URL_PREFIX ) ) { throw new IllegalArgumentException( "URL must start with " + P4MAVEN_URL_PREFIX ); } url = url.substring( P4MAVEN_URL_PREFIX.length() ); // extract protocol String protocol = null; int i0 = url.indexOf( P4MAVEN_URL_DELIMETER ); if ( i0 > 0 ) { protocol = url.substring( 0, i0 ); HashSet<String> protocols = new HashSet<String>( Arrays.asList( P4Port.PROTOCOLS ) ); if ( protocols.contains( protocol ) ) { url = url.substring( i0 + 1 ); } else { protocol = null; } } String user = null; String password = null; int index = url.indexOf( '@' ); String theRest = url; if ( index != -1 ) { String userAndPassword = url.substring( 0, index ); theRest = url.substring( index + 1 ); index = userAndPassword.indexOf( ':' ); if ( index != -1 ) { user = userAndPassword.substring( 0, index ); password = userAndPassword.substring( index + 1 ); } else { user = userAndPassword; } } String[] tokens = StringUtils.split( theRest, P4MAVEN_URL_DELIMETER ); String host = null; String port = null; String path = null; switch ( tokens.length ) { case 3: host = tokens[0]; port = tokens[1]; path = tokens[2]; break; case 2: host = tokens[0]; path = tokens[1]; break; case 1: // path only path = tokens[0]; break; default: throw new IllegalArgumentException( PARSING_ERROR ); } if ( System.getenv( "P4PORT" ) != null ) { // if url does not have host info, will try to fill it from ENV P4Port p4PortEnv = new P4Port( System.getenv( "P4PORT" ) ); if ( host == null ) { // if user fill the URL's host field, we don't want ENV to influence it host = p4PortEnv.getHost(); if ( protocol == null ) { protocol = p4PortEnv.getProtocol(); } if ( port == null ) { port = p4PortEnv.getPort(); } } } if ( System.getProperty( "P4PORT" ) != null ) { // system properties has highest precedent P4Port p4PortProps = new P4Port( System.getProperty( "P4PORT" ) ); protocol = p4PortProps.getProtocol(); host = p4PortProps.getHost(); port = p4PortProps.getPort(); } int portValue = 1066; if ( port != null ) { portValue = Integer.parseInt( port ); } this.setProtocol( protocol ); this.setHost( host ); this.setPort( portValue ); this.setPath( path ); this.setUsername( user ); this.setPassword( password ); p4Port = ""; if ( !StringUtils.isBlank( protocol ) ) { p4Port += protocol + ":"; } p4Port += getHost() + ":" + getPort(); } public String getUrl() { StringBuilder buffer = new StringBuilder(); buffer.append( P4MAVEN_URL_PREFIX ); if ( !StringUtils.isBlank( getProtocol() ) ) { buffer.append( getProtocol() ); } if ( ( !StringUtils.isBlank( getPassword() ) ) ) { buffer.append( ":" ).append( getPassword() ).append( ":" ).append( getUsername() ).append( "@" ).append( host ); } else if ( !StringUtils.isBlank( getUsername() ) ) { buffer.append( ":" ).append( getUsername() ).append( "@" ).append( host ); } if ( port > 0 ) { buffer.append( ":" ).append( getPort() ); } buffer.append( ":" ).append( getPath() ); return buffer.toString(); } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#10 | 11616 | dantran | Honor P4USER env | ||
#9 | 11513 | dantran |
Allow .. (dotdot) notation inside the SCM URL. needed to by buildnumber-maven-plugin walk up the tree from a submodule SCM URL |
||
#8 | 11512 | dantran |
Allow .. (dotdot) notation inside the SCM URL. needed to by buildnumber-maven-plugin walk up the tree from a submodule SCM URL |
||
#7 | 9632 | dantran |
- use groupId:artifactId:version to test release automation - Implment p4maven-settings.xml's p4Port field |
||
#6 | 9628 | dantran | better P4ScmUrl parser | ||
#5 | 9627 | dantran |
fix invalid default port. some minor cleanup |
||
#4 | 9590 | dantran |
- add option to fail tag when it already exist - add P4ScmUrl parser test - cleanup |
||
#3 | 9548 | dantran |
- pickup P4PORT form env and system properties to support multi site using proxy - add development doc - check for error after each action |
||
#2 | 9533 | dantran | source format | ||
#1 | 9528 | dantran | Isolate P2 url parsing task into its own class to be reusable by test |