001/*
002 * (C) Copyright 2010-2014 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-2.1.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 *     Nuxeo - initial API and implementation
016 */
017
018package org.nuxeo.runtime.management.metrics;
019
020import java.io.File;
021import java.io.FileWriter;
022import java.io.IOException;
023import java.io.ObjectOutputStream;
024import java.util.Calendar;
025
026import org.javasimon.Sample;
027import org.javasimon.SimonManager;
028
029import org.nuxeo.runtime.api.Framework;
030
031import com.thoughtworks.xstream.XStream;
032
033public class MetricSerializer implements MetricSerializerMXBean {
034
035    protected File file;
036
037    protected ObjectOutputStream outputStream;
038
039    protected int count;
040
041    protected long lastUsage;
042
043    public void toStream(Sample... samples) throws IOException {
044        if (outputStream == null) {
045            return;
046        }
047        for (Sample sample : samples) {
048            outputStream.writeObject(sample);
049        }
050        count += 1;
051        lastUsage = Calendar.getInstance().getTimeInMillis();
052    }
053
054    @Override
055    public String getOutputLocation() {
056        if (file == null) {
057            return "/dev/null";
058        }
059        return file.getAbsolutePath();
060    }
061
062    public File getOutputFile() {
063        return file;
064    }
065
066    @Override
067    public void resetOutput(String path) throws IOException {
068        file = new File(path);
069        resetOutput();
070    }
071
072    @Override
073    public void resetOutput() throws IOException {
074        if (file == null) {
075            createTempFile();
076        }
077        closeOutput();
078        outputStream = new XStream().createObjectOutputStream(new FileWriter(file));
079        for (String name : SimonManager.simonNames()) {
080            SimonManager.getSimon(name).reset();
081        }
082    }
083
084    public void flushOuput() throws IOException {
085        outputStream.flush();
086    }
087
088    @Override
089    public void closeOutput() throws IOException {
090        if (outputStream == null) {
091            return;
092        }
093        outputStream.close();
094        outputStream = null;
095    }
096
097    private void createTempFile() throws IOException {
098        file = File.createTempFile("nx-samples-", ".xml");
099        Framework.trackFile(file, file);
100    }
101
102    @Override
103    public int getCount() {
104        return count;
105    }
106
107    @Override
108    public long getLastUsage() {
109        return lastUsage;
110    }
111
112}