one more case added in DmaapClientUtilTest.java
[dmaap/messagerouter/dmaapclient.git] / src / main / java / org / onap / dmaap / mr / logging / MRAppender.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
4  *  ================================================================================
5  *  Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *  
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  *
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22 /**
23  * 
24  */
25 package org.onap.dmaap.mr.logging;
26
27 import java.io.IOException;
28
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import org.apache.log4j.AppenderSkeleton;
33 import org.apache.log4j.spi.LoggingEvent;
34
35 import org.onap.dmaap.mr.client.MRClientFactory;
36 import org.onap.dmaap.mr.client.MRPublisher;
37
38 /**
39  * @author author
40  *
41  */
42 public class MRAppender extends AppenderSkeleton {
43
44         private Logger logger = LoggerFactory.getLogger(this.getClass().getName());
45
46         private MRPublisher fPublisher;
47
48         //Provided through log4j configuration
49         private String topic;
50         private String partition;
51         private String hosts;
52         private int maxBatchSize = 1;
53         private int maxAgeMs = 1000;
54         private boolean compress = false;
55
56         /**
57          * 
58          */
59         public MRAppender() {
60                 super();
61         }
62
63         /**
64          * @param isActive
65          */
66         public MRAppender(boolean isActive) {
67                 super(isActive);
68         }
69
70         /* (non-Javadoc)
71          * @see org.apache.log4j.Appender#close()
72          */
73         @Override
74         public void close() {
75                 if (!this.closed) {
76                         this.closed = true;
77                         fPublisher.close();
78                 }
79         }
80
81         /* (non-Javadoc)
82          * @see org.apache.log4j.Appender#requiresLayout()
83          */
84         @Override
85         public boolean requiresLayout() {
86                 return false;
87         }
88
89         /* (non-Javadoc)
90          * @see org.apache.log4j.AppenderSkeleton#append(org.apache.log4j.spi.LoggingEvent)
91          */
92         @Override
93         protected void append(LoggingEvent event) {
94                 final String message;
95                 
96                 if (this.layout == null) {
97                         message = event.getRenderedMessage();
98                 } else {
99                         message = this.layout.format(event);
100                 }
101                 
102                 try {
103                         fPublisher.send(partition, message);
104                 } catch (IOException e) {
105                     logger.error("IOException: ", e);
106                 }
107         }
108
109         @Override
110         public void activateOptions() {
111                 if (hosts != null && topic != null && partition != null) {
112                         fPublisher = MRClientFactory.createBatchingPublisher(hosts.split(","), topic, maxBatchSize, maxAgeMs, compress);
113                 } else {
114                         logger.error("The Hosts, Topic, and Partition parameter are required to create a MR Log4J Appender");
115                 }
116         }
117         public String getTopic() {
118                 return topic;
119         }
120
121         public void setTopic(String topic) {
122                 this.topic = topic;
123         }
124
125         public String getPartition() {
126                 return partition;
127         }
128
129         public void setPartition(String partition) {
130                 this.partition = partition;
131         }
132
133         public String getHosts() {
134                 return hosts;
135         }
136
137         public void setHosts(String hosts) {
138                 this.hosts = hosts;
139         }
140         
141         public int getMaxBatchSize() {
142                 return maxBatchSize;
143         }
144
145         public void setMaxBatchSize(int maxBatchSize) {
146                 this.maxBatchSize = maxBatchSize;
147         }
148
149         public int getMaxAgeMs() {
150                 return maxAgeMs;
151         }
152
153         public void setMaxAgeMs(int maxAgeMs) {
154                 this.maxAgeMs = maxAgeMs;
155         }       
156         
157         public boolean isCompress() {
158                 return compress;
159         }
160
161         public void setCompress(boolean compress) {
162                 this.compress = compress;
163         }
164
165 }