Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/thermidor/xml/output/HandlerRegister.java


1   package com.thermidor.xml.output;
2   import java.util.*;
3   import java.lang.reflect.*;
4   import com.thermidor.util.exception.GenericException;
5   import com.thermidor.util.Assert;
6   public class HandlerRegister extends Hashtable {
7     public class HandlerRegisterException extends GenericException {
8       public HandlerRegisterException(){super();}
9       public HandlerRegisterException(String message){super(message);}
10      public HandlerRegisterException(Throwable cause){super(cause);}
11      public HandlerRegisterException(String message,Throwable cause){
12        super(cause,message);
13      }
14    }
15    public class InvalidHandlerClass extends HandlerRegisterException {
16      public InvalidHandlerClass(){super();}
17      public InvalidHandlerClass(String message){super(message);}
18      public InvalidHandlerClass(Throwable cause){super(cause);}
19      public InvalidHandlerClass(String message,Throwable cause){
20        super(message,cause);
21      }
22    }
23    public class NoHandlerForType extends HandlerRegisterException {
24      public NoHandlerForType(){super();}
25      public NoHandlerForType(String message){super(message);}
26      public NoHandlerForType(Throwable cause){super(cause);}
27      public NoHandlerForType(String message,Throwable cause){
28        super(message,cause);
29      }
30    }
31    public class HandlerInitException extends HandlerRegisterException {
32      public HandlerInitException(){super();}
33      public HandlerInitException(String message){super(message);}
34      public HandlerInitException(Throwable cause){super(cause);}
35      public HandlerInitException(String message,Throwable cause){
36        super(message,cause);
37      }
38    }
39    private static final Class[] DEFAULT_CONS = new Class[0];
40    private static final Object[] DEFAULT_CONS_ARGS = new Object[0];
41    public HandlerRegister() {
42      super();
43    }
44    public void putHandlerType(OutputType type,Class handlerClass) 
45      throws HandlerRegisterException {
46      Assert.notNull
47        (type,
48         "putHandlerType(OutputType type,Class handlerClass)" +
49         "type must be non null");
50      Assert.notNull
51        (handlerClass,
52         "putHandlerType(OutputType type,Class handlerClass)" +
53         "handlerClass must be non null");
54  
55      if( !OutputHandler.class.isAssignableFrom(handlerClass) ) {
56        throw new InvalidHandlerClass
57          ("class [" + 
58           handlerClass + 
59           "] must be derived from [" +
60           OutputHandler.class + "]");
61      }
62      try {
63        Constructor cons = handlerClass.getConstructor(DEFAULT_CONS);
64        put(type,cons);
65      }
66      catch(NoSuchMethodException nsme) {
67        throw new InvalidHandlerClass
68          ("class [" + 
69           handlerClass + 
70           "] must have a default constructor",nsme);
71      }
72      catch(SecurityException se) {
73        throw new InvalidHandlerClass
74          ("VM prevented access to default constructor of class [" + 
75           handlerClass,se);
76      }
77    }
78    public OutputHandler newHandler(OutputType type) 
79    throws HandlerRegisterException {
80      OutputHandler retval = null;
81      Constructor cons=(Constructor)get(type);
82      if(cons == null) {
83        throw new NoHandlerForType
84          ("no handler registered for type [" +
85           type +
86           "]");
87      }
88      try {
89        retval = (OutputHandler)cons.newInstance(DEFAULT_CONS_ARGS);
90      }
91      catch(Exception e) {
92        if(e instanceof RuntimeException) {
93          throw (RuntimeException)e;
94        }
95        throw new HandlerInitException
96          ("could not initialise handler for type [" + 
97           type + "]",e);
98      }
99      return retval;
100   }
101 }