/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2010-2011 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2011 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.protocols.xml.collector;
import org.opennms.netmgt.collectd.CollectionAgent;
import org.opennms.netmgt.collectd.PersistenceSelectorStrategy;
import org.opennms.netmgt.config.StorageStrategy;
import org.opennms.netmgt.config.datacollection.ResourceType;
import org.springframework.orm.ObjectRetrievalFailureException;
/**
* The Class XmlResourceType.
*
* @author <a href="mailto:agalue@opennms.org">Alejandro Galue</a>
*/
public class XmlResourceType {
/** The resource type name. */
private String m_resourceType;
/** The collection agent. */
private CollectionAgent m_agent;
/** The persistence selector strategy. */
private PersistenceSelectorStrategy m_persistenceSelectorStrategy;
/** The storage strategy. */
private StorageStrategy m_storageStrategy;
/**
* Instantiates a new XML resource type.
*
* @param agent the agent
* @param resourceType the resource type
*/
public XmlResourceType(CollectionAgent agent, ResourceType resourceType) {
m_agent = agent;
m_resourceType = resourceType.getName();
instantiatePersistenceSelectorStrategy(resourceType.getPersistenceSelectorStrategy().getClazz());
instantiateStorageStrategy(resourceType.getStorageStrategy().getClazz());
m_storageStrategy.setParameters(resourceType.getStorageStrategy().getParameterCollection());
m_persistenceSelectorStrategy.setParameters(resourceType.getPersistenceSelectorStrategy().getParameterCollection());
}
/**
* Instantiate persistence selector strategy.
*
* @param className the class name
*/
private void instantiatePersistenceSelectorStrategy(String className) {
Class<?> cinst;
try {
cinst = Class.forName(className);
} catch (ClassNotFoundException e) {
throw new ObjectRetrievalFailureException(PersistenceSelectorStrategy.class, className, "Could not load class", e);
}
try {
m_persistenceSelectorStrategy = (PersistenceSelectorStrategy) cinst.newInstance();
} catch (InstantiationException e) {
throw new ObjectRetrievalFailureException(PersistenceSelectorStrategy.class, className, "Could not instantiate", e);
} catch (IllegalAccessException e) {
throw new ObjectRetrievalFailureException(PersistenceSelectorStrategy.class, className, "Could not instantiate", e);
}
}
/**
* Instantiate storage strategy.
*
* @param className the class name
*/
private void instantiateStorageStrategy(String className) {
Class<?> cinst;
try {
cinst = Class.forName(className);
} catch (ClassNotFoundException e) {
throw new ObjectRetrievalFailureException(StorageStrategy.class, className, "Could not load class", e);
}
try {
m_storageStrategy = (StorageStrategy) cinst.newInstance();
} catch (InstantiationException e) {
throw new ObjectRetrievalFailureException(StorageStrategy.class, className, "Could not instantiate", e);
} catch (IllegalAccessException e) {
throw new ObjectRetrievalFailureException(StorageStrategy.class, className, "Could not instantiate", e);
}
m_storageStrategy.setResourceTypeName(m_resourceType);
if (m_agent != null)
m_storageStrategy.setStorageStrategyService(m_agent);
}
/**
* Gets the resource type name.
*
* @return the name
*/
public String getName() {
return m_resourceType;
}
/**
* Gets the storage strategy.
*
* @return the storage strategy
*/
public StorageStrategy getStorageStrategy() {
return m_storageStrategy;
}
/**
* Gets the persistence selector strategy.
*
* @return the persistence selector strategy
*/
public PersistenceSelectorStrategy getPersistenceSelectorStrategy() {
return m_persistenceSelectorStrategy;
}
}