001    package 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    
022    import org.apache.maven.scm.CommandParameters;
023    import org.apache.maven.scm.ScmException;
024    import org.apache.maven.scm.ScmFileSet;
025    import org.apache.maven.scm.ScmResult;
026    import org.apache.maven.scm.command.AbstractCommand;
027    import org.apache.maven.scm.log.ScmLogger;
028    import org.apache.maven.scm.provider.ScmProviderRepository;
029    import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
030    import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
031    import org.codehaus.plexus.util.IOUtil;
032    import org.codehaus.plexus.util.cli.CommandLineException;
033    import org.codehaus.plexus.util.cli.Commandline;
034    
035    import java.io.BufferedReader;
036    import java.io.IOException;
037    import java.io.InputStreamReader;
038    import java.util.HashMap;
039    import 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     * @version $Id: $
063     */
064    public class PerforceInfoCommand
065        extends AbstractCommand
066        implements PerforceCommand
067    {
068        private static PerforceInfoCommand singleton = null;
069    
070        private Map<String, String> entries = null;
071    
072        public static PerforceInfoCommand getInfo( ScmLogger logger, PerforceScmProviderRepository repo )
073        {
074            return getSingleton( logger, repo );
075        }
076    
077        public String getEntry( String key )
078        {
079            return (String) entries.get( key );
080        }
081    
082        private static synchronized PerforceInfoCommand getSingleton( ScmLogger logger, PerforceScmProviderRepository repo )
083        {
084            if ( singleton == null )
085            {
086                PerforceInfoCommand pic = new PerforceInfoCommand();
087                if ( logger != null )
088                {
089                    pic.setLogger( logger );
090                }
091                try
092                {
093                    pic.executeCommand( repo, null, null );
094                    singleton = pic;
095                }
096                catch ( ScmException e )
097                {
098                    if ( pic.getLogger().isErrorEnabled() )
099                    {
100                        pic.getLogger().error( "ScmException " + e.getMessage(), e );
101                    }
102                }
103            }
104    
105            return singleton;
106        }
107    
108        /**
109         * {@inheritDoc}
110         */
111        protected ScmResult executeCommand( ScmProviderRepository repo, ScmFileSet scmFileSet,
112                                            CommandParameters commandParameters )
113            throws ScmException
114        {
115            if ( !PerforceScmProvider.isLive() )
116            {
117                return null;
118            }
119            InputStreamReader isReader = null;
120            try
121            {
122                Commandline command = PerforceScmProvider.createP4Command( (PerforceScmProviderRepository) repo, null );
123                command.createArg().setValue( "info" );
124                if ( getLogger().isDebugEnabled() )
125                {
126                    getLogger().debug( PerforceScmProvider.clean( "Executing: " + command.toString() ) );
127                }
128                Process proc = command.execute();
129                isReader = new InputStreamReader( proc.getInputStream() );
130                BufferedReader br = new BufferedReader( isReader );
131                String line;
132                entries = new HashMap<String, String>();
133                while ( ( line = br.readLine() ) != null )
134                {
135                    int idx = line.indexOf( ':' );
136                    if ( idx == -1 )
137                    {
138                        if ( line.indexOf( "Client unknown." ) == -1 )
139                        {
140                            throw new IllegalStateException( "Unexpected results from 'p4 info' command: " + line );
141                        }
142    
143                        if ( getLogger().isDebugEnabled() )
144                        {
145                            getLogger().debug( "Cannot find client." );
146                        }
147                        entries.put( "Client root", "" );
148                    }
149                    else
150                    {
151                        String key = line.substring( 0, idx );
152                        String value = line.substring( idx + 1 ).trim();
153                        entries.put( key, value );
154                    }
155                }
156            }
157            catch ( CommandLineException e )
158            {
159                throw new ScmException( e.getLocalizedMessage() );
160            }
161            catch ( IOException e )
162            {
163                throw new ScmException( e.getLocalizedMessage() );
164            }
165            finally
166            {
167                IOUtil.close( isReader );
168            }
169            return null;
170        }
171    }