package cl.alma.camel.acslog;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.impl.DefaultConsumer;
import org.apache.camel.impl.DefaultExchange;
import org.apache.camel.impl.DefaultMessage;
import com.cosylab.logging.engine.ACS.ACSRemoteErrorListener;
import com.cosylab.logging.engine.ACS.ACSRemoteLogListener;
import com.cosylab.logging.engine.audience.Audience;
import com.cosylab.logging.engine.audience.Audience.AudienceInfo;
import com.cosylab.logging.engine.log.ILogEntry;
import com.cosylab.logging.engine.log.LogTypeHelper;
/**
* This class consume ACS logs from the logging channel.
* Inherit jLog classes in order to connect to ACS, must be defined
* -DACS.manager=$MANAGER_REFERENCE
* if -DACS.tmp=$ACS_TMP is defined, also should be as well:
* -Djlog.cache.size=4096 -Djlog.cache.writebuffersize=1024
*
* @author atejeda
*/
public abstract class ACSLogConsumer extends DefaultConsumer implements ACSRemoteLogListener, ACSRemoteErrorListener {
protected final ACSLogEndpoint endpoint;
public ACSLogConsumer(Endpoint endpoint, Processor processor) {
super(endpoint, processor);
this.endpoint = (ACSLogEndpoint) endpoint;
}
/**
* Returns the audience for reading the log, defined in the endpoint,
* ACS logging api use this as a filter.
* @return the audience, by default: Enginner.
*/
public Audience getAudience() {
String audienceString = this.endpoint.getAudience();
Audience audience;
audience = AudienceInfo.fromShortName(audienceString).getAudience();
if(audience == null) {
audience = AudienceInfo.ENGINEER.getAudience();
}
return audience;
}
/**
* Returns the discard level for reading the log, defined in the endpoint,
* ACS logging api use this as a filter.
* @return the discard level, by default: Debug.
*/
public LogTypeHelper getLogDiscardLevel() {
String logdiscardString = this.endpoint.getDiscard();
LogTypeHelper logDiscard;
logDiscard = LogTypeHelper.fromLogTypeDescription(logdiscardString);
if(logDiscard == null) {
logDiscard= LogTypeHelper.DEBUG;
}
return logDiscard;
}
/**
* Returns the log level for reading the log, defined in the endpoint,
* ACS logging api use this as a filter.
* @return the log level, by default: Info.
*/
public LogTypeHelper getLogLevel() {
String loglevelString = this.endpoint.getLevel();
LogTypeHelper loglevel;
loglevel = LogTypeHelper.fromLogTypeDescription(loglevelString);
if(loglevel == null) {
loglevel= LogTypeHelper.INFO;
}
return loglevel;
}
/**
* Receive a log entry from the xml files, updates the read log count
* and the read log parsed count, also add the "origin" field to the
* data file of the ACS logging api structure, the purpuse is to
* identify the origin of this log, eg.: my-localhost-name.
* <br/>
* Also it will create the Camel exchange to be sent to the processor.
*/
@Override
public void logEntryReceived(ILogEntry logEntry) {
try {
this.getProcessor().process(this.createExchange(logEntry));
} catch (Exception e) {
log.error(e.toString());
e.printStackTrace();
}
}
/**
*
* @param logEntry
* @return
*/
public Exchange createExchange(ILogEntry logEntry) {
Exchange exchange = new DefaultExchange(this.endpoint.getCamelContext(),
this.endpoint.getExchangePattern());
Message message = new DefaultMessage();
switch(this.endpoint.getModeEnum()) {
case WRAPPED:
message.setBody(this.wrappedLogEntry(logEntry));
case XML:
logEntry.addData("origin", this.endpoint.getOrigin());
message.setBody(logEntry.toXMLString());
case OBJECT:
logEntry.addData("origin", this.endpoint.getOrigin());
message.setBody(logEntry);
}
exchange.setIn(message);
return exchange;
}
/**
*
* @param logEntry
* @return
*/
public Object wrappedLogEntry(ILogEntry logEntry) {
return new ACSLogEntryWrapper(logEntry, this.endpoint.getOrigin());
}
}