Project

General

Profile

Download (2.99 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2015 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9

    
10
package eu.etaxonomy.cdm.vaadin.servlet;
11

    
12
import java.io.IOException;
13

    
14
import javax.servlet.ServletException;
15

    
16
import org.apache.log4j.Logger;
17

    
18
import com.vaadin.server.ServiceException;
19
import com.vaadin.server.SessionDestroyEvent;
20
import com.vaadin.server.SessionDestroyListener;
21
import com.vaadin.server.SessionInitEvent;
22
import com.vaadin.server.SessionInitListener;
23
import com.vaadin.server.VaadinSession;
24
import com.vaadin.spring.server.SpringVaadinServlet;
25

    
26
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
27
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
28
import eu.etaxonomy.cdm.vaadin.util.DistributionEditorUtil;
29

    
30

    
31
/**
32
 * This class is part of the strategy to solve the lazy loading problem
33
 * and in more general to manage sessions properly.
34
 * <p>
35
 * Conversational Sessions essentially involve linking any UI which requires
36
 * long running sessions to a {@link CdmVaadinConversationalServlet}.
37
 * The servlet creates an instance of the ConversationHolder when a VaadinSession
38
 * is initialized and binds it on every service call, ensuring that a single
39
 * hibernate session is attached to a corresponding vaadin session.
40
 * <b>NOTE</b>: One major issue with this strategy is the bug (#4528) which
41
 * flushes the entire session even if a save / saveOrUpdate call is made on a
42
 * single CDM entity. This implies that this strategy is safe to use only in
43
 * 'session-save' UIs and not 'auto-save' UIs.
44
 *
45
 * @author c.mathew
46
 *
47
 */
48
public class CdmVaadinConversationalServlet extends SpringVaadinServlet implements SessionInitListener, SessionDestroyListener {
49

    
50
	private static final Logger logger = Logger.getLogger(CdmVaadinConversationalServlet.class);
51

    
52
	private static final long serialVersionUID = -2973231251266766766L;
53

    
54
	private ConversationHolder conversation;
55

    
56
	@Override
57
	protected void servletInitialized() throws ServletException {
58
		super.servletInitialized();
59
		getService().addSessionInitListener(this);
60
		getService().addSessionDestroyListener(this);
61
	}
62

    
63
	@Override
64
	public void sessionInit(SessionInitEvent event)
65
			throws ServiceException {
66
		conversation = (ConversationHolder) CdmSpringContextHelper.getCurrent().getBean("conversationHolder");
67
		conversation.bind();
68
		VaadinSession.getCurrent().setAttribute(DistributionEditorUtil.SATTR_CONVERSATION, conversation);
69
	}
70

    
71
	@Override
72
	public void sessionDestroy(SessionDestroyEvent event) {
73
		conversation.close();
74
	}
75

    
76
	@Override
77
	protected void service(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws ServletException, IOException {
78
		if(conversation != null) {
79
			logger.info("Servlet Service call - Binding Vaadin Session Conversation : " + conversation);
80
			conversation.bind();
81
		}
82

    
83
		super.service(request, response);
84
	}
85

    
86

    
87
}
    (1-1/1)