View Javadoc

1   package org.apache.maven.perforcelib;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import java.io.IOException;
22  
23  import java.util.Date;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.maven.changelog.AbstractChangeLogGenerator;
28  import org.apache.tools.ant.types.Commandline;
29  
30  
31  /**
32   * A Perforce implementation of the {@link org.apache.maven.changelog.ChangeLog}
33   * interface.
34   *
35   * @author <a href="mailto:jim@crossleys.org">Jim Crossley</a>
36   * @version $Id:
37   */
38  public class PerforceChangeLogGenerator extends AbstractChangeLogGenerator
39  {
40      /** The position after the connection prefix, 'scm:perforce:' */
41      private static final int POS_PARAMS = 13;
42  
43      /** Log */
44      private static final Log LOG =
45          LogFactory.getLog( PerforceChangeLogGenerator.class );
46  
47      /**
48       * Constructs the appropriate command line to execute the perforce
49       * log command whose output is then later parsed.
50       *
51       * Repository connection syntax:
52       * scm:perforce[:host:port]://depot/projects/name/...
53       *
54       * @return the cvs command line to be executed.
55       */
56      protected Commandline getScmLogCommand()
57      {
58          String conn = getConnection();
59  
60          if ( !conn.startsWith( "scm:perforce:" ) )
61          {
62              throw new IllegalArgumentException( "repository connection string"
63                  + " does not specify 'perforce' as the scm" );
64          }
65  
66          int lastColon = conn.lastIndexOf( ':' );
67          String fileSpec = conn.substring( lastColon + 1 );
68          String p4port =
69              ( POS_PARAMS > lastColon ) ? null
70                                         : conn.substring( POS_PARAMS, lastColon );
71  
72          Commandline command = new Commandline();
73  
74          command.setExecutable( "p4" );
75  
76          if ( p4port != null )
77          {
78              command.createArgument().setValue( "-p" );
79              command.createArgument().setValue( p4port );
80          }
81  
82          command.createArgument().setValue( "filelog" );
83          command.createArgument().setValue( "-tl" );
84          command.createArgument().setValue( fileSpec );
85  
86          return command;
87      }
88  
89      /**
90       * The Perforce filelog doesn't support a revision, i.e. date,
91       * range
92       *
93       * @param before The starting point.
94       * @param to The ending point.
95       * @return An empty string
96       */
97      protected String getScmDateArgument( Date before, Date to )
98      {
99          return "";
100     }
101 
102     /**
103      * Handle ChangeLogParser IOExceptions.
104      *
105      * @param ioe The IOException thrown.
106      * @throws IOException If the handler doesn't wish to handle the
107      * exception.
108      */
109     protected void handleParserException( IOException ioe )
110         throws IOException
111     {
112         if ( ( ioe.getMessage().indexOf( "CreateProcess" ) != -1 )
113             || ( ioe.getMessage().indexOf( "p4: not found" ) != -1 ) )
114         {
115             // can't find p4 on Win32 or Linux...
116             LOG.error( "Unable to find p4 executable. Changelog will be empty" );
117         }
118         else
119         {
120             throw ioe;
121         }
122     }
123 
124     /**
125      * @see AbstractChangeLogGenerator#getScmTagArgument(String, String)
126      */
127     protected String getScmTagArgument( String tagStart, String tagEnd )
128     {
129         throw new UnsupportedOperationException( 
130             "This plugin currently does not support generating logs from tags with Perforce." );
131     }
132 }