01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: **/package org.araneaframework.core;
16:
17: import org.apache.commons.logging.Log;
18: import org.apache.commons.logging.LogFactory;
19: import org.araneaframework.Component;
20: import org.araneaframework.Message;
21: import org.araneaframework.Path;
22: import org.araneaframework.core.util.ExceptionUtil;
23:
24: /**
25: * {@link Message} that is sent to exactly one {@link Component} in hierarchy.
26: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
27: */
28: public abstract class RoutedMessage implements Message {
29: private static final Log log = LogFactory
30: .getLog(RoutedMessage.class);
31:
32: private Path path;
33: private String destination;
34:
35: public RoutedMessage(Path path) {
36: Assert.notNullParam(this , path, "path");
37:
38: this .path = path;
39: destination = path.toString();
40: }
41:
42: /**
43: * Sends method that causes {@link RoutedMessage#execute(Component)} to be called for
44: * {@link Component} identified in hierarchy by the given <code>id</code>.
45: *
46: * @see org.araneaframework.Message#send(java.lang.Object, org.araneaframework.Component)
47: */
48: public final void send(Object id, Component component) {
49: //After routing finished
50: if (!path.hasNext())
51: return;
52:
53: //Before named hierarchy
54: if (id == null) {
55: component._getComponent().propagate(this );
56: return;
57: }
58:
59: //Routing to next
60: if (path.getNext().equals(id)) {
61: path.next();
62:
63: if (path.hasNext())
64: component._getComponent().propagate(this );
65: else {
66: log.debug("Delivering message routed to '"
67: + destination + "'");
68: try {
69: execute(component);
70: } catch (Exception e) {
71: throw ExceptionUtil.uncheckException(e);
72: }
73: }
74: }
75: }
76:
77: /**
78: * Method that is called on {@link Component} that is target of this {@link RoutedMessage}.
79: * @param component {@link Component} this {@link RoutedMessage} has reached
80: */
81: protected abstract void execute(Component component)
82: throws Exception;
83: }
|