1 package dolda.jsvc.util;
4 import java.lang.reflect.*;
6 public class PerSession implements Responder {
7 private final Class<?> rcl;
8 private final Class<?> dcl;
10 public PerSession(Class<?> rcl, Class<?> dcl) {
15 public PerSession(Class<?> rcl) {
19 private Object makedata(Session sess) {
22 return(dcl.getConstructor(Session.class).newInstance(sess));
23 } catch(NoSuchMethodException e) {
26 return(dcl.getConstructor().newInstance());
27 } catch(NoSuchMethodException e) {
29 } catch(InstantiationException e) {
30 throw(new RuntimeException(e));
31 } catch(IllegalAccessException e) {
32 throw(new RuntimeException(e));
33 } catch(InvocationTargetException e) {
34 throw(new RuntimeException(e));
36 throw(new RuntimeException("Found no way to create an instance of " + dcl.getName()));
39 private Object getdata(Session sess) {
40 Object d = sess.get(dcl, null);
48 private Responder create(Session sess) {
52 return((Responder)rcl.getMethod("responder", dcl).invoke(null, getdata(sess)));
53 } catch(NoSuchMethodException e) {
57 return((Responder)rcl.getMethod("responder", Session.class).invoke(null, sess));
58 } catch(NoSuchMethodException e) {
61 return((Responder)rcl.getMethod("responder").invoke(null));
62 } catch(NoSuchMethodException e) {
66 return((Responder)rcl.getConstructor(dcl).newInstance(getdata(sess)));
67 } catch(NoSuchMethodException e) {
71 return((Responder)rcl.getConstructor(Session.class).newInstance(sess));
72 } catch(NoSuchMethodException e) {
75 return((Responder)rcl.getConstructor().newInstance());
76 } catch(NoSuchMethodException e) {
78 } catch(InstantiationException e) {
79 throw(new RuntimeException(e));
80 } catch(IllegalAccessException e) {
81 throw(new RuntimeException(e));
82 } catch(InvocationTargetException e) {
83 throw(new RuntimeException(e));
85 throw(new RuntimeException("Found no way to create a responder from the class " + rcl.getName()));
88 public void respond(Request req) {
89 Session sess = Session.get(req);
92 resp = (Responder)sess.get(rcl, null);