001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     matic
018 */
019package org.nuxeo.ecm.core.management.jtajca.internal;
020
021import java.io.PrintWriter;
022import java.io.StringWriter;
023import java.text.DateFormat;
024import java.util.Date;
025
026import org.javasimon.Split;
027import org.nuxeo.ecm.core.management.jtajca.TransactionStatistics;
028
029/**
030 * @author matic
031 */
032public class DefaultTransactionStatistics implements TransactionStatistics {
033
034    protected final String id;
035
036    protected long startTimestamp;
037
038    protected Throwable startCapturedContext;
039
040    protected String threadName;
041
042    protected long endTimestamp;
043
044    protected Throwable endCapturedContext;
045
046    protected Status status;
047
048    protected Split split;
049
050    protected DefaultTransactionStatistics(Object key) {
051        id = DefaultTransactionMonitor.id(key);
052    }
053
054    @Override
055    public String getId() {
056        return id.toString();
057    }
058
059    @Override
060    public String getThreadName() {
061        return threadName;
062    }
063
064    @Override
065    public Status getStatus() {
066        return status;
067    }
068
069    @Override
070    public Date getStartDate() {
071        return new Date(startTimestamp);
072    }
073
074    public String getStartCapturedContext() {
075        return printCapturedContext(startCapturedContext);
076    }
077
078    @Override
079    public String getStartCapturedContextMessage() {
080        return printCapturedContext(startCapturedContext);
081    }
082
083    @Override
084    public Date getEndDate() {
085        return new Date(endTimestamp);
086    }
087
088    public Throwable getEndCapturedContext() {
089        return endCapturedContext;
090    }
091
092    @Override
093    public String getEndCapturedContextMessage() {
094        if (endCapturedContext == null) {
095            return "no context";
096        }
097        return printCapturedContext(endCapturedContext);
098    }
099
100    @Override
101    public long getDuration() {
102        return endTimestamp - startTimestamp;
103    }
104
105    @Override
106    public boolean isEnded() {
107        return endTimestamp != 0;
108    }
109
110    protected static String printCapturedContext(Throwable e) {
111        StringWriter sw = new StringWriter();
112        PrintWriter pw = new PrintWriter(sw, false);
113        e.printStackTrace(pw);
114        pw.flush();
115        return sw.toString();
116    }
117
118    public void print(PrintWriter writer) {
119        final String date = DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date(startTimestamp));
120        final long duration = getDuration();
121        if (duration > 0) {
122            writer.write(String.format(
123                    "Transaction has started at %s with a duration of %d ms and was in status %s\n%s", date, duration,
124                    status, getEndCapturedContextMessage()));
125        } else {
126            writer.write(String.format("Transaction has started at %s and was in status %s\n%s", date, status,
127                    getStartCapturedContextMessage()));
128        }
129        writer.flush();
130    }
131
132    @Override
133    public String toString() {
134        StringWriter sw = new StringWriter();
135        print(new PrintWriter(sw));
136        return sw.toString();
137    }
138}