improve exception handling

This commit is contained in:
Sebastian Sdorra
2013-07-22 10:35:58 +02:00
parent 96972480ae
commit 264b3b988b
4 changed files with 203 additions and 17 deletions

View File

@@ -0,0 +1,88 @@
/**
* Copyright (c) 2010, Sebastian Sdorra All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer. 2. Redistributions in
* binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution. 3. Neither the name of SCM-Manager;
* nor the names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.repository.api;
/**
* Base exception for all exceptions which can occur during the hook
* initialization.
*
* @author Sebastian Sdorra
* @since 1.33
*/
public class HookException extends RuntimeException
{
/** Field description */
private static final long serialVersionUID = 2572704247316715734L;
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*/
public HookException() {}
/**
* Constructs ...
*
*
* @param message
*/
public HookException(String message)
{
super(message);
}
/**
* Constructs ...
*
*
* @param cause
*/
public HookException(Throwable cause)
{
super(cause);
}
/**
* Constructs ...
*
*
* @param message
* @param cause
*/
public HookException(String message, Throwable cause)
{
super(message, cause);
}
}

View File

@@ -0,0 +1,92 @@
/**
* Copyright (c) 2010, Sebastian Sdorra All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer. 2. Redistributions in
* binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution. 3. Neither the name of SCM-Manager;
* nor the names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.repository.api;
/**
* This exception is thrown if the underlying provider of the
* {@link HookContext} does not support the requested {@link HookFeature}.
*
* @author Sebastian Sdorra
* @since 1.33
*/
public class HookFeatureIsNotSupportedException extends HookException
{
/** Field description */
private static final long serialVersionUID = -7670872902321373610L;
//~--- constructors ---------------------------------------------------------
/**
* Constructs a new {@link HookFeatureIsNotSupportedException}.
*
*
* @param unsupportedFeature feature which is not supported
*/
public HookFeatureIsNotSupportedException(HookFeature unsupportedFeature)
{
super(unsupportedFeature.toString().concat(" is not supported"));
this.unsupportedFeature = unsupportedFeature;
}
/**
* Constructs a new {@link HookFeatureIsNotSupportedException}.
*
*
* @param message message for the exception
* @param unsupportedFeature feature which is not supported
*/
public HookFeatureIsNotSupportedException(String message,
HookFeature unsupportedFeature)
{
super(message);
this.unsupportedFeature = unsupportedFeature;
}
//~--- get methods ----------------------------------------------------------
/**
* Returns the feature which is not supported.
*
*
* @return unsupported hook feature
*/
public HookFeature getUnsupportedFeature()
{
return unsupportedFeature;
}
//~--- fields ---------------------------------------------------------------
/** unsupported hook feature */
private HookFeature unsupportedFeature;
}

View File

@@ -33,7 +33,9 @@ package sonia.scm.repository.spi;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.repository.api.HookException;
import sonia.scm.repository.api.HookFeature;
import sonia.scm.repository.api.HookFeatureIsNotSupportedException;
import sonia.scm.repository.api.HookMessageProvider;
//~--- JDK imports ------------------------------------------------------------
@@ -56,16 +58,28 @@ public abstract class HookContextProvider
*/
public final HookMessageProvider getMessageProvider()
{
if (clientFinalization)
if (clientDisconnected)
{
// TODO
throw new IllegalArgumentException();
throw new HookException(
"message provider is only available in a synchronous hook execution.");
}
return createMessageProvider();
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*/
final void handleClientDisconnect()
{
clientDisconnected = true;
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
@@ -82,20 +96,12 @@ public abstract class HookContextProvider
*/
public HookChangesetProvider getChangesetProvider()
{
return null;
throw new HookFeatureIsNotSupportedException(
HookFeature.CHANGESET_PROVIDER);
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*/
void handleClientFinalization()
{
clientFinalization = true;
}
/**
* Method description
*
@@ -104,11 +110,11 @@ public abstract class HookContextProvider
*/
protected HookMessageProvider createMessageProvider()
{
return null;
throw new HookFeatureIsNotSupportedException(HookFeature.MESSAGE_PROVIDER);
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private boolean clientFinalization = false;
private boolean clientDisconnected = false;
}

View File

@@ -169,7 +169,7 @@ public final class HookEventFacade
new ExtendedRepositoryHookEvent(context, repository, type);
repositoryManager.fireHookEvent(repository, event);
hookContextProvider.handleClientFinalization();
hookContextProvider.handleClientDisconnect();
}
//~--- fields -------------------------------------------------------------