Remove unused exceptions
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / common / actor / AbstractUntypedActor.java
1 /*
2  * Copyright (c) 2014 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.controller.cluster.common.actor;
10
11 import akka.actor.ActorRef;
12 import akka.actor.UntypedActor;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 public abstract class AbstractUntypedActor extends UntypedActor implements ExecuteInSelfActor {
18     // The member name should be lower case but it's referenced in many subclasses. Suppressing the CS warning for now.
19     @SuppressWarnings("checkstyle:MemberName")
20     protected final Logger LOG = LoggerFactory.getLogger(getClass());
21
22     protected AbstractUntypedActor() {
23         LOG.debug("Actor created {}", getSelf());
24         getContext().system().actorSelection("user/termination-monitor").tell(new Monitor(getSelf()), getSelf());
25     }
26
27     @Override
28     public final void executeInSelf(@NonNull final Runnable runnable) {
29         final ExecuteInSelfMessage message = new ExecuteInSelfMessage(runnable);
30         self().tell(message, ActorRef.noSender());
31     }
32
33     @Override
34     public final void onReceive(final Object message) {
35         if (message instanceof ExecuteInSelfMessage) {
36             ((ExecuteInSelfMessage) message).run();
37         } else {
38             handleReceive(message);
39         }
40     }
41
42     /**
43      * Receive and handle an incoming message. If the implementation does not handle this particular message,
44      * it should call {@link #ignoreMessage(Object)} or {@link #unknownMessage(Object)}.
45      *
46      * @param message the incoming message
47      */
48     protected abstract void handleReceive(Object message);
49
50     protected final void ignoreMessage(final Object message) {
51         LOG.debug("Ignoring unhandled message {}", message);
52     }
53
54     protected final void unknownMessage(final Object message) {
55         LOG.debug("Received unhandled message {}", message);
56         unhandled(message);
57     }
58
59     protected boolean isValidSender(final ActorRef sender) {
60         // If the caller passes in a null sender (ActorRef.noSender()), akka translates that to the
61         // deadLetters actor.
62         return sender != null && !getContext().system().deadLetters().equals(sender);
63     }
64 }