You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EventExceptionManager.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.events;
  19. import java.util.Iterator;
  20. import java.util.Map;
  21. import org.apache.xmlgraphics.util.Service;
  22. /**
  23. * This class is responsible for converting events into exceptions.
  24. */
  25. public final class EventExceptionManager {
  26. private EventExceptionManager() {
  27. }
  28. private static final Map EXCEPTION_FACTORIES = new java.util.HashMap();
  29. static {
  30. Iterator iter;
  31. iter = Service.providers(ExceptionFactory.class, true);
  32. while (iter.hasNext()) {
  33. ExceptionFactory factory = (ExceptionFactory)iter.next();
  34. EXCEPTION_FACTORIES.put(factory.getExceptionClass().getName(), factory);
  35. }
  36. }
  37. /**
  38. * Converts an event into an exception and throws that. If the exception class is null,
  39. * a {@link RuntimeException} will be thrown.
  40. * @param event the event to be converted
  41. * @param exceptionClass the exception class to be thrown
  42. * @throws Throwable this happens always
  43. */
  44. public static void throwException(Event event, String exceptionClass) throws Throwable {
  45. if (exceptionClass != null) {
  46. ExceptionFactory factory = (ExceptionFactory)EXCEPTION_FACTORIES.get(exceptionClass);
  47. if (factory != null) {
  48. throw factory.createException(event);
  49. } else {
  50. throw new IllegalArgumentException(
  51. "No such ExceptionFactory available: " + exceptionClass);
  52. }
  53. } else {
  54. String msg = EventFormatter.format(event);
  55. //Get original exception as cause if it is given as one of the parameters
  56. Throwable t = null;
  57. Iterator iter = event.getParams().values().iterator();
  58. while (iter.hasNext()) {
  59. Object o = iter.next();
  60. if (o instanceof Throwable) {
  61. t = (Throwable)o;
  62. break;
  63. }
  64. }
  65. if (t != null) {
  66. throw new RuntimeException(msg, t);
  67. } else {
  68. throw new RuntimeException(msg);
  69. }
  70. }
  71. }
  72. /**
  73. * This interface is implementation by exception factories that can create exceptions from
  74. * events.
  75. */
  76. public interface ExceptionFactory {
  77. /**
  78. * Creates an exception from an event.
  79. * @param event the event
  80. * @return the newly created exception
  81. */
  82. Throwable createException(Event event);
  83. /**
  84. * Returns the {@link Exception} class created by this factory.
  85. * @return the exception class
  86. */
  87. Class getExceptionClass();
  88. }
  89. }