From: Petter Holmström Date: Fri, 30 Oct 2009 07:16:08 +0000 (+0000) Subject: Added application context for portlet 2.0 applications. X-Git-Tag: 6.7.0.beta1~2266^2~29 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=db921c215f3fcc8fb8f4944db1e9d2b4e6ebdae1;p=vaadin-framework.git Added application context for portlet 2.0 applications. svn changeset:9476/svn branch:portlet_2.0 --- diff --git a/src/com/vaadin/terminal/gwt/server/PortletApplicationContext2.java b/src/com/vaadin/terminal/gwt/server/PortletApplicationContext2.java new file mode 100644 index 0000000000..08546c4366 --- /dev/null +++ b/src/com/vaadin/terminal/gwt/server/PortletApplicationContext2.java @@ -0,0 +1,158 @@ +package com.vaadin.terminal.gwt.server; + +import java.io.File; +import java.io.PrintWriter; +import java.io.Serializable; +import java.io.StringWriter; +import java.net.URL; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; + +import javax.portlet.PortletRequest; +import javax.portlet.PortletSession; + +import com.vaadin.Application; +import com.vaadin.service.ApplicationContext; + +/** + * TODO Write documentation, fix JavaDoc tags. + * + * @author peholmst + */ +@SuppressWarnings("serial") +public class PortletApplicationContext2 implements ApplicationContext, + Serializable { + + protected LinkedList listeners; + + protected transient PortletSession session; + + protected final HashSet applications = new HashSet(); + + protected WebBrowser browser = new WebBrowser(); + + protected HashMap applicationToAjaxAppMgrMap = new HashMap(); + + @Override + public void addTransactionListener(TransactionListener listener) { + if (listeners == null) { + listeners = new LinkedList(); + } + listeners.add(listener); + } + + @Override + public Collection getApplications() { + return Collections.unmodifiableCollection(applications); + } + + @Override + public File getBaseDirectory() { + String resultPath = session.getPortletContext().getRealPath("/"); + if (resultPath != null) { + return new File(resultPath); + } else { + try { + final URL url = session.getPortletContext().getResource("/"); + return new File(url.getFile()); + } catch (final Exception e) { + // FIXME: Handle exception + e.printStackTrace(); + } + } + return null; + } + + @Override + public void removeTransactionListener(TransactionListener listener) { + if (listeners != null) { + listeners.remove(listener); + } + } + + protected PortletCommunicationManager getApplicationManager( + Application application) { + PortletCommunicationManager mgr = applicationToAjaxAppMgrMap + .get(application); + + if (mgr == null) { + // Creates a new manager + // TODO Use a factory instead + mgr = new PortletCommunicationManagerImpl(); + applicationToAjaxAppMgrMap.put(application, mgr); + } + return mgr; + } + + public static PortletApplicationContext2 getApplicationContext( + PortletSession session) { + PortletApplicationContext2 cx = (PortletApplicationContext2) session + .getAttribute(PortletApplicationContext2.class.getName()); + if (cx == null) { + cx = new PortletApplicationContext2(); + session + .setAttribute(PortletApplicationContext2.class.getName(), + cx); + } + if (cx.session == null) { + cx.session = session; + } + return cx; + } + + public WebBrowser getBrowser() { + return browser; + } + + @SuppressWarnings("unchecked") + protected void startTransaction(Application application, + PortletRequest request) { + if (listeners == null) { + return; + } + for (TransactionListener listener : (LinkedList) listeners + .clone()) { + listener.transactionStart(application, request); + } + } + + @SuppressWarnings("unchecked") + protected void endTransaction(Application application, + PortletRequest request) { + if (listeners == null) { + return; + } + + LinkedList exceptions = null; + for (TransactionListener listener : (LinkedList) listeners + .clone()) { + try { + listener.transactionEnd(application, request); + } catch (final RuntimeException e) { + if (exceptions == null) { + exceptions = new LinkedList(); + } + exceptions.add(e); + } + } + + // If any runtime exceptions occurred, throw a combined exception + if (exceptions != null) { + final StringBuffer msg = new StringBuffer(); + for (Exception e : exceptions) { + if (msg.length() == 0) { + msg.append("\n\n--------------------------\n\n"); + } + msg.append(e.getMessage() + "\n"); + final StringWriter trace = new StringWriter(); + e.printStackTrace(new PrintWriter(trace, true)); + msg.append(trace.toString()); + } + throw new RuntimeException(msg.toString()); + } + } + +}