/
AutoDeployment.java
executable file
·676 lines (585 loc) · 24.8 KB
/
AutoDeployment.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/*
* Copyright (c) 2007, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.ts.lib.implementation.sun.javaee.glassfish;
import org.apache.tools.ant.*;
import java.io.*;
import java.util.*;
import com.sun.ts.lib.porting.*;
import com.sun.ts.lib.util.*;
import com.sun.ts.lib.deliverable.*;
import java.util.jar.JarFile;
import java.util.jar.JarEntry;
import org.jdom.input.SAXBuilder;
import org.jdom.Document;
import org.jdom.Element;
import com.sun.ts.lib.implementation.sun.javaee.RuntimeInfo;
/**
*
* This class implements the TSDeploymentInterface. It does so by copying
* to/from the V3 autodeploy directory and delegates out to ant build files
* under bin/xml/glassfish/deploy.xml
*
* @author Kyle Grucci
*
*/
public class AutoDeployment implements TSDeploymentInterface {
protected static final String DEPLOYED_MODULES_FILE = "ts-deployed-modules";
protected static String TEMP_DIR;
protected static String SERVER_LOG = "server.log";
private static String newLine = System.getProperty("line.separator", "\n"); // used
// for
// log
// messages
protected PrintWriter log; // Log to harness
protected int iPortingSet = 1; // Porting set using this impl
// of TSDeploymentInterface
private String url = "deployer:???:???:999"; // URI for DM
protected Hashtable htJNDIRefs = null;
protected Hashtable htDeployedModules = // Map ear files to IDs so we
new Hashtable(); // can later undeploy
protected PropertyManagerInterface propMgr; // Properties from ts.jte
protected String deployStateFile;
private String sDepNumber = "1";
/*
* 1. Need to handle common apps, we can do this programatically
*
*
*
* /
*
* /**
*
* Static initialization of this class
*
*/
static {
TestUtil.initJavaTest();
TEMP_DIR = System.getProperty("java.io.tmpdir");
if (TEMP_DIR != null && TEMP_DIR.endsWith(File.separator)) {
TEMP_DIR = TEMP_DIR.substring(0, TEMP_DIR.length() - 1);
}
}
/**
*
* Initializes logging output, gets the Deliverable instance
*
* @param writer
* PrintWriter for harness tracing
*
* @return void
*
*/
@Override
public void init(PrintWriter writer) {
this.log = writer;
TestUtil.logHarness("AutoDeployment.init()");
iPortingSet = TSDeployment.iPortingSet;
TestUtil.logHarness("AutoDeployment: Using porting set #" + iPortingSet);
try {
propMgr = DeliverableFactory.getDeliverableInstance()
.getPropertyManager();
String portingString = String.valueOf(iPortingSet);
// initDeployTestUtils(portingString);
String portClass = "porting.ts.deploy2.class." + iPortingSet;
TestUtil.logHarness("Using " + portClass);
} catch (Exception e) {
e.printStackTrace();
TestUtil.logHarness(
"Creation of TSDeployment implementation instance failed."
+ " Please check the values of 'porting.ts.deploy2.class.1' and"
+ " 'porting.ts.deploy2.class.2'");
}
}
private void antSetup(String archive, Project p, String propsNumber) {
String tsHome = propMgr.getProperty("ts.home", "/");
String pkgNameMinusArchive = archive.substring(
archive.indexOf(File.separator + "dist" + File.separator) + 6,
archive.lastIndexOf(File.separator));
// String sBuildFile = tsHome + File.separator + "src" + File.separator +
// pkgNameMinusArchive + File.separator + "build.xml";
String deployHandlerBuildFile;
if (iPortingSet == 1) {
p.setProperty("deploy.dir",
propMgr.getProperty("impl.vi.deploy.dir", ""));
deployHandlerBuildFile = tsHome + File.separator + "bin" + File.separator
+ "xml" + File.separator + "impl" + File.separator
+ propMgr.getProperty("impl.vi", "glassfish") + File.separator
+ "deploy.xml";
} else {
p.setProperty("deploy.dir",
propMgr.getProperty("impl.ri.deploy.dir", ""));
deployHandlerBuildFile = tsHome + File.separator + "bin" + File.separator
+ "xml" + File.separator + "impl" + File.separator
+ propMgr.getProperty("impl.ri", "glassfish") + File.separator
+ "deploy.xml";
}
TestUtil.logHarnessDebug(
"antsetup: deploy.dir = " + p.getProperty("deploy.dir"));
File buildFile = new File(deployHandlerBuildFile);
p.setUserProperty("ant.file", buildFile.getAbsolutePath());
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
p.addReference("ant.projectHelper", helper);
helper.parse(p, buildFile);
DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
p.addBuildListener(consoleLogger);
}
private void antSetupFromInfo(DeploymentInfo info, Project p) {
String archive = info.getEarFile();
sDepNumber = info.getProperty("deployment.props.number");
p.setProperty("archive.file", archive);
antSetup(archive, p, sDepNumber);
}
private void antSetupFromProps(Properties props, Project p) {
String archive = props.getProperty("ear_file");
sDepNumber = props.getProperty("deployment.props.number");
p.setProperty("archive.file", archive);
antSetup(archive, p, sDepNumber);
}
@Override
public String deploy(DeploymentInfo info) throws TSDeploymentException {
// what about interop - do we need to autodeploy directories
// we need to determine if the archives are interop or rebuildable and set
// the
// correct props
Project p = new Project();
antSetupFromInfo(info, p);
String javaeeLevel = propMgr.getProperty("javaee.level", "full");
if (javaeeLevel.contains("full")) {
//Sets file name to be used while generating client stubs
String archiveFile =p.getProperty("archive.file");
String applicationName = this.getAppNameFromApplicationXML(archiveFile);
if (applicationName == null) { // if we didn't have an ear or there was no
// application-name use the old scheme
applicationName = archiveFile.substring(archiveFile.lastIndexOf(File.separator) + 1,
archiveFile.lastIndexOf("."));
}
p.setProperty("deploy.app.name", applicationName);
p.setProperty("get.stub.clients", "true");
String sEarFile = info.getEarFile();
String sTSDeploymentDir = sEarFile.substring(0,
sEarFile.lastIndexOf(File.separator) + 1) + "ts_dep";
p.setProperty("get.stub.clients.ts_dep", sTSDeploymentDir);
}
try {
p.executeTarget("-deploy");
} catch (Throwable t) {
log("Deployment failed for: " + info.getEarFile());
t.printStackTrace();
throw new TSDeploymentException(t.getMessage()
+ System.getProperty("line.separator") + getServerLogContents());
}
return "";
}
@Override
public void undeploy(Properties p) throws TSDeploymentException {
// String sArchive = p.getProperty("ear_file");
TestUtil.logHarness("AutoDeployment.undeploy()");
// initDeployTestUtils(p.getProperty("deployment.props.number"));
Project pr = new Project();
antSetupFromProps(p, pr);
try {
pr.executeTarget("-undeploy");
} catch (Throwable t) {
log("Undeployment failed for: " + p.getProperty("ear_file"));
t.printStackTrace();
throw new TSDeploymentException(t.getMessage()
+ System.getProperty("line.separator") + getServerLogContents());
}
}
@Override
public boolean isDeployed(Properties p) throws TSDeploymentException {
sDepNumber = p.getProperty("deployment.props.number");
String sArchive = p.getProperty("ear_file");
TestUtil.logHarness("AutoDeployment.isDeployed()");
String archiveNameOnly = sArchive
.substring(sArchive.lastIndexOf(File.separator) + 1);
TestUtil.logHarnessDebug("$$$$$$$$$$$$$ " + archiveNameOnly);
String autoDeployDir;
if (archiveNameOnly.startsWith("vi_built_")) {
archiveNameOnly = archiveNameOnly.substring(9);
}
if (iPortingSet == 1) {
autoDeployDir = propMgr.getProperty("impl.vi.deploy.dir",
"UNSET glassfish.deploy.dir");
} else {
autoDeployDir = propMgr.getProperty("impl.ri.deploy.dir",
"UNSET glassfish.deploy.dir");
}
TestUtil.logHarnessDebug("isdeployed: deploy.dir = " + autoDeployDir);
if ((new File(
autoDeployDir + File.separator + archiveNameOnly + "_deployed"))
.exists()) {
TestUtil.logHarnessDebug("isdeployed: returning true");
return true;
} else {
TestUtil.logHarnessDebug("isdeployed: returning false");
return false;
}
}
@Override
public void deployConnector(Properties p) throws TSDeploymentException {
TestUtil.logHarness("AutoDeployment.deployConnector()");
p.setProperty("ear_file", p.getProperty("rar_file"));
Project pr = new Project();
antSetupFromProps(p, pr);
try {
pr.executeTarget("-deploy");
} catch (Throwable t) {
t.printStackTrace();
throw new TSDeploymentException(t.getMessage());
}
}
@Override
public void undeployConnector(Properties p) throws TSDeploymentException {
TestUtil.logHarness("AutoDeployment.undeployConnector()");
p.setProperty("ear_file", p.getProperty("rar_file"));
undeploy(p);
}
@Override
public boolean isConnectorDeployed(Properties p)
throws TSDeploymentException {
TestUtil.logHarness("AutoDeployment.isConnectorDeployed()");
p.setProperty("ear_file", p.getProperty("rar_file"));
return isDeployed(p);
}
@Override
public String getAppClientArgs(Properties p) {
String sAppName = null;
String executeArgs = p.getProperty("executeArgs");
String sApp = p.getProperty("ear_file");
String sClientname = p.getProperty("client_name");
if (sApp.endsWith(".ear")) { // Check for an application-name in the
// application.xml
sAppName = getAppNameFromApplicationXML(sApp);
}
String sTSDeploymentDir = sApp.substring(0,
sApp.lastIndexOf(File.separator) + 1) + "ts_dep";
if (sAppName == null) { // if we didn't have an ear or there was no
// application-name use the old scheme
sAppName = sApp.substring(sApp.lastIndexOf(File.separator) + 1,
sApp.lastIndexOf("."));
}
if (sAppName.startsWith("vi_built_")) {
// strip off the vi_built_ string
sAppName = sAppName.substring(9);
sTSDeploymentDir = sApp.substring(0, sApp.lastIndexOf(File.separator) + 1)
+ "ts_dep_vi_built";
sClientname = sClientname.substring(9);
}
sApp = sTSDeploymentDir + File.separator + sAppName + "Client.jar";
if (executeArgs == null) {
executeArgs = sApp + ",arg=-name,arg=" + sClientname + " -classpath "+ propMgr.getProperty("javaee.home", "/") + "/lib/gf-client.jar" + ":" + sApp + " org.glassfish.appclient.client.AppClientGroupFacade " ; // +
// "
// -stubs
// "
// +
// sClientClasspath;
} else {
executeArgs = sApp + ",arg=-name,arg=" + sClientname + " -classpath "+ propMgr.getProperty("javaee.home", "/") + "/lib/gf-client.jar" + ":" + sApp + " org.glassfish.appclient.client.AppClientGroupFacade "
+ " " + executeArgs; // + " -name " + p.getProperty("client_name"); //
// + " -stubs " + sClientClasspath;
}
// reset the property to nothing so it isn't added to the classpath
// p.put("clientClasspath", "");
return executeArgs;
}
public Hashtable getInteropJNDINames(DeploymentInfo[] infoArray) {
htJNDIRefs = new Hashtable();
StringBuffer buf = null;
if (TestUtil.harnessDebug) {
for (int ii = 0; ii < infoArray.length; ii++) {
buf = new StringBuffer("**** ii=" + ii
+ " ********************************************" + newLine);
buf.append(
"***************************************************************************************"
+ newLine);
buf.append(
"***************************************************************************************"
+ newLine);
buf.append((new RuntimeInfo().getRuntimeInfo(infoArray[ii])) + newLine);
buf.append(
"***************************************************************************************"
+ newLine);
buf.append(
"***************************************************************************************"
+ newLine);
buf.append(
"***************************************************************************************"
+ newLine);
TestUtil.logHarnessDebug(buf.toString());
// 1.3 version of runtime paring code
// Vector resources =
// infoArray[ii].getAppClientResources();
// for(int i = 0; i< resources.size(); i++) {
// DeploymentInfo.AppClient appResource =
// (DeploymentInfo.AppClient)
// resources.elementAt(i);
// Hashtable jndiChanges =
// checkJNDINames(appResource.ejbs);
// if(!jndiChanges.isEmpty()) {
// htJNDIRefs.putAll(jndiChanges);
// }
// }
List resources = infoArray[ii].getAppClientRuntimeDDs();
for (int i = 0; i < resources.size(); i++) {
com.sun.ts.lib.implementation.sun.javaee.runtime.appclient.SunApplicationClient appResource = (com.sun.ts.lib.implementation.sun.javaee.runtime.appclient.SunApplicationClient) resources
.get(i);
Hashtable jndiChanges = checkJNDINames(appResource.getEjbRef());
if (!jndiChanges.isEmpty()) {
htJNDIRefs.putAll(jndiChanges);
}
}
// 1.3 version of runtime paring code
// /* Web Container */
// resources = infoArray[ii].getWebResources();
// for(int i = 0; i< resources.size(); i++) {
// DeploymentInfo.WebResource webResource =
// (DeploymentInfo.WebResource)
// resources.elementAt(i);
// Hashtable jndiChanges =
// checkJNDINames(webResource.ejbs);
// if(!jndiChanges.isEmpty()) {
// htJNDIRefs.putAll(jndiChanges);
// }
// }
resources = infoArray[ii].getWebRuntimeDDs();
for (int i = 0; i < resources.size(); i++) {
com.sun.ts.lib.implementation.sun.javaee.runtime.web.SunWebApp webResource = (com.sun.ts.lib.implementation.sun.javaee.runtime.web.SunWebApp) resources
.get(i);
Hashtable jndiChanges = checkJNDINames(webResource.getEjbRef());
if (!jndiChanges.isEmpty()) {
htJNDIRefs.putAll(jndiChanges);
}
}
// 1.3 version of runtime paring code
// /* EJB Container */
// Vector ejbJars = infoArray[ii].getEjbJars();
// for(int y= 0; y< ejbJars.size(); y++) {
// DeploymentInfo.EJBJar ejbJar =
// (DeploymentInfo.EJBJar)ejbJars.elementAt(y);
// resources = ejbJar.getEjbResources();
// for(int i = 0; i< resources.size(); i++) {
// DeploymentInfo.Ejb ejbResource =
// (DeploymentInfo.Ejb) resources.elementAt(i);
// Hashtable jndiChanges =
// checkJNDINames(ejbResource.ejbs);
// if(!jndiChanges.isEmpty()) {
// htJNDIRefs.putAll(jndiChanges);
// }
// }
// }
// }
List ejbJars = infoArray[ii].getEjbRuntimeDDs();
for (int y = 0; y < ejbJars.size(); y++) {
com.sun.ts.lib.implementation.sun.javaee.runtime.ejb.SunEjbJar ejbJar = (com.sun.ts.lib.implementation.sun.javaee.runtime.ejb.SunEjbJar) ejbJars
.get(y);
resources = ejbJar.getEnterpriseBeans().getEjb();
for (int i = 0; i < resources.size(); i++) {
com.sun.ts.lib.implementation.sun.javaee.runtime.ejb.Ejb ejbResource = (com.sun.ts.lib.implementation.sun.javaee.runtime.ejb.Ejb) resources
.get(i);
Hashtable jndiChanges = checkJNDINames(ejbResource.getEjbRef());
if (!jndiChanges.isEmpty()) {
htJNDIRefs.putAll(jndiChanges);
}
}
}
} // end for loop
} // end if debug
buf = new StringBuffer(
"***** Returning the following Hashtable from SunRIDeployment.getInteropJNDINames *****"
+ newLine);
buf.append(
"***************************************************************************************"
+ newLine);
for (Enumeration e = htJNDIRefs.keys(); e.hasMoreElements();) {
String sKey = (String) e.nextElement();
buf.append("Original value: " + sKey + newLine);
buf.append("Modified value: " + (String) htJNDIRefs.get(sKey) + newLine);
}
buf.append(
"***************************************************************************************"
+ newLine);
TestUtil.logHarnessDebug(buf.toString());
return htJNDIRefs;
}
private void addJNDIName(String jndiName, Hashtable jndiChanges) {
if (jndiName.startsWith("corbaname")) {
int endPos = jndiName.lastIndexOf("#");
// Example JNDI Name:
// String newJNDI = ref.jndi.substring(0, endPos) + "#myfoo/" +
// ref.jndi.substring(endPos + 1);
String newJNDI = jndiName.substring(0, endPos) + "#"
+ jndiName.substring(endPos + 1);
jndiChanges.put(jndiName, newJNDI);
TestUtil.logHarnessDebug("******** old JNDI name \"" + jndiName
+ "\", new JNDI name \"" + newJNDI + "\"");
}
}
private Hashtable checkJNDINames(List ejbRefs) {
Hashtable jndiChanges = new Hashtable();
if (ejbRefs == null || ejbRefs.size() == 0) {
return jndiChanges;
}
Object o = ejbRefs.get(0);
if (o instanceof com.sun.ts.lib.implementation.sun.javaee.runtime.ejb.EjbRef) {
for (int j = 0; j < ejbRefs.size(); j++) {
com.sun.ts.lib.implementation.sun.javaee.runtime.ejb.EjbRef ref = (com.sun.ts.lib.implementation.sun.javaee.runtime.ejb.EjbRef) ejbRefs
.get(j);
String jndiName = ref.getJndiName();
addJNDIName(jndiName, jndiChanges);
}
} else if (o instanceof com.sun.ts.lib.implementation.sun.javaee.runtime.web.EjbRef) {
for (int j = 0; j < ejbRefs.size(); j++) {
com.sun.ts.lib.implementation.sun.javaee.runtime.web.EjbRef ref = (com.sun.ts.lib.implementation.sun.javaee.runtime.web.EjbRef) ejbRefs
.get(j);
String jndiName = ref.getJndiName();
addJNDIName(jndiName, jndiChanges);
}
} else if (o instanceof com.sun.ts.lib.implementation.sun.javaee.runtime.appclient.EjbRef) {
for (int j = 0; j < ejbRefs.size(); j++) {
com.sun.ts.lib.implementation.sun.javaee.runtime.appclient.EjbRef ref = (com.sun.ts.lib.implementation.sun.javaee.runtime.appclient.EjbRef) ejbRefs
.get(j);
String jndiName = ref.getJndiName();
addJNDIName(jndiName, jndiChanges);
}
}
return jndiChanges;
}
private String getAppNameFromApplicationXML(String archivePathAndName) {
/*
* Read the application.xml jar entry and look for the
* /application/application-name element. If it exists return its textual
* content else return null. Allowing users the ability to specify an
* application name is new feature in JavaEE 6.0.
*/
String appName = null;
JarFile jarFile = null;
try {
jarFile = new JarFile(archivePathAndName);
JarEntry appEntry = jarFile.getJarEntry("META-INF/application.xml");
if (appEntry != null) {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(jarFile.getInputStream(appEntry));
Element root = doc.getRootElement();
appName = root.getChildTextNormalize("application-name",
root.getNamespace());
}
} catch (Exception e) {
// if any errors occur just log a message and return null
TestUtil.logHarness(
"Error parsing application.xml looking for application-name, returning null");
} finally {
if (jarFile != null) {
try {
jarFile.close();
} catch (Exception e) {
}
}
}
TestUtil.logHarness(
"getAppNameFromApplicationXML() returning \"" + appName + "\"");
return appName;
}
private void log(String s) {
log.println("GF Deployment: " + s);
}
/**
* Fetch the entire contents of a text file, and return it in a String. This
* style of implementation does not throw Exceptions to the caller.
*
* @param aFile
* is a file which already exists and can be read.
*/
public String getServerLogContents() {
// ...checks on aFile are elided
StringBuilder contents = new StringBuilder();
File serverLog;
String aFile = "";
int logLineCount;
try {
if (iPortingSet == 1) {
serverLog = new File(
new File(propMgr.getProperty("impl.vi.deploy.dir", "")).getParent()
+ File.separator + "logs" + File.separator + SERVER_LOG);
} else {
serverLog = new File(
new File(propMgr.getProperty("impl.ri.deploy.dir", "")).getParent()
+ File.separator + "logs" + File.separator + SERVER_LOG);
}
logLineCount = countLines(serverLog);
contents.append("************************************************");
contents.append(System.getProperty("line.separator"));
contents.append("GLASSFISH SERVER LOG CONTENTS: Last 200 lines");
contents.append(serverLog.getPath());
contents.append(System.getProperty("line.separator"));
contents.append("************************************************");
contents.append(System.getProperty("line.separator"));
// use buffering, reading one line at a time
// FileReader always assumes default encoding is OK!
BufferedReader input = new BufferedReader(new FileReader(serverLog));
try {
String line = null; // not declared within while loop
/*
* readLine is a bit quirky : it returns the content of a line MINUS the
* newline. it returns null only for the END of the stream. it returns
* an empty String if two newlines appear in a row.
*/
int iCurrentLine = 1;
System.err.println("line count = " + logLineCount);
// only print the last 200 lines
// start at linecount -(linecount-200)
while ((line = input.readLine()) != null) {
if (iCurrentLine > logLineCount - 200) {
// log.println(line);
contents.append(iCurrentLine + ": " + line);
contents.append(System.getProperty("line.separator"));
}
iCurrentLine++;
}
contents.append("************************************************");
contents.append(System.getProperty("line.separator"));
contents.append("END OF GLASSFISH SERVER LOG CONTENTS");
contents.append(serverLog.getPath());
contents.append(System.getProperty("line.separator"));
contents.append("************************************************");
contents.append(System.getProperty("line.separator"));
} finally {
input.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
return contents.toString();
}
private int countLines(File file) throws IOException {
Reader reader = new InputStreamReader(new FileInputStream(file));
int lineCount = 0;
char[] buffer = new char[4096];
for (int charsRead = reader.read(buffer); charsRead >= 0; charsRead = reader
.read(buffer)) {
for (int charIndex = 0; charIndex < charsRead; charIndex++) {
if (buffer[charIndex] == '\n')
lineCount++;
}
}
reader.close();
return lineCount;
}
}