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   */
63  public class PerforceInfoCommand
64      extends AbstractCommand
65      implements PerforceCommand
66  {
67      private static PerforceInfoCommand singleton = null;
68  
69      private Map<String, String> entries = null;
70  
71      public static PerforceInfoCommand getInfo( ScmLogger logger, PerforceScmProviderRepository repo )
72      {
73          return getSingleton( logger, repo );
74      }
75  
76      public String getEntry( String key )
77      {
78          return (String) entries.get( key );
79      }
80  
81      private static synchronized PerforceInfoCommand getSingleton( ScmLogger logger, PerforceScmProviderRepository repo )
82      {
83          if ( singleton == null )
84          {
85              PerforceInfoCommand pic = new PerforceInfoCommand();
86              if ( logger != null )
87              {
88                  pic.setLogger( logger );
89              }
90              try
91              {
92                  pic.executeCommand( repo, null, null );
93                  singleton = pic;
94              }
95              catch ( ScmException e )
96              {
97                  if ( pic.getLogger().isErrorEnabled() )
98                  {
99                      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 e )
157         {
158             throw new ScmException( e.getLocalizedMessage() );
159         }
160         catch ( IOException e )
161         {
162             throw new ScmException( e.getLocalizedMessage() );
163         }
164         finally
165         {
166             IOUtil.close( isReader );
167         }
168         return null;
169     }
170 }