42a8c67b5039b79144e41de8b38ceb1bc5f23535
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / ErrorHandlerQueueImpl.java
1 /**
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.openflowplugin.openflow.md.core;
10
11 import java.util.Arrays;
12 import java.util.concurrent.LinkedBlockingQueue;
13
14 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /**
19  * dumping all exceptions to log
20  * @author mirehak
21  */
22 public class ErrorHandlerQueueImpl implements ErrorHandler {
23
24     private static final Logger LOG = LoggerFactory
25             .getLogger(ErrorHandlerQueueImpl.class);
26
27     private LinkedBlockingQueue<Exception> errorQueue;
28
29     /**
30      * default ctor
31      */
32     public ErrorHandlerQueueImpl() {
33         this.errorQueue = new LinkedBlockingQueue<>();
34     }
35
36     @Override
37     public void run() {
38         while (true) {
39             Exception error;
40             try {
41                 error = errorQueue.take();
42                 if (error instanceof QueueShutdownItem) {
43                     break;
44                 }
45                 Throwable cause = error.getCause();
46                 LOG.error(error.getMessage()+" -> "+cause.getMessage(), cause);
47             } catch (InterruptedException e) {
48                 LOG.warn(e.getMessage());
49             }
50         }
51     }
52     
53     @Override
54     public void handleException(Throwable e, SessionContext sessionContext) {
55         String sessionKeyId = null;
56         if (sessionContext != null) {
57             sessionKeyId = Arrays.toString(sessionContext.getSessionKey().getId());
58         }
59         
60         Exception causeAndThread = new Exception(
61                 "IN THREAD: "+Thread.currentThread().getName() +
62                 "; session:"+sessionKeyId, e);
63         try {
64             errorQueue.put(causeAndThread);
65         } catch (InterruptedException e1) {
66             LOG.error(e1.getMessage(), e1);
67         }
68     }
69
70     @Override
71     public void close() {
72         // add special exception to queue and recognize it in run method
73         errorQueue.add(new QueueShutdownItem());
74     }
75     
76     static class QueueShutdownItem extends Exception {
77         private static final long serialVersionUID = 1L;
78         // nothing
79     }
80 }