001: package com.xoetrope.service.servlet;
002:
003: import com.xoetrope.carousel.build.BuildProperties;
004: import com.xoetrope.service.LicenceCheck;
005: import java.awt.Frame;
006: import java.util.Hashtable;
007: import java.util.Vector;
008: import javax.swing.JOptionPane;
009: import net.xoetrope.optional.service.ServiceContext;
010: import net.xoetrope.optional.service.ServiceProxy;
011: import net.xoetrope.optional.service.ServiceProxyArgs;
012: import net.xoetrope.optional.service.ServiceProxyException;
013: import net.xoetrope.optional.service.XRouteManager;
014: import net.xoetrope.xui.XProject;
015:
016: import net.xoetrope.xui.XProjectManager;
017: import net.xoetrope.xui.data.XModel;
018:
019: /**
020: * Description ServiceProxy Object for handling session management on the server
021: * To be used with servlet engines. The session can be managed automatically or
022: * manually depending on the 'selfmanaged' attribute of the XML declaration.
023: *
024: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
025: * the GNU Public License (GPL), please see license.txt for more details. If
026: * you make commercial use of this software you must purchase a commercial
027: * license from Xoetrope.</p>
028: * <p> $Revision: 1.14 $</p>
029: */
030: public class SessionManager extends ServiceProxy {
031: private String sessionKey;
032: private XModel sessIdMdl;
033:
034: public static final String INVALID_SESSION = "INVALID_SESSION";
035: public static final String NEW_SESSION = "NEW_SESSION";
036: public static final String VALID_SESSION = "VALID_SESSION";
037:
038: public static final String ARG_NAME_SESSION_STATE = "session:state";
039:
040: /**
041: * The error which is returned in case the session has expired
042: */
043: public static final String ERR_SESSION_EXPIRED = "sessionerror:sessionexpired";
044:
045: /**
046: * The name of the parameter which will be used to check the session validity
047: */
048: public static final String ARG_NAME_CHECKPARAM = "session:checkparam";
049:
050: /**
051: * The session argument to be passes to the servlet engine
052: */
053: public static final String ARG_NAME_PASS_SESSION = ";jsessionid";
054:
055: /**
056: * The returned session key
057: */
058: public static final String ARG_NAME_RETURN_SESSION = "session:sessionkey";
059:
060: /**
061: * If the sessionmanager is selfmanaged and this parameter is set it sets and
062: * retrieves the session key in this node in the model.
063: */
064: public static final String ARG_NAME_MODELSESSID = "session:modelsessionid";
065:
066: /**
067: * Indicates whether the session key is managed automatically by an instance of this object
068: */
069: protected boolean selfManaged = false;
070:
071: /**
072: * The owner project and the context in which this object operates.
073: */
074: protected XProject currentProject = XProjectManager
075: .getCurrentProject();
076:
077: /**
078: * The overloaded call for the ServiceProxy
079: * Syntax for servler SessionID...
080: * ;jsessionid=789ec62424e5865edf3ceec4f0976bdf
081: * @param method The name of the service
082: * @param context The ServiceContext containing arguments
083: * @throws net.xoetrope.optional.service.ServiceProxyException Throw ServiceProxyException if the call fails
084: * @return The result of the call
085: */
086: public Object call(String method, ServiceContext context)
087: throws ServiceProxyException {
088: Object res = null;
089: ServiceProxyArgs args = context.getArgs();
090:
091: boolean client = (side == XRouteManager.CLIENT_SIDE);
092: if (client) {
093: if (BuildProperties.IS_EVAL)
094: LicenceCheck.checkLicence();
095:
096: /**
097: * Pass the current session key to the server, call the next proxy. If
098: * there was an error on the server show the error message otherwise store
099: * the session key for future calls.
100: */
101: String modelSessid = (String) args
102: .getPassParam(ARG_NAME_MODELSESSID);
103: if ((modelSessid != null) && (modelSessid.length() > 0))
104: sessIdMdl = (XModel) XProjectManager.getModel().get(
105: modelSessid);
106:
107: if (sessIdMdl != null)
108: sessionKey = (String) sessIdMdl.get();
109:
110: if (selfManaged)
111: args.setPassParam(ARG_NAME_PASS_SESSION, sessionKey);
112:
113: res = nextProxy.call(method, context);
114: String error = getSessionError(context);
115: if (error != null) {
116: showErrorMessage();
117: setSessionModelValue(args, null);
118: } else if ((selfManaged) && !context.hasErrors())
119: setSessionModelValue(args, (String) args
120: .getReturnParam(ARG_NAME_RETURN_SESSION));
121: }
122: return res;
123: }
124:
125: /**
126: * @param args
127: * @param value
128: */
129: protected void setSessionModelValue(ServiceProxyArgs args,
130: String value) {
131: if (sessIdMdl != null)
132: sessIdMdl.set(value);
133: }
134:
135: /**
136: * @param context
137: */
138: protected String getSessionError(ServiceContext context) {
139: Vector errors = context.getErrors();
140: for (int i = 0; i < errors.size(); i++) {
141: String err = (String) errors.get(i);
142: if (err.compareTo(ERR_SESSION_EXPIRED) == 0)
143: return ERR_SESSION_EXPIRED;
144: }
145: return null;
146: }
147:
148: /**
149: * If there is a problem with the session show a message on the client.
150: */
151: private void showErrorMessage() {
152: Frame fra = currentProject.getAppFrame();
153: JOptionPane dlg = new JOptionPane();
154: dlg.showMessageDialog(fra,
155: "You session has expired... Please log on again!");
156: }
157:
158: /**
159: * Check the self managed attribute of the ServiceProxy so we can store state
160: * or pass it back through the return arguments
161: * @param attribs Hashtable of attributes from the XML declaration
162: */
163: public void setAttributes(Hashtable attribs) {
164: String temp = (String) attribs.get("selfmanaged");
165: if (temp != null) {
166: selfManaged = temp.compareTo("true") == 0;
167: if (side == XRouteManager.CLIENT_SIDE) {
168: String modelSessid = (String) attribs.get("modelid");
169: if (modelSessid != null)
170: sessIdMdl = (XModel) XProjectManager.getModel()
171: .get(modelSessid);
172: }
173: }
174: }
175: }
|