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 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 reponsible for converting events into exceptions.
  24. */
  25. public class EventExceptionManager {
  26. private static final Map EXCEPTION_FACTORIES = new java.util.HashMap();
  27. static {
  28. Iterator iter;
  29. iter = Service.providers(ExceptionFactory.class, true);
  30. while (iter.hasNext()) {
  31. ExceptionFactory factory = (ExceptionFactory)iter.next();
  32. EXCEPTION_FACTORIES.put(factory.getExceptionClass().getName(), factory);
  33. }
  34. }
  35. /**
  36. * Converts an event into an exception and throws that. If the exception class is null,
  37. * a {@link RuntimeException} will be thrown.
  38. * @param event the event to be converted
  39. * @param exceptionClass the exception class to be thrown
  40. * @throws Throwable this happens always
  41. */
  42. public static void throwException(Event event, String exceptionClass) throws Throwable {
  43. if (exceptionClass != null) {
  44. ExceptionFactory factory = (ExceptionFactory)EXCEPTION_FACTORIES.get(exceptionClass);
  45. if (factory != null) {
  46. throw factory.createException(event);
  47. } else {
  48. throw new IllegalArgumentException(
  49. "No such ExceptionFactory available: " + exceptionClass);
  50. }
  51. } else {
  52. String msg = EventFormatter.format(event);
  53. throw new RuntimeException(msg);
  54. }
  55. }
  56. /**
  57. * This interface is implementation by exception factories that can create exceptions from
  58. * events.
  59. */
  60. public interface ExceptionFactory {
  61. /**
  62. * Creates an exception from an event.
  63. * @param event the event
  64. * @return the newly created exception
  65. */
  66. Throwable createException(Event event);
  67. /**
  68. * Returns the {@link Exception} class created by this factory.
  69. * @return the exception class
  70. */
  71. Class getExceptionClass();
  72. }
  73. }