| 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 org.apache.commons.logging.Log; | 
| 21 | import org.apache.hivemind.Resource; | 
| 22 | import org.apache.hivemind.events.RegistryShutdownListener; | 
| 23 |   | 
| 24 | import net.sf.ehcache.Cache; | 
| 25 | import net.sf.ehcache.CacheException; | 
| 26 | import net.sf.ehcache.CacheManager; | 
| 27 | import net.sourceforge.hiveboard.Board; | 
| 28 | import net.sourceforge.hiveboard.Event; | 
| 29 | import net.sourceforge.hiveboard.EventType; | 
| 30 | import net.sourceforge.hiveboard.event.ConstraintEventFilter; | 
| 31 | import net.sourceforge.hiveboard.event.Constraints; | 
| 32 | import net.sourceforge.hiveboard.event.EventsPriorities; | 
| 33 | import net.sourceforge.hiveboard.event.PersistentDeferredConsumer; | 
| 34 | import net.sourceforge.hiveboard.main.IdentityHolder; | 
| 35 | import net.sourceforge.hiveevents.Channel; | 
| 36 | import net.sourceforge.hiveevents.Filter; | 
| 37 | import net.sourceforge.hiveutils.service.ObjectBuilder; | 
| 38 |   | 
| 39 | /** | 
| 40 |  * @author Jean-Francois Poilpret | 
| 41 |  */ | 
| 42 | public class BoardImageModelRepositoryImpl | 
| 43 |         implements BoardImageModelRepository, RegistryShutdownListener | 
| 44 | { | 
| 45 |         public BoardImageModelRepositoryImpl(        Log                                                                logger, | 
| 46 |                                                                                         final BoardRepository                        repository, | 
| 47 |                                                                                         final ObjectBuilder                                builder, | 
| 48 |                                                                                         final Channel<Event>                        eventChannel, | 
| 49 |                                                                                         final Channel<BoardImageModel>        openBoardChannel, | 
| 50 |                                                                                         final IdentityHolder                        holder, | 
| 51 |                                                                                         final Resource                                        cacheConfig) | 
| 52 |                 throws CacheException | 
| 53 |         { | 
| 54 |                 _logger = logger; | 
| 55 |                 _logger.debug("<init>"); | 
| 56 |   | 
| 57 |                 // Initialize images cache (memroy/disk) | 
| 58 |                 _manager = CacheManager.create(cacheConfig.getResourceURL()); | 
| 59 |                 _cache = _manager.getCache("BoardImageCache"); | 
| 60 |   | 
| 61 |                 int priority = EventsPriorities.IMAGE_REPOSITORY_UPDATE; | 
| 62 |                 String constraint = Constraints.eventTypeWho(holder.getId(), EventType.EVT_JOIN_BOARD); | 
| 63 |                 Filter<Event> filter = new ConstraintEventFilter(constraint); | 
| 64 |                 PersistentDeferredConsumer consumer = new PersistentDeferredConsumer() | 
| 65 |                 { | 
| 66 |                         @Override protected void pushEvent(Event event) | 
| 67 |                         { | 
| 68 |                                 int id = event.getWhere(); | 
| 69 |                                 Board board = repository.getBoard(id); | 
| 70 |                                 byte[] raster = event.getRaster(); | 
| 71 |                                 byte[] action = event.getDrawingAction(); | 
| 72 |                                 // Create new ImageModel for Board | 
| 73 |                                 BoardImageModel model = (BoardImageModel) builder.create( | 
| 74 |                                                                         "BoardImageModel", _cache, board, raster, action); | 
| 75 |                                 _models.put(id, model); | 
| 76 |                                 // Add it to the board areas view | 
| 77 |                                 openBoardChannel.push(model); | 
| 78 |                         } | 
| 79 |                 }; | 
| 80 |                 eventChannel.registerPushConsumer(priority, filter, consumer); | 
| 81 |   | 
| 82 |                 // Listen to event of current user leaving a board | 
| 83 |                 constraint = Constraints.eventTypeWho(holder.getId(), EventType.EVT_LEAVE_BOARD); | 
| 84 |                 filter = new ConstraintEventFilter(constraint); | 
| 85 |                 consumer = new PersistentDeferredConsumer() | 
| 86 |                 { | 
| 87 |                         @Override protected void pushEvent(Event event) | 
| 88 |                         { | 
| 89 |                                 BoardImageModel model = _models.remove(event.getWhere()); | 
| 90 |                                 model.dispose(); | 
| 91 |                         } | 
| 92 |                 }; | 
| 93 |                 eventChannel.registerPushConsumer(priority, filter, consumer); | 
| 94 |         } | 
| 95 |          | 
| 96 |         public void registryDidShutdown() | 
| 97 |         { | 
| 98 |                 _logger.debug("registryShutDown()"); | 
| 99 |                 _manager.shutdown(); | 
| 100 |         } | 
| 101 |   | 
| 102 |         public BoardImageModel        getModel(int board) | 
| 103 |         { | 
| 104 |                 return _models.get(board); | 
| 105 |         } | 
| 106 |   | 
| 107 |         public BoardImageModel[]        getModels() | 
| 108 |         { | 
| 109 |                 return _models.values().toArray(new BoardImageModel[_models.size()]); | 
| 110 |         } | 
| 111 |          | 
| 112 |         private final Log                                                        _logger; | 
| 113 |         private final Map<Integer, BoardImageModel>        _models =  | 
| 114 |                                                                                                         new HashMap<Integer, BoardImageModel>(); | 
| 115 |         private final CacheManager                                        _manager; | 
| 116 |         private final Cache                                                        _cache; | 
| 117 | } |