001: package org.directwebremoting.beehive;
002:
003: import java.lang.reflect.InvocationTargetException;
004: import java.lang.reflect.Method;
005:
006: import javax.servlet.http.HttpServletRequest;
007:
008: import org.apache.commons.logging.LogFactory;
009: import org.apache.commons.logging.Log;
010: import org.directwebremoting.WebContextFactory;
011: import org.directwebremoting.create.AbstractCreator;
012: import org.directwebremoting.extend.Creator;
013: import org.directwebremoting.util.LocalUtil;
014:
015: /**
016: * Page Flow Creator
017: * The name Creator is a little misleading in that implies that a PageFlow is
018: * being created. This class merely returns the current PageFlowController from
019: * the Request
020: * @author Kevin Conaway
021: * @author Joe Walker [joe at getahead dot ltd dot uk]
022: */
023: public class PageFlowCreator extends AbstractCreator implements Creator {
024: /**
025: * Test to see what implementations of PageFlow are available.
026: * @throws ClassNotFoundException If neither Beehive or Weblogic are around.
027: */
028: public PageFlowCreator() throws ClassNotFoundException {
029: try {
030: bhFlowClass = LocalUtil
031: .classForName("org.apache.beehive.netui.pageflow.PageFlowController");
032:
033: Class<?> bhUtilClass = LocalUtil
034: .classForName("org.apache.beehive.netui.pageflow.PageFlowUtils");
035: bhGetter = bhUtilClass.getMethod("getCurrentPageFlow",
036: HttpServletRequest.class);
037: } catch (Exception ex) {
038: // We're expecting this to fail, and notice below
039: }
040:
041: try {
042: wlFlowClass = LocalUtil
043: .classForName("com.bea.wlw.netui.pageflow.PageFlowController");
044:
045: Class<?> wlUtilClass = LocalUtil
046: .classForName("com.bea.wlw.netui.pageflow.PageFlowUtils");
047: wlGetter = wlUtilClass.getMethod("getCurrentPageFlow",
048: HttpServletRequest.class);
049: } catch (Exception ex) {
050: // We're expecting this to fail, and notice below
051: }
052:
053: if ((bhGetter == null && wlGetter == null)
054: || (bhFlowClass == null && wlFlowClass == null)) {
055: throw new ClassNotFoundException(
056: "Beehive/Weblogic jar file not available.");
057: }
058: }
059:
060: /**
061: * What do we do if both Weblogic and Beehive are available.
062: * The default is to use Beehive, but this allows us to alter that.
063: * @param forceWebLogic Do we use Weblogic if both are available.
064: */
065: public void setForceWebLogic(boolean forceWebLogic) {
066: if (forceWebLogic) {
067: bhGetter = null;
068: bhFlowClass = null;
069: }
070: }
071:
072: /* (non-Javadoc)
073: * @see org.directwebremoting.Creator#getInstance()
074: */
075: public Object getInstance() throws InstantiationException {
076: if (getter == null) {
077: getter = (bhGetter != null) ? bhGetter : wlGetter;
078: }
079:
080: try {
081: HttpServletRequest request = WebContextFactory.get()
082: .getHttpServletRequest();
083: return getter.invoke(null, request);
084: } catch (InvocationTargetException ex) {
085: throw new InstantiationException(ex.getTargetException()
086: .toString());
087: } catch (Exception ex) {
088: throw new InstantiationException(ex.toString());
089: }
090: }
091:
092: /**
093: * @return The PageFlowController that we are using (Beehive/Weblogic)
094: */
095: public Class<?> getType() {
096: if (instanceType == null) {
097: try {
098: instanceType = getInstance().getClass();
099: } catch (InstantiationException ex) {
100: log.error(
101: "Failed to instansiate object to detect type.",
102: ex);
103: return Object.class;
104: }
105: }
106:
107: return instanceType;
108: }
109:
110: /**
111: * The log stream
112: */
113: private static final Log log = LogFactory
114: .getLog(PageFlowCreator.class);
115:
116: private Class<?> instanceType = null;
117:
118: private Method getter = null;
119:
120: private Method bhGetter = null;
121:
122: private Method wlGetter = null;
123:
124: private Class<?> bhFlowClass = null;
125:
126: private Class<?> wlFlowClass = null;
127: }
|