1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hiveboard.main; |
16 | |
17 | import org.apache.commons.logging.Log; |
18 | |
19 | import net.sourceforge.hiveboard.Account; |
20 | import net.sourceforge.hiveboard.Event; |
21 | import net.sourceforge.hiveboard.EventType; |
22 | import net.sourceforge.hiveboard.event.EventsPriorities; |
23 | import net.sourceforge.hiveboard.event.PersistentDeferredConsumer; |
24 | import net.sourceforge.hiveevents.Channel; |
25 | import net.sourceforge.hiveevents.Filter; |
26 | |
27 | public class IdentityHolderImpl implements IdentityHolder |
28 | { |
29 | public IdentityHolderImpl( Log logger, |
30 | Channel<Event> eventsChannel, |
31 | Channel<Account> identityChannel) |
32 | { |
33 | _logger = logger; |
34 | _identityChannel = identityChannel; |
35 | int priority = EventsPriorities.IDENTITY_UPDATE; |
36 | Filter<Event> filter = new Filter<Event>() |
37 | { |
38 | public boolean passEvent(Event event) |
39 | { |
40 | return ( (event.getType() == EventType.EVT_MODIFY_ACCOUNT) |
41 | && (_identity != null) |
42 | && (_identity.getId() == event.getAccount().getId())); |
43 | } |
44 | }; |
45 | eventsChannel.registerPushConsumer(priority, filter, new PersistentDeferredConsumer() |
46 | { |
47 | @Override protected void pushEvent(Event event) |
48 | { |
49 | _identity = event.getAccount(); |
50 | _identityChannel.push(_identity); |
51 | } |
52 | }); |
53 | } |
54 | |
55 | public Account getIdentity() |
56 | { |
57 | if (_identity == null) |
58 | { |
59 | _logger.warn("getIdentity() [_identity == null]"); |
60 | } |
61 | return _identity; |
62 | } |
63 | |
64 | public int getId() |
65 | { |
66 | return getIdentity().getId(); |
67 | } |
68 | |
69 | public void setIdentity(Account account) |
70 | { |
71 | if (_identity == null) |
72 | { |
73 | _identity = account; |
74 | _identityChannel.push(_identity); |
75 | } |
76 | else |
77 | { |
78 | _logger.warn("setIdentity() [_identity != null]"); |
79 | } |
80 | if (_identity == null) |
81 | { |
82 | _logger.warn("setIdentity() [_identity == null]"); |
83 | _identity = new Account(); |
84 | _identity.setId(-1); |
85 | } |
86 | } |
87 | |
88 | private final Log _logger; |
89 | private final Channel<Account> _identityChannel; |
90 | private Account _identity; |
91 | } |