001package org.apache.maven.scm.provider.accurev.command.login;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.scm.CommandParameters;
023import org.apache.maven.scm.ScmException;
024import org.apache.maven.scm.ScmFileSet;
025import org.apache.maven.scm.ScmResult;
026import org.apache.maven.scm.command.login.LoginScmResult;
027import org.apache.maven.scm.log.ScmLogger;
028import org.apache.maven.scm.provider.ScmProviderRepository;
029import org.apache.maven.scm.provider.accurev.AccuRev;
030import org.apache.maven.scm.provider.accurev.AccuRevException;
031import org.apache.maven.scm.provider.accurev.AccuRevInfo;
032import org.apache.maven.scm.provider.accurev.AccuRevScmProviderRepository;
033import org.apache.maven.scm.provider.accurev.command.AbstractAccuRevCommand;
034
035/**
036 * 
037 */
038public class AccuRevLoginCommand
039    extends AbstractAccuRevCommand
040{
041
042    public AccuRevLoginCommand( ScmLogger logger )
043    {
044        super( logger );
045    }
046
047    @Override
048    protected ScmResult executeAccurevCommand( AccuRevScmProviderRepository repository, ScmFileSet fileSet,
049                                               CommandParameters parameters )
050        throws ScmException, AccuRevException
051    {
052        boolean result = false;
053        AccuRev accurev = repository.getAccuRev();
054        AccuRevInfo info = accurev.info( null );
055
056        String providerMessage = "";
057        if ( info == null )
058        {
059            providerMessage = "Unable to retrieve accurev info";
060        }
061        else if ( repository.getUser() != null )
062        {
063            // Check if we've already logged in as this user
064            result = repository.getUser().equals( info.getUser() );
065            if ( result )
066            {
067                providerMessage = "Skipping login - already logged in as " + repository.getUser();
068            }
069            else
070            {
071                result = accurev.login( repository.getUser(), repository.getPassword() );
072                providerMessage = ( result ? "Success" : "Failure" ) + " logging in as " + repository.getUser();
073            }
074        }
075        else
076        {
077            result = info.isLoggedIn();
078            providerMessage = result ? ( "Logged in externally as " + info.getUser() ) : "Not logged in";
079        }
080
081        getLogger().debug( providerMessage );
082        return new LoginScmResult( accurev.getCommandLines(), providerMessage, accurev.getErrorOutput(), result );
083
084    }
085
086    public LoginScmResult login( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
087        throws ScmException
088    {
089        return (LoginScmResult) execute( repository, fileSet, parameters );
090    }
091
092}