Clover coverage report - Replica - 1.0-Alpha
Coverage timestamp: Dom Fev 1 2004 17:00:58 WET
file stats: LOC: 210   Methods: 13
NCLOC: 100   Classes: 2
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
HashtableMappings.java 0% 2,1% 7,7% 2,6%
coverage coverage
 1   
 /*
 2   
  * Replica is published under the terms
 3   
  * of the Apache Software License.
 4   
  */
 5   
 package replica.command;
 6   
 
 7   
 import java.util.Enumeration;
 8   
 import java.util.Hashtable;
 9   
 
 10   
 import replica.utils.BeanUtils;
 11   
 
 12   
 
 13   
 /**
 14   
  *  This Mappings implementation stores the mapped configuration in a hashtable.
 15   
  * 
 16   
  * @author Pedro Costa
 17   
  * @author Helder Silva
 18   
  * @since 17/Jan/2004
 19   
  */
 20   
 public class HashtableMappings implements Mappings {
 21   
     
 22   
     // contain the class names mapped to action ids.
 23   
     Hashtable classMapping = new Hashtable();
 24   
     
 25   
     // contain the initial parameters for a given class name
 26   
     // the key is the class name
 27   
     ParametersMappings initParameters = new ParametersMappings();
 28   
 
 29   
     /**
 30   
      * Default constructor. 
 31   
      */
 32  0
     public HashtableMappings() {
 33  0
         super();
 34   
     }
 35   
     
 36  0
     public void addMapping(String actionId, String className){
 37  0
         classMapping.put(actionId, className);
 38   
     }
 39   
     
 40  0
     public void addMapping(String actionId, String className, Hashtable parameters ){
 41  0
         if( parameters == null ){
 42  0
             addMapping(actionId, className);
 43  0
             return;
 44   
         }
 45   
         
 46  0
         int size = parameters.size();
 47  0
         addMapping( actionId, className, (String[])parameters.keySet().toArray(new String[size]),
 48   
                                 parameters.values().toArray(new Object[size]) );
 49   
     }
 50   
     
 51  0
     public void addMapping(String actionId, String className, String[] paramNames, Object[] paramValues){
 52  0
         addMapping(actionId, className);
 53  0
         initParameters.addParameters(className, paramNames, paramValues);
 54   
     }
 55   
     
 56   
     /** 
 57   
      * 
 58   
      * @see replica.command.Mappings#getMappedClass(java.lang.String)
 59   
      */
 60  0
     public Class getMappedClass(String actionId) throws CommandException{
 61   
         
 62  0
         try{
 63  0
             Class clazz = Class.forName((String)classMapping.get( actionId ));
 64   
             
 65  0
             if( clazz == null )
 66  0
                 throw new NoSuchCommandException("No command found for action id " + actionId, actionId);
 67   
                 
 68  0
             return clazz;
 69   
         }
 70   
         catch(ClassNotFoundException e){
 71  0
             throw new CommandException(e);
 72   
         }
 73   
     }
 74   
 
 75   
     /* (non-Javadoc)
 76   
      * @see replica.server.cmd.Mappings#getNewCommand(java.lang.String)
 77   
      */
 78  0
     public Command getNewCommand(String actionId) throws CommandException {
 79   
         
 80  0
         Class clazz = getMappedClass( actionId );
 81   
         
 82  0
         try{
 83  0
             Command command = (Command)clazz.newInstance();
 84   
             
 85  0
             initCommand( command );
 86   
             
 87  0
             return command;
 88   
         }
 89   
         catch(IllegalAccessException e){
 90  0
             throw new CommandException("Error creating command from class [" + clazz + 
 91   
                     "] for action id [" + actionId + "]. Look for the root cause on this exception." + e.toString(), e );
 92   
         }
 93   
         catch(InstantiationException e ){
 94  0
             throw new CommandException("Error creating command from class [" + clazz + 
 95   
                     "] for action id [" + actionId + "]. Look for the root cause on this exception." + e.toString(), e );
 96   
         }
 97   
          
 98   
     }
 99   
     
 100   
     /**
 101   
      * Initialize the given command with the defined parameters (if any).
 102   
      * 
 103   
      * @param command
 104   
      */
 105  0
     protected void initCommand(Command command){
 106   
         
 107  0
         Hashtable table = initParameters.getParameters(command.getClass().getName());
 108   
         
 109  0
         if( table == null || table.size() == 0 )
 110  0
             return;
 111   
         
 112  0
         Enumeration enum = table.keys();
 113  0
         while( enum.hasMoreElements() ){
 114  0
             String key = (String)enum.nextElement();
 115  0
             BeanUtils.writeProperty( command, key, table.get(key).toString() );
 116   
         }
 117   
     }
 118   
     
 119   
     class ParametersMappings{
 120   
         
 121   
         private Hashtable mappings = new Hashtable();
 122   
         
 123  2
         ParametersMappings(){
 124  2
             super();
 125   
         }
 126   
         
 127   
         /**
 128   
          * Adds initial parameter values for the given class.
 129   
          * 
 130   
          * @param clazz the class
 131   
          * @param paramName parameter name
 132   
          * @param paramValue parameter value
 133   
          */
 134  0
         public void addParameters( String className, String[] paramNames, Object[] paramValues ){
 135   
             
 136  0
             if( paramNames.length != paramValues.length )
 137  0
                 throw new IllegalArgumentException("Parameter Names and values must have the same lenght.");
 138   
             
 139  0
             Hashtable params = (Hashtable)mappings.get(className);
 140   
             
 141  0
             if( params == null ){
 142  0
                 params = new Hashtable();
 143  0
                 mappings.put(className, params);
 144   
             }
 145   
             
 146  0
             for (int i = 0; i < paramNames.length; i++)
 147  0
                 params.put(paramNames[i], paramValues[i]);
 148   
         }
 149   
         
 150   
         /**
 151   
          * Adds initial Parameter values for the given class.
 152   
          * 
 153   
          * @param clazz the class
 154   
          * @param parameters Hashtable containing the parameters.
 155   
          *                  Where the key must the parameter name and the value the parameter value.
 156   
          */
 157  0
         public void addParameters( String className, Hashtable parameters){
 158   
             
 159  0
             int size = parameters.size();
 160   
             
 161  0
             addParameters( className, (String[])parameters.entrySet().toArray( new String[ size ] ),
 162   
                                                         parameters.values().toArray( new Object[ size ] ) );
 163   
         }
 164   
 
 165   
         /**
 166   
          * Shortcut method.
 167   
          * 
 168   
          * @see #addParameters( Class, String[], Object[])
 169   
          * @param clazz
 170   
          * @param paramName
 171   
          * @param paramValue
 172   
          */        
 173  0
         public void addParameter( String className, String paramName, Object paramValue ){
 174   
             
 175  0
             addParameters(className, new String[]{paramName}, new Object[]{paramValue});
 176   
         }
 177   
         
 178   
         /**
 179   
          * Return the initial parameters for the given class.
 180   
          * 
 181   
          * @param clazz
 182   
          * @return table conting as key the parameters names and as values the <i>values</i>.
 183   
          */
 184  0
         public Hashtable getParameters( String className  ){
 185   
             
 186  0
             Hashtable table = (Hashtable)mappings.get( className );
 187   
             
 188  0
             if( table == null )
 189  0
                 return new Hashtable();
 190   
                 
 191  0
             return table;
 192   
         }
 193   
     }
 194   
 
 195   
     /**
 196   
          * Adds mappings for the received commands.
 197   
          * 
 198   
          * @param className the name of the command class to map the commands in the received list.
 199   
          * @param paramsTable a table with the initial parameters value for the command class.
 200   
          * @param commands a list of commands to map to the received action class.
 201   
          */
 202  0
     protected void addMappingsForClass(String className, Hashtable paramsTable, String[] commands) {
 203   
             
 204  0
         for (int i = 0; i < commands.length; i++) {
 205  0
             addMapping(commands[i], className, paramsTable );
 206   
         }
 207   
     }
 208   
 
 209   
 }
 210