use of com.cosylab.logging.engine.log.LogTypeHelper in project ACS by ACS-Community.
the class Filter method buildFilter.
/**
* Build a Filter object All the parameters are String objects. Before
* building the object, the value of each parameter is checked This method
* is too long (and boring) for my taste but it is very easy
*
* @param type The type of filter to build
* @param field The filed parameter fo Filter
* @param lethal The isLethal parameter of Filter
* @param not The applyAsNOT parameter of Filter
* @param min The minimum parameter of Filter
* @param minType The type of minimum
* @param max The max parameter of Filter
* @param maxType The type of max
* @param exact The exact parameter of Filter
* @param exactType The type of exact
* @param wildChar The regularExpression parameter of Filter
* @return The Filter object built or null if an error occurred decoding the
* parameters
* @throws Exception in case of error building the filter
*/
public static Filter buildFilter(Constraint type, LogField field, String lethal, String not, String min, String minType, String max, String maxType, String exact, String exactType, String wildChar) throws Exception {
// Trim all the strings
if (lethal != null) {
lethal = lethal.trim();
}
if (not != null) {
not = not.trim();
}
if (min != null) {
min = min.trim();
}
if (minType != null) {
minType = minType.trim();
}
if (max != null) {
max = max.trim();
}
if (maxType != null) {
maxType = maxType.trim();
}
if (exact != null) {
exact = exact.trim();
}
if (exactType != null) {
exactType = exactType.trim();
}
if (wildChar != null) {
wildChar = wildChar.trim();
}
// Read the int from field
if (field == null) {
throw new IllegalArgumentException("Parameter field can't be null");
}
// Translate lethal into boolean
int temp;
if (lethal == null) {
throw new IllegalArgumentException("Parameter lethal can't be null");
}
temp = Integer.parseInt(lethal);
boolean isLethal = (temp == 1);
// Translate not into boolean
if (not == null) {
throw new IllegalArgumentException("Parameter not can't be null");
}
temp = Integer.parseInt(not);
boolean notPolicy = (temp == 1);
// If wildChar is defined then min, max and exact should not
if (wildChar != null && (min != null || max != null || exact != null)) {
throw new IllegalArgumentException("Ambiguous parameters: wildChar, min, max, exact");
}
// exat must be not null
if (wildChar == null && min == null && max == null && exact == null) {
throw new IllegalArgumentException("Ambiguous null params: wildChar, min, max, exact");
}
// If exact is defined then min and max should not
if (exact != null && (min != null || max != null)) {
throw new IllegalArgumentException("Ambiguous parameters: exact, min, max");
}
// For min, max and exact the type must be specified
if (exact != null && exactType == null) {
throw new IllegalArgumentException("Exact parameter can't be null");
}
if (min != null && minType == null) {
throw new IllegalArgumentException("Min - minType parameters wrong");
}
if (max != null && maxType == null) {
throw new IllegalArgumentException("Max - maxType parameters wrong");
}
// If both min and max are specified they must have the same type
if (minType != null && maxType != null) {
if (minType.compareTo(maxType) != 0) {
throw new IllegalArgumentException("minType- maxType mismatch");
}
}
//
if (type == Constraint.STRING_WILDCHAR) {
return new RegExpFilter(field, isLethal, wildChar, notPolicy);
} else if (type == Constraint.EXACT) {
if (exact != null && !exact.isEmpty() && exactType != null && !exactType.isEmpty()) {
if (exactType.equals(Integer.class.getName())) {
Integer integer;
try {
integer = new Integer(exact);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Wrong int parameter " + exact);
}
return new ExactFilter(field, isLethal, integer, notPolicy);
} else if (exactType.equals(Long.class.getName())) {
Long date = null;
try {
date = Long.decode(exact);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Wrong date parameter " + exact);
}
return new ExactFilter(field, isLethal, date, notPolicy);
} else if (exactType.equals(String.class.getName())) {
return new ExactFilter(field, isLethal, exact, notPolicy);
} else if (exactType.equals(LogTypeHelper.class.getName())) {
LogTypeHelper logType = LogTypeHelper.fromLogTypeDescription(exact);
return new ExactFilter(field, isLethal, logType, notPolicy);
} else {
// Unrecognized type
throw new IllegalArgumentException("Unrecognized type " + exactType);
}
} else {
throw new IllegalArgumentException("Value and type can't be null/empty to build a ExactFilter");
}
} else if (type == Constraint.MINMAX || type == Constraint.MINIMUM || type == Constraint.MAXIMUM) {
if (minType == null) {
minType = maxType;
} else {
maxType = minType;
}
if (minType.equals(String.class.getName())) {
return new MinMaxFilter(field, isLethal, min, max, notPolicy);
} else if (minType.equals(Long.class.getName())) {
Long minDate = null;
Long maxDate = null;
try {
if (min != null) {
minDate = Long.decode(min);
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Wrong min date parameter " + min);
}
try {
if (max != null) {
maxDate = Long.decode(max);
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Wrong max date parameter " + max);
}
return new MinMaxFilter(field, isLethal, minDate, maxDate, notPolicy);
} else if (minType.equals(Integer.class.getName())) {
Integer minInt = null;
Integer maxInt = null;
try {
if (min != null) {
minInt = new Integer(min);
}
if (max != null) {
maxInt = new Integer(max);
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid min/max " + min + "/" + max);
}
return new MinMaxFilter(field, isLethal, minInt, maxInt, notPolicy);
} else if (minType.equals(LogTypeHelper.class.getName())) {
LogTypeHelper minLogType = null;
LogTypeHelper maxLogType = null;
try {
if (min != null) {
minLogType = LogTypeHelper.fromLogTypeDescription(min);
}
if (max != null) {
maxLogType = LogTypeHelper.fromLogTypeDescription(max);
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid min/max " + min + "/" + max);
}
return new MinMaxFilter(field, isLethal, minLogType, maxLogType, notPolicy);
} else {
throw new IllegalArgumentException("Min/Max values/type can't be null/empty to build a MinMaxFilter");
}
} else {
throw new Exception("Error building a filter");
}
}
use of com.cosylab.logging.engine.log.LogTypeHelper in project ACS by ACS-Community.
the class ACSLogParserVTD method makeLogEntryFromParsedXML.
/**
* Creates a LogEntry from raw XML, using a VTD XML parser.
*
* @param xmlString the XML string that is being parsed.
* @param bytesArray the array of bytes (also containing the xml string that we are parsing, in byte form)
* to be used by VTD.
* @param vtdGen the instance of VTDGen to use for parsing the XML
* @return A LogEntry populated with the data for the log entry contained in the XML string passed in.
* @throws LogParseException if the parsing fails
*/
private LogEntry makeLogEntryFromParsedXML(byte[] bytesArray, String xmlString) throws LogParseException {
// TODO: this method, though relatively simple, is a bit long; consider making it shorter
LogEntry retVal = null;
Object vtdNav;
try {
vtdNav = VTDGen_getNav.invoke(vtdGen, nullObj);
if (// to root element
(Boolean) VTDNav_toElement.invoke(vtdNav, NAV_ROOT)) {
if ((Boolean) VTDNav_matchElement.invoke(vtdNav, ILogEntry.LOG_ELEMENT_TAG_NAME)) {
// navigate to child
if ((Boolean) VTDNav_toElement.invoke(vtdNav, NAV_FIRST_CHILD)) {
if ((Boolean) VTDNav_matchElement.invoke(vtdNav, ILogEntry.HEADER_ELEMENT_TAG_NAME)) {
// navigate to sibling
VTDNav_toElement.invoke(vtdNav, NAV_NEXT_SIBLING);
}
}
}
if ((Boolean) VTDNav_matchElement.invoke(vtdNav, ILogEntry.HEADER_ELEMENT_TAG_NAME)) {
// navigate to sibling
VTDNav_toElement.invoke(vtdNav, NAV_NEXT_SIBLING);
}
Long milliseconds = null;
Integer line = null;
Integer priority = null;
Integer stackLevel = null;
String fileName = null;
String routineName = null;
String hostName = null;
String processName = null;
String contextName = null;
String threadName = null;
String logId = null;
String uri = null;
String stackId = null;
String logMessage = null;
String srcObjectName = null;
String audience = null;
String array = null;
String antenna = null;
ByteArrayOutputStream os = new ByteArrayOutputStream();
LogTypeHelper entryType = determineEntryType(vtdNav);
// test for timestamp attribute
if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.TIMESTAMP.getTagAttribute())) {
milliseconds = getLongFromTimestamp(vtdNav, os, LogField.TIMESTAMP.getTagAttribute(), bytesArray);
}
// test for File attribute
if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.FILE.getTagAttribute())) {
fileName = this.getString(vtdNav, os, LogField.FILE.getTagAttribute(), bytesArray);
}
// test for Line attribute
if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.LINE.getTagAttribute())) {
line = getInteger(vtdNav, os, LogField.LINE.getTagAttribute(), bytesArray);
}
// test for Routine attribute
if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.ROUTINE.getTagAttribute())) {
routineName = this.getString(vtdNav, os, LogField.ROUTINE.getTagAttribute(), bytesArray);
}
// test for host attribute
if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.HOST.getTagAttribute())) {
hostName = this.getString(vtdNav, os, LogField.HOST.getTagAttribute(), bytesArray);
}
// test for process attribute
if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.PROCESS.getTagAttribute())) {
processName = this.getString(vtdNav, os, LogField.PROCESS.getTagAttribute(), bytesArray);
}
// test for context attribute
if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.CONTEXT.getTagAttribute())) {
contextName = this.getString(vtdNav, os, LogField.CONTEXT.getTagAttribute(), bytesArray);
}
// test for thread attribute
if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.THREAD.getTagAttribute())) {
threadName = this.getString(vtdNav, os, LogField.THREAD.getTagAttribute(), bytesArray);
}
// test for logid attribute
if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.LOGID.getTagAttribute())) {
logId = this.getString(vtdNav, os, LogField.LOGID.getTagAttribute(), bytesArray);
}
// test for priority attribute
if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.PRIORITY.getTagAttribute())) {
priority = getInteger(vtdNav, os, LogField.PRIORITY.getTagAttribute(), bytesArray);
}
// test for uri attribute
if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.URI.getTagAttribute())) {
uri = this.getString(vtdNav, os, LogField.URI.getTagAttribute(), bytesArray);
}
// test for stackid attribute
if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.STACKID.getTagAttribute())) {
stackId = this.getString(vtdNav, os, LogField.STACKID.getTagAttribute(), bytesArray);
}
// test for stacklevel attribute
if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.STACKLEVEL.getTagAttribute())) {
stackLevel = getInteger(vtdNav, os, LogField.STACKLEVEL.getTagAttribute(), bytesArray);
}
// test for srcObject attribute
if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.SOURCEOBJECT.getTagAttribute())) {
srcObjectName = getString(vtdNav, os, LogField.SOURCEOBJECT.getTagAttribute(), bytesArray);
}
// test for Audience attribute
if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.AUDIENCE.getTagAttribute())) {
audience = getString(vtdNav, os, LogField.AUDIENCE.getTagAttribute(), bytesArray);
}
// test for Array attribute
if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.ARRAY.getTagAttribute())) {
array = getString(vtdNav, os, LogField.ARRAY.getTagAttribute(), bytesArray);
}
// test for Antenna attribute
if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.ANTENNA.getTagAttribute())) {
antenna = getString(vtdNav, os, LogField.ANTENNA.getTagAttribute(), bytesArray);
}
// Get the body of the message
int tIndex = (Integer) VTDNav_getText.invoke(vtdNav, nullObj);
if (tIndex != -1) {
// int tOffset = vn.getTokenOffset(tIndex);
// int tLen = vn.getTokenLength(tIndex);
logMessage = (String) VTDNav_toString.invoke(vtdNav, tIndex);
}
// get the additional data, if present
Vector<AdditionalData> extraDataList = getAdditionalData(vtdNav, os, bytesArray);
// get again the body of the message.
if (logMessage == null) {
try {
logMessage = getLogMessage(vtdNav);
} catch (Exception e) {
logMessage = null;
}
}
retVal = new LogEntry(milliseconds, entryType.ordinal(), fileName, line, routineName, hostName, processName, contextName, threadName, logId, priority, uri, stackId, stackLevel, logMessage, srcObjectName, audience, array, antenna, extraDataList);
} else {
throw new LogParseException("Error: VTD cannot find root element; string is: " + xmlString);
}
} catch (Exception navEx) {
navEx.printStackTrace();
throw new LogParseException("Error navigating parsed XML with VTD navigator!", navEx);
}
return retVal;
}
use of com.cosylab.logging.engine.log.LogTypeHelper in project ACS by ACS-Community.
the class EngineAudienceTest method testOperatorModeFiltering.
/**
* Test the filtering for OPERATOR.
*
* The logs that should not be filtered are those that:
* - have a level of WARINING or greater
* - have the audience set to OPERATOR
*
* The test is done in 2 times:
* 1. a collection of logs with no audience is tested
* 2. a collection of logs with different values for audience
* is tested
*
* @throws Exception
*/
public void testOperatorModeFiltering() throws Exception {
audience = AudienceInfo.OPERATOR.getAudience();
logRetieval.setAudience(audience);
//
// Step 1: generate a collection of logs with no audience
//
// Generate 1000 log of each type
Collection<ILogEntry> logs = new Vector<ILogEntry>();
for (LogTypeHelper logType : LogTypeHelper.values()) {
if (logType != LogTypeHelper.OFF) {
Collection<ILogEntry> tempLogs = CacheUtils.generateLogsType(1000, logType);
logs.addAll(tempLogs);
}
}
assertEquals(1000 * (LogTypeHelper.values().length - 1), logs.size());
for (ILogEntry log : logs) {
logRetieval.addLog(log.toXMLString());
}
// Wait until logRetrieval publish all the logs
assertFalse("Timeout waiting for logs", waitForLogs());
// Check the received logs
assertEquals(logs.size(), numOfReceivedXMLLogs);
assertEquals(1000 * 5, numOfReceivedLogs);
//
// Step 2: test a collection of logs with the audience
//
ACSLogParser parser = ACSLogParserFactory.getParser();
assertNotNull(parser);
SimpleDateFormat df = new IsoDateFormat();
assertNotNull(df);
// Generate 2 logs for each type.
// only one of them with audience OPERATOR
logs.clear();
for (LogTypeHelper logType : LogTypeHelper.values()) {
if (logType == LogTypeHelper.OFF) {
continue;
}
Date dt = new Date(System.currentTimeMillis());
StringBuffer dateSB = new StringBuffer();
FieldPosition pos = new FieldPosition(0);
df.format(dt, dateSB, pos);
StringBuilder logOperatorStr = new StringBuilder("<");
StringBuilder logNoOperatorStr = new StringBuilder();
// (see COMP-3749 : JDK levels FINER and FINEST were previously mapped to TRACE, while now FINER maps to DELOUSE).
if (logType == LogTypeHelper.TRACE || logType == LogTypeHelper.DELOUSE) {
logType = LogTypeHelper.INFO;
}
logOperatorStr.append(logType.logEntryType);
logOperatorStr.append(logHeaderStr);
logOperatorStr.append(dateSB.toString());
logOperatorStr.append(logBodyStr);
// Insert the audience
logNoOperatorStr = new StringBuilder(logOperatorStr);
logOperatorStr.append(alma.log_audience.OPERATOR.value);
logNoOperatorStr.append(alma.log_audience.DEVELOPER.value);
logOperatorStr.append(logEndOfBodyStr);
logNoOperatorStr.append(logEndOfBodyStr);
logOperatorStr.append("LOG Txt>");
logNoOperatorStr.append("LOG Txt>");
logOperatorStr.append(logFooterStr);
logNoOperatorStr.append(logFooterStr);
logOperatorStr.append(logType.logEntryType);
logNoOperatorStr.append(logType.logEntryType);
logOperatorStr.append('>');
logNoOperatorStr.append('>');
ILogEntry logOp = parser.parse(logOperatorStr.toString());
logs.add(logOp);
ILogEntry logNoOp = parser.parse(logNoOperatorStr.toString());
logs.add(logNoOp);
}
assertEquals(2 * (LogTypeHelper.values().length - 1), logs.size());
numOfReceivedLogs = 0;
numOfReceivedXMLLogs = 0;
for (ILogEntry log : logs) {
logRetieval.addLog(log.toXMLString());
}
assertFalse("Timeout waiting for logs", waitForLogs());
assertEquals(logs.size(), numOfReceivedXMLLogs);
assertEquals(10, numOfReceivedLogs);
}
use of com.cosylab.logging.engine.log.LogTypeHelper in project ACS by ACS-Community.
the class LogDispatcherTest method testGetSetDiscardLevel.
/**
* Test setting and getting of the discard level.
*
* @throws Exception
*/
public void testGetSetDiscardLevel() throws Exception {
ACSListenersDispatcher listenerDispatcher = new ACSListenersDispatcher();
assertNotNull("Null listener dispatcher!", listenerDispatcher);
ACSLogRetrieval logDispatcher = new ACSLogRetrieval(listenerDispatcher);
assertNotNull("Null log dispatcher!", logDispatcher);
logDispatcher.start();
logDispatcher.setDiscardLevel(null);
assertNull(logDispatcher.getDiscardLevel());
for (LogTypeHelper logType : LogTypeHelper.values()) {
logDispatcher.setDiscardLevel(logType);
assertEquals(logType, logDispatcher.getDiscardLevel());
}
logDispatcher.close(true);
}
use of com.cosylab.logging.engine.log.LogTypeHelper in project ACS by ACS-Community.
the class CacheUtils method generateLogs.
/**
* Generate a set of logs to be used for testing
* Each log has
* - a different time stamp.
* - the message contains the key of the log
* - the log type is random (all types but Trace because trace
* has no body)
*
* @param numOfLogs The number of logs to put in the collection
* @return The collection with the logs
*/
public static Collection<ILogEntry> generateLogs(int numOfLogs) throws Exception {
Random rnd = new Random(Calendar.getInstance().getTimeInMillis());
// Yesterday
long now = Calendar.getInstance().getTimeInMillis() - 1000 * 60 * 60 * 24;
SimpleDateFormat df = new IsoDateFormat();
Vector<ILogEntry> v = new Vector<ILogEntry>(numOfLogs);
for (int t = 0; t < numOfLogs; t++) {
Date dt = new Date(now + t * 1000);
StringBuffer dateSB = new StringBuffer();
FieldPosition pos = new FieldPosition(0);
df.format(dt, dateSB, pos);
StringBuilder logStr = new StringBuilder("<");
int typePos = rnd.nextInt(LogTypeHelper.values().length - 1);
LogTypeHelper type = LogTypeHelper.values()[typePos];
if (type == LogTypeHelper.TRACE) {
type = LogTypeHelper.INFO;
}
logStr.append(type.logEntryType);
logStr.append(logHeaderStr);
logStr.append(dateSB.toString());
logStr.append(logBodyStr);
logStr.append(t);
logStr.append(logFooterStr);
logStr.append(type.logEntryType);
logStr.append('>');
if (parser == null) {
parser = ACSLogParserFactory.getParser();
}
ILogEntry log = parser.parse(logStr.toString());
v.add(log);
}
return v;
}
Aggregations