| 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.model; | 
| 16 |   | 
| 17 | import java.util.HashMap; | 
| 18 | import java.util.Map; | 
| 19 |   | 
| 20 | import net.sourceforge.hiveboard.Account; | 
| 21 | import net.sourceforge.hiveboard.Event; | 
| 22 | import net.sourceforge.hiveboard.EventType; | 
| 23 | import net.sourceforge.hiveboard.event.ConstraintEventFilter; | 
| 24 | import net.sourceforge.hiveboard.event.Constraints; | 
| 25 | import net.sourceforge.hiveboard.event.EventsPriorities; | 
| 26 | import net.sourceforge.hiveboard.event.PersistentDeferredConsumer; | 
| 27 | import net.sourceforge.hiveevents.Channel; | 
| 28 | import net.sourceforge.hiveevents.Filter; | 
| 29 |   | 
| 30 | /** | 
| 31 |  * Service acting as a memory repository of all active Accounts in the system. | 
| 32 |  * <p> | 
| 33 |  * This service must use the singleton service model. | 
| 34 |  * | 
| 35 |  * @author Jean-Francois Poilpret | 
| 36 |  */ | 
| 37 | public class AccountRepositoryImpl implements AccountRepository | 
| 38 | { | 
| 39 |         public AccountRepositoryImpl(Channel<Event> eventChannel) | 
| 40 |         { | 
| 41 |                 // Add events listeners                 | 
| 42 |                 int priority = EventsPriorities.ACCOUNT_REPOSITORY_UPDATE; | 
| 43 |                 String constraint = Constraints.eventType(        EventType.EVT_CREATE_ACCOUNT, | 
| 44 |                                                                                                         EventType.EVT_MODIFY_ACCOUNT); | 
| 45 |                 Filter<Event> filter = new ConstraintEventFilter(constraint); | 
| 46 |                 PersistentDeferredConsumer consumer = new PersistentDeferredConsumer() | 
| 47 |                 { | 
| 48 |                         @Override protected void pushEvent(Event event) | 
| 49 |                         { | 
| 50 |                                 _accounts.put(event.getWho(), event.getAccount()); | 
| 51 |                         } | 
| 52 |                 }; | 
| 53 |   | 
| 54 |                 eventChannel.registerPushConsumer(priority, filter, consumer); | 
| 55 |   | 
| 56 |                 constraint = Constraints.eventType(EventType.EVT_DESTROY_ACCOUNT); | 
| 57 |                 filter = new ConstraintEventFilter(constraint); | 
| 58 |                 consumer = new PersistentDeferredConsumer() | 
| 59 |                 { | 
| 60 |                         @Override protected void pushEvent(Event event) | 
| 61 |                         { | 
| 62 |                                 _accounts.remove(event.getWho()); | 
| 63 |                         } | 
| 64 |                 }; | 
| 65 |                 eventChannel.registerPushConsumer(priority, filter, consumer); | 
| 66 |   | 
| 67 |                 constraint = Constraints.eventType(EventType.EVT_LOGIN); | 
| 68 |                 filter = new ConstraintEventFilter(constraint); | 
| 69 |                 consumer = new PersistentDeferredConsumer() | 
| 70 |                 { | 
| 71 |                         @Override protected void pushEvent(Event event) | 
| 72 |                         { | 
| 73 |                                 getAccount(event.getWho()).setConnected(true); | 
| 74 |                         } | 
| 75 |                 }; | 
| 76 |                 eventChannel.registerPushConsumer(priority, filter, consumer); | 
| 77 |   | 
| 78 |                 constraint = Constraints.eventType(EventType.EVT_LOGOUT); | 
| 79 |                 filter = new ConstraintEventFilter(constraint); | 
| 80 |                 consumer = new PersistentDeferredConsumer() | 
| 81 |                 { | 
| 82 |                         @Override protected void pushEvent(Event event) | 
| 83 |                         { | 
| 84 |                                 getAccount(event.getWho()).setConnected(false); | 
| 85 |                         } | 
| 86 |                 }; | 
| 87 |                 eventChannel.registerPushConsumer(priority, filter, consumer); | 
| 88 |         } | 
| 89 |          | 
| 90 |         public Account                getAccount(int id) | 
| 91 |         { | 
| 92 |                 return _accounts.get(id); | 
| 93 |         } | 
| 94 |          | 
| 95 |         public Account[]        getAccounts() | 
| 96 |         { | 
| 97 |                 return _accounts.values().toArray(new Account[0]); | 
| 98 |         } | 
| 99 |   | 
| 100 |         protected final Map<Integer, Account>        _accounts = new HashMap<Integer, Account>(); | 
| 101 | } |