Clover coverage report - Ledge Components - SNAPSHOT
Coverage timestamp: Fri Nov 17 2006 05:13:20 CET
file stats: LOC: 144   Methods: 4
NCLOC: 80   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
PersistenceValueFactory.java 50% 54.2% 50% 52.6%
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;
 30   
 31    import org.jcontainer.dna.Configuration;
 32    import org.objectledge.cache.spi.CacheFactorySPI;
 33    import org.objectledge.cache.spi.ConfigurableValueFactory;
 34    import org.objectledge.database.persistence.Persistence;
 35    import org.objectledge.database.persistence.PersistenceException;
 36    import org.objectledge.database.persistence.Persistent;
 37    import org.objectledge.database.persistence.PersistentFactory;
 38   
 39    /**
 40    * An implementation of ValueFactory interface that uses the persistency.
 41    *
 42    * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
 43    * @version $Id: PersistenceValueFactory.java,v 1.5 2005/02/10 17:49:17 rafal Exp $
 44    */
 45    public class PersistenceValueFactory
 46    implements ConfigurableValueFactory
 47    {
 48    /**
 49    * The persistent factory.
 50    */
 51    private PersistentFactory factory;
 52   
 53    /**
 54    * The persistence.
 55    */
 56    private Persistence persistence;
 57   
 58    /**
 59    * Initializes the factory.
 60    *
 61    * @param persistence the persistence.
 62    * @param cl the class of the values.
 63    */
 64  828 public void init(final Class cl, Persistence persistence)
 65    {
 66  828 this.persistence = persistence;
 67  828 if(!Persistent.class.isAssignableFrom(cl))
 68    {
 69  0 throw new IllegalArgumentException(cl.getName()+" does not implement " +
 70    "Persistent interface");
 71    }
 72  828 factory = new PersistentFactory()
 73    {
 74  0 public Persistent newInstance()
 75    throws PersistenceException
 76    {
 77  0 try
 78    {
 79  0 return (Persistent)cl.newInstance();
 80    }
 81    catch(Exception e)
 82    {
 83  0 throw new PersistenceException("failed to instantiate "+cl.getName());
 84    }
 85    }
 86    };
 87    }
 88   
 89    /**
 90    * Produces a value that corresponds to a given key.
 91    *
 92    * <p>This metod expects that the key extends the
 93    * <code>java.lang.Number</code> interface.</p>
 94    *
 95    * @param key the key.
 96    * @return the object.
 97    */
 98  0 public Object getValue(Object key)
 99    {
 100  0 if(key instanceof Number)
 101    {
 102  0 try
 103    {
 104  0 return persistence.load(((Number)key).longValue(), factory);
 105    }
 106    catch(PersistenceException e)
 107    {
 108  0 throw new RuntimeException("failed to produce value", e);
 109    }
 110    }
 111  0 throw new IllegalArgumentException(key.getClass().getName()+
 112    " does not extend java.lang.Number");
 113    }
 114   
 115    /**
 116    * {@inheritDoc}
 117    */
 118  782 public void configure(CacheFactorySPI caching, String name, Configuration config)
 119    {
 120  782 Configuration[] parameters = config.getChildren("parameter");
 121  782 String vClass = null;
 122  782 for(int i = 0; i < parameters.length; i++)
 123    {
 124  782 if(parameters[i].getAttribute("name","").equals("valueClass"))
 125    {
 126  782 vClass = parameters[i].getAttribute("value","");
 127    }
 128    }
 129  782 if(vClass == null || vClass.length()==0)
 130    {
 131  0 throw new IllegalArgumentException("required parameter valueClass is missing");
 132    }
 133  782 Class cl = null;
 134  782 try
 135    {
 136  782 cl = Class.forName(vClass);
 137    }
 138    catch(Exception e)
 139    {
 140  0 throw new IllegalArgumentException("cannot instantiate the class"+e.getMessage());
 141    }
 142  782 init(cl, caching.getPersistence());
 143    }
 144    }