[jboss-cvs] JBossAS SVN: r91160 - in projects/jboss-deployers/trunk: deployers-spi/src/main/java/org/jboss/deployers/spi/deployer and 1 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Jul 13 10:02:28 EDT 2009
Author: alesj
Date: 2009-07-13 10:02:28 -0400 (Mon, 13 Jul 2009)
New Revision: 91160
Added:
projects/jboss-deployers/trunk/deployers-spi/src/main/java/org/jboss/deployers/spi/deployer/exceptions/
projects/jboss-deployers/trunk/deployers-spi/src/main/java/org/jboss/deployers/spi/deployer/exceptions/ExceptionHandler.java
Modified:
projects/jboss-deployers/trunk/deployers-impl/src/main/java/org/jboss/deployers/plugins/deployers/DeployersImpl.java
Log:
[JBDEPLOY-205]; add exception handlers.
Modified: projects/jboss-deployers/trunk/deployers-impl/src/main/java/org/jboss/deployers/plugins/deployers/DeployersImpl.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-impl/src/main/java/org/jboss/deployers/plugins/deployers/DeployersImpl.java 2009-07-13 13:50:14 UTC (rev 91159)
+++ projects/jboss-deployers/trunk/deployers-impl/src/main/java/org/jboss/deployers/plugins/deployers/DeployersImpl.java 2009-07-13 14:02:28 UTC (rev 91160)
@@ -33,7 +33,6 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
-
import javax.management.MBeanRegistration;
import javax.management.MBeanServer;
import javax.management.ObjectName;
@@ -59,6 +58,7 @@
import org.jboss.deployers.spi.deployer.Deployers;
import org.jboss.deployers.spi.deployer.DeploymentStage;
import org.jboss.deployers.spi.deployer.DeploymentStages;
+import org.jboss.deployers.spi.deployer.exceptions.ExceptionHandler;
import org.jboss.deployers.spi.deployer.managed.ManagedObjectCreator;
import org.jboss.deployers.structure.spi.DeploymentContext;
import org.jboss.deployers.structure.spi.DeploymentMBean;
@@ -134,6 +134,9 @@
/** The ManagedDeploymentCreator plugin */
private ManagedObjectCreator mgtObjectCreator = null;
+ /** The exception handlers */
+ private Set<ExceptionHandler<? extends Throwable>> exceptionHandlers = new HashSet<ExceptionHandler<? extends Throwable>>();
+
/**
* Create a new DeployersImpl.
*
@@ -295,6 +298,7 @@
{
if (deployer == null)
throw new IllegalArgumentException("Null deployer");
+
deployers.remove(new DeployerWrapper(deployer));
DeploymentStage stage = deployer.getStage();
@@ -363,6 +367,62 @@
}
/**
+ * Set exception handlers.
+ *
+ * @param exceptionHandlers the exception handlers
+ */
+ public void setExceptionHandlers(Set<ExceptionHandler<? extends Throwable>> exceptionHandlers)
+ {
+ if (exceptionHandlers == null)
+ throw new IllegalArgumentException("Null exception handlers");
+
+ for (ExceptionHandler<? extends Throwable> handler : exceptionHandlers)
+ checkExceptionHandler(handler);
+
+ this.exceptionHandlers = exceptionHandlers;
+ }
+
+ /**
+ * Check exception handler.
+ *
+ * @param handler the handler
+ */
+ protected void checkExceptionHandler(ExceptionHandler<? extends Throwable> handler)
+ {
+ if (handler == null)
+ throw new IllegalArgumentException("Null handler");
+
+ if (handler.getExceptionType() == null)
+ throw new IllegalArgumentException("Null exception type: " + handler);
+ }
+
+ /**
+ * Add exception handler.
+ *
+ * @param handler the exception handler
+ * @return Set::add(handler)
+ */
+ public boolean addExceptionHandler(ExceptionHandler<? extends Throwable> handler)
+ {
+ checkExceptionHandler(handler);
+ return exceptionHandlers.add(handler);
+ }
+
+ /**
+ * Remove exception handler.
+ *
+ * @param handler the exception handler
+ * @return Set::remove(handler)
+ */
+ public boolean removeExceptionHandler(ExceptionHandler<? extends Throwable> handler)
+ {
+ if (handler == null)
+ throw new IllegalArgumentException("Null handler");
+
+ return exceptionHandlers.remove(handler);
+ }
+
+ /**
* Get the scopeBuilder.
*
* @return the scopeBuilder.
@@ -819,6 +879,79 @@
return result;
}
+ /**
+ * Get root problem of deployment context.
+ * Handle sub causes while going for the root cause.
+ *
+ * @param dc the deployment context
+ * @return the root
+ */
+ protected Throwable getRootCause(DeploymentContext dc)
+ {
+ ControllerContext context = dc.getTransientAttachments().getAttachment(ControllerContext.class);
+ Throwable error = null;
+ if (context != null)
+ error = getRootCause(context);
+
+ Throwable problem = dc.getProblem();
+ return (problem != null) ? problem : error;
+ }
+
+ /**
+ * Get the root cause of a context's problem.
+ * Handle sub causes while going for the root cause.
+ *
+ * @param context the controller context
+ * @return the root
+ */
+ protected Throwable getRootCause(ControllerContext context)
+ {
+ if (context == null)
+ throw new IllegalArgumentException("Null context");
+
+ Throwable original = context.getError();
+ if (original == null)
+ return null;
+
+ Throwable result = original;
+ handleException(original, context);
+ Throwable cause = result.getCause();
+ while (cause != null)
+ {
+ handleException(cause, context);
+
+ result = cause;
+ cause = cause.getCause();
+ }
+ return result;
+ }
+
+ /**
+ * Handle exception.
+ *
+ * @param exception the exception to handle
+ * @param context the context that has this exception as its problem
+ */
+ @SuppressWarnings("unchecked")
+ protected void handleException(Throwable exception, ControllerContext context)
+ {
+ for (ExceptionHandler handler : exceptionHandlers)
+ {
+ Class<? extends Throwable> type = handler.getExceptionType();
+ if (handler.matchExactExceptionType())
+ {
+ if (type.equals(exception.getClass()))
+ {
+ handler.handleException(exception, context);
+ }
+ }
+ else if (type.isInstance(exception))
+ {
+ handler.handleException(exception, context);
+ }
+ }
+ }
+
public void checkComplete(Collection<DeploymentContext> errors, Collection<Deployment> missingDeployer) throws DeploymentException
{
Map<String, Throwable> deploymentsInError = null;
@@ -830,7 +963,7 @@
{
deploymentsInError = new HashMap<String, Throwable>();
for (DeploymentContext context : errors)
- deploymentsInError.put(context.getName(), getRootCause(context.getProblem()));
+ deploymentsInError.put(context.getName(), getRootCause(context));
}
if (missingDeployer != null && missingDeployer.isEmpty() == false)
@@ -882,7 +1015,7 @@
{
if (context.getState().equals(ControllerState.ERROR))
{
- contextsInError.put(context.getName().toString(), getRootCause(context.getError()));
+ contextsInError.put(context.getName().toString(), getRootCause(context));
}
else if (isBeingInstalledAsynchronously(context) == false)
{
Added: projects/jboss-deployers/trunk/deployers-spi/src/main/java/org/jboss/deployers/spi/deployer/exceptions/ExceptionHandler.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-spi/src/main/java/org/jboss/deployers/spi/deployer/exceptions/ExceptionHandler.java (rev 0)
+++ projects/jboss-deployers/trunk/deployers-spi/src/main/java/org/jboss/deployers/spi/deployer/exceptions/ExceptionHandler.java 2009-07-13 14:02:28 UTC (rev 91160)
@@ -0,0 +1,55 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This 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 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 for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.deployers.spi.deployer.exceptions;
+
+import org.jboss.dependency.spi.ControllerContext;
+
+/**
+ * Deployment exception handler.
+ *
+ * @param <T> exact exception type
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public interface ExceptionHandler<T extends Throwable>
+{
+ /**
+ * Get the exception type.
+ *
+ * @return the exception type
+ */
+ Class<T> getExceptionType();
+
+ /**
+ * Do we match exact exception type.
+ *
+ * @return true if we only match T, or false if any super type as well.
+ */
+ boolean matchExactExceptionType();
+
+ /**
+ * Handle exception.
+ *
+ * @param exception the exception to handle
+ * @param context the context the caused the exception
+ */
+ void handleException(T exception, ControllerContext context);
+}
More information about the jboss-cvs-commits
mailing list