001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.model;
018
019 import java.util.Collections;
020 import java.util.List;
021 import javax.xml.bind.annotation.XmlAccessType;
022 import javax.xml.bind.annotation.XmlAccessorType;
023 import javax.xml.bind.annotation.XmlAttribute;
024 import javax.xml.bind.annotation.XmlRootElement;
025
026 import org.apache.camel.Expression;
027 import org.apache.camel.LoggingLevel;
028 import org.apache.camel.Processor;
029 import org.apache.camel.processor.LogProcessor;
030 import org.apache.camel.processor.Logger;
031 import org.apache.camel.spi.RouteContext;
032 import org.apache.camel.util.ObjectHelper;
033
034 /**
035 * Represents an XML <log/> element
036 *
037 * @version $Revision: 892606 $
038 */
039 @XmlRootElement(name = "log")
040 @XmlAccessorType(XmlAccessType.FIELD)
041 public class LogDefinition extends ProcessorDefinition {
042
043 @XmlAttribute
044 private String message;
045 @XmlAttribute
046 private LoggingLevel loggingLevel = LoggingLevel.INFO;
047 @XmlAttribute
048 private String logName;
049
050 public LogDefinition() {
051 }
052
053 public LogDefinition(String message) {
054 this.message = message;
055 }
056
057 @Override
058 public String toString() {
059 return "Log[" + message + "]";
060 }
061
062 @Override
063 public String getShortName() {
064 return "log";
065 }
066
067 @Override
068 @SuppressWarnings("unchecked")
069 public List<ProcessorDefinition> getOutputs() {
070 return Collections.EMPTY_LIST;
071 }
072
073 @Override
074 public Processor createProcessor(RouteContext routeContext) throws Exception {
075 ObjectHelper.notEmpty(message, "message", this);
076
077 // use simple language for the message string to give it more power
078 Expression exp = routeContext.getCamelContext().resolveLanguage("simple").createExpression(message);
079
080 String name = getLogName();
081 if (name == null) {
082 name = routeContext.getRoute().getId();
083 }
084 Logger logger = new Logger(name, getLoggingLevel());
085
086 return new LogProcessor(exp, logger);
087 }
088
089 @Override
090 public void addOutput(ProcessorDefinition processorType) {
091 // add outputs on parent as this log does not support outputs
092 getParent().addOutput(processorType);
093 }
094
095 public LoggingLevel getLoggingLevel() {
096 return loggingLevel;
097 }
098
099 public void setLoggingLevel(LoggingLevel loggingLevel) {
100 this.loggingLevel = loggingLevel;
101 }
102
103 public String getMessage() {
104 return message;
105 }
106
107 public void setMessage(String message) {
108 this.message = message;
109 }
110
111 public String getLogName() {
112 return logName;
113 }
114
115 public void setLogName(String logName) {
116 this.logName = logName;
117 }
118
119 }