KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > beehive > PageFlowCreator


1 package org.directwebremoting.beehive;
2
3 import java.lang.reflect.InvocationTargetException JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5
6 import javax.servlet.http.HttpServletRequest JavaDoc;
7
8 import org.directwebremoting.WebContextFactory;
9 import org.directwebremoting.create.AbstractCreator;
10 import org.directwebremoting.extend.Creator;
11 import org.directwebremoting.util.LocalUtil;
12 import org.directwebremoting.util.Logger;
13
14 /**
15  * Page Flow Creator
16  * The name Creator is a little misleading in that implies that a PageFlow is
17  * being created. This class merely returns the current PageFlowController from
18  * the Request
19  * @author Kevin Conaway
20  * @author Joe Walker [joe at getahead dot ltd dot uk]
21  */

22 public class PageFlowCreator extends AbstractCreator implements Creator
23 {
24     /**
25      * Test to see what implementations of PageFlow are available.
26      * @throws ClassNotFoundException If neither Beehive or Weblogic are around.
27      */

28     public PageFlowCreator() throws ClassNotFoundException JavaDoc
29     {
30         // noinspection EmptyCatchBlock
31
try
32         {
33             bhFlowClass = LocalUtil.classForName("org.apache.beehive.netui.pageflow.PageFlowController");
34
35             Class JavaDoc bhUtilClass = LocalUtil.classForName("org.apache.beehive.netui.pageflow.PageFlowUtils");
36             bhGetter = bhUtilClass.getMethod("getCurrentPageFlow", new Class JavaDoc[] { HttpServletRequest JavaDoc.class });
37         }
38         catch (Exception JavaDoc ex)
39         {
40             // We're expecting this to fail, and notice below
41
}
42
43         // noinspection EmptyCatchBlock
44
try
45         {
46             wlFlowClass = LocalUtil.classForName("com.bea.wlw.netui.pageflow.PageFlowController");
47
48             Class JavaDoc wlUtilClass = LocalUtil.classForName("com.bea.wlw.netui.pageflow.PageFlowUtils");
49             wlGetter = wlUtilClass.getMethod("getCurrentPageFlow", new Class JavaDoc[] { HttpServletRequest JavaDoc.class });
50         }
51         catch (Exception JavaDoc ex)
52         {
53             // We're expecting this to fail, and notice below
54
}
55
56         if ((bhGetter == null && wlGetter == null) || (bhFlowClass == null && wlFlowClass == null))
57         {
58             throw new ClassNotFoundException JavaDoc("Beehive/Weblogic jar file not available.");
59         }
60     }
61
62     /**
63      * What do we do if both Weblogic and Beehive are available.
64      * The default is to use Beehive, but this allows us to alter that.
65      * @param forceWebLogic Do we use Weblogic if both are available.
66      */

67     public void setForceWebLogic(boolean forceWebLogic)
68     {
69         if (forceWebLogic)
70         {
71             bhGetter = null;
72             bhFlowClass = null;
73         }
74     }
75
76     /* (non-Javadoc)
77      * @see org.directwebremoting.Creator#getInstance()
78      */

79     public Object JavaDoc getInstance() throws InstantiationException JavaDoc
80     {
81         if (getter == null)
82         {
83             getter = (bhGetter != null) ? bhGetter : wlGetter;
84         }
85
86         try
87         {
88             HttpServletRequest JavaDoc request = WebContextFactory.get().getHttpServletRequest();
89             return getter.invoke(null, new Object JavaDoc[] { request });
90         }
91         catch (InvocationTargetException JavaDoc ex)
92         {
93             throw new InstantiationException JavaDoc(ex.getTargetException().toString());
94         }
95         catch (Exception JavaDoc ex)
96         {
97             throw new InstantiationException JavaDoc(ex.toString());
98         }
99     }
100
101     /**
102      * @return The PageFlowController that we are using (Beehive/Weblogic)
103      */

104     public Class JavaDoc getType()
105     {
106         if (instanceType == null)
107         {
108             try
109             {
110                 instanceType = getInstance().getClass();
111             }
112             catch (InstantiationException JavaDoc ex)
113             {
114                 log.error("Failed to instansiate object to detect type.", ex);
115                 return Object JavaDoc.class;
116             }
117         }
118
119         return instanceType;
120     }
121
122     /**
123      * The log stream
124      */

125     private static final Logger log = Logger.getLogger(PageFlowCreator.class);
126
127     private Class JavaDoc instanceType;
128
129     private Method JavaDoc getter;
130
131     private Method JavaDoc bhGetter;
132
133     private Method JavaDoc wlGetter;
134
135     private Class JavaDoc bhFlowClass;
136
137     private Class JavaDoc wlFlowClass;
138 }
139
Popular Tags