package com.perforce.maven.scm.provider.p4.util; import java.io.File; import java.io.IOException; import org.codehaus.plexus.util.StringUtils; public class P4ScmUrl { public static final String P4MAVEN_URL_PREFIX = "scm:p4:"; public static final String P4PORT_KEY = "P4PORT"; public static final String P4USER_KEY = "P4USER"; public static final String P4MAVEN_URL_FORMAT = P4MAVEN_URL_PREFIX + "[protocol:][[username[:password]@]hostname:port:]//depot/path/to/project"; 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 = 0; private String username; private String password; private String path; public String getProtocol() { return protocol; } public String getHost() { return host; } public int getPort() { return port; } public String getUsername() { return username; } public String getPassword() { return password; } public String getPath() { return path; } private P4ScmUrl() { } public static P4ScmUrl parse( String url ) { P4ScmUrl o = parseInternal( url ); if ( !StringUtils.isBlank( P4Utils.getSettings().getP4Port() ) ) { if ( o.host == null ) { P4Port p4PortEnv = P4Port.parse( P4Utils.getSettings().getP4Port() ); o.host = p4PortEnv.getHost(); o.protocol = p4PortEnv.getProtocol(); o.port = p4PortEnv.getPort(); o.username = p4PortEnv.getUser(); } } if ( !StringUtils.isBlank( System.getenv( P4PORT_KEY ) ) ) { // if url does not have host info, will try to fill it from ENV if ( o.host == null ) { P4Port p4PortEnv = P4Port.parse( System.getenv( P4PORT_KEY ) ); o.host = p4PortEnv.getHost(); o.protocol = p4PortEnv.getProtocol(); o.port = p4PortEnv.getPort(); o.username = p4PortEnv.getUser(); } } if ( !StringUtils.isBlank( System.getProperty( P4PORT_KEY ) ) ) { // system properties has highest precedent P4Port p4PortProps = P4Port.parse( System.getProperty( P4PORT_KEY ) ); o.host = p4PortProps.getHost(); o.protocol = p4PortProps.getProtocol(); o.port = p4PortProps.getPort(); o.username = p4PortProps.getUser(); } return o; } private static P4ScmUrl parseInternal( String url ) { P4ScmUrl o = new P4ScmUrl(); String[] tokens = url.split( "://" ); if ( tokens.length != 2 ) { throw new IllegalArgumentException( PARSING_ERROR ); } o.path = "//" + removeDotDotFromPath( tokens[1] ); if ( "scm:p4".equals( tokens[0] ) ) { // no hostinfo. done parsing return o; } if ( !tokens[0].startsWith( P4MAVEN_URL_PREFIX ) ) { throw new IllegalArgumentException( PARSING_ERROR ); } String hostInfo = tokens[0].substring( P4MAVEN_URL_PREFIX.length() ); P4Port p4Port = P4Port.parse( hostInfo ); o.protocol = p4Port.getProtocol(); o.host = p4Port.getHost(); o.port = p4Port.getPort(); o.username = p4Port.getUser(); o.password = p4Port.getPassword(); return o; } public String getUrl() { StringBuilder buffer = new StringBuilder(); buffer.append( P4MAVEN_URL_PREFIX ); if ( !StringUtils.isBlank( getProtocol() ) ) { buffer.append( getProtocol() ).append( ":" ); } if ( ( !StringUtils.isBlank( getPassword() ) ) ) { buffer.append( getPassword() ).append( ":" ).append( getUsername() ).append( "@" ); } else if ( !StringUtils.isBlank( getUsername() ) ) { buffer.append( getUsername() ).append( "@" ); } if ( host != null ) { buffer.append( host ); } if ( port > 0 ) { buffer.append( ":" ).append( getPort() ); } buffer.append( ":" ).append( getPath() ); return buffer.toString(); } private static String removeDotDotFromPath( String path ) { if ( !path.contains( ".." ) ) { return path; } try { //borrow java.io.File to do path manipulation String base = new File( "." ).getCanonicalPath(); String fullPath = new File( path ).getCanonicalPath(); return new File( base ).toURI().relativize( new File( fullPath ).toURI() ).getPath(); } catch ( IOException e ) { throw new IllegalArgumentException( PARSING_ERROR, e ); } } }
# | 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 |