/*
* Embedded Jopr Project
* Copyright (C) 2006-2009 Red Hat, Inc.
* All rights reserved.
*
* 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 2.1 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.jboss.on.embedded.util;
import java.io.File;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.rhq.core.domain.resource.Resource;
import org.rhq.core.domain.resource.ResourceType;
import org.rhq.core.pc.PluginContainer;
import org.rhq.core.pc.PluginContainerConfiguration;
import org.rhq.core.pc.plugin.FileSystemPluginFinder;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.jboss.on.embedded.manager.ResourceManager;
import org.jboss.on.embedded.manager.ResourceManagerFactory;
/**
* Base class for tests which need to initialize the plugin container Subclasses can override getScenario() to return
* the name of a particular scenario they want the mock plugin loaded with
*/
public class EmbeddedPluginContainerHelper
{
private static String DEFAULT_SCENARIO = "scenario1.xml";
private static final Log LOG = LogFactory.getLog(EmbeddedPluginContainerHelper.class);
private PluginContainer PC = PluginContainer.getInstance();
@BeforeClass
public void initializePluginContainer()
{
initializeScenario();
File pluginDir = new File("target/itest/plugins");
PluginContainerConfiguration pcConfig = new PluginContainerConfiguration();
pcConfig.setPluginFinder(new FileSystemPluginFinder(pluginDir));
pcConfig.setPluginDirectory(pluginDir);
pcConfig.setInsideAgent(false);
PC.setConfiguration(pcConfig);
PC.initialize();
ResourceManager resourceManager = ResourceManagerFactory.resourceManager();
resourceManager.discoverResources();
LOG.info("Successfully initialized Plugin Container");
}
protected String getScenario()
{
return DEFAULT_SCENARIO;
}
@AfterClass
public void shutdownPluginContainer()
{
LOG.info("Shutting down Plugin Container");
PC.shutdown();
LOG.info("Successfully shutdown Plugin Container");
}
protected void initializeScenario()
{
LOG.info("Initializing Plugin Container with [" + getScenario() + "]");
System.setProperty("on.mock.jboss.scenario", getScenario());
}
public ResourceManager getManager()
{
return ResourceManagerFactory.resourceManager();
}
/**
* gets the resource instance with the given resource name and resource type name
*
* @param resourceTypeName the name of the resource type
* @param resourceInstanceName the name of the resource instance
* @return the actual resource instance
*/
protected Resource getResourceInstance(String resourceTypeName, String resourceInstanceName)
{
ResourceType resourceType = getManager().getResourceType(resourceTypeName);
return getResourceInstance(resourceType, resourceInstanceName);
}
/**
* gets the resource instance with the given resource name and resource type
*
* @param resourceType the resource type
* @param resourceInstanceName the name of the resource instance
* @return the actual resource instance
*/
protected Resource getResourceInstance(ResourceType resourceType, String resourceInstanceName)
{
Set<Resource> resources = getManager().getResources(resourceType, getManager().getPlatform());
Resource resource = null;
for (Resource s : resources)
{
if (s.getName().equals(resourceInstanceName))
{
resource = s;
break;
}
}
return resource;
}
}