eb77dc2e79205ef3f0944694ed117c2f4df9a8f0
[dmaap/messagerouter/msgrtr.git] / src / main / java / com / att / dmf / mr / backends / memory / MessageLogger.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 package com.att.dmf.mr.backends.memory;
23
24 import java.io.File;
25 import java.io.FileNotFoundException;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import org.apache.kafka.clients.producer.ProducerRecord;
32
33 import com.att.dmf.mr.backends.Publisher;
34
35
36
37 /**
38  * class used for logging perspective
39  * 
40  * @author anowarul.islam
41  *
42  */
43 public class MessageLogger implements Publisher {
44         public MessageLogger() {
45         }
46
47         public void setFile(File f) throws FileNotFoundException {
48                 fStream = new FileOutputStream(f, true);
49         }
50
51         /** 
52          * 
53          * @param topic
54          * @param msg
55          * @throws IOException
56          */
57         @Override
58         public void sendMessage(String topic, message msg) throws IOException {
59                 logMsg(msg);
60         }
61
62         /**
63          * @param topic
64          * @param msgs
65          * @throws IOException
66          */
67         @Override
68         public void sendMessages(String topic, List<? extends message> msgs) throws IOException {
69                 for (message m : msgs) {
70                         logMsg(m);
71                 }
72         }
73
74         /**
75          * @param topic
76          * @param kms
77          * @throws IOException
78         
79         @Override
80         public void sendBatchMessage(String topic, ArrayList<KeyedMessage<String, String>> kms) throws
81
82         IOException {
83         }
84  */
85         private FileOutputStream fStream;
86
87         /**
88          * 
89          * @param msg
90          * @throws IOException
91          */
92         private void logMsg(message msg) throws IOException {
93                 String key = msg.getKey();
94                 if (key == null)
95                         key = "<none>";
96
97                 fStream.write('[');
98                 fStream.write(key.getBytes());
99                 fStream.write("] ".getBytes());
100                 fStream.write(msg.getMessage().getBytes());
101                 fStream.write('\n');
102         }
103         public void sendBatchMessageNew(String topic, ArrayList<ProducerRecord<String, String>> kms) throws IOException {
104
105         }
106
107         public void sendMessagesNew(String topic, List<? extends message> msgs) throws IOException {
108         }
109 }