9e8265a872c29cb24902e83c2ed5311139682770
[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                 Throwable cause = error.getCause();
43                 LOG.error(error.getMessage()+" -> "+cause.getMessage(), cause);
44             } catch (InterruptedException e) {
45                 LOG.warn(e.getMessage());
46             }
47         }
48     }
49     
50     @Override
51     public void handleException(Throwable e, SessionContext sessionContext) {
52         String sessionKeyId = null;
53         if (sessionContext != null) {
54             sessionKeyId = Arrays.toString(sessionContext.getSessionKey().getId());
55         }
56         
57         Exception causeAndThread = new Exception(
58                 "IN THREAD: "+Thread.currentThread().getName() +
59                 "; session:"+sessionKeyId, e);
60         try {
61             errorQueue.put(causeAndThread);
62         } catch (InterruptedException e1) {
63             LOG.error(e1.getMessage(), e1);
64         }
65     }
66
67     @Override
68     public void close() throws Exception {
69         //TODO: add special exception to queue and recognize it in run method
70     }
71 }