001: /*
002: * This file is not part of the ItsNat framework.
003: *
004: * Original source code use and closed source derivatives are authorized
005: * to third parties with no restriction or fee.
006: * The original source code is owned by the author.
007: *
008: * This program is distributed AS IS in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
011: *
012: * Author: Jose Maria Arranz Santamaria
013: * (C) Innowhere Software Services S.L., Spanish company, year 2007
014: */
015: package org.itsnat.feashow.features.core;
016:
017: import java.util.LinkedList;
018: import java.util.List;
019: import javax.servlet.http.HttpSession;
020: import org.itsnat.core.ClientDocument;
021: import org.itsnat.core.DocumentTemplate;
022: import org.itsnat.core.ItsNatDocument;
023: import org.itsnat.core.ItsNatServlet;
024: import org.itsnat.core.ItsNatServletContext;
025: import org.itsnat.core.ItsNatSession;
026: import org.itsnat.core.ItsNatSessionCallback;
027: import org.itsnat.core.ItsNatVariableResolver;
028: import org.itsnat.core.domutil.ElementList;
029: import org.itsnat.core.http.ItsNatHttpSession;
030: import org.itsnat.feashow.FeatureTreeNode;
031: import org.w3c.dom.Document;
032: import org.w3c.dom.Element;
033:
034: public class RemoteControlTreeNode extends FeatureTreeNode {
035: public RemoteControlTreeNode() {
036: }
037:
038: public void startExamplePanel() {
039: this Session();
040:
041: otherSessions();
042: }
043:
044: public void this Session() {
045: ItsNatDocument itsNatDoc = getItsNatDocument();
046: Document doc = itsNatDoc.getDocument();
047: ItsNatVariableResolver resolver = itsNatDoc
048: .createItsNatVariableResolver();
049: ClientDocument owner = itsNatDoc.getClientDocumentOwner();
050: ItsNatHttpSession itsNatSession = (ItsNatHttpSession) owner
051: .getItsNatSession();
052: HttpSession session = itsNatSession.getHttpSession();
053: session.setAttribute("sessionId", itsNatSession.getId());
054: itsNatDoc.setAttribute("docId", itsNatDoc.getId());
055: resolver.setLocalVariable("refreshInterval", new Integer(3000));
056: resolver.setLocalVariable("syncMode", new Integer(itsNatDoc
057: .getDefaultSyncMode()));
058:
059: Element linkTimer = doc.getElementById("remoteCtrlLinkTimer");
060: resolver.resolve(linkTimer);
061: Element linkComet = doc.getElementById("remoteCtrlLinkComet");
062: resolver.resolve(linkComet);
063: }
064:
065: public void otherSessions() {
066: ItsNatDocument itsNatDoc = getItsNatDocument();
067: Document doc = itsNatDoc.getDocument();
068: DocumentTemplate this DocTemplate = itsNatDoc
069: .getDocumentTemplate();
070:
071: ItsNatServlet itsNatServlet = itsNatDoc.getDocumentTemplate()
072: .getItsNatServlet();
073: ItsNatServletContext appCtx = itsNatServlet
074: .getItsNatServletConfig().getItsNatServletContext();
075:
076: final List sessionList = new LinkedList();
077: ItsNatSessionCallback cb = new ItsNatSessionCallback() {
078: public boolean handleSession(ItsNatSession session) {
079: sessionList.add(session);
080: return true; // continue
081: }
082: };
083: appCtx.enumerateSessions(cb);
084:
085: ElementList sessionNodeList = itsNatDoc.createElementList(doc
086: .getElementById("otherSessionsId"), true);
087:
088: ItsNatVariableResolver resolver = itsNatDoc
089: .createItsNatVariableResolver();
090: resolver.setLocalVariable("refreshInterval", new Integer(3000));
091: resolver.setLocalVariable("syncMode", new Integer(itsNatDoc
092: .getDefaultSyncMode()));
093:
094: for (int i = 0; i < sessionList.size(); i++) {
095: ItsNatHttpSession otherSession = (ItsNatHttpSession) sessionList
096: .get(i);
097:
098: ItsNatDocument[] remDocs = otherSession
099: .getItsNatDocuments();
100:
101: for (int j = 0; j < remDocs.length; j++) {
102: ItsNatDocument currRemDoc = remDocs[j];
103: if (itsNatDoc == currRemDoc)
104: continue;
105:
106: // currRemDoc should be synchronized, but a dead lock could occur by other process doing the same (docs locked mutually, the parent doc is already locked)
107: // there is no problem, the ItsNatDocument.getDocumentTemplate() is thread safe
108: DocumentTemplate docTemplate = currRemDoc
109: .getDocumentTemplate();
110: if (docTemplate != this DocTemplate)
111: continue;
112:
113: String docId = currRemDoc.getId(); // No sync is needed
114: Element sessionElem = (Element) sessionNodeList
115: .addElement();
116:
117: ItsNatVariableResolver resolver2 = resolver
118: .createItsNatVariableResolver();
119: resolver2.setLocalVariable("sessionId", otherSession
120: .getId());
121: resolver2.setLocalVariable("agentInfo", otherSession
122: .getUserAgent());
123: resolver2.setLocalVariable("docId", docId);
124: resolver2.resolve(sessionElem);
125: }
126: }
127:
128: }
129:
130: public void endExamplePanel() {
131: }
132: }
|