001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     matic
016 */
017package org.nuxeo.ecm.core.management.jtajca.internal;
018
019import java.io.PrintWriter;
020import java.io.StringWriter;
021import java.text.DateFormat;
022import java.util.Date;
023
024import org.javasimon.Split;
025import org.nuxeo.ecm.core.management.jtajca.TransactionStatistics;
026
027/**
028 * @author matic
029 */
030public class DefaultTransactionStatistics implements TransactionStatistics {
031
032    protected final String id;
033
034    protected long startTimestamp;
035
036    protected Throwable startCapturedContext;
037
038    protected String threadName;
039
040    protected long endTimestamp;
041
042    protected Throwable endCapturedContext;
043
044    protected Status status;
045
046    protected Split split;
047
048    protected DefaultTransactionStatistics(Object key) {
049        id = DefaultTransactionMonitor.id(key);
050    }
051
052    @Override
053    public String getId() {
054        return id.toString();
055    }
056
057    @Override
058    public String getThreadName() {
059        return threadName;
060    }
061
062    @Override
063    public Status getStatus() {
064        return status;
065    }
066
067    @Override
068    public Date getStartDate() {
069        return new Date(startTimestamp);
070    }
071
072    public String getStartCapturedContext() {
073        return printCapturedContext(startCapturedContext);
074    }
075
076    @Override
077    public String getStartCapturedContextMessage() {
078        return printCapturedContext(startCapturedContext);
079    }
080
081    @Override
082    public Date getEndDate() {
083        return new Date(endTimestamp);
084    }
085
086    public Throwable getEndCapturedContext() {
087        return endCapturedContext;
088    }
089
090    @Override
091    public String getEndCapturedContextMessage() {
092        if (endCapturedContext == null) {
093            return "no context";
094        }
095        return printCapturedContext(endCapturedContext);
096    }
097
098    @Override
099    public long getDuration() {
100        return endTimestamp - startTimestamp;
101    }
102
103    @Override
104    public boolean isEnded() {
105        return endTimestamp != 0;
106    }
107
108    protected static String printCapturedContext(Throwable e) {
109        StringWriter sw = new StringWriter();
110        PrintWriter pw = new PrintWriter(sw, false);
111        e.printStackTrace(pw);
112        pw.flush();
113        return sw.toString();
114    }
115
116    public void print(PrintWriter writer) {
117        final String date = DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date(startTimestamp));
118        final long duration = getDuration();
119        if (duration > 0) {
120            writer.write(String.format(
121                    "Transaction has started at %s with a duration of %d ms and was in status %s\n%s", date, duration,
122                    status, getEndCapturedContextMessage()));
123        } else {
124            writer.write(String.format("Transaction has started at %s and was in state %s\n%s", date, status,
125                    getStartCapturedContextMessage()));
126        }
127        writer.flush();
128    }
129
130    @Override
131    public String toString() {
132        StringWriter sw = new StringWriter();
133        print(new PrintWriter(sw));
134        return sw.toString();
135    }
136}