com.abiquo.commons.plugin.exception.HypervisorPluginException Maven / Gradle / Ivy
/**
* The Abiquo Platform
* Cloud management application for hybrid clouds
* Copyright (C) 2008 - Abiquo Holdings S.L.
*
* This application 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 under
* version 3 of the License
*
* This software 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 v.3 for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/**
* Abiquo community edition
* cloud management application for hybrid clouds
* Copyright (C) 2008-2010 - Abiquo Holdings S.L.
*
* This application 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 under
* version 3 of the License
*
* This software 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 v.3 for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
package com.abiquo.commons.plugin.exception;
import com.google.common.base.Enums;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
public class HypervisorPluginException extends ComputeException
{
private static final long serialVersionUID = -8193478568635170646L;
protected final HypervisorPluginError error;
/** Error reported from the cloud provider. */
private final Optional internalCode;
protected Throwable afterRollbackException = null;
public HypervisorPluginException(final HypervisorPluginError error)
{
super(error.getMessage());
this.error = error;
this.internalCode = Optional.absent();
}
public HypervisorPluginException(final HypervisorPluginError error, final Throwable t)
{
super(formatMessage(error.getMessage(), null, null), t);
this.error = error;
this.internalCode = Optional.absent();
}
public HypervisorPluginException(final HypervisorPluginError error, final String detail)
{
super(formatMessage(error.getMessage(), detail, null));
this.error = error;
this.internalCode = Optional.absent();
}
/**
* This method won't prepend the default error message as the message might already contain it.
* Use it when you want to set the exact exception message.
*/
public HypervisorPluginException(final String errorCode, final String message)
{
super(formatMessage(message == null ? "Error:" + errorCode : message, null, null));
this.error =
Enums.getIfPresent(HypervisorPluginError.class, errorCode).or(
HypervisorPluginError.UNHANDLED);
this.internalCode = Optional.absent();
}
public HypervisorPluginException(final HypervisorPluginError error, final String detail,
final Throwable t)
{
super(formatMessage(error.getMessage(), detail, null), t);
this.error = error;
this.internalCode = Optional.absent();
}
public HypervisorPluginException(final HypervisorPluginError error, final String message,
final String internalCode, final Throwable throwable)
{
super(formatMessage(error.getMessage(), message, internalCode), throwable);
this.error = error;
this.internalCode = Optional.fromNullable(internalCode);
}
public HypervisorPluginException(final HypervisorPluginError error, final String detail,
final String internalCode)
{
super(formatMessage(error.getMessage(), detail, internalCode));
this.error = error;
this.internalCode = Optional.fromNullable(internalCode);
}
/**
* This method won't prepend the default error message as the message might already contain it.
* Use it when you want to set the exact exception message.
*
* The exception message cannot be null
.
*/
public HypervisorPluginException(final String errorCode, final String message,
final String internalCode)
{
super(formatMessage(message == null ? "Error:" + errorCode : message, null, null));
this.error =
Enums.getIfPresent(HypervisorPluginError.class, errorCode).or(
HypervisorPluginError.UNHANDLED);
this.internalCode = Optional.fromNullable(internalCode);
}
/**
* Use only for unhandled exceptions
*/
public HypervisorPluginException(final Throwable cause)
{
super("Unhandled DatacenterJob exception", cause);
this.error = HypervisorPluginError.UNHANDLED;
this.internalCode = Optional.absent();
}
public Throwable getAfterRollbackException()
{
return afterRollbackException;
}
public void setAfterRollbackException(final Throwable afterRollbackException)
{
this.afterRollbackException = afterRollbackException;
}
public HypervisorPluginError getError()
{
return error;
}
/** Error reported from the cloud provider. */
public Optional getInternalCode()
{
return internalCode;
}
private static String formatMessage(final String errorMessage, final String details,
final String internalCode)
{
StringBuilder sb = new StringBuilder();
sb.append(errorMessage);
if (!errorMessage.endsWith("."))
{
sb.append(".");
}
if (details != null)
{
sb.append("\n");
sb.append(details);
}
if (!Strings.isNullOrEmpty(internalCode))
{
sb.append(" (").append(internalCode).append(")");
}
return sb.toString();
}
}