001package com.ganteater.ae;
002
003import java.io.ByteArrayOutputStream;
004import java.io.IOException;
005import java.util.Map;
006import java.util.Map.Entry;
007import java.util.Properties;
008import java.util.Set;
009
010import org.apache.commons.lang.ObjectUtils;
011
012public class AELogRecord {
013
014        private Object message;
015        private String type;
016        private String varName;
017
018        public AELogRecord(Object text, String type, String varName) {
019                this.message = text;
020                this.type = type;
021                this.varName = varName;
022        }
023
024        @Override
025        public String toString() {
026                return varName == null ? getText() : varName + " = " + getText();
027        }
028
029        public String getText() {
030                if (message instanceof byte[]) {
031                        return new String((byte[]) message);
032                } else if (message instanceof Properties) {
033                        ByteArrayOutputStream out = new ByteArrayOutputStream();
034                        Properties pr = (Properties) message;
035                        try {
036                                pr.store(out, null);
037                        } catch (IOException e) {
038                                throw new IllegalArgumentException(e);
039                        }
040                        return out.toString();
041                } else if (message instanceof Map) {
042                        ByteArrayOutputStream out = new ByteArrayOutputStream();
043                        @SuppressWarnings("unchecked")
044                        Map<String, Object> map = (Map<String, Object>) message;
045                        
046                        Set<Entry<String, Object>> entrySet = map.entrySet();
047                        Properties pr = new Properties();
048                        for (Entry<String, Object> entry : entrySet) {
049                                Object value = entry.getValue();
050                                if (value instanceof String) {
051                                        pr.setProperty(entry.getKey(), ObjectUtils.toString(value));
052                                }
053                        }
054                        
055                        try {
056                                pr.store(out, null);
057                        } catch (IOException e) {
058                                throw new IllegalArgumentException(e);
059                        }
060                        return out.toString();
061                }
062                return ObjectUtils.toString(message, null);
063        }
064
065        public Object getMessage() {
066                return message;
067        }
068
069        public String getType() {
070                return type != null ? type : "txt";
071        }
072
073        public String getVarName() {
074                return varName;
075        }
076
077        public void setVarName(String varName) {
078                this.varName = varName;
079        }
080        
081}