Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package net.welen.jmole;
/*
* #%L
* JMole, https://bitbucket.org/awelen/jmole
* %%
* Copyright (C) 2015 - 2019 Anders Welén, [email protected]
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
import java.util.Map;
import java.util.HashMap;
import java.util.Properties;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.InstanceAlreadyExistsException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanRegistrationException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;
import javax.management.NotificationListener;
import javax.management.MBeanServerNotification;
import javax.management.relation.MBeanServerNotificationFilter;
import javax.management.MBeanServerDelegate;
import javax.management.ListenerNotFoundException;
import javax.management.MBeanException;
import javax.management.AttributeNotFoundException;
import javax.management.ReflectionException;
import javax.management.IntrospectionException;
import javax.xml.XMLConstants;
import javax.xml.transform.ErrorListener;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import com.google.gson.Gson;
import net.welen.jmole.presentation.PresentationInformation;
import net.welen.jmole.threshold.Threshold;
import java.util.List;
import java.util.Collections;
import java.util.ArrayList;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.beans.XMLDecoder;
import java.beans.ExceptionListener;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* The MBean implementation of JMole and more or less the main class
*/
public class JMole extends NotificationBroadcasterSupport implements NotificationListener, JMoleMBean {
private final static Logger LOG = Logger.getLogger(JMole.class.getName());
static public final String OBJECT_NAME = "net.welen.jmole:service=jmole";
static public final String CONFIG_FILENAMES_PROPERTY = "jmole.config.filenames";
static public final String CONFIG_FILENAME_PROPERTY_PREFIX = "jmole.config.filename.";
static public final String CONFIG_FILENAME_XSLT_PROPERTY_PREFIX = "jmole.config.xslt.";
static public final String CONFIG_LEVEL_PROPERTY = "jmole.config.level";
public static final String NOTIFICATION_TYPE = "jmole.reconfigured";
private MBeanServer server = Utils.getMBeanServer();
private List configurations = new ArrayList();
private DiscoveryThread discoveryThread = new DiscoveryThread(this);
private long numOfDiscoveries = 0;
private List thresholdThreads = new ArrayList();
// Inner class that is used by XML libs
private static class SimpleExceptionListener implements ExceptionListener {
private Exception e = null;
public void exceptionThrown(Exception e) {
this.e = e;
}
Exception getException() {
return e;
}
}
/**
* Register the JMole MBean and a listener (that react to all new and
* removed MBeans)
*
* @throws MalformedObjectNameException
* @throws InstanceAlreadyExistsException
* @throws MBeanRegistrationException
* @throws NotCompliantMBeanException
* @throws InstanceNotFoundException
*/
public void register() throws InstanceAlreadyExistsException, MBeanRegistrationException,
NotCompliantMBeanException, MalformedObjectNameException, InstanceNotFoundException {
LOG.log(Level.FINE, "Registering JMole MBean");
server.registerMBean(this, new ObjectName(OBJECT_NAME));
try {
MBeanServerNotificationFilter filter = new MBeanServerNotificationFilter();
filter.enableAllObjectNames();
Thread t = new Thread(discoveryThread, "JMole discovery thread");
t.setDaemon(true);
t.start();
server.addNotificationListener(MBeanServerDelegate.DELEGATE_NAME, this, filter, null);
} catch (Exception e) {
LOG.log(Level.SEVERE, e.getMessage(), e);
server.unregisterMBean(new ObjectName(OBJECT_NAME));
throw new RuntimeException(e);
}
}
/**
* Unregister the JMole MBean and remove the listener
*
* @throws MalformedObjectNameException
* @throws InstanceNotFoundException
* @throws MBeanRegistrationException
* @throws ListenerNotFoundException
*/
public void unregister() throws MalformedObjectNameException, InstanceNotFoundException, MBeanRegistrationException,
ListenerNotFoundException {
LOG.log(Level.FINE, "Removing JMole MBean");
deactivateThresholds();
discoveryThread.stop();
try {
server.removeNotificationListener(MBeanServerDelegate.DELEGATE_NAME, this);
} finally {
server.unregisterMBean(new ObjectName(OBJECT_NAME));
}
}
@Override
public synchronized void configure() throws MalformedObjectNameException, FileNotFoundException, MBeanException,
AttributeNotFoundException, InstanceNotFoundException, ReflectionException, IntrospectionException {
List newConfigurations = Collections.synchronizedList(new ArrayList());
String fileNames = getConfigFileNames();
for (String fileName : fileNames.split("\\|")) {
LOG.log(Level.INFO, "Configuring JMole with config file: " + fileName);
XMLDecoder decoder = null;
InputStream configStream = null;
SimpleExceptionListener myListener = new SimpleExceptionListener();
byte[] data = null;
try {
// Get inputstream
if (new File(fileName).exists()) {
configStream = new BufferedInputStream(new FileInputStream(fileName));
} else {
configStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
}
if (configStream == null) {
LOG.log(Level.SEVERE, String.format("Unable to load config from '%s'", fileName));
continue;
}
// Put all in memory
try {
data = readData(configStream);
} catch (IOException e) {
LOG.log(Level.SEVERE, String.format("Unable to load config from '%s'", fileName), e);
continue;
}
// Replace all ${systemProperty} syntax
data = replaceAllSystemProperties(data);
// Find matching XSLT (if any)
String xslt = null;
for (Entry