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