001package org.apache.maven.scm.provider.perforce.command;
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.AbstractCommand;
027import org.apache.maven.scm.log.ScmLogger;
028import org.apache.maven.scm.provider.ScmProviderRepository;
029import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
030import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
031import org.codehaus.plexus.util.IOUtil;
032import org.codehaus.plexus.util.cli.CommandLineException;
033import org.codehaus.plexus.util.cli.Commandline;
034
035import java.io.BufferedReader;
036import java.io.IOException;
037import java.io.InputStreamReader;
038import java.util.HashMap;
039import java.util.Map;
040
041/**
042 * Encapsulates the 'p4 info' command which can be very useful in determining
043 * the runtime environment.  Use <code>getEntry(String key)</code> to query
044 * the info set for a particular property.  The data from p4 info looks like this:
045 * <p>
046 * <pre>
047 * User name: mperham
048 * Client name: mikeperham-dt
049 * Client host: mikeperham-dt
050 * Client root: d:\perforce
051 * </pre>
052 * <p>
053 * where the key is the content before the first colon and the value is the data after
054 * the first colon, trimmed.  For example:
055 * <code>PerforceInfoCommand.getInfo( this, repo ).getEntry( "User name" )</code>
056 * <p>
057 * Note that this is not a traditional SCM command.  This uses the Command class
058 * simply because it needs a logger for error handling and the current repository data for
059 * command line creation.
060 *
061 * @author mperham
062 */
063public class PerforceInfoCommand
064    extends AbstractCommand
065    implements PerforceCommand
066{
067    private static PerforceInfoCommand singleton = null;
068
069    private Map<String, String> entries = null;
070
071    public static PerforceInfoCommand getInfo( ScmLogger logger, PerforceScmProviderRepository repo )
072    {
073        return getSingleton( logger, repo );
074    }
075
076    public String getEntry( String key )
077    {
078        return (String) entries.get( key );
079    }
080
081    private static synchronized PerforceInfoCommand getSingleton( ScmLogger logger, PerforceScmProviderRepository repo )
082    {
083        if ( singleton == null )
084        {
085            PerforceInfoCommand pic = new PerforceInfoCommand();
086            if ( logger != null )
087            {
088                pic.setLogger( logger );
089            }
090            try
091            {
092                pic.executeCommand( repo, null, null );
093                singleton = pic;
094            }
095            catch ( ScmException e )
096            {
097                if ( pic.getLogger().isErrorEnabled() )
098                {
099                    pic.getLogger().error( "ScmException " + e.getMessage(), e );
100                }
101            }
102        }
103
104        return singleton;
105    }
106
107    /**
108     * {@inheritDoc}
109     */
110    protected ScmResult executeCommand( ScmProviderRepository repo, ScmFileSet scmFileSet,
111                                        CommandParameters commandParameters )
112        throws ScmException
113    {
114        if ( !PerforceScmProvider.isLive() )
115        {
116            return null;
117        }
118        InputStreamReader isReader = null;
119        try
120        {
121            Commandline command = PerforceScmProvider.createP4Command( (PerforceScmProviderRepository) repo, null );
122            command.createArg().setValue( "info" );
123            if ( getLogger().isDebugEnabled() )
124            {
125                getLogger().debug( PerforceScmProvider.clean( "Executing: " + command.toString() ) );
126            }
127            Process proc = command.execute();
128            isReader = new InputStreamReader( proc.getInputStream() );
129            BufferedReader br = new BufferedReader( isReader );
130            String line;
131            entries = new HashMap<String, String>();
132            while ( ( line = br.readLine() ) != null )
133            {
134                int idx = line.indexOf( ':' );
135                if ( idx == -1 )
136                {
137                    if ( line.indexOf( "Client unknown." ) == -1 )
138                    {
139                        throw new IllegalStateException( "Unexpected results from 'p4 info' command: " + line );
140                    }
141
142                    if ( getLogger().isDebugEnabled() )
143                    {
144                        getLogger().debug( "Cannot find client." );
145                    }
146                    entries.put( "Client root", "" );
147                }
148                else
149                {
150                    String key = line.substring( 0, idx );
151                    String value = line.substring( idx + 1 ).trim();
152                    entries.put( key, value );
153                }
154            }
155        }
156        catch ( CommandLineException | IOException e )
157        {
158            throw new ScmException( e.getLocalizedMessage() );
159        }
160        finally
161        {
162            IOUtil.close( isReader );
163        }
164        return null;
165    }
166}