]> source.dussan.org Git - vaadin-framework.git/commitdiff
Added application context for portlet 2.0 applications.
authorPetter Holmström <petter.holmstrom@itmill.com>
Fri, 30 Oct 2009 07:16:08 +0000 (07:16 +0000)
committerPetter Holmström <petter.holmstrom@itmill.com>
Fri, 30 Oct 2009 07:16:08 +0000 (07:16 +0000)
svn changeset:9476/svn branch:portlet_2.0

src/com/vaadin/terminal/gwt/server/PortletApplicationContext2.java [new file with mode: 0644]

diff --git a/src/com/vaadin/terminal/gwt/server/PortletApplicationContext2.java b/src/com/vaadin/terminal/gwt/server/PortletApplicationContext2.java
new file mode 100644 (file)
index 0000000..08546c4
--- /dev/null
@@ -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<TransactionListener> listeners;
+
+    protected transient PortletSession session;
+
+    protected final HashSet<Application> applications = new HashSet<Application>();
+
+    protected WebBrowser browser = new WebBrowser();
+
+    protected HashMap<Application, PortletCommunicationManager> applicationToAjaxAppMgrMap = new HashMap<Application, PortletCommunicationManager>();
+
+    @Override
+    public void addTransactionListener(TransactionListener listener) {
+        if (listeners == null) {
+            listeners = new LinkedList<TransactionListener>();
+        }
+        listeners.add(listener);
+    }
+
+    @Override
+    public Collection<Application> 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<TransactionListener>) listeners
+                .clone()) {
+            listener.transactionStart(application, request);
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    protected void endTransaction(Application application,
+            PortletRequest request) {
+        if (listeners == null) {
+            return;
+        }
+
+        LinkedList<Exception> exceptions = null;
+        for (TransactionListener listener : (LinkedList<TransactionListener>) listeners
+                .clone()) {
+            try {
+                listener.transactionEnd(application, request);
+            } catch (final RuntimeException e) {
+                if (exceptions == null) {
+                    exceptions = new LinkedList<Exception>();
+                }
+                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());
+        }
+    }
+
+}