001: /*
002: * Copyright (c) 2005 Your Corporation. All Rights Reserved.
003: */
004: package org.wings.plaf.css.dwr;
005:
006: import java.lang.reflect.Method;
007: import java.util.Iterator;
008: import java.util.Set;
009: import org.wings.header.SessionHeaders;
010: import org.wings.header.JavaScriptHeader;
011: import org.wings.session.SessionManager;
012: import org.wings.session.Session;
013: import org.wings.resource.SessionResource;
014: import org.directwebremoting.WebContextFactory;
015: import org.directwebremoting.WebContextFactory.WebContextBuilder;
016: import org.directwebremoting.impl.DefaultWebContextBuilder;
017: import org.directwebremoting.impl.DefaultContainer;
018:
019: import java.util.Collection;
020: import java.io.Serializable;
021:
022: /**
023: * @author hengels
024: */
025: public class CallableManager implements Serializable {
026:
027: private static final long serialVersionUID = 1L;
028:
029: private SessionCreatorManager creatorManager = new SessionCreatorManager();
030: private SessionAccessControl accessControl = new SessionAccessControl();
031:
032: public static CallableManager getInstance() {
033: Session session = SessionManager.getSession();
034: CallableManager callableManager = (CallableManager) session
035: .getProperty("CallableManager");
036: if (callableManager == null) {
037: callableManager = new CallableManager();
038: session.setProperty("CallableManager", callableManager);
039: }
040: return callableManager;
041: }
042:
043: /**
044: * Register callable and expose all public methods.
045: * @param scriptName
046: * @param callable
047: */
048: public void registerCallable(String scriptName, Object callable) {
049: Session session = SessionManager.getSession();
050:
051: WebContextBuilder builder = new DefaultWebContextBuilder();
052: builder.set(session.getServletRequest(), session
053: .getServletResponse(), null, session
054: .getServletContext(), new DefaultContainer());
055:
056: WebContextFactory.setWebContextBuilder(builder);
057:
058: creatorManager.addCreator(scriptName, new SessionCreator(
059: callable));
060: builder.unset();
061:
062: JavaScriptHeader header = new JavaScriptHeader(
063: new SessionResource("../dwr/interface/" + scriptName
064: + ".js"));
065: SessionHeaders.getInstance().registerHeader(header);
066: }
067:
068: /**
069: * Register callable and only expose a limited number of methods.
070: * @param scriptName
071: * @param callable
072: * @param exportInterface interface that defines the methods to be exposed
073: * to the client by DWR
074: */
075: public void registerCallable(String scriptName, Object callable,
076: Class exportInterface) {
077: Session session = SessionManager.getSession();
078:
079: WebContextBuilder builder = new DefaultWebContextBuilder();
080: builder.set(session.getServletRequest(), session
081: .getServletResponse(), null, session
082: .getServletContext(), new DefaultContainer());
083:
084: WebContextFactory.setWebContextBuilder(builder);
085:
086: Method[] methods = exportInterface.getMethods();
087:
088: for (int i = 0; i < methods.length; i++) {
089: accessControl.addIncludeRule(scriptName, methods[i]
090: .getName());
091: }
092:
093: creatorManager.addCreator(scriptName, new SessionCreator(
094: callable));
095: builder.unset();
096:
097: JavaScriptHeader header = new JavaScriptHeader(
098: new SessionResource("../dwr/interface/" + scriptName
099: + ".js"));
100: SessionHeaders.getInstance().registerHeader(header);
101: }
102:
103: public void unregisterCallable(String scriptName) {
104: Session session = SessionManager.getSession();
105:
106: WebContextBuilder builder = new DefaultWebContextBuilder();
107: builder.set(session.getServletRequest(), session
108: .getServletResponse(), null, session
109: .getServletContext(), new DefaultContainer());
110:
111: WebContextFactory.setWebContextBuilder(builder);
112: creatorManager.removeCreator(scriptName);
113: builder.unset();
114:
115: JavaScriptHeader header = new JavaScriptHeader(
116: new SessionResource("../dwr/interface/" + scriptName
117: + ".js"));
118: SessionHeaders.getInstance().deregisterHeader(header);
119: }
120:
121: public boolean containsCallable(String scriptName) {
122: Session session = SessionManager.getSession();
123:
124: WebContextBuilder builder = new DefaultWebContextBuilder();
125: builder.set(session.getServletRequest(), session
126: .getServletResponse(), null, session
127: .getServletContext(), new DefaultContainer());
128:
129: WebContextFactory.setWebContextBuilder(builder);
130: boolean b = creatorManager.getCreatorNames().contains(
131: scriptName);
132: builder.unset();
133:
134: return b;
135: }
136:
137: public Collection callableNames() {
138: Session session = SessionManager.getSession();
139:
140: WebContextBuilder builder = new DefaultWebContextBuilder();
141: builder.set(session.getServletRequest(), session
142: .getServletResponse(), null, session
143: .getServletContext(), new DefaultContainer());
144:
145: WebContextFactory.setWebContextBuilder(builder);
146: Collection c = creatorManager.getCreatorNames();
147: builder.unset();
148:
149: return c;
150: }
151: }
|