/** * Copyright (c) 2010 Perforce Software. All rights reserved. */ package com.perforce.maven.scm.provider.p4; import java.util.Arrays; import java.util.HashSet; import org.apache.maven.scm.CommandParameter; import org.apache.maven.scm.CommandParameters; import org.apache.maven.scm.ScmBranch; import org.apache.maven.scm.ScmException; import org.apache.maven.scm.ScmFileSet; import org.apache.maven.scm.ScmTag; import org.apache.maven.scm.ScmVersion; import org.apache.maven.scm.command.add.AddScmResult; import org.apache.maven.scm.command.blame.BlameScmResult; import org.apache.maven.scm.command.branch.BranchScmResult; import org.apache.maven.scm.command.changelog.ChangeLogScmResult; import org.apache.maven.scm.command.checkin.CheckInScmResult; import org.apache.maven.scm.command.checkout.CheckOutScmResult; import org.apache.maven.scm.command.diff.DiffScmResult; import org.apache.maven.scm.command.edit.EditScmResult; import org.apache.maven.scm.command.export.ExportScmResult; import org.apache.maven.scm.command.list.ListScmResult; import org.apache.maven.scm.command.login.LoginScmResult; import org.apache.maven.scm.command.remove.RemoveScmResult; import org.apache.maven.scm.command.status.StatusScmResult; import org.apache.maven.scm.command.tag.TagScmResult; import org.apache.maven.scm.command.unedit.UnEditScmResult; import org.apache.maven.scm.command.update.UpdateScmResult; import org.apache.maven.scm.provider.AbstractScmProvider; import org.apache.maven.scm.provider.ScmProviderRepository; import org.apache.maven.scm.repository.ScmRepositoryException; import org.codehaus.plexus.util.StringUtils; import com.perforce.maven.scm.provider.p4.command.add.P4AddCommand; import com.perforce.maven.scm.provider.p4.command.blame.P4BlameCommand; import com.perforce.maven.scm.provider.p4.command.branch.P4BranchCommand; import com.perforce.maven.scm.provider.p4.command.changelog.P4ChangeLogCommand; import com.perforce.maven.scm.provider.p4.command.checkin.P4CheckInCommand; import com.perforce.maven.scm.provider.p4.command.checkout.P4CheckOutCommand; import com.perforce.maven.scm.provider.p4.command.diff.P4DiffCommand; import com.perforce.maven.scm.provider.p4.command.edit.P4EditCommand; import com.perforce.maven.scm.provider.p4.command.export.P4ExportCommand; import com.perforce.maven.scm.provider.p4.command.list.P4ListCommand; import com.perforce.maven.scm.provider.p4.command.login.P4LoginCommand; import com.perforce.maven.scm.provider.p4.command.remove.P4RemoveCommand; import com.perforce.maven.scm.provider.p4.command.status.P4StatusCommand; import com.perforce.maven.scm.provider.p4.command.tag.P4TagCommand; import com.perforce.maven.scm.provider.p4.command.unedit.P4UnEditCommand; import com.perforce.maven.scm.provider.p4.command.update.P4UpdateCommand; import com.perforce.maven.scm.provider.p4.manager.P4ClientManager; import com.perforce.maven.scm.provider.p4.repository.P4ScmProviderRepository; /** * This class validates the SCM URL, populate the ScmProviderRepository and provide all commands supported by the P4 * provider. * * @plexus.component role="org.apache.maven.scm.provider.ScmProvider" role-hint="p4" */ public class P4ScmProvider extends AbstractScmProvider { private static final String[] PROTOCOLS = { "tcp", "tcp4", "tcp6", "tcp46", "tcp64", "ssl", "ssl4", "ssl6", "ssl46", "ssl64" }; /** * @plexus.requirement role="com.perforce.maven.scm.provider.p4.manager.P4ClientManager" */ private P4ClientManager clientManager; /** * @see org.apache.maven.scm.provider.ScmProvider#makeProviderScmRepository */ public ScmProviderRepository makeProviderScmRepository( String scmSpecificUrl, char delimiter ) throws ScmRepositoryException { String host = ""; int port = 0; String path = null; // minimal logic to support perforce protocols in scm url, and keep the next part unchanged String protocol = null; int i0 = scmSpecificUrl.indexOf( delimiter ); if ( i0 > 0 ) { protocol = scmSpecificUrl.substring( 0, i0 ); HashSet<String> protocols = new HashSet<String>( Arrays.asList( PROTOCOLS ) ); if ( protocols.contains( protocol ) ) { scmSpecificUrl = scmSpecificUrl.substring( i0 + 1 ); } else { protocol = null; } } int i1 = scmSpecificUrl.indexOf( delimiter ); int i2 = scmSpecificUrl.indexOf( delimiter, i1 + 1 ); if ( i1 > 0 ) { int lastDelimiter = scmSpecificUrl.lastIndexOf( delimiter ); if ( lastDelimiter > 0 ) { path = scmSpecificUrl.substring( lastDelimiter + 1 ); } host = scmSpecificUrl.substring( 0, i1 ); // If there is tree parts in the scm url, the second is the port if ( i2 >= 0 ) { try { if ( lastDelimiter > ( i1 + 1 ) ) { String tmp = scmSpecificUrl.substring( i1 + 1, lastDelimiter ); port = Integer.parseInt( tmp ); } } catch ( NumberFormatException ex ) { throw new ScmRepositoryException( "The port has to be a number." ); } } } else { path = scmSpecificUrl; } String user = null; String password = null; if ( host != null && host.indexOf( "@" ) > 1 ) { user = host.substring( 0, host.indexOf( "@" ) ); host = host.substring( host.indexOf( "@" ) + 1 ); } if ( path != null && path.indexOf( "@" ) > 1 ) { if ( host != null ) { if ( getLogger().isWarnEnabled() ) { getLogger().warn( "Username as part of path is deprecated, the new format is " + "scm:p4:[username@]host:port:path_to_repository" ); } } user = path.substring( 0, path.indexOf( "@" ) ); path = path.substring( path.indexOf( "@" ) + 1 ); } // Honor ENV when SCM Url is unknown or missing part String envHost = "localhost"; String envProtocol = null; int envPort = 1666; String p4Port = System.getenv( "P4PORT" ); if ( StringUtils.isNotEmpty( p4Port ) ) { String[] tokens = StringUtils.split( ":" ); switch ( tokens.length ) { case 3: envProtocol = tokens[0]; envHost = tokens[1]; envPort = Integer.parseInt( tokens[2] ); break; case 2: if ( StringUtils.isNumeric( tokens[1] ) ) { envHost = tokens[0]; envPort = Integer.parseInt( tokens[2] ); } else { envProtocol = tokens[0]; envHost = tokens[1]; } case 1: envHost = tokens[0]; break; default: throw new IllegalArgumentException( "Invalid P4PORT environment variable" ); } } if ( StringUtils.isEmpty( host ) ) { host = envHost; protocol = envProtocol; } if ( port <= 0 ) { port = envPort; } return new P4ScmProviderRepository( protocol, host, port, path, user, password ); } /** * @see org.apache.maven.scm.provider.AbstractScmProvider#requiresEditMode() */ public boolean requiresEditMode() { return true; } /** * @see org.apache.maven.scm.provider.ScmProvider#getScmType() */ public String getScmType() { return "p4"; } /** * @see org.apache.maven.scm.provider.AbstractScmProvider .scm.provider.ScmProviderRepository, * org.apache.maven.scm.ScmFileSet, org.apache.maven.scm.CommandParameters) */ public AddScmResult add( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) throws ScmException { P4AddCommand command = new P4AddCommand(); command.setLogger( getLogger() ); command.setClient( clientManager.getClient( (P4ScmProviderRepository) repository, null, fileSet.getBasedir() ) ); return (AddScmResult) command.execute( repository, fileSet, params ); } /** * @see org.apache.maven.scm.provider.AbstractScmProvider#blame */ protected BlameScmResult blame( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) throws ScmException { P4BlameCommand command = new P4BlameCommand(); command.setLogger( getLogger() ); command.setClient( clientManager.getClient( (P4ScmProviderRepository) repository, null, fileSet.getBasedir() ) ); return (BlameScmResult) command.execute( repository, fileSet, params ); } /** * @see org.apache.maven.scm.provider.AbstractScmProvider#branch */ protected BranchScmResult branch( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) throws ScmException { P4BranchCommand command = new P4BranchCommand(); command.setLogger( getLogger() ); command.setClient( clientManager.getClient( (P4ScmProviderRepository) repository, null, fileSet.getBasedir() ) ); return (BranchScmResult) command.execute( repository, fileSet, params ); } /** * @see org.apache.maven.scm.provider.AbstractScmProvider#checkin */ protected CheckInScmResult checkin( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) throws ScmException { P4CheckInCommand command = new P4CheckInCommand(); command.setLogger( getLogger() ); command.setClient( clientManager.getClient( (P4ScmProviderRepository) repository, null, fileSet.getBasedir() ) ); return (CheckInScmResult) command.execute( repository, fileSet, params ); } /** * @see org.apache.maven.scm.provider.AbstractScmProvider#checkout */ protected CheckOutScmResult checkout( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) throws ScmException { ScmVersion scmVersion = params.getScmVersion( CommandParameter.SCM_VERSION, null ); P4ScmProviderRepository p4Repo = (P4ScmProviderRepository) repository; if ( scmVersion != null && !(scmVersion instanceof ScmTag) && scmVersion instanceof ScmBranch ) { ScmBranch scmBranch = (ScmBranch) scmVersion; p4Repo.setPath( scmBranch.getName() ); } P4CheckOutCommand command = new P4CheckOutCommand(); command.setLogger( getLogger() ); command.setClient( clientManager.getClient( p4Repo, null, fileSet.getBasedir() ) ); return (CheckOutScmResult) command.execute( repository, fileSet, params ); } /** * @see org.apache.maven.scm.provider.AbstractScmProvider#export */ protected ExportScmResult export( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) throws ScmException { // @TODO, use a random client name so that we can auto remove it P4ExportCommand command = new P4ExportCommand(); command.setLogger( getLogger() ); command.setClient( clientManager.getClient( (P4ScmProviderRepository) repository, null, fileSet.getBasedir() ) ); return (ExportScmResult) command.execute( repository, fileSet, params ); } /** * @see org.apache.maven.scm.provider.AbstractScmProvider#changelog */ protected ChangeLogScmResult changelog( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters ) throws ScmException { P4ChangeLogCommand command = new P4ChangeLogCommand(); command.setLogger( getLogger() ); command.setClient( clientManager.getClient( (P4ScmProviderRepository) repository, null, fileSet.getBasedir() ) ); return (ChangeLogScmResult) command.execute( repository, fileSet, parameters ); } /** * @see org.apache.maven.scm.provider.AbstractScmProvider#diff */ protected DiffScmResult diff( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) throws ScmException { P4DiffCommand command = new P4DiffCommand(); command.setLogger( getLogger() ); command.setClient( clientManager.getClient( (P4ScmProviderRepository) repository, null, fileSet.getBasedir() ) ); return (DiffScmResult) command.execute( repository, fileSet, params ); } /** * @see org.apache.maven.scm.provider.AbstractScmProvider#edit */ protected EditScmResult edit( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) throws ScmException { P4EditCommand command = new P4EditCommand(); command.setLogger( getLogger() ); command.setClient( clientManager.getClient( (P4ScmProviderRepository) repository, null, fileSet.getBasedir() ) ); return (EditScmResult) command.execute( repository, fileSet, params ); } /** * @see org.apache.maven.scm.provider.AbstractScmProvider#list */ public ListScmResult list( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) throws ScmException { P4ListCommand command = new P4ListCommand(); command.setLogger( getLogger() ); command.setClient( clientManager.getClient( (P4ScmProviderRepository) repository, null, fileSet.getBasedir() ) ); return (ListScmResult) command.execute( repository, fileSet, params ); } /** * @see org.apache.maven.scm.provider.AbstractScmProvider#login */ protected LoginScmResult login( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) throws ScmException { P4LoginCommand command = new P4LoginCommand(); command.setLogger( getLogger() ); command.setClient( clientManager.getClient( (P4ScmProviderRepository) repository, null, fileSet.getBasedir() ) ); return (LoginScmResult) command.execute( repository, fileSet, params ); } /** * @see org.apache.maven.scm.provider.AbstractScmProvider#remove */ protected RemoveScmResult remove( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) throws ScmException { P4RemoveCommand command = new P4RemoveCommand(); command.setLogger( getLogger() ); command.setClient( clientManager.getClient( (P4ScmProviderRepository) repository, null, fileSet.getBasedir() ) ); return (RemoveScmResult) command.execute( repository, fileSet, params ); } /** * @see org.apache.maven.scm.provider.AbstractScmProvider#status */ protected StatusScmResult status( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) throws ScmException { P4StatusCommand command = new P4StatusCommand(); command.setLogger( getLogger() ); command.setClient( clientManager.getClient( (P4ScmProviderRepository) repository, null, fileSet.getBasedir() ) ); return (StatusScmResult) command.execute( repository, fileSet, params ); } /** * @see org.apache.maven.scm.provider.AbstractScmProvider#tag */ protected TagScmResult tag( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) throws ScmException { P4TagCommand command = new P4TagCommand(); command.setLogger( getLogger() ); command.setClient( clientManager.getClient( (P4ScmProviderRepository) repository, null, fileSet.getBasedir() ) ); return (TagScmResult) command.execute( repository, fileSet, params ); } /** * @see org.apache.maven.scm.provider.AbstractScmProvider#unedit */ protected UnEditScmResult unedit( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) throws ScmException { P4UnEditCommand command = new P4UnEditCommand(); command.setLogger( getLogger() ); command.setClient( clientManager.getClient( (P4ScmProviderRepository) repository, null, fileSet.getBasedir() ) ); return (UnEditScmResult) command.execute( repository, fileSet, params ); } /** * @see org.apache.maven.scm.provider.AbstractScmProvider#update */ protected UpdateScmResult update( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) throws ScmException { P4UpdateCommand command = new P4UpdateCommand(); command.setLogger( getLogger() ); command.setClient( clientManager.getClient( (P4ScmProviderRepository) repository, null, fileSet.getBasedir() ) ); return (UpdateScmResult) command.execute( repository, fileSet, params ); } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#8 | 9519 | dantran | folder rename to match with its artifactId | ||
#7 | 9514 | dantran |
- Remove support to pick up p4port and its credential from ENV and System properties. These setttings has no use since Maven requires user to provide the required URL which has both p4port and credential. Also user can always fall back to settings.xml to store credential - Rewrite URL parser and also support password as part of URL - TCK test now has default URL and its credential is store under settings.xml - User can also issue her how scmTckUrl system properties ( to be doc as part of maven site) |
||
#6 | 9498 | dantran |
- Setup Maven site for documetation - Implement ~/m2/p4maven-settings.xml to store external config overridable by system properties. All global settings now can be retrieved va P4Utils |
||
#5 | 9471 | dantran |
- cleanup diff command - simplify branch command and full compliant with maven-scm-test - add checkout with branch - add repo's path as client manager lookup key in addition to p4port and rootdir so that we can handle mutiple clients on the same rootdir |
||
#4 | 9250 | dantran | caching client across mutiple commands | ||
#3 | 9184 | dantran | Correctly handle perforce protocols rather then hardwire only 'ssl' | ||
#2 | 9177 | dantran |
Compatible with maven-scm-api-1.5+. The current 1.4 is too old |
||
#1 | 9167 | dantran |
Populate //guest/dantran/p4maven/... from //guest/perforce_software/p4maven/main/.... |
||
//guest/perforce_software/p4maven/main/com.perforce.maven/src/main/java/com/perforce/maven/scm/provider/p4/P4ScmProvider.java | |||||
#2 | 9014 | Matt Attaway |
Add SSL support to P4Maven (committed on behalf of Dan Tran) USer visible change |
||
#1 | 8496 | Matt Attaway | Move P4Ant and P4Maven into their proper homes | ||
//public/perforce/p4maven/main/com.perforce.maven/src/main/java/com/perforce/maven/scm/provider/p4/P4ScmProvider.java | |||||
#1 | 8270 | Matt Attaway | Initial add of P4Maven source code |