Java tutorial
/******************************************************************************* * This file is part of OpenNMS(R). * * Copyright (C) 2012 The OpenNMS Group, Inc. * OpenNMS(R) is Copyright (C) 1999-2012 The OpenNMS Group, Inc. * * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc. * * OpenNMS(R) is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published * by the Free Software Foundation, either version 3 of the License, * or (at your option) any later version. * * OpenNMS(R) 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 Public License for more details. * * You should have received a copy of the GNU General Public License * along with OpenNMS(R). If not, see: * http://www.gnu.org/licenses/ * * For more information contact: * OpenNMS(R) Licensing <license@opennms.org> * http://www.opennms.org/ * http://www.opennms.com/ *******************************************************************************/ package org.opennms.ng.services.pollerconfig; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.io.Writer; import org.apache.commons.io.IOUtils; import org.exolab.castor.xml.MarshalException; import org.exolab.castor.xml.ValidationException; import org.opennms.core.utils.ConfigFileConstants; import org.opennms.netmgt.config.poller.PollerConfiguration; import org.opennms.ng.services.opennmsserverconfig.OpennmsServerConfig; import org.opennms.ng.util.xml.CastorUtils; import org.osgi.framework.BundleContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * This is the singleton class used to load the configuration for the OpenNMS * Poller service from the poller-configuration xml file. * <p/> * A mapping of the configured URLs to the iplist they contain is built at * init() time so as to avoid numerous file reads. * <p/> * <strong>Note: </strong>Users of this class should make sure the * <em>init()</em> is called before calling any other method to ensure the * config is loaded before accessing other convenience methods. * * @author <a href="mailto:jamesz@opennms.com">James Zuo </a> * @author <a href="mailto:mike@opennms.org">Mike Davidson </a> * @author <a href="mailto:sowmya@opennms.org">Sowmya Nataraj </a> * @author <a href="http://www.opennms.org/">OpenNMS </a> */ public class PollerConfigFactory extends PollerConfigManager { private final Logger LOG = LoggerFactory.getLogger(PollerConfigFactory.class); /** * The singleton instance of this factory */ private PollerConfig m_singleton = null; /** * This member is set to true if the configuration file has been loaded. */ private boolean m_loaded = false; /** * Loaded version */ private long m_currentVersion = -1L; public PollerConfigFactory() { } public PollerConfigFactory(final long currentVersion, final InputStream stream, final String localServer, final boolean verifyServer, BundleContext bundleContext, OpennmsServerConfig opennmsServerConfig) throws MarshalException, ValidationException { super(stream, localServer, verifyServer, bundleContext, opennmsServerConfig); m_currentVersion = currentVersion; } /** * Return the singleton instance of this factory. * * @return The current factory instance. * @throws IllegalStateException Thrown if the factory has not yet been initialized. */ public synchronized PollerConfig getInstance() { if (!m_loaded) { throw new IllegalStateException("The factory has not been initialized"); } return m_singleton; } /** * <p>setInstance</p> * * @param instance a {@link org.opennms.netmgt.config.PollerConfig} object. */ public synchronized void setInstance(final PollerConfig instance) { m_singleton = instance; m_loaded = true; } public PollerConfig getConf() { return m_singleton; } /** * Load the config from the default config file and create the singleton * instance of this factory. * * @throws java.io.IOException Thrown if the specified config file cannot be read * @throws org.exolab.castor.xml.MarshalException Thrown if the file does not conform to the schema. * @throws org.exolab.castor.xml.ValidationException Thrown if the contents do not match the required schema. * @throws java.io.IOException if any. * @throws org.exolab.castor.xml.MarshalException if any. * @throws org.exolab.castor.xml.ValidationException if any. */ public synchronized void init() throws IOException, MarshalException, ValidationException { if (m_loaded) { // init already called - return // to reload, reload() will need to be called return; } final File cfgFile = ConfigFileConstants.getFile(ConfigFileConstants.POLLER_CONFIG_FILE_NAME); LOG.debug("init: config file path: {}", cfgFile.getPath()); InputStream stream = null; PollerConfigFactory config = null; if (getOpennmsServerConfig() == null) { LOG.error("ServerConfigFactory is null"); } try { stream = new FileInputStream(cfgFile); config = new PollerConfigFactory(cfgFile.lastModified(), stream, getOpennmsServerConfig().getServerName(), getOpennmsServerConfig().verifyServer(), getBundleContext(), getOpennmsServerConfig()); } finally { IOUtils.closeQuietly(stream); } for (final org.opennms.netmgt.config.poller.Package pollerPackage : config.getConfiguration() .getPackageCollection()) { for (final org.opennms.netmgt.config.poller.Service service : pollerPackage.getServiceCollection()) { for (final org.opennms.netmgt.config.poller.Parameter parm : service.getParameterCollection()) { if (parm.getKey().equals("ds-name")) { if (parm.getValue().length() > ConfigFileConstants.RRD_DS_MAX_SIZE) { throw new ValidationException(String.format( "ds-name '%s' in service '%s' (poller package '%s') is greater than %d characters", parm.getValue(), service.getName(), pollerPackage.getName(), ConfigFileConstants.RRD_DS_MAX_SIZE)); } } } } } setInstance(config); } /** * Reload the config from the default config file * * @throws java.io.IOException Thrown if the specified config file cannot be read/loaded * @throws org.exolab.castor.xml.MarshalException Thrown if the file does not conform to the schema. * @throws org.exolab.castor.xml.ValidationException Thrown if the contents do not match the required schema. * @throws java.io.IOException if any. * @throws org.exolab.castor.xml.MarshalException if any. * @throws org.exolab.castor.xml.ValidationException if any. */ public synchronized void reload() throws IOException, MarshalException, ValidationException { init(); getInstance().update(); } /** * {@inheritDoc} */ @Override protected void saveXml(final String xml) throws IOException { if (xml != null) { getWriteLock().lock(); try { final long timestamp = System.currentTimeMillis(); final File cfgFile = ConfigFileConstants.getFile(ConfigFileConstants.POLLER_CONFIG_FILE_NAME); LOG.debug("saveXml: saving config file at {}: {}", timestamp, cfgFile.getPath()); final Writer fileWriter = new OutputStreamWriter(new FileOutputStream(cfgFile), "UTF-8"); fileWriter.write(xml); fileWriter.flush(); fileWriter.close(); LOG.debug("saveXml: finished saving config file: {}", cfgFile.getPath()); } finally { getWriteLock().unlock(); } } } /** * <p>update</p> * * @throws java.io.IOException if any. * @throws org.exolab.castor.xml.MarshalException if any. * @throws org.exolab.castor.xml.ValidationException if any. */ @Override public void update() throws IOException, MarshalException, ValidationException { getWriteLock().lock(); try { final File cfgFile = ConfigFileConstants.getFile(ConfigFileConstants.POLLER_CONFIG_FILE_NAME); if (cfgFile.lastModified() > m_currentVersion) { m_currentVersion = cfgFile.lastModified(); LOG.debug("init: config file path: {}", cfgFile.getPath()); InputStream stream = null; try { stream = new FileInputStream(cfgFile); m_config = CastorUtils.unmarshal(PollerConfiguration.class, stream); } finally { IOUtils.closeQuietly(stream); } init(); LOG.debug("init: finished loading config file: {}", cfgFile.getPath()); } } finally { getWriteLock().unlock(); } } }