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