/*
* 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.ui.content;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.rhq.core.clientapi.agent.inventory.CreateResourceResponse;
import org.rhq.core.domain.configuration.Configuration;
import org.rhq.core.domain.configuration.definition.ConfigurationDefinition;
import org.rhq.core.domain.content.PackageDetailsKey;
import org.rhq.core.domain.content.PackageType;
import org.rhq.core.domain.content.transfer.ResourcePackageDetails;
import org.rhq.core.domain.resource.Resource;
import org.rhq.core.domain.resource.ResourceType;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.on.embedded.manager.ResourceManager;
import org.jboss.on.embedded.manager.ResourceManagerFactory;
import org.jboss.on.embedded.ui.nav.JONTreeNode;
import org.jboss.on.embedded.ui.nav.ResourceTypeTreeNode;
import org.jboss.on.embedded.util.ContentUtility;
/**
* A Seam action for creating a content-backed Resource (e.g. a WAR or an EAR).
*
* @author Ian Springer
*/
@Name("createContentBackedResourceAction")
@Scope(ScopeType.CONVERSATION)
public class CreateContentBackedResourceAction extends AbstractFileUploadAction implements Serializable
{
private static final String INITIAL_PACKAGE_VERSION = "1.0";
private final Log log = LogFactory.getLog(this.getClass());
private final ResourceManager resourceManager = ResourceManagerFactory.resourceManager();
@Out(required = false)
private ConfigurationDefinition configurationDefinition;
@Out(required = false)
private Configuration configuration;
@Out
private ResourceType resourceType;
private PackageType packageType;
@Begin(join = true)
public String init()
{
JONTreeNode selectedNode = this.navigationAction.getSelectedNode();
ResourceTypeTreeNode resourceTypeTreeNode = (ResourceTypeTreeNode)selectedNode;
this.resourceType = resourceTypeTreeNode.getResourceType();
this.packageType = ContentUtility.getCreationPackageType(this.resourceType);
if (this.packageType == null)
{
facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR, "content.resourceInstance.create.resourceTypeHasNoCreationPackageType");
return FAILURE_OUTCOME;
}
this.configurationDefinition = this.packageType.getDeploymentConfigurationDefinition();
this.configuration = ContentUtility.getDefaultDeploymentConfiguration(this.packageType);
return SUCCESS_OUTCOME;
}
@End(ifOutcome = {"success"})
public String createContentBackedResource()
{
if (getFileName() == null)
{
// NOTE: This check is necessary, because the "required" attribute of the Seam FileUpload component doesn't
// work.
facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR,
"content.resourceInstance.create.noFileSelected");
return FAILURE_OUTCOME;
}
log.debug("Creating content-backed " + this.resourceType + " Resource " + getFileName() + "...");
File tempFile;
try
{
tempFile = writeTempFile();
}
catch (IOException e)
{
return FAILURE_OUTCOME;
}
PackageDetailsKey key = new PackageDetailsKey(tempFile.getPath(), INITIAL_PACKAGE_VERSION, this.packageType.getName(),
PACKAGE_ARCHITECTURE);
ResourcePackageDetails detail = new ResourcePackageDetails(key);
detail.setDeploymentTimeConfiguration(this.configuration);
Configuration pluginConfiguration = null;
JONTreeNode currentNode = navigationAction.getSelectedNode();
Resource ancestorResource = currentNode.getClosestResource();
final String resourceName = getFileName();
CreateResourceResponse createResourceResponse = this.resourceManager.createResource(resourceName,
this.resourceType, ancestorResource, pluginConfiguration, detail);
switch (createResourceResponse.getStatus())
{
case SUCCESS:
facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_INFO, "content.resourceInstance.create.success",
getFileName());
return SUCCESS_OUTCOME;
default:
String cause = (createResourceResponse.getErrorMessage() != null) ?
createResourceResponse.getErrorMessage() : "unknown";
facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR, "content.resourceInstance.create.failure",
getFileName(), cause);
return FAILURE_OUTCOME;
}
}
}