AbstractCacheWrapper.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.utils.cache;

  21. import java.lang.reflect.Method;

  22. import org.slf4j.Logger;
  23. import org.openspcoop2.utils.SemaphoreLock;
  24. import org.openspcoop2.utils.UtilsException;

  25. /**
  26.  * AbstractCacheManager
  27.  *
  28.  * @author Poli Andrea (apoli@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */
  32. public abstract class AbstractCacheWrapper {

  33.     private final org.openspcoop2.utils.Semaphore lockCache = new org.openspcoop2.utils.Semaphore("AbstractCacheWrapper");
  34.     private Cache cache = null;
  35.     private Logger log = null;
  36.     private String cacheName = null;
  37.    
  38.     public AbstractCacheWrapper(CacheType cacheType, String cacheName, Logger log) throws UtilsException{
  39.        
  40.         this(cacheType, cacheName, true, log, null, null, null, null);
  41.        
  42.     }
  43.    
  44.     public AbstractCacheWrapper(CacheType cacheType, String cacheName, boolean initializeCache, Logger log) throws UtilsException{
  45.        
  46.         this(cacheType, cacheName, initializeCache, log, null, null, null, null);
  47.        
  48.     }
  49.    
  50.     public AbstractCacheWrapper(CacheType cacheType, String cacheName, Logger log,
  51.             Integer cacheSize,CacheAlgorithm cacheAlgorithm,
  52.             Integer itemIdleTimeSeconds, Integer itemLifeTimeSeconds) throws UtilsException{
  53.        
  54.         this(cacheType, cacheName, true, log, cacheSize, cacheAlgorithm, itemIdleTimeSeconds, itemLifeTimeSeconds);
  55.        
  56.     }
  57.    
  58.     private AbstractCacheWrapper(CacheType cacheType, String cacheName, boolean initializeCache, Logger log,
  59.             Integer cacheSize,CacheAlgorithm cacheAlgorithm,
  60.             Integer itemIdleTimeSeconds, Integer itemLifeTimeSeconds) throws UtilsException{
  61.        
  62.         if(cacheName==null){
  63.             throw new UtilsException("Cache name undefined");
  64.         }
  65.         this.cacheName = cacheName;
  66.        
  67.         if(log==null){
  68.             throw new UtilsException("Logger undefined");
  69.         }
  70.         this.log = log;
  71.        
  72.         if(initializeCache){
  73.             this.initCacheConfigurazione(cacheType, cacheSize, cacheAlgorithm, itemIdleTimeSeconds, itemLifeTimeSeconds);
  74.         }
  75.     }
  76.    
  77.     public String getCacheName() {
  78.         return this.cacheName;
  79.     }
  80.        
  81.     private void initCacheConfigurazione(CacheType cacheType, Integer cacheSize,CacheAlgorithm cacheAlgorithm,
  82.             Integer itemIdleTimeSeconds, Integer itemLifeTimeSeconds) throws UtilsException{
  83.        
  84.         this.cache = new Cache(cacheType, this.cacheName);
  85.        
  86.         String msg = null;
  87.         if( (cacheSize!=null) ||
  88.                 (cacheAlgorithm != null) ){
  89.          
  90.             if( cacheSize!=null ){
  91.                 int dimensione = -1;
  92.                 try{
  93.                     dimensione = cacheSize.intValue();
  94.                     msg = "Cache size ("+this.cacheName+"): "+dimensione;
  95.                     this.log.debug(msg);
  96.                     this.cache.setCacheSize(dimensione);
  97.                 }catch(Exception error){
  98.                     throw new UtilsException("Cache size parameter wrong ("+this.cacheName+"): "+error.getMessage());
  99.                 }
  100.             }
  101.             if( cacheAlgorithm != null ){
  102.                 msg = "Cache algorithm ("+this.cacheName+"): "+cacheAlgorithm.name();
  103.                 this.log.debug(msg);
  104.                 this.cache.setCacheAlgoritm(cacheAlgorithm);
  105.             }
  106.            
  107.         }
  108.        
  109.         if( (itemIdleTimeSeconds != null) ||
  110.                 (itemLifeTimeSeconds != null) ){

  111.             if( itemIdleTimeSeconds != null  ){
  112.                 int itemIdleTime = -1;
  113.                 try{
  114.                     itemIdleTime = itemIdleTimeSeconds.intValue();
  115.                     msg = "Cache 'IdleTime' attribute ("+this.cacheName+"): "+itemIdleTimeSeconds;
  116.                     this.log.debug(msg);
  117.                     this.cache.setItemIdleTime(itemIdleTime);
  118.                 }catch(Exception error){
  119.                     throw new UtilsException("Cache 'IdleTime' attribute wrong ("+this.cacheName+"): "+error.getMessage());
  120.                 }
  121.             }
  122.             if( itemLifeTimeSeconds != null  ){
  123.                 int itemLifeSecond = -1;
  124.                 try{
  125.                     itemLifeSecond = itemLifeTimeSeconds.intValue();
  126.                     msg = "Cache 'LifeTime' attribute ("+this.cacheName+"): "+itemLifeSecond;
  127.                     this.log.debug(msg);
  128.                     this.cache.setItemLifeTime(itemLifeSecond);
  129.                 }catch(Exception error){
  130.                     throw new UtilsException("Cache 'LifeTime' attribute wrong ("+this.cacheName+"): "+error.getMessage());
  131.                 }
  132.             }
  133.            
  134.         }
  135.        
  136.         this.cache.build();
  137.     }
  138.    
  139.    
  140.    
  141.     /* --------------- Cache --------------------*/
  142.    
  143.     public Logger getLog() {
  144.         return this.log;
  145.     }
  146.    
  147.     public boolean isCacheEnabled(){
  148.         return this.cache!=null;
  149.     }
  150.    
  151.     @SuppressWarnings("deprecation")
  152.     @Deprecated
  153.     public void disableSyncronizedGet() throws UtilsException {
  154.         if(this.cache==null) {
  155.             throw new UtilsException("Cache disabled");
  156.         }
  157.         this.cache.disableSyncronizedGet();
  158.     }
  159.     @SuppressWarnings("deprecation")
  160.     @Deprecated
  161.     public boolean isDisableSyncronizedGet() throws UtilsException {
  162.         if(this.cache==null) {
  163.             throw new UtilsException("Cache disabled");
  164.         }
  165.         return this.cache.isDisableSyncronizedGet();
  166.     }
  167.    
  168.     public void resetCache() throws UtilsException{
  169.         if(this.cache!=null){
  170.             try{
  171.                 this.cache.clear();
  172.             }catch(Exception e){
  173.                 throw new UtilsException(e.getMessage(),e);
  174.             }
  175.         }
  176.     }
  177.     public String printStatsCache(String separator) throws UtilsException{
  178.         if(this.cache!=null){
  179.             try{
  180.                 return this.cache.printStats(separator);
  181.             }catch(Exception e){
  182.                 throw new UtilsException(e.getMessage(),e);
  183.             }
  184.         }else{
  185.             throw new UtilsException("Cache disabled");
  186.         }
  187.     }
  188.     public void enableCache() throws UtilsException{
  189.         if(this.cache!=null)
  190.             throw new UtilsException("Cache already enabled");
  191.         else{
  192.             try{
  193.                 this.cache = new Cache(CacheType.JCS, this.cacheName); // lascio JCS come default abilitato via jmx
  194.             }catch(Exception e){
  195.                 throw new UtilsException(e.getMessage(),e);
  196.             }
  197.         }
  198.     }
  199.     public void enableCache(Integer cacheSize,Boolean cacheAlgorithmLRU,Integer itemIdleTimeSeconds,Integer itemLifeTimeSeconds) throws UtilsException{
  200.        
  201.         CacheAlgorithm cacheAlgorithm = null;
  202.         if(cacheAlgorithmLRU!=null){
  203.             if(cacheAlgorithmLRU){
  204.                 cacheAlgorithm = CacheAlgorithm.LRU;
  205.             }
  206.             else{
  207.                 cacheAlgorithm = CacheAlgorithm.MRU;
  208.             }
  209.         }
  210.        
  211.         this.enableCache(cacheSize, cacheAlgorithm, itemIdleTimeSeconds, itemLifeTimeSeconds);
  212.        
  213.     }
  214.     public void enableCache(Integer cacheSize,CacheAlgorithm cacheAlgorithm,Integer itemIdleTimeSeconds,Integer itemLifeTimeSeconds) throws UtilsException{
  215.         if(this.cache!=null)
  216.             throw new UtilsException("Cache already enabled");
  217.         else{
  218.             try{
  219.                 initCacheConfigurazione(CacheType.JCS, cacheSize, cacheAlgorithm, itemIdleTimeSeconds, itemLifeTimeSeconds); // lascio JCS come default abilitato via jmx
  220.             }catch(Exception e){
  221.                 throw new UtilsException(e.getMessage(),e);
  222.             }
  223.         }
  224.     }
  225.     public void disableCache() throws UtilsException{
  226.         if(this.cache==null)
  227.             throw new UtilsException("Cache already disabled");
  228.         else{
  229.             try{
  230.                 this.cache.clear();
  231.                 this.cache = null;
  232.             }catch(Exception e){
  233.                 throw new UtilsException(e.getMessage(),e);
  234.             }
  235.         }
  236.     }
  237.     public String listKeysCache(String separator) throws UtilsException{
  238.         if(this.cache!=null){
  239.             try{
  240.                 return this.cache.printKeys(separator);
  241.             }catch(Exception e){
  242.                 throw new UtilsException(e.getMessage(),e);
  243.             }
  244.         }else{
  245.             throw new UtilsException("Cache disabled");
  246.         }
  247.     }
  248.     public String getObjectCache(String key) throws UtilsException{
  249.         if(this.cache!=null){
  250.             try{
  251.                 Object o = this.cache.get(key);
  252.                 if(o!=null){
  253.                     return o.toString();
  254.                 }else{
  255.                     return "Object with key ["+key+"] not exists";
  256.                 }
  257.             }catch(Exception e){
  258.                 throw new UtilsException(e.getMessage(),e);
  259.             }
  260.         }else{
  261.             throw new UtilsException("Cache disabled");
  262.         }
  263.     }
  264.     public String removeObjectCache(String key) throws UtilsException{
  265.         if(this.cache!=null){
  266.             try{
  267.                 Object o = this.cache.get(key);
  268.                 if(o!=null){
  269.                     this.cache.remove(key);
  270.                     return "Object with key ["+key+"] deleted";
  271.                 }else{
  272.                     return "Object with key ["+key+"] not exists";
  273.                 }
  274.             }catch(Exception e){
  275.                 throw new UtilsException(e.getMessage(),e);
  276.             }
  277.         }else{
  278.             throw new UtilsException("Cache disabled");
  279.         }
  280.     }
  281.    
  282.    
  283.    
  284.    
  285.    
  286.     /* --------------- CacheWrapper --------------------*/
  287.    
  288.     public abstract Object getDriver(Object param) throws UtilsException;
  289.     public abstract boolean isCachableException(Throwable e);
  290.    
  291.     // Serve per poter sapere quale sia la chiave ed usarla nei metodi soprastanti
  292.     public String getKeyCache(String keyCacheParam,String methodName){
  293.         return this.buildKeyCache(keyCacheParam, methodName);
  294.     }
  295.    
  296.     private String buildKeyCache(String keyCacheParam,String methodName){
  297.         if(keyCacheParam!=null && !"".equals(keyCacheParam))
  298.             return methodName + "." + keyCacheParam;
  299.         else
  300.             return methodName;
  301.     }
  302.    
  303.     public void duplicateObjectCache(String oldKeyCacheParam,String oldMethodName,
  304.             String newKeyCacheParam,String newMethodName,
  305.             boolean debug,boolean throwExceptionIfNotExists) throws UtilsException{
  306.        
  307.         if(this.cache==null){
  308.             throw new UtilsException("Cache disabled");
  309.         }
  310.        
  311.         //synchronized(this.cache){
  312.         SemaphoreLock lock = this.lockCache.acquireThrowRuntime("duplicateObjectCache");
  313.         try {
  314.            
  315. //          if(debug){
  316. //              this.log.debug("@"+keyCache+"@ Cache info: "+this.cache.toString());
  317. //              this.log.debug("@"+keyCache+"@ Keys: \n\t"+this.cache.printKeys("\n\t"));
  318. //          }
  319.            
  320.             String oldKey = this.buildKeyCache(oldKeyCacheParam, oldMethodName);
  321.             org.openspcoop2.utils.cache.CacheResponse response =
  322.                     (org.openspcoop2.utils.cache.CacheResponse) this.cache.get(oldKey);
  323.             if(response != null){
  324.                 String newKey = this.buildKeyCache(newKeyCacheParam, newMethodName);
  325.                 this.cache.remove(newKey);
  326.                 this.cache.put(newKey,response);
  327.                 this.log.debug("@"+newKey+"@ Entry add");
  328.             }
  329.             else{
  330.                 if(debug){
  331.                     this.log.debug("@"+oldKey+"@ Entry not exists");
  332.                 }
  333.                 if(throwExceptionIfNotExists){
  334.                     throw new UtilsException("Entry with key ["+oldKey+"] not exists");
  335.                 }
  336.             }
  337.         }finally {
  338.             this.lockCache.release(lock, "duplicateObjectCache");
  339.         }
  340.        
  341.     }
  342.    
  343.     public Object getObjectCache(Object driverParam,boolean debug,String keyCacheParam,String methodName,Object... arguments) throws Throwable{
  344.         return _getObjectCache(driverParam,debug,keyCacheParam,methodName,null,arguments);
  345.     }
  346.     public Object getObjectCache(Object driverParam,boolean debug,String keyCacheParam,String methodName,Class<?> [] cArguments, Object... arguments) throws Throwable{
  347.         return _getObjectCache(driverParam,debug,keyCacheParam,methodName,cArguments,arguments);
  348.     }
  349.     public Object _getObjectCache(Object driverParam,boolean debug,String keyCacheParam,String methodName,Class<?> [] cArguments, Object... arguments) throws Throwable{
  350.                
  351.        
  352.         Throwable cachableException = null;
  353.         Throwable notCachableException = null;
  354.         Object obj = null;
  355.         boolean throwException = false;
  356.        
  357.         if(methodName == null)
  358.             throw new UtilsException("MethodName undefined");
  359.        
  360.         String keyCache = null;
  361.        
  362.         if(this.cache==null){
  363.             if(debug){
  364.                 this.log.debug("@method:"+methodName+"@ (Cache Disabled) search object with driver...");
  365.             }
  366.             try{
  367.                 obj = getObject(driverParam, debug, methodName, cArguments, arguments);
  368.             }catch(Throwable e){
  369.                 if(this.isCachableException(e)){
  370.                     cachableException = e;
  371.                 }
  372.                 else{
  373.                     notCachableException = e;
  374.                 }
  375.             }
  376.         }
  377.         else{
  378.        
  379.             // Fix: devo prima verificare se ho la chiave in cache prima di mettermi in sincronizzazione.
  380. //          if(keyCacheParam == null)
  381. //              throw new UtilsException("["+methodName+"]: KeyCache undefined");  
  382.             keyCache = this.buildKeyCache(keyCacheParam, methodName);
  383.            
  384. //          if(debug){
  385. //              this.log.debug("@"+keyCache+"@ Cache info: "+this.cache.toString());
  386. //              this.log.debug("@"+keyCache+"@ Keys: \n\t"+this.cache.printKeys("\n\t"));
  387. //          }
  388.    
  389.             // se e' attiva una cache provo ad utilizzarla
  390.             org.openspcoop2.utils.cache.CacheResponse response =
  391.                 (org.openspcoop2.utils.cache.CacheResponse) this.cache.get(keyCache);
  392.             if(response != null){
  393.                 if(response.getObject()!=null){
  394.                     if(debug){
  395.                         this.log.debug("@"+keyCache+"@ Object (type:"+response.getObject().getClass().getName()+") (method:"+methodName+") found in cache.");
  396.                     }
  397.                     return response.getObject();
  398.                 }
  399.                 else if(response.isObjectNull()){
  400.                     if(debug){
  401.                         this.log.debug("@"+keyCache+"@ Response null (method:"+methodName+") found in cache.");
  402.                     }
  403.                     return null;
  404.                 }
  405.                 else if(response.getException()!=null){
  406.                     this.log.debug("@"+keyCache+"@ Exception (type:"+response.getException().getClass().getName()+") (method:"+methodName+") found in cache.");
  407.                     throwException = true;
  408.                     throw (Throwable) response.getException();
  409.                 }else{
  410.                     this.log.error("@"+keyCache+"@ Found entry in cache with key ["+keyCache+"] (method:"+methodName+") without object and exception???");
  411.                 }
  412.             }
  413.            
  414.             //synchronized(this.cache){
  415.             SemaphoreLock lock = this.lockCache.acquire("getObjectCache");
  416.             try {
  417.                
  418.                 try{
  419.                                        
  420. //                  if(debug){
  421. //                      this.log.debug("@"+keyCache+"@ Cache info: "+this.cache.toString());
  422. //                      this.log.debug("@"+keyCache+"@ Keys: \n\t"+this.cache.printKeys("\n\t"));
  423. //                  }
  424.        
  425.                     // se e' attiva una cache provo ad utilizzarla
  426.                     response =
  427.                         (org.openspcoop2.utils.cache.CacheResponse) this.cache.get(keyCache);
  428.                     if(response != null){
  429.                         if(response.getObject()!=null){
  430.                             if(debug){
  431.                                 this.log.debug("@"+keyCache+"@ Object (type:"+response.getObject().getClass().getName()+") (method:"+methodName+") found in cache.");
  432.                             }
  433.                             return response.getObject();
  434.                         }
  435.                         else if(response.isObjectNull()){
  436.                             if(debug){
  437.                                 this.log.debug("@"+keyCache+"@ Response null (method:"+methodName+") found in cache.");
  438.                             }
  439.                             return null;
  440.                         }
  441.                         else if(response.getException()!=null){
  442.                             this.log.debug("@"+keyCache+"@ Exception (type:"+response.getException().getClass().getName()+") (method:"+methodName+") found in cache.");
  443.                             throwException = true;
  444.                             throw (Throwable) response.getException();
  445.                         }else{
  446.                             this.log.error("@"+keyCache+"@ Found entry in cache with key ["+keyCache+"] (method:"+methodName+") without object and exception???");
  447.                         }
  448.                     }
  449.        
  450.                     // Effettuo le query
  451.                     if(debug){
  452.                         this.log.debug("@"+keyCache+"@ search object (method:"+methodName+") with driver...");
  453.                     }
  454.                     boolean nullObject = false;
  455.                     boolean cacheble = false;
  456.                     try{
  457.                         obj = getObject(driverParam, debug, methodName, cArguments, arguments);
  458.                         nullObject = true;
  459.                         cacheble = true;
  460.                     }catch(Throwable e){
  461.                         if(this.isCachableException(e)){
  462.                             cachableException = e;
  463.                             cacheble = true;
  464.                         }
  465.                         else{
  466.                             notCachableException = e;
  467.                         }
  468.                     }
  469.                    
  470.                            
  471.                     // Aggiungo la risposta in cache (se esiste una cache)  
  472.                     // Se ho una eccezione aggiungo in cache solo una not found
  473.                     if( cacheble ){    
  474.                         try{    
  475.                             org.openspcoop2.utils.cache.CacheResponse responseCache = new org.openspcoop2.utils.cache.CacheResponse();
  476.                             if(cachableException!=null){
  477.                                 if(debug){
  478.                                     this.log.debug("@"+keyCache+"@ Add Exception in cache");
  479.                                 }
  480.                                 responseCache.setException(cachableException);
  481.                             }else if(obj!=null){
  482.                                 if(debug){
  483.                                     this.log.debug("@"+keyCache+"@ Add Object in cache");
  484.                                 }
  485.                                 responseCache.setObject((java.io.Serializable)obj);
  486.                             }else if(nullObject){
  487.                                 if(debug){
  488.                                     this.log.debug("@"+keyCache+"@ Add Null Response in cache");
  489.                                 }
  490.                                 responseCache.setObjectNull(true);
  491.                             }
  492.                             this.cache.put(keyCache,responseCache);
  493.                         }catch(UtilsException e){
  494.                             this.log.error("@"+keyCache+"@ error occurs during insert in cache: "+e.getMessage(),e);
  495.                         }
  496.                     }
  497.        
  498.                 }catch(Throwable e){
  499.                     if(throwException == false){
  500.                         this.log.error("@"+keyCache+"@ Error occurs: "+e.getMessage(),e);
  501.                         throw new UtilsException(e.getMessage(),e);
  502.                     }
  503.                     else{
  504.                         cachableException = e;
  505.                     }
  506.                 }
  507.                
  508.             }finally {
  509.                 this.lockCache.release(lock, "getObjectCache");
  510.             }
  511.         }
  512.        
  513.         String cacheMsg = "";
  514.         if(this.cache!=null && keyCache!=null){
  515.             cacheMsg = "@key:"+keyCache+"@";
  516.         }
  517.         else{
  518.             cacheMsg = "@method:"+methodName+"@";
  519.         }
  520.         if(cachableException!=null){
  521.             if(debug){
  522.                 this.log.debug(cacheMsg+" throw CachableException: "+cachableException.getClass().getName());
  523.             }
  524.             throw cachableException;
  525.         }
  526.         else if(notCachableException!=null){
  527.             if(debug){
  528.                 this.log.debug(cacheMsg+" throw NotCachableException: "+notCachableException.getClass().getName());
  529.             }
  530.             throw notCachableException;
  531.         }
  532.         else if(obj!=null){
  533.             if(debug){
  534.                 this.log.debug(cacheMsg+" return Object: "+obj.getClass().getName());
  535.             }
  536.             return obj;
  537.         }else{
  538.             if(debug){
  539.                 this.log.debug(cacheMsg+" return null response");
  540.             }
  541.             return null;
  542.         }
  543.     }
  544.    
  545.     private Object getObject(Object driverParam,boolean debug,String methodName,Class<?> [] cArguments, Object... arguments) throws Throwable{

  546.        
  547.         // Effettuo le query nella mia gerarchia di registri.
  548.         Object obj = null;
  549.         try{
  550.             Object driver = this.getDriver(driverParam);
  551.             if(driver==null){
  552.                 throw new UtilsException("Driver undefined");
  553.             }
  554.             if(arguments.length==0){
  555.                 Method method =  driver.getClass().getMethod(methodName);
  556.                 obj = method.invoke(driver);
  557.             }else if(arguments.length==1){
  558.                 Method method =  driver.getClass().getMethod(methodName,
  559.                         (cArguments!=null ? cArguments[0] :arguments[0].getClass()));
  560.                 obj = method.invoke(driver,
  561.                         arguments[0]);
  562.             }else if(arguments.length==2){
  563.                 Method method =  driver.getClass().getMethod(methodName,
  564.                         (cArguments!=null ? cArguments[0] :arguments[0].getClass()),
  565.                         (cArguments!=null ? cArguments[1] :arguments[1].getClass()));
  566.                 obj = method.invoke(driver,
  567.                         arguments[0],
  568.                         arguments[1]);
  569.             }else if(arguments.length==3){
  570.                 Method method =  driver.getClass().getMethod(methodName,
  571.                         (cArguments!=null ? cArguments[0] :arguments[0].getClass()),
  572.                         (cArguments!=null ? cArguments[1] :arguments[1].getClass()),
  573.                         (cArguments!=null ? cArguments[2] :arguments[2].getClass()));
  574.                 obj = method.invoke(driver,
  575.                         arguments[0],
  576.                         arguments[1],
  577.                         arguments[2]);
  578.             }else if(arguments.length==4){
  579.                 Method method =  driver.getClass().getMethod(methodName,
  580.                         (cArguments!=null ? cArguments[0] :arguments[0].getClass()),
  581.                         (cArguments!=null ? cArguments[1] :arguments[1].getClass()),
  582.                         (cArguments!=null ? cArguments[2] :arguments[2].getClass()),
  583.                         (cArguments!=null ? cArguments[3] :arguments[3].getClass()));
  584.                 obj = method.invoke(driver,
  585.                         arguments[0],
  586.                         arguments[1],
  587.                         arguments[2],
  588.                         arguments[3]);
  589.             }else if(arguments.length==5){
  590.                 Method method =  driver.getClass().getMethod(methodName,
  591.                         (cArguments!=null ? cArguments[0] :arguments[0].getClass()),
  592.                         (cArguments!=null ? cArguments[1] :arguments[1].getClass()),
  593.                         (cArguments!=null ? cArguments[2] :arguments[2].getClass()),
  594.                         (cArguments!=null ? cArguments[3] :arguments[3].getClass()),
  595.                         (cArguments!=null ? cArguments[4] :arguments[4].getClass()));
  596.                 obj = method.invoke(driver,
  597.                         arguments[0],
  598.                         arguments[1],
  599.                         arguments[2],
  600.                         arguments[3],
  601.                         arguments[4]);
  602.             }else if(arguments.length==6){
  603.                 Method method =  driver.getClass().getMethod(methodName,
  604.                         (cArguments!=null ? cArguments[0] :arguments[0].getClass()),
  605.                         (cArguments!=null ? cArguments[1] :arguments[1].getClass()),
  606.                         (cArguments!=null ? cArguments[2] :arguments[2].getClass()),
  607.                         (cArguments!=null ? cArguments[3] :arguments[3].getClass()),
  608.                         (cArguments!=null ? cArguments[4] :arguments[4].getClass()),
  609.                         (cArguments!=null ? cArguments[5] :arguments[5].getClass()));
  610.                 obj = method.invoke(driver,
  611.                         arguments[0],
  612.                         arguments[1],
  613.                         arguments[2],
  614.                         arguments[3],
  615.                         arguments[4],
  616.                         arguments[5]);
  617.             }else if(arguments.length==7){
  618.                 Method method =  driver.getClass().getMethod(methodName,
  619.                         (cArguments!=null ? cArguments[0] :arguments[0].getClass()),
  620.                         (cArguments!=null ? cArguments[1] :arguments[1].getClass()),
  621.                         (cArguments!=null ? cArguments[2] :arguments[2].getClass()),
  622.                         (cArguments!=null ? cArguments[3] :arguments[3].getClass()),
  623.                         (cArguments!=null ? cArguments[4] :arguments[4].getClass()),
  624.                         (cArguments!=null ? cArguments[5] :arguments[5].getClass()),
  625.                         (cArguments!=null ? cArguments[6] :arguments[6].getClass()));
  626.                 obj = method.invoke(driver,
  627.                         arguments[0],
  628.                         arguments[1],
  629.                         arguments[2],
  630.                         arguments[3],
  631.                         arguments[4],
  632.                         arguments[5],
  633.                         arguments[6]);
  634.             }else if(arguments.length==8){
  635.                 Method method =  driver.getClass().getMethod(methodName,
  636.                         (cArguments!=null ? cArguments[0] :arguments[0].getClass()),
  637.                         (cArguments!=null ? cArguments[1] :arguments[1].getClass()),
  638.                         (cArguments!=null ? cArguments[2] :arguments[2].getClass()),
  639.                         (cArguments!=null ? cArguments[3] :arguments[3].getClass()),
  640.                         (cArguments!=null ? cArguments[4] :arguments[4].getClass()),
  641.                         (cArguments!=null ? cArguments[5] :arguments[5].getClass()),
  642.                         (cArguments!=null ? cArguments[6] :arguments[6].getClass()),
  643.                         (cArguments!=null ? cArguments[7] :arguments[7].getClass()));
  644.                 obj = method.invoke(driver,
  645.                         arguments[0],
  646.                         arguments[1],
  647.                         arguments[2],
  648.                         arguments[3],
  649.                         arguments[4],
  650.                         arguments[5],
  651.                         arguments[6],
  652.                         arguments[7]);
  653.             }else if(arguments.length==9){
  654.                 Method method =  driver.getClass().getMethod(methodName,
  655.                         (cArguments!=null ? cArguments[0] :arguments[0].getClass()),
  656.                         (cArguments!=null ? cArguments[1] :arguments[1].getClass()),
  657.                         (cArguments!=null ? cArguments[2] :arguments[2].getClass()),
  658.                         (cArguments!=null ? cArguments[3] :arguments[3].getClass()),
  659.                         (cArguments!=null ? cArguments[4] :arguments[4].getClass()),
  660.                         (cArguments!=null ? cArguments[5] :arguments[5].getClass()),
  661.                         (cArguments!=null ? cArguments[6] :arguments[6].getClass()),
  662.                         (cArguments!=null ? cArguments[7] :arguments[7].getClass()),
  663.                         (cArguments!=null ? cArguments[8] :arguments[8].getClass()));
  664.                 obj = method.invoke(driver,
  665.                         arguments[0],
  666.                         arguments[1],
  667.                         arguments[2],
  668.                         arguments[3],
  669.                         arguments[4],
  670.                         arguments[5],
  671.                         arguments[6],
  672.                         arguments[7],
  673.                         arguments[8]);
  674.             }else if(arguments.length==10){
  675.                 Method method =  driver.getClass().getMethod(methodName,
  676.                         (cArguments!=null ? cArguments[0] :arguments[0].getClass()),
  677.                         (cArguments!=null ? cArguments[1] :arguments[1].getClass()),
  678.                         (cArguments!=null ? cArguments[2] :arguments[2].getClass()),
  679.                         (cArguments!=null ? cArguments[3] :arguments[3].getClass()),
  680.                         (cArguments!=null ? cArguments[4] :arguments[4].getClass()),
  681.                         (cArguments!=null ? cArguments[5] :arguments[5].getClass()),
  682.                         (cArguments!=null ? cArguments[6] :arguments[6].getClass()),
  683.                         (cArguments!=null ? cArguments[7] :arguments[7].getClass()),
  684.                         (cArguments!=null ? cArguments[8] :arguments[8].getClass()),
  685.                         (cArguments!=null ? cArguments[9] :arguments[9].getClass()));
  686.                 obj = method.invoke(driver,
  687.                         arguments[0],
  688.                         arguments[1],
  689.                         arguments[2],
  690.                         arguments[3],
  691.                         arguments[4],
  692.                         arguments[5],
  693.                         arguments[6],
  694.                         arguments[7],
  695.                         arguments[8],
  696.                         arguments[9]);
  697.             }else if(arguments.length==11){
  698.                 Method method =  driver.getClass().getMethod(methodName,
  699.                         (cArguments!=null ? cArguments[0] :arguments[0].getClass()),
  700.                         (cArguments!=null ? cArguments[1] :arguments[1].getClass()),
  701.                         (cArguments!=null ? cArguments[2] :arguments[2].getClass()),
  702.                         (cArguments!=null ? cArguments[3] :arguments[3].getClass()),
  703.                         (cArguments!=null ? cArguments[4] :arguments[4].getClass()),
  704.                         (cArguments!=null ? cArguments[5] :arguments[5].getClass()),
  705.                         (cArguments!=null ? cArguments[6] :arguments[6].getClass()),
  706.                         (cArguments!=null ? cArguments[7] :arguments[7].getClass()),
  707.                         (cArguments!=null ? cArguments[8] :arguments[8].getClass()),
  708.                         (cArguments!=null ? cArguments[9] :arguments[9].getClass()),
  709.                         (cArguments!=null ? cArguments[10] :arguments[10].getClass()));
  710.                 obj = method.invoke(driver,
  711.                         arguments[0],
  712.                         arguments[1],
  713.                         arguments[2],
  714.                         arguments[3],
  715.                         arguments[4],
  716.                         arguments[5],
  717.                         arguments[6],
  718.                         arguments[7],
  719.                         arguments[8],
  720.                         arguments[9],
  721.                         arguments[10]);
  722.             }else if(arguments.length==12){
  723.                 Method method =  driver.getClass().getMethod(methodName,
  724.                         (cArguments!=null ? cArguments[0] :arguments[0].getClass()),
  725.                         (cArguments!=null ? cArguments[1] :arguments[1].getClass()),
  726.                         (cArguments!=null ? cArguments[2] :arguments[2].getClass()),
  727.                         (cArguments!=null ? cArguments[3] :arguments[3].getClass()),
  728.                         (cArguments!=null ? cArguments[4] :arguments[4].getClass()),
  729.                         (cArguments!=null ? cArguments[5] :arguments[5].getClass()),
  730.                         (cArguments!=null ? cArguments[6] :arguments[6].getClass()),
  731.                         (cArguments!=null ? cArguments[7] :arguments[7].getClass()),
  732.                         (cArguments!=null ? cArguments[8] :arguments[8].getClass()),
  733.                         (cArguments!=null ? cArguments[9] :arguments[9].getClass()),
  734.                         (cArguments!=null ? cArguments[10] :arguments[10].getClass()),
  735.                         (cArguments!=null ? cArguments[11] :arguments[11].getClass()));
  736.                 obj = method.invoke(driver,
  737.                         arguments[0],
  738.                         arguments[1],
  739.                         arguments[2],
  740.                         arguments[3],
  741.                         arguments[4],
  742.                         arguments[5],
  743.                         arguments[6],
  744.                         arguments[7],
  745.                         arguments[8],
  746.                         arguments[9],
  747.                         arguments[10],
  748.                         arguments[11]);
  749.             }else if(arguments.length==13){
  750.                 Method method =  driver.getClass().getMethod(methodName,
  751.                         (cArguments!=null ? cArguments[0] :arguments[0].getClass()),
  752.                         (cArguments!=null ? cArguments[1] :arguments[1].getClass()),
  753.                         (cArguments!=null ? cArguments[2] :arguments[2].getClass()),
  754.                         (cArguments!=null ? cArguments[3] :arguments[3].getClass()),
  755.                         (cArguments!=null ? cArguments[4] :arguments[4].getClass()),
  756.                         (cArguments!=null ? cArguments[5] :arguments[5].getClass()),
  757.                         (cArguments!=null ? cArguments[6] :arguments[6].getClass()),
  758.                         (cArguments!=null ? cArguments[7] :arguments[7].getClass()),
  759.                         (cArguments!=null ? cArguments[8] :arguments[8].getClass()),
  760.                         (cArguments!=null ? cArguments[9] :arguments[9].getClass()),
  761.                         (cArguments!=null ? cArguments[10] :arguments[10].getClass()),
  762.                         (cArguments!=null ? cArguments[11] :arguments[11].getClass()),
  763.                         (cArguments!=null ? cArguments[12] :arguments[12].getClass()));
  764.                 obj = method.invoke(driver,
  765.                         arguments[0],
  766.                         arguments[1],
  767.                         arguments[2],
  768.                         arguments[3],
  769.                         arguments[4],
  770.                         arguments[5],
  771.                         arguments[6],
  772.                         arguments[7],
  773.                         arguments[8],
  774.                         arguments[9],
  775.                         arguments[10],
  776.                         arguments[11],
  777.                         arguments[12]);
  778.             }else if(arguments.length==14){
  779.                 Method method =  driver.getClass().getMethod(methodName,
  780.                         (cArguments!=null ? cArguments[0] :arguments[0].getClass()),
  781.                         (cArguments!=null ? cArguments[1] :arguments[1].getClass()),
  782.                         (cArguments!=null ? cArguments[2] :arguments[2].getClass()),
  783.                         (cArguments!=null ? cArguments[3] :arguments[3].getClass()),
  784.                         (cArguments!=null ? cArguments[4] :arguments[4].getClass()),
  785.                         (cArguments!=null ? cArguments[5] :arguments[5].getClass()),
  786.                         (cArguments!=null ? cArguments[6] :arguments[6].getClass()),
  787.                         (cArguments!=null ? cArguments[7] :arguments[7].getClass()),
  788.                         (cArguments!=null ? cArguments[8] :arguments[8].getClass()),
  789.                         (cArguments!=null ? cArguments[9] :arguments[9].getClass()),
  790.                         (cArguments!=null ? cArguments[10] :arguments[10].getClass()),
  791.                         (cArguments!=null ? cArguments[11] :arguments[11].getClass()),
  792.                         (cArguments!=null ? cArguments[12] :arguments[12].getClass()),
  793.                         (cArguments!=null ? cArguments[13] :arguments[13].getClass()));
  794.                 obj = method.invoke(driver,
  795.                         arguments[0],
  796.                         arguments[1],
  797.                         arguments[2],
  798.                         arguments[3],
  799.                         arguments[4],
  800.                         arguments[5],
  801.                         arguments[6],
  802.                         arguments[7],
  803.                         arguments[8],
  804.                         arguments[9],
  805.                         arguments[10],
  806.                         arguments[11],
  807.                         arguments[12],
  808.                         arguments[13]);
  809.             }else if(arguments.length==15){
  810.                 Method method =  driver.getClass().getMethod(methodName,
  811.                         (cArguments!=null ? cArguments[0] :arguments[0].getClass()),
  812.                         (cArguments!=null ? cArguments[1] :arguments[1].getClass()),
  813.                         (cArguments!=null ? cArguments[2] :arguments[2].getClass()),
  814.                         (cArguments!=null ? cArguments[3] :arguments[3].getClass()),
  815.                         (cArguments!=null ? cArguments[4] :arguments[4].getClass()),
  816.                         (cArguments!=null ? cArguments[5] :arguments[5].getClass()),
  817.                         (cArguments!=null ? cArguments[6] :arguments[6].getClass()),
  818.                         (cArguments!=null ? cArguments[7] :arguments[7].getClass()),
  819.                         (cArguments!=null ? cArguments[8] :arguments[8].getClass()),
  820.                         (cArguments!=null ? cArguments[9] :arguments[9].getClass()),
  821.                         (cArguments!=null ? cArguments[10] :arguments[10].getClass()),
  822.                         (cArguments!=null ? cArguments[11] :arguments[11].getClass()),
  823.                         (cArguments!=null ? cArguments[12] :arguments[12].getClass()),
  824.                         (cArguments!=null ? cArguments[13] :arguments[13].getClass()),
  825.                         (cArguments!=null ? cArguments[14] :arguments[14].getClass()));
  826.                 obj = method.invoke(driver,
  827.                         arguments[0],
  828.                         arguments[1],
  829.                         arguments[2],
  830.                         arguments[3],
  831.                         arguments[4],
  832.                         arguments[5],
  833.                         arguments[6],
  834.                         arguments[7],
  835.                         arguments[8],
  836.                         arguments[9],
  837.                         arguments[10],
  838.                         arguments[11],
  839.                         arguments[12],
  840.                         arguments[13],
  841.                         arguments[14]);
  842.             }else
  843.                 throw new Exception("More than 15 arguments unsupported");
  844.         }catch(java.lang.reflect.InvocationTargetException e){
  845.             if(e.getTargetException()!=null){
  846.                 throw e.getTargetException();
  847.             }else{
  848.                 throw e;
  849.             }
  850.         }
  851.         catch(Exception e){
  852.             throw e;
  853.         }
  854.        
  855.         return obj;
  856.        
  857.     }
  858. }