View Javadoc

1   package org.apache.maven.scm.provider.perforce.command;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.CommandParameters;
23  import org.apache.maven.scm.ScmException;
24  import org.apache.maven.scm.ScmFileSet;
25  import org.apache.maven.scm.ScmResult;
26  import org.apache.maven.scm.command.AbstractCommand;
27  import org.apache.maven.scm.log.ScmLogger;
28  import org.apache.maven.scm.provider.ScmProviderRepository;
29  import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
30  import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
31  import org.codehaus.plexus.util.IOUtil;
32  import org.codehaus.plexus.util.cli.CommandLineException;
33  import org.codehaus.plexus.util.cli.Commandline;
34  
35  import java.io.BufferedReader;
36  import java.io.IOException;
37  import java.io.InputStreamReader;
38  import java.util.HashMap;
39  import java.util.Map;
40  
41  /**
42   * Encapsulates the 'p4 info' command which can be very useful in determining
43   * the runtime environment.  Use <code>getEntry(String key)</code> to query
44   * the info set for a particular property.  The data from p4 info looks like this:
45   * <p/>
46   * <pre>
47   * User name: mperham
48   * Client name: mikeperham-dt
49   * Client host: mikeperham-dt
50   * Client root: d:\perforce
51   * </pre>
52   * <p/>
53   * where the key is the content before the first colon and the value is the data after
54   * the first colon, trimmed.  For example:
55   * <code>PerforceInfoCommand.getInfo( this, repo ).getEntry( "User name" )</code>
56   * <p/>
57   * Note that this is not a traditional SCM command.  This uses the Command class
58   * simply because it needs a logger for error handling and the current repository data for
59   * command line creation.
60   *
61   * @author mperham
62   * @version $Id: $
63   */
64  public class PerforceInfoCommand
65      extends AbstractCommand
66      implements PerforceCommand
67  {
68      private static PerforceInfoCommand singleton = null;
69  
70      private Map<String, String> entries = null;
71  
72      public static PerforceInfoCommand getInfo( ScmLogger logger, PerforceScmProviderRepository repo )
73      {
74          return getSingleton( logger, repo );
75      }
76  
77      public String getEntry( String key )
78      {
79          return (String) entries.get( key );
80      }
81  
82      private static synchronized PerforceInfoCommand getSingleton( ScmLogger logger, PerforceScmProviderRepository repo )
83      {
84          if ( singleton == null )
85          {
86              PerforceInfoCommand pic = new PerforceInfoCommand();
87              if ( logger != null )
88              {
89                  pic.setLogger( logger );
90              }
91              try
92              {
93                  pic.executeCommand( repo, null, null );
94                  singleton = pic;
95              }
96              catch ( ScmException e )
97              {
98                  if ( pic.getLogger().isErrorEnabled() )
99                  {
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 }