Project

General

Profile

Revision 42d326df

ID42d326df479a2586bd2e8350fb04a2180f5c1a64
Parent b5d06161
Child 21c9f167

Added by Andreas Kohlbecker over 3 years ago

ref #7241 DelegatingErrorHandler more generically

View differences:

src/main/java/eu/etaxonomy/cdm/vaadin/event/error/DelegatingErrorHandler.java
1
// $Id$
2 1
/**
3 2
* Copyright (C) 2018 EDIT
4 3
* European Distributed Institute of Taxonomy
......
9 8
*/
10 9
package eu.etaxonomy.cdm.vaadin.event.error;
11 10

  
12
import java.util.HashMap;
13
import java.util.Map;
11
import java.util.HashSet;
12
import java.util.Set;
14 13

  
15 14
import org.apache.log4j.Logger;
16 15

  
......
26 25
 * see https://dev.e-taxonomy.eu/redmine/issues/7241
27 26
 *
28 27
 * @author freimeier
29
 * @date 26.02.2018
30 28
 *
31 29
 */
32 30
public class DelegatingErrorHandler implements ErrorHandler{
33 31

  
34 32
    private static final long serialVersionUID = 3378605204517477112L;
35 33

  
36
    Map<Class<? extends Exception>, ErrorTypeHandler<? extends Exception>> handlerMap = new HashMap<>();
34
    Set<ErrorTypeHandler<? extends Throwable>> handlers = new HashSet<>();
35

  
36
    public <E extends Throwable> void  registerHandler(ErrorTypeHandler<E> handler) {
37
        assert findHandler(handler.supports()) == null;
38
        handlers.add(handler);
39
    }
40

  
41
    @SuppressWarnings("unchecked")
42
    public <E extends Throwable> ErrorTypeHandler<E> findHandler(Class<E> errorClass){
43
        for(ErrorTypeHandler<?> h : handlers){
44
            if(h.supports().equals(errorClass)){
45
                return (ErrorTypeHandler<E>) h;
46
            }
47
        }
48
        return null;
37 49

  
38
    public <E extends Exception> void  registerHandler(Class<E> type, ErrorTypeHandler<E> handler) {
39
        handlerMap.put(type, handler);
40 50
    }
41 51

  
42 52
    /* (non-Javadoc)
......
45 55
    @Override
46 56
    public void error(ErrorEvent event) {
47 57

  
48
        Class<? extends Throwable> errorClass = event.getThrowable().getCause().getClass();
49
        Logger.getLogger(this.getClass()).debug(errorClass);
50
        if(handlerMap.get(errorClass) != null){
51
            handlerMap.get(errorClass).error(event);
52
          } else {
58
        boolean handlerFound = true;
59
        Throwable throwable = event.getThrowable();
60
        while(throwable != null){
61
            if(delegate(event, throwable)){
62
                break;
63
            }
64
            throwable = throwable.getCause();
65
        }
66
        if(!handlerFound){
53 67
            Notification.show(event.getThrowable().getMessage());
54 68
          }
55 69
    }
70

  
71
    private <E extends Throwable> boolean delegate(ErrorEvent event, E throwable){
72
        Class<E> errorClass = (Class<E>) throwable.getClass();
73
        Logger.getLogger(this.getClass()).debug(errorClass);
74
        ErrorTypeHandler<E> handler = findHandler(errorClass);
75
        if(handler != null){
76
            handler.handleError(event, throwable);
77
            return true;
78
        }
79
        return false;
80
    }
56 81
}
src/main/java/eu/etaxonomy/cdm/vaadin/event/error/ErrorTypeHandler.java
1
// $Id$
2 1
/**
3 2
* Copyright (C) 2018 EDIT
4 3
* European Distributed Institute of Taxonomy
......
16 15
 * see {@link DelegatingErrorHandler}
17 16
 *
18 17
 * @author freimeier
19
 * @date 26.02.2018
20 18
 *
21 19
 */
22
public abstract class ErrorTypeHandler<E extends Exception> implements ErrorHandler {
20
public abstract class ErrorTypeHandler<E extends Throwable> implements ErrorHandler {
23 21

  
24
    private static final long serialVersionUID = 1782060185842059311L;
22
    private  static final long serialVersionUID = 1782060185842059311L;
25 23

  
26
    /* (non-Javadoc)
27
     * @see com.vaadin.server.ErrorHandler#error(com.vaadin.server.ErrorEvent)
28
     */
29
    @Override
30
    public final void error(ErrorEvent event) {
31
        handleError((E) event.getThrowable().getCause());
24
    public abstract Class<E> supports();
32 25

  
26
    public final void handleError(ErrorEvent event, E throwable){
27
        error(event);
28
        exception(throwable);
33 29
    }
34 30

  
35
    public abstract void handleError(E exception);
31
    /**
32
     * @param exception
33
     */
34
    public abstract void exception(E throwable);
36 35
}
37 36

  
38 37

  
src/main/java/eu/etaxonomy/cdm/vaadin/event/error/ExternalServiceExceptionHandler.java
1
/**
2
* Copyright (C) 2018 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
package eu.etaxonomy.cdm.vaadin.event.error;
10

  
11
import com.vaadin.server.ErrorEvent;
12
import com.vaadin.server.Page;
13
import com.vaadin.ui.Notification;
14
import com.vaadin.ui.Notification.Type;
15

  
16
import eu.etaxonomy.cdm.ext.common.ExternalServiceException;
17
import eu.etaxonomy.cdm.vaadin.ui.RegistrationUIDefaults;
18

  
19

  
20
public class ExternalServiceExceptionHandler extends ErrorTypeHandler<ExternalServiceException>{
21

  
22
    private static final long serialVersionUID = -5703485298578474572L;
23
    private String mainMessage;
24

  
25
    public ExternalServiceExceptionHandler(String mainMessage){
26
        this.mainMessage = mainMessage;
27
    }
28

  
29

  
30
    /**
31
     * {@inheritDoc}
32
     */
33
    @Override
34
    public Class<ExternalServiceException> supports() {
35
        return ExternalServiceException.class;
36
    }
37

  
38

  
39
    /**
40
     * {@inheritDoc}
41
     */
42
    @Override
43
    public void error(ErrorEvent event) {
44
        // only exception() needed in this class
45

  
46
    }
47

  
48
    @Override
49
    public void exception(ExternalServiceException exception) {
50
        Notification notification = new Notification(mainMessage,
51
                "<div><strong>Service:</strong> "+ exception.getExternalService() + "</div>" +
52
                "<div><strong>Problem:</strong> "+ exception.getProblem() + "</div>" +
53
                "<p>" + RegistrationUIDefaults.ERROR_CONTACT_MESSAGE_LINE + "</p>"
54
                , Type.ERROR_MESSAGE);
55
        notification.setHtmlContentAllowed(true);
56
        notification.show(Page.getCurrent());
57
    }
58

  
59

  
60

  
61
}
src/main/java/eu/etaxonomy/cdm/vaadin/event/error/HibernateSystemErrorHandler.java
11 11

  
12 12
import org.springframework.orm.hibernate5.HibernateSystemException;
13 13

  
14
import com.vaadin.server.ErrorEvent;
14 15
import com.vaadin.ui.Notification;
15 16

  
16 17
import eu.etaxonomy.cdm.database.PermissionDeniedException;
......
18 19

  
19 20
/**
20 21
 * @author freimeier
21
 * @date 26.02.2018
22 22
 *
23 23
 */
24 24
public class HibernateSystemErrorHandler extends ErrorTypeHandler<HibernateSystemException>{
25 25

  
26 26
    private static final long serialVersionUID = -5703485298578474572L;
27 27

  
28
    /* (non-Javadoc)
29
     * @see eu.etaxonomy.cdm.vaadin.util.errorhandler.ErrorTypeHandler#handleError(com.vaadin.server.ErrorEvent)
28
    /**
29
     * {@inheritDoc}
30 30
     */
31 31
    @Override
32
    public void handleError(HibernateSystemException exception) {
32
    public Class<HibernateSystemException> supports() {
33
        return HibernateSystemException.class;
34
    }
35

  
36
    /**
37
     * {@inheritDoc}
38
     */
39
    @Override
40
    public void error(ErrorEvent event) {
41
        // not needed in this class so far
42
    }
43

  
44
    @Override
45
    public void exception(HibernateSystemException exception) {
46

  
33 47
        if(exception != null) {
34 48
            if(exception.getCause().getClass().equals(PermissionDeniedException.class)) {
35 49
                Notification.show(Messages.getLocalizedString(Messages.PermissionDeniedErrorHandler_ERROR_MSG));
......
39 53
        }
40 54
    }
41 55

  
56

  
57

  
42 58
}
src/main/java/eu/etaxonomy/cdm/vaadin/view/distributionStatus/DistributionTableViewBean.java
16 16
import java.util.UUID;
17 17

  
18 18
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.orm.hibernate5.HibernateSystemException;
20 19
import org.springframework.security.core.GrantedAuthority;
21 20
import org.vaadin.addons.lazyquerycontainer.LazyQueryContainer;
22 21

  
......
175 174
                //popup window
176 175
                final Window popup = new Window(Messages.getLocalizedString(Messages.DistributionTableViewBean_CHOOSE_DISTRIBUTION_STATUS));
177 176
                DelegatingErrorHandler errorHandler = new DelegatingErrorHandler();
178
                errorHandler.registerHandler(HibernateSystemException.class, new HibernateSystemErrorHandler());
177
                errorHandler.registerHandler(new HibernateSystemErrorHandler());
179 178
                popup.setErrorHandler(errorHandler);
180 179
                final ListSelect termSelect = new ListSelect();
181 180
                termSelect.setSizeFull();

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)