587
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
package com.nokia.helium.sbs.plexus;
|
|
18 |
|
628
|
19 |
import java.io.BufferedWriter;
|
587
|
20 |
import java.io.File;
|
|
21 |
import java.io.FileNotFoundException;
|
|
22 |
import java.io.IOException;
|
|
23 |
import java.util.regex.Matcher;
|
|
24 |
import java.util.regex.Pattern;
|
|
25 |
|
|
26 |
import org.apache.log4j.Logger;
|
628
|
27 |
|
587
|
28 |
import com.nokia.helium.core.plexus.FileStreamConsumer;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* Record a stream into a file.
|
|
32 |
*
|
|
33 |
*/
|
|
34 |
public class SBSErrorStreamConsumer extends FileStreamConsumer {
|
|
35 |
private Logger log = Logger.getLogger(SBSErrorStreamConsumer.class);
|
|
36 |
private String errorPattern ;
|
|
37 |
|
|
38 |
/**
|
|
39 |
* Create a FileStreamConsumer which will record content to
|
|
40 |
* the output file.
|
|
41 |
* @param output the file to write the output to.
|
|
42 |
* @param string
|
|
43 |
* @throws FileNotFoundException if an error occur while opening the file.
|
|
44 |
*/
|
|
45 |
public SBSErrorStreamConsumer(File output, String string) throws FileNotFoundException {
|
|
46 |
super(output);
|
|
47 |
this.errorPattern = string;
|
|
48 |
}
|
|
49 |
|
|
50 |
/**
|
|
51 |
* {@inheritDoc}
|
|
52 |
*/
|
|
53 |
@Override
|
|
54 |
public synchronized void consumeLine(String line) {
|
|
55 |
if (this.errorPattern == null) {
|
|
56 |
try {
|
|
57 |
BufferedWriter writer = getWriter();
|
|
58 |
writer.write("Error:" + line);
|
|
59 |
writer.newLine();
|
|
60 |
} catch (IOException e) {
|
|
61 |
log.error("Error while writing to file: " + e.getMessage(), e);
|
|
62 |
}
|
|
63 |
} else {
|
|
64 |
try {
|
|
65 |
Pattern pattern = Pattern.compile(this.errorPattern, Pattern.CASE_INSENSITIVE);
|
|
66 |
Matcher match = pattern.matcher(line);
|
|
67 |
if (match.find()) {
|
|
68 |
BufferedWriter writer = getWriter();
|
|
69 |
writer.write("Error:" + line);
|
|
70 |
writer.newLine();
|
|
71 |
}
|
|
72 |
|
|
73 |
} catch (IOException e) {
|
|
74 |
log.error("Error while writing to file: " + e.getMessage(), e);
|
|
75 |
}
|
|
76 |
}
|
|
77 |
|
|
78 |
}
|
|
79 |
}
|