-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJPerforce.java
268 lines (239 loc) · 7.79 KB
/
JPerforce.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import java.util.*;
import java.util.logging.*;
import com.perforce.api.*;
import java.io.*;
import java.lang.*;
import java.text.*;
public class JPerforce
{
Properties prp = new Properties();
Env env;
private static String logFile = "BOForce.log";
private final static DateFormat df = new SimpleDateFormat ("yyyy.mm.dd hh:mm:ss ");
public FileHandler handler;
// Add to the desired logger
Logger logger;
public JPerforce(String prpFile)
{
try
{
handler = new FileHandler(logFile, true);
logger = Logger.getLogger("BOForce");
logger.addHandler(handler);
this.prp.load(new BufferedInputStream(new FileInputStream(prpFile)));
env = new Env(prp);
}
catch(IOException ioEx)
{
//log error
//System.out.println(ioEx.getMessage());
logger.severe(JPerforce.df.format(new Date()) + " " + ioEx.getMessage());
return;
}
catch(Exception ex)
{
//log error
//System.out.println(ex.getMessage());
logger.severe(JPerforce.df.format(new Date()) + " " + ex.getMessage());
return;
}
}
public boolean p4CheckIn(String filename)
{
String l,location, comments,user ;
P4Process p = null;
location = prp.getProperty("p4.Location");
//filename = prp.getProperty("p4.CheckInFile");
comments = prp.getProperty("p4.CheckInComments");
user = prp.getProperty("p4.user");
if (location==null)
{
//System.out.println("Location argument cannot be null. Please check p4.properties file.");
logger.severe(JPerforce.df.format(new Date()) + " " + "Location argument cannot be null. Please check p4.properties file.");
return false;
}
if (filename==null)
{
//System.out.println("No file to check in.");
logger.severe(JPerforce.df.format(new Date()) + " " + "No file to check in.");
return false;
}
if (comments==null)
{
//System.out.println("Comments argument cannot be null. Please check p4.properties file.");
logger.severe(JPerforce.df.format(new Date()) + " " + "Comments argument cannot be null. Please check p4.properties file.");
return false;
}
//CHECK IN command
String[] cmd = { "p4", "add", "-f", location + filename};
String[] cmd1 = { "p4", "submit","-d",comments, location + filename};
String changeno = null;
try
{
env.checkValidity();
p = new P4Process(env);
p.exec(cmd);
while (null != (l = p.readLine()))
{
if (l.startsWith("Client 'robot-client'"))
{
//System.out.println("Client argument cannot be null. Please check p4.properties file.");
logger.severe(JPerforce.df.format(new Date()) + " " + "Client argument cannot be null. Please check p4.properties file.");
return false;
}
if (l.startsWith("Access for user 'robot'"))
{
//System.out.println("User argument cannot be null. Please check p4.properties file.");
logger.severe(JPerforce.df.format(new Date()) + " " + "User argument cannot be null. Please check p4.properties file.");
return false;
}
// Parse the output.
//System.out.println(l);
logger.info(JPerforce.df.format(new Date()) + " " + l);
}
p.flush();
//p.close();
Thread.currentThread().sleep(3000);
//p1 = new P4Process(env);
p.exec(cmd1);
String l1 = l;
while (null != (l = p.readLine()))
{
l1 = l;
if (l.startsWith("Submitting"))
{
String[] temp = null;
temp = l.split(" ");
changeno = temp[2].substring(0,temp[2].length()-1);
}
//System.out.println(l);
logger.info(JPerforce.df.format(new Date()) + " " + l);
}
//p1.close();
if (!l1.endsWith("submitted."))
{
String[] cmd2 = { "p4", "revert", location + filename};
Thread.currentThread().sleep(3000);
//p2 = new P4Process(env);
p.exec(cmd2);
String[] cmd3 = { "p4", "change","-d",changeno};
//p3 = new P4Process(env);
Thread.currentThread().sleep(3000);
p.exec(cmd3);
p.close();
return false;
}
else
{
p.close();
return true;
}
}
catch (PerforceException pex)
{
//log
if (pex.getMessage().startsWith("No output"))
{
//System.out.println("Either port argument or sysroot argument is invalid.Please check p4.properties file");
logger.severe(JPerforce.df.format(new Date()) + " " + "Either port argument or sysroot argument is invalid.Please check p4.properties file.");
}
else
{
//System.out.println(pex.getMessage());
logger.severe(JPerforce.df.format(new Date()) + " " + pex.getMessage());
}
return false;
}
catch(IOException ioEx)
{
//System.out.println(ioEx.getMessage());
logger.severe(JPerforce.df.format(new Date()) + " " + ioEx.getMessage());
return false;
}
catch(Exception ex)
{
//System.out.println(ex.getMessage());
logger.severe(JPerforce.df.format(new Date()) + " " + ex.getMessage());
return false;
}
}
//make sure the System.out lines are replaced in the methods.
public boolean p4CheckOut(StringBuffer FileName)
{
String l,location, filename, workspaceloc, client,s ;
P4Process p = null;
location = prp.getProperty("p4.Location");
filename = prp.getProperty("p4.CheckOutFile");
workspaceloc = prp.getProperty("p4.WorkspaceLocation");
client = prp.getProperty("p4.client");
if (location==null)
{
//System.out.println("Location argument cannot be null. Please check p4.properties file.");
logger.severe(JPerforce.df.format(new Date()) + " " + "Location argument cannot be null. Please check p4.properties file.");
return false;
}
if (filename==null)
{
//System.out.println("Filename argument cannot be null. Please check p4.properties file.");
logger.severe(JPerforce.df.format(new Date()) + " " + "Filename argument cannot be null. Please check p4.properties file.");
return false;
}
if (workspaceloc==null)
{
//System.out.println("Workspace location is the client workspace root and is required argument. It cannot be null. Please check p4.properties file.");
logger.severe(JPerforce.df.format(new Date()) + " " + "Workspace location is the client workspace root and is required argument. It cannot be null. Please check p4.properties file.");
return false;
}
s = location;
s =location.replace("//" + client , workspaceloc );
FileName.append(s.replace("/" ,"\\"));
FileName.append(filename);
try
{
//CHECK OUT
env.checkValidity();
String[] cmd = { "p4", "edit", location + filename};
p = new P4Process(env);
p.exec(cmd);
while (null != (l = p.readLine()))
{
if (l.startsWith("Client 'robot-client'"))
{
//System.out.println("Client argument cannot be null. Please check p4.properties file.");
logger.severe(JPerforce.df.format(new Date()) + " " + "Client argument cannot be null. Please check p4.properties file.");
return false;
}
if (l.startsWith("Access for user 'robot'"))
{
//System.out.println("User argument cannot be null. Please check p4.properties file.");
logger.severe(JPerforce.df.format(new Date()) + " " + "User argument cannot be null. Please check p4.properties file.");
return false;
}
// Parse the output.
// System.out.println(l);
logger.info(JPerforce.df.format(new Date()) + " " + l);
if (l.endsWith("opened for edit"))
{
p.close();
return true;
}
}
p.close();
return false;
}
catch (PerforceException pex)
{
//log
//System.out.println("P4:");
//System.out.println(pex.getMessage());
logger.severe(JPerforce.df.format(new Date()) + " " + pex.getMessage());
return false;
}
catch(IOException ioEx)
{
//System.out.println(ioEx.getMessage());
logger.severe(JPerforce.df.format(new Date()) + " " + ioEx.getMessage());
return false;
}
}
}