Clover coverage report - Ledge Components - SNAPSHOT
Coverage timestamp: Fri Nov 17 2006 05:13:20 CET
file stats: LOC: 207   Methods: 9
NCLOC: 111   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DistributedMapImpl.java 0% 10.3% 33.3% 12.5%
coverage coverage
 1    //
 2    //Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
 3    //All rights reserved.
 4    //
 5    //Redistribution and use in source and binary forms, with or without modification,
 6    //are permitted provided that the following conditions are met:
 7    //
 8    //* Redistributions of source code must retain the above copyright notice,
 9    //this list of conditions and the following disclaimer.
 10    //* Redistributions in binary form must reproduce the above copyright notice,
 11    //this list of conditions and the following disclaimer in the documentation
 12    //and/or other materials provided with the distribution.
 13    //* Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
 14    //nor the names of its contributors may be used to endorse or promote products
 15    //derived from this software without specific prior written permission.
 16    //
 17    //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 18    //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 19    //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 20    //IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 21    //INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 22    //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 23    //OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 24    //WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 25    //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 26    //POSSIBILITY OF SUCH DAMAGE.
 27    //
 28   
 29    package org.objectledge.cache.impl;
 30   
 31    import java.io.ByteArrayInputStream;
 32    import java.io.ByteArrayOutputStream;
 33    import java.io.IOException;
 34    import java.io.ObjectInputStream;
 35    import java.io.ObjectOutputStream;
 36   
 37    import org.objectledge.cache.NoLongerValidException;
 38    import org.objectledge.cache.Refreshable;
 39    import org.objectledge.cache.spi.CacheFactorySPI;
 40    import org.objectledge.cache.spi.ConfigurableMap;
 41    import org.objectledge.cache.spi.DistributedMap;
 42    import org.objectledge.notification.Notification;
 43    import org.objectledge.notification.NotificationReceiver;
 44   
 45    /**
 46    * An implementation of {@link DistributedMap} using the {@link Notification}.
 47    *
 48    * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
 49    * @version $Id: DistributedMapImpl.java,v 1.3 2004/02/26 11:34:28 fil Exp $
 50    */
 51    public class DistributedMapImpl
 52    extends DelegateMap
 53    implements DistributedMap, ConfigurableMap,
 54    NotificationReceiver
 55    {
 56    // constants /////////////////////////////////////////////////////////////
 57   
 58    /** The channel base name. */
 59    public static final String CHANNEL_NAME = "ledge.cache:1.0";
 60   
 61    // instance variables ////////////////////////////////////////////////////
 62   
 63    /** The notification */
 64    private Notification notification;
 65   
 66    /** The actual channel name. */
 67    private String channel;
 68   
 69    // Initialization ////////////////////////////////////////////////////////
 70   
 71    /**
 72    * Constructs a DistributedMapImpl.
 73    */
 74  828 public DistributedMapImpl()
 75    {
 76  828 super();
 77    }
 78   
 79    // ConfigurableMap interface /////////////////////////////////////////////
 80   
 81    /**
 82    * {@inheritDoc}
 83    */
 84  782 public void configure(CacheFactorySPI caching, String name, String config)
 85    {
 86  782 attach(caching.getNotification(), name);
 87    }
 88   
 89    // DistributedMap SPI inteface ///////////////////////////////////////////
 90   
 91    /**
 92    * {@inheritDoc}
 93    */
 94  828 public void attach(Notification notification, String name)
 95    {
 96  828 this.notification = notification;
 97  828 channel = CHANNEL_NAME+"/"+name;
 98    //TODO uncomment it when notification implemented
 99    //notification.addReceiver(channel, this);
 100    }
 101   
 102    /**
 103    * {@inheritDoc}
 104    */
 105  0 public void detach()
 106    {
 107  0 if(notification != null)
 108    {
 109  0 notification.removeReceiver(channel, this);
 110  0 notification = null;
 111    }
 112    }
 113   
 114    // DistributedMap interface //////////////////////////////////////////////
 115   
 116    /**
 117    * {@inheritDoc}
 118    */
 119  0 public void updated(Object key)
 120    {
 121  0 notify(true, key);
 122    }
 123   
 124    // Map interface /////////////////////////////////////////////////////////
 125   
 126    /**
 127    * {@inheritDoc}
 128    */
 129  0 public Object put(Object key, Object value)
 130    {
 131  0 notify(false, key);
 132  0 return delegate.put(key, value);
 133    }
 134   
 135    /**
 136    * {@inheritDoc}
 137    */
 138  0 public Object remove(Object key)
 139    {
 140  0 notify(false, key);
 141  0 return delegate.remove(key);
 142    }
 143   
 144    // NotificationReceiver interface ////////////////////////////////////////
 145   
 146    /**
 147    * {@inheritDoc}
 148    */
 149  0 public void receive(String channel, byte[] message)
 150    {
 151  0 boolean update;
 152  0 Object key;
 153  0 try
 154    {
 155  0 ByteArrayInputStream bais = new ByteArrayInputStream(message);
 156  0 ObjectInputStream is = new ObjectInputStream(bais);
 157  0 update = is.readBoolean();
 158  0 key = is.readObject();
 159    }
 160    catch(Exception e)
 161    {
 162  0 throw new RuntimeException("failed to decode message", e);
 163    }
 164  0 if(update)
 165    {
 166  0 Object value = delegate.get(key);
 167  0 if(value != null)
 168    {
 169  0 if(value instanceof Refreshable)
 170    {
 171  0 try
 172    {
 173  0 ((Refreshable)value).refresh();
 174  0 return;
 175    }
 176    catch(NoLongerValidException e)
 177    {
 178  0 delegate.remove(key);
 179    }
 180    }
 181    }
 182    }
 183    // not update || non refreshable
 184  0 delegate.remove(key);
 185    }
 186   
 187    // implementation ////////////////////////////////////////////////////////
 188   
 189  0 private void notify(boolean update, Object key)
 190    {
 191  0 byte[] message;
 192  0 try
 193    {
 194  0 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 195  0 ObjectOutputStream os = new ObjectOutputStream(baos);
 196  0 os.writeBoolean(update);
 197  0 os.writeObject(key);
 198  0 os.flush();
 199  0 message = baos.toByteArray();
 200    }
 201    catch(IOException e)
 202    {
 203  0 throw new RuntimeException("failed to encode requeuest", e);
 204    }
 205  0 notification.sendNotification(channel, message, false);
 206    }
 207    }