KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > perforce > P4Add


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 /*
19  * Portions of this software are based upon public domain software
20  * originally written at the National Center for Supercomputing Applications,
21  * University of Illinois, Urbana-Champaign.
22  */

23
24 package org.apache.tools.ant.taskdefs.optional.perforce;
25
26 import java.io.File JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import org.apache.tools.ant.Project;
30 import org.apache.tools.ant.BuildException;
31 import org.apache.tools.ant.DirectoryScanner;
32 import org.apache.tools.ant.types.FileSet;
33
34 /**
35  * Adds specified files to Perforce.
36  *
37  * <b>Example Usage:</b>
38  * <table border="1">
39  * <th>Function</th><th>Command</th>
40  * <tr><td>Add files using P4USER, P4PORT and P4CLIENT settings specified</td>
41  * <td>&lt;P4add <br>P4view="//projects/foo/main/source/..." <br>P4User="fbloggs"
42  * <br>P4Port="km01:1666"
43  * <br>P4Client="fbloggsclient"&gt;<br>&lt;fileset basedir="dir" includes="**&#47;*.java"&gt;<br>
44  * &lt;/p4add&gt;</td></tr>
45  * <tr><td>Add files using P4USER, P4PORT and P4CLIENT settings defined in environment</td><td>
46  * &lt;P4add P4view="//projects/foo/main/source/..." /&gt;<br>&lt;fileset basedir="dir"
47  * includes="**&#47;*.java"&gt;<br>&lt;/p4add&gt;</td></tr>
48  * <tr><td>Specify the length of command line arguments to pass to each invocation of p4</td>
49  * <td>&lt;p4add Commandlength="450"&gt;</td></tr>
50  * </table>
51  *
52  * @ant.task category="scm"
53  */

54 public class P4Add extends P4Base {
55     private static final int DEFAULT_CMD_LENGTH = 450;
56     private int changelist;
57     private String JavaDoc addCmd = "";
58     private Vector JavaDoc filesets = new Vector JavaDoc();
59     private int cmdLength = DEFAULT_CMD_LENGTH;
60
61     /**
62      * Set the maximum length
63      * of the commandline when calling Perforce to add the files.
64      * Defaults to 450, higher values mean faster execution,
65      * but also possible failures.
66      * @param len maximum length of command line default is 450.
67      * @throws BuildException if trying to set the command line length to 0 or less.
68      */

69
70     public void setCommandlength(int len) throws BuildException {
71         if (len <= 0) {
72             throw new BuildException("P4Add: Commandlength should be a positive number");
73         }
74         this.cmdLength = len;
75     }
76
77     /**
78      * If specified the open files are associated with the
79      * specified pending changelist number; otherwise the open files are
80      * associated with the default changelist.
81      *
82      * @param changelist the change list number.
83      *
84      * @throws BuildException if trying to set a change list number &lt;=0.
85      */

86     public void setChangelist(int changelist) throws BuildException {
87         if (changelist <= 0) {
88             throw new BuildException("P4Add: Changelist# should be a positive number");
89         }
90         this.changelist = changelist;
91     }
92
93     /**
94      * Add a fileset whose files will be added to Perforce.
95      *
96      * @param set the FileSet that one wants to add to Perforce Source Control.
97      */

98     public void addFileset(FileSet set) {
99         filesets.addElement(set);
100     }
101
102     /**
103      * Run the task.
104      *
105      * @throws BuildException if the execution of the Perforce command fails.
106      */

107     public void execute() throws BuildException {
108         if (P4View != null) {
109             addCmd = P4View;
110         }
111         P4CmdOpts = (changelist > 0) ? ("-c " + changelist) : "";
112
113         StringBuffer JavaDoc filelist = new StringBuffer JavaDoc();
114
115         for (int i = 0; i < filesets.size(); i++) {
116             FileSet fs = (FileSet) filesets.elementAt(i);
117             DirectoryScanner ds = fs.getDirectoryScanner(getProject());
118
119             String JavaDoc[] srcFiles = ds.getIncludedFiles();
120             if (srcFiles != null) {
121                 for (int j = 0; j < srcFiles.length; j++) {
122                     File JavaDoc f = new File JavaDoc(ds.getBasedir(), srcFiles[j]);
123                     filelist.append(" ").append('"').append(f.getAbsolutePath()).append('"');
124                     if (filelist.length() > cmdLength) {
125                         execP4Add(filelist);
126                         filelist = new StringBuffer JavaDoc();
127                     }
128                 }
129                 if (filelist.length() > 0) {
130                     execP4Add(filelist);
131                 }
132             } else {
133                 log("No files specified to add!", Project.MSG_WARN);
134             }
135         }
136     }
137
138     private void execP4Add(StringBuffer JavaDoc list) {
139         log("Execing add " + P4CmdOpts + " " + addCmd + list, Project.MSG_INFO);
140         execP4Command("-s add " + P4CmdOpts + " " + addCmd + list, new SimpleP4OutputHandler(this));
141     }
142 }
143
Popular Tags