
de.mcs.jmeasurement.MeasureFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of JMeasurement Show documentation
Show all versions of JMeasurement Show documentation
JMeasurement profiling programs in production enviroment
/*
* MCS Media Computer Software Copyright (c) 2005 by MCS
* -------------------------------------- Created on 23.04.2005 by w.klaas
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package de.mcs.jmeasurement;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Timer;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.management.InstanceAlreadyExistsException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import org.xml.sax.SAXException;
import de.mcs.jmeasurement.MeasurePoint.PRIORITY;
import de.mcs.jmeasurement.exception.MeasurementException;
import de.mcs.jmeasurement.exception.RendererMustNotBeNullException;
import de.mcs.jmeasurement.jmx.JmxConfig;
import de.mcs.jmeasurement.jmx.JmxConfigMBean;
import de.mcs.jmeasurement.jmx.JmxPointsImpl;
import de.mcs.jmeasurement.jmx.JmxPointsMXBean;
import de.mcs.jmeasurement.proxy.ProxyMonitor;
import de.mcs.jmeasurement.renderer.DefaultTextRenderer;
import de.mcs.jmeasurement.renderer.MeasureDataRenderer;
import de.mcs.jmeasurement.renderer.MeasurementReporter;
import de.mcs.jmeasurement.utils.MeasurePointXMLWriter;
/**
* This is the main entry point into the measurement system.
*
* @author w.klaas
*/
public final class MeasureFactory {
/** default text renderer page size. */
private static final int DEFAULT_TEXT_RENDERER_PAGE_SIZE = 80;
/** default text renderer field sizes. */
private static final int[] DEFAULT_TEXT_RENDERER_FIELD_SIZES = new int[] { 60, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12 };
/** to prevent instancing of this static class. */
private MeasureFactory() {
}
/** map with all measure points. */
private static Map measurePoints = new HashMap<>();
/** map with all snapshot objects. */
private static List snapShots = new ArrayList<>();
/** priority of this factory. */
private static PRIORITY priority = PRIORITY.LOWEST;
/** master switch of this factory. */
// private static boolean enable = true;
/** the null monitor, whenever it will be needed. */
private static NullMonitor nullMonitor = new NullMonitor();
/** call back function for every measure point. */
private static MeasureDataCallback measureDataCallback = null;
/**
* for automatic memory savings, all not actually used MeasurePoints will be referenced here.
*/
private static List savePoints = new ArrayList<>();
/** the timer for background processing. */
private static Timer backgroundtimer;
/** configuration class. */
private static JMConfig config = new JMConfig();
private static ObjectName jmxConfigObjectName;
private static ObjectName jmxPointsObjectName;
/**
* load configuration from jmconfig.properties file in the classpath.
*
* @return true
if the file could be loaded, otherwise false
.
*/
public static boolean configure() {
if (config == null) {
config = new JMConfig();
}
return config.configure();
}
/**
* load configuration from the given properties file.
*
* @param jmConfig
* the config file to load.
* @return true
if the file could be loaded, otherwise false
.
*/
public static boolean configure(final File jmConfig) {
if (config == null) {
config = new JMConfig();
}
return config.configure(jmConfig);
}
/**
* Getting a new monitor for the measurement point pointName. If the point doesn't exists, it will be created.
*
* @param pointName
* name of the measurement point to use.
* @return Monitor
*/
public static Monitor getMonitor(final String pointName) {
if (!config.isEnableMeasurement()) {
return nullMonitor;
}
MeasurePoint point = getMeasurePoint(pointName);
if (point.getPriority().compareTo(priority) >= 0) {
return point.getMonitor();
}
return nullMonitor;
}
/**
* getting the desired measurement point. If it doesn't exists it will be created.
*
* @param pointName
* name of the desired point (fully qualified)
* @return MeasurePoint the desired MeasurePoint
*/
public static MeasurePoint getMeasurePoint(final String pointName) {
String pointNameTrimmed = pointName.trim();
MeasurePoint point = null;
synchronized (measurePoints) {
if (measurePoints.containsKey(pointNameTrimmed)) {
point = (MeasurePoint) measurePoints.get(pointNameTrimmed);
}
if (point == null) {
if ((null != savePoints) && savePoints.contains(pointNameTrimmed)) {
point = loadAndReactivateMeasurePoint(pointNameTrimmed);
synchronized (savePoints) {
savePoints.remove(pointNameTrimmed);
}
}
}
if (point == null) {
point = new DefaultMeasurePoint(pointNameTrimmed, config);
register(point);
}
}
return point;
}
/**
* Adding a new measurepoint to the point registration.
*
* @param measurePoint
* measure point to add
*/
public static void register(final MeasurePoint measurePoint) {
measurePoint.setMeasureDataCallback(measureDataCallback);
if (measurePoint instanceof DefaultMeasurePoint) {
DefaultMeasurePoint defaultMeasurePoint = (DefaultMeasurePoint) measurePoint;
defaultMeasurePoint.setConfig(config);
}
synchronized (measurePoints) {
measurePoints.put(measurePoint.getName(), measurePoint);
}
}
/**
* getting an array of measurement points. The pointname is a regular expression of the desired points.
*
* @param pointName
* regular expression of the desired point names (or null for all points)
* @return MeasurePoint[] the desired MeasurePoints
*/
public static MeasurePoint[] getMeasurePoints(final String pointName) {
List points = new ArrayList();
for (String name : getMeasurePointNames(pointName)) {
MeasurePoint point = getMeasurePoint(name);
points.add(point);
}
return (MeasurePoint[]) points.toArray(new MeasurePoint[0]);
}
/**
* getting a collection of all measurepoints.
* @return Collection
*/
public static Collection getMeasurePoints() {
synchronized (measurePoints) {
Collection values = measurePoints.values();
List list = new ArrayList<>();
values.forEach(m -> list.add(m));
return list;
}
}
/**
* getting an sorted array of measurement point names. The pointname is a regular expression of the desired points.
*
* @param pointName
* regular expression of the desired point names (or null for all points)
* @return String[] the desired sorted array of MeasurePointNames
*/
public static String[] getMeasurePointNames(final String pointName) {
String regexPointName = pointName;
if (null == regexPointName) {
regexPointName = ".*";
}
Pattern pointNamePattern = Pattern.compile(regexPointName);
List pointNames = new ArrayList();
pointNames.addAll(getActivePointNames(pointNamePattern));
pointNames.addAll(getSavedPointNames(pointNamePattern));
pointNames.sort(Comparator.naturalOrder());
return (String[]) pointNames.toArray(new String[0]);
}
private static List getActivePointNames(Pattern pattern) {
synchronized (measurePoints) {
Set keySet = measurePoints.keySet();
List list = keySet.stream().filter(p -> pattern.matcher(p).matches()).sequential()
.collect(Collectors.toCollection(ArrayList::new));
return list;
}
}
private static List getSavedPointNames(Pattern pattern) {
List list = new ArrayList<>();
if (null != savePoints) {
synchronized (savePoints) {
list = savePoints.stream().filter(p -> pattern.matcher(p).matches()).sequential()
.collect(Collectors.toCollection(ArrayList::new));
}
}
return list;
}
/**
* Just handle a measure point as a counter for some things. If the point doesn't exists,
* it will be created.
* @param pointName name of the counter.
*/
public static void increaseCount(final String pointName) {
getMeasurePoint(pointName).increaseCount();
}
/**
* Just handle a measure point as a counter for some things. If the point doesn't exists,
* it will be created. The name of the monitor will be build from the object class name and the method name like
* this: #
*
* @param object
* object of the measurement point to use.
* @param methodName
* the name of the method
*/
public static void increaseCount(final Object object, final String methodName) {
String pointName = buildPointnameFromObjectMethod(object, methodName);
increaseCount(pointName);
}
private static String buildPointnameFromObjectMethod(final Object object, final String methodName) {
String pointName = String.format("%s#%s", object.getClass().getCanonicalName(), methodName);
return pointName;
}
/**
* Getting a new monitor for the measurement point pointName and starting this monitor. If the point doesn't exists,
* it will be created.
*
* @param pointName
* name of the measurement point to use.
* @return Monitor
*/
public static Monitor start(final String pointName) {
Monitor monitor = getMonitor(pointName);
monitor.start();
return monitor;
}
/**
* Getting a new monitor for the measurement point pointName and starting this monitor. If the point doesn't exists,
* it will be created. The anme of the monitor will be build from the object class name and the methode name like
* this: #
*
* @param object
* object of the measurement point to use.
* @param methodName
* the name of the method
* @return Monitor
*/
public static Monitor start(final Object object, final String methodName) {
String pointName = buildPointnameFromObjectMethod(object, methodName);
return start(pointName);
}
/**
* This is the standard report feature with all Points as an text Report.
*
* @return the string representation of this measurement factory
*/
public static String asString() {
try {
return getReport(new DefaultTextRenderer(DEFAULT_TEXT_RENDERER_PAGE_SIZE, DEFAULT_TEXT_RENDERER_FIELD_SIZES));
} catch (RendererMustNotBeNullException e) {
e.printStackTrace();
}
return null;
}
/**
* Return the actual priority setting of the factory.
*
* @return Returns the priority.
*/
public static PRIORITY getPriority() {
return priority;
}
/**
* Setting the actual priority of the factory.
*
* @param priority
* The priority to set.
*/
public static void setPriority(final PRIORITY priority) {
MeasureFactory.priority = priority;
}
/**
* Getting a rendered report of the measure data. All points be evaluating. To get some report the renderer must be
* given. If not, RendererMustNotBeNullException
will be thrown.
*
* @param renderer
* the renderer to use for this report
* @return a string with the report
* @throws RendererMustNotBeNullException
* thrown, if the renderer is not set.
*/
public static String getReport(final MeasureDataRenderer renderer) throws RendererMustNotBeNullException {
return getReport(".*", renderer);
}
/**
* Getting a rendered report of the measure data. The points in the requested report will be evaluating with the
* point string. To get some report the renderer must be given. If not, RendererMustNotBeNullException
* will be thrown.
*
* @param pointname
* regular expression to match the point names (or null for all points)
* @param renderer
* the renderer to use for this report
* @return a string with the report
* @throws RendererMustNotBeNullException
* thrown, if the renderer is not set.
*/
public static String getReport(final String pointname, final MeasureDataRenderer renderer)
throws RendererMustNotBeNullException {
StringWriter writer = new StringWriter();
try {
getReport(pointname, renderer, writer);
} catch (IOException e) {
// should never occure
}
return writer.toString();
}
/**
* Getting a rendered report of the measure data. The points in the requested report will be evaluating with the
* point string. To get some report the renderer must be given. If not, RendererMustNotBeNullException
* will be thrown.
*
* @param pointname
* regular expression to match the point names (or null for all points)
* @param renderer
* the renderer to use for this report
* @param output
* the writer to output the report
* @throws RendererMustNotBeNullException
* thrown, if the renderer is not set.
* @throws IOException
* if something goes wrong with the writer IO operation.
*/
public static void getReport(final String pointname, final MeasureDataRenderer renderer, final Writer output)
throws RendererMustNotBeNullException, IOException {
MeasurePoint[] points = getMeasurePoints(pointname);
MeasurementReporter reporter = new MeasurementReporter(getConfig(), points, snapShots.toArray(new SnapShot[0]));
reporter.setPriority(getPriority());
reporter.getReport(pointname, renderer, output);
}
/** Clearing all measure data and measure points. */
public static void clear() {
synchronized (measurePoints) {
measurePoints.clear();
}
synchronized (snapShots) {
snapShots.clear();
}
synchronized (savePoints) {
savePoints.clear();
}
}
/**
* saving all measurementpoints to an xml file structure for later reloading with loadFromXMLFile()
.
*
* @see #loadFromXMLFile(String, boolean)
* @param filename
* filename of the file to save xml to
* @throws IOException
* if something goes wrong in the filesystem.
* @throws SAXException
* if something goes wrong with xml writing
*/
public static void saveToXMLFile(final String filename) throws IOException, SAXException {
saveToXMLStream(new FileOutputStream(filename));
}
/**
* saving all measurement points to an xml file structure for later reloading.
*
* @param stream
* the output stream to write the xml data to.
* @throws IOException
* if something goes wrong in the filesystem.
* @throws SAXException
* if something goes wrong with xml writing
*/
public static void saveToXMLStream(final OutputStream stream) throws IOException, SAXException {
MeasurePointXMLWriter xmlWriter = new MeasurePointXMLWriter();
xmlWriter.writeMeasurePointsToXML(stream, getMeasurePoints(".*"), snapShots);
}
/**
* Load the measurepoints from an external XML file. (Possible saved with saveToXMLFile
)
*
* @see #saveToXMLFile(String)
* @param filename
* xml file
* @param loadSnapShotData
* loading the snapshot data if present.
* @throws IOException
* if something goes wrong
* @throws SAXException
* if something goes wrong
* @throws MeasurementException
* if something goes wrong
*/
public static void loadFromXMLFile(final String filename, final boolean loadSnapShotData)
throws IOException, SAXException, MeasurementException {
MeasurePointXMLWriter xmlWriter = new MeasurePointXMLWriter();
xmlWriter.loadFromXMLStream(new FileInputStream(filename), loadSnapShotData);
}
public static void loadFromXMLStream(InputStream in, boolean loadSnapShotData)
throws IOException, SAXException, MeasurementException {
MeasurePointXMLWriter xmlWriter = new MeasurePointXMLWriter();
xmlWriter.loadFromXMLStream(in, loadSnapShotData);
}
/**
* Returns the applicationName.
*
* @return String Returns the applicationName.
*/
public static String getApplicationName() {
return config.getProperty(JMConfig.OPTION_APPLICATION_NAME);
}
/**
* Setting the application name.
*
* @param aApplicationName
* The applicationName to set.
*/
public static void setApplicationName(final String aApplicationName) {
config.setProperty(JMConfig.OPTION_APPLICATION_NAME, aApplicationName);
}
/**
* Returns the measureDataCallback
object.
*
* @return Returns the measureDataCallback.
*/
public static MeasureDataCallback getMeasureDataCallback() {
return measureDataCallback;
}
/**
* setting the measureDataCallback
object.
*
* @param aMeasureDataCallback
* The measureDataCallback to set.
*/
public static void setMeasureDataCallback(final MeasureDataCallback aMeasureDataCallback) {
MeasureFactory.measureDataCallback = aMeasureDataCallback;
}
/**
* Returns if this factory can deliver monitors or not.
*
* @return boolean true
if this factory can deliver monitors or false
.
*/
public static boolean isEnable() {
return config.isEnableMeasurement();
}
/**
* This methode will enable the monitor factory. If it's set to false, start()
methode will only
* return NullMoniotr
Objects.
* This value has no influenz on the start()
methode of the MeasurementsPoints. They will return
* normal monitor objects even when the factory is turned of.
*
*
* @param aEnable
* The enable to set.
*/
public static void setEnable(final boolean aEnable) {
config.setEnableMeasurement(aEnable);
}
/**
* new methode for making a snapshot. In a snapshot all measure points data will be saved under the desired name.
* All Snapshot data will be saved into the xml files and will be renderd, if desired. The Snapshot will contains
* some individual data like the a memory snapshot.
*
* @param snapshotname
* name of the snapshot.
* @return the snapshot
*/
public static SnapShot takeSnapshot(final String snapshotname) {
SnapShot snapShot = new SnapShot(snapshotname);
synchronized (measurePoints) {
snapShot.cloneMeasurePoints(measurePoints);
}
addSnapShot(snapShot);
return snapShot;
}
public static void addSnapShot(SnapShot shot) {
snapShots.add(shot);
}
/**
* getting a snapshot from the system.
*
* @param snapshotname
* name of the snapshot to get
* @return the desired snapshot or null
if no snapshot with this name is present.
*/
public static SnapShot getSnapShot(final String snapshotname) {
for (SnapShot snapshot : snapShots) {
if (snapshot.getName().equals(snapshotname)) {
return snapshot;
}
}
return null;
}
/**
* save snapshot as XMl.
*
* @param snapshot
* the snapshot to save.
* @param file
* the file to save to.
* @throws SAXException
* if something goes wrong.
* @throws IOException
* if something goes wrong.
*/
public static void saveSnapShot(final SnapShot snapshot, final File file) throws SAXException, IOException {
MeasurePointXMLWriter xmlWriter = new MeasurePointXMLWriter();
xmlWriter.saveSnapshotToXMlFile(snapshot, file);
}
/**
* getting the names of all snapshots from the system.
*
* @return String[] array with all snapshot names .
*/
public static String[] getSnapShotNames() {
List names = snapShots.stream().map(s -> s.getName()).collect(Collectors.toCollection(ArrayList::new));
return names.toArray(new String[0]);
}
/**
* removing a snapshot from the system.
*
* @param snapshotname
* name of the snapshot to remove
* @return the removed snapshot or null
if no snapshot with this name is present.
*/
public static SnapShot removeSnapShot(final String snapshotname) {
SnapShot snapShot = null;
for (Iterator iter = snapShots.iterator(); iter.hasNext();) {
snapShot = iter.next();
if (snapShot.getName().equals(snapshotname)) {
iter.remove();
return snapShot;
}
}
return null;
}
/**
* Adding a Interface for automatic methode measurement. Exception handling as defined here.
*
* @param object
* Interface to add
* @return the interface to the proxy class.
* @since 0.66
*/
public static Object registerInterface(final Object object) {
return registerInterface(object, null);
}
/**
* Adding a Interface for automatic methode measurement. Exception handling as defined here.
*
* @param object
* Interface to add
* @param aMethodNames
* names of the methodes to monitor. null
if all methodes should be monitored.
* @since 0.72 If you wan't to define an argument as a point name you can use the following syntax for the method
* name: <method name>[.arg<index of argument>]
* @return the interface to the proxy class.
* @since 0.66
*/
public static Object registerInterface(final Object object, final String[] aMethodNames) {
return Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(),
new ProxyMonitor(object, aMethodNames));
}
/**
* Adding a Interface for automatic methode measurement.
*
* @param object
* Interface to add
* @param storeExceptions
* if the exception stacktraces should be stored.
* @param fullExceptions
* full exceptions with stacktraces
* @return the interface to the proxy class.
* @since 0.64
*/
public static Object registerInterface(final Object object, final boolean storeExceptions,
final boolean fullExceptions) {
return registerInterface(object, storeExceptions, fullExceptions, null);
}
/**
* Adding a Interface for automatic method measurement.
*
* @param object
* Interface to add
* @param storeExceptions
* if the exception stack traces should be stored.
* @param fullExceptions
* full exceptions with stack traces
* @param aMethodNames
* list with all method names that should be monitored. If this parameter is null all methods will be
* monitored.
* @since 0.72 If you wan't to define an argument as a point name you can use the following syntax for the method
* name: <method name>[.arg<index of argument>]
* @return the interface to the proxy class.
* @since 0.64
*/
public static Object registerInterface(final Object object, final boolean storeExceptions,
final boolean fullExceptions, final String[] aMethodNames) {
return Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(),
new ProxyMonitor(object, storeExceptions, fullExceptions, isPointnameWithParameter(), aMethodNames));
}
private static boolean isPointnameWithParameter() {
if (config.contains(JMConfig.OPTION_POINT_NAME_WITH_PARAMETER)) {
return config.getBoolean(JMConfig.OPTION_POINT_NAME_WITH_PARAMETER);
}
return false;
}
/**
* @return value of exception handling
*/
public static int getExceptionHandling() {
return config.getInteger(JMConfig.OPTION_EXCEPTION_HANDLING);
}
/**
* setting the value of the exception handling.
*
* @param aExceptionHandling
* one of the constants for exception handling.
*/
public static void setExceptionHandling(final int aExceptionHandling) {
config.setInteger(JMConfig.OPTION_EXCEPTION_HANDLING, aExceptionHandling);
}
/**
* setting some special options for this factory. Possible option names are:
*
* - JMConfig#OPTION_DISABLE_DEVIATION disable calculation of the deviation. Possible values are
*
true,false
.
* - JMConfig#OPTION_ENABLE_AUTOSNAPSHOT Enable the automatic snapshot system. Possible values are
*
true,false
.
* - JMConfig#OPTION_BACKGROUND_TIME Time (in msec) to generate a new snapshot and to prove the memory
* saving settings.
* - JMConfig#OPTION_ENABLE_MEMORY_SAVINGS Enable the automatic memory saving system. All not actually
* needed measure points and snapshots will be save to the harddisk. To determine where to store the data, use the
* JMConfig#OPTIONS_WORKINGPATH. Possible values are
true,false
.
* - JMConfig#OPTION_WORKINGPATH Path where to store the measure data for memory savings.
* - JMConfig#OPTION_POINT_IDLETIME Time (in sec) how long a point must be idle before it will be saved to
* disk.
*
* The memory savings and the auto snapshot feature will start a
* background process, which is controlled by the {@link MeasureFactory#setBackgroundProcessing(boolean)} method. If
* you wan't to suspend the feature, set the background processing to false. But don't forget to set it back to
* true, if you are ready. If you set {@link JMConfig#OPTION_BACKGROUND_TIME} you have to manually enable the
* background processing via {@link MeasureFactory#setBackgroundProcessing(boolean)}. If you use the method
* {@link MeasureFactory#setOptions(Map)} the background processing will automatically started.
*
* @param aOptionName
* the option that should be set.
* @param aOptionValue
* value of this option as String
* @since 0.67
*/
public static void setOption(final String aOptionName, final String aOptionValue) {
if ((aOptionName != null) && (aOptionValue != null)) {
config.setProperty(aOptionName, aOptionValue);
}
}
/**
* Setting a bunch of options. {@link MeasureFactory#setOption(String, String)}
*
* @param aOptions
* a map with all options.
* @since 0.68
*/
public static void setOptions(final Map aOptions) {
for (Entry entry : aOptions.entrySet()) {
if (null != entry.getValue()) {
config.setProperty(entry.getKey(), entry.getValue());
}
}
}
/**
* starting and stopping background processing.
*
* @param enableBackground
* true
to allow background processing, otherwise false
* @since 0.68
*/
public static void setBackgroundProcessing(final boolean enableBackground) {
if (enableBackground) {
if (backgroundtimer == null) {
backgroundtimer = new Timer("JMeasurement Background Timer", true);
long period = config.getLong(JMConfig.OPTION_BACKGROUND_TIME);
backgroundtimer.scheduleAtFixedRate(new MeasureTask(config), period, period);
}
} else {
if (backgroundtimer != null) {
backgroundtimer.cancel();
backgroundtimer.purge();
backgroundtimer = null;
}
}
}
/**
* testing if backgorund processing is enabled.
*
* @return true
if background processing is enabled, otherwise false
* @since 0.68
*/
public static boolean isBackgroundProcessing() {
return backgroundtimer != null;
}
/**
* @return getting the actual config object.
*/
public static JMConfig getConfig() {
return config;
}
/**
* registering the JMX extensions for JMeasurement in the standard MBean server of this platform.
*
* @throws NotCompliantMBeanException
* if something goes wrong with the registration
* @throws MBeanRegistrationException
* if something goes wrong with the registration
* @throws InstanceAlreadyExistsException
* if something goes wrong with the registration
*/
public static void registerMBeans() {
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
try {
if (jmxConfigObjectName == null) {
jmxConfigObjectName = new ObjectName("de.mcs.jmeasurement.jmx:type=JmxConfig");
JmxConfigMBean mbean = new JmxConfig();
mBeanServer.registerMBean(mbean, jmxConfigObjectName);
}
if (jmxPointsObjectName == null) {
jmxPointsObjectName = new ObjectName("de.mcs.jmeasurement.jmx:type=JmxPoints");
JmxPointsMXBean mxbean = new JmxPointsImpl();
mBeanServer.registerMBean(mxbean, jmxPointsObjectName);
}
} catch (MalformedObjectNameException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (InstanceAlreadyExistsException e) {
e.printStackTrace();
} catch (MBeanRegistrationException e) {
e.printStackTrace();
} catch (NotCompliantMBeanException e) {
e.printStackTrace();
}
}
public static void deregisterMBeans() {
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
try {
if (jmxConfigObjectName != null) {
if (mBeanServer.isRegistered(jmxConfigObjectName)) {
mBeanServer.unregisterMBean(jmxConfigObjectName);
}
jmxConfigObjectName = null;
}
if (jmxPointsObjectName != null) {
if (mBeanServer.isRegistered(jmxPointsObjectName)) {
mBeanServer.unregisterMBean(jmxPointsObjectName);
}
jmxPointsObjectName = null;
}
} catch (InstanceNotFoundException e) {
e.printStackTrace();
} catch (MBeanRegistrationException e) {
e.printStackTrace();
}
}
private static MeasurePoint loadAndReactivateMeasurePoint(String pointNameTrimmed) {
MeasurePoint measurePoint = null;
File pointFile = new File(config.getProperty(JMConfig.OPTION_WORKINGPATH), pointNameTrimmed);
if (pointFile.exists()) {
try {
measurePoint = loadMeasurePoint(pointFile);
register(measurePoint);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return measurePoint;
}
protected static void saveMeasurePointandRemove(MeasurePoint measurePoint, File workingPath) {
if (measurePoint != null) {
synchronized (measurePoints) {
try {
saveMeasurePoint(workingPath, measurePoint);
synchronized (savePoints) {
savePoints.add(measurePoint.getName());
}
measurePoints.remove(measurePoint.getName());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static MeasurePoint loadMeasurePoint(File pointFile)
throws IOException, FileNotFoundException, ClassNotFoundException {
MeasurePoint point = null;
ObjectInputStream oin = new ObjectInputStream(new FileInputStream(pointFile));
point = (MeasurePoint) oin.readObject();
oin.close();
pointFile.delete();
return point;
}
private static void saveMeasurePoint(File workingPath, MeasurePoint point) throws IOException, FileNotFoundException {
String measurePointName = point.getName();
String filename = measurePointName.replace('#', '_');
ObjectOutputStream oout = new ObjectOutputStream(new FileOutputStream(new File(workingPath, filename)));
oout.writeObject(point);
oout.close();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy