Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register
Show
Ignore:
Timestamp:
08/18/08 18:57:52 (5 months ago)
Author:
TorLye
Message:

gmapsimageviewer: Some cleaning up and removal of commented out code.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openmrs-modules/gmapsimageviewer/src/org/openmrs/module/gmapsimageviewer/ImageBuffer.java

    r5277 r5311  
    88import org.openmrs.api.context.Context; 
    99 
     10/** 
     11 * This class represents a buffer for Obs objects, and provides methods to 
     12 * add/load Obs objects. Usually only one instance of this methods should be 
     13 * used, the getInstance() method returns the active instance. New 
     14 * instances can be created if desired by invoking the constructor. 
     15 */ 
    1016public class ImageBuffer { 
    1117 
    12     private static ImageBuffer instance; 
     18       private static ImageBuffer instance; 
    1319 
    14     private CircularBuffer<Obs> buffer; 
     20       private CircularBuffer<Obs> buffer; 
    1521 
    16     private static Log log = LogFactory.getLog(ImageBuffer.class); 
     22       private static Log log = LogFactory.getLog(ImageBuffer.class); 
    1723 
    18     public ImageBuffer() { 
    19         int bufSize; 
     24       public ImageBuffer() { 
     25               int bufSize; 
    2026 
    21         try { 
    22             bufSize = Integer.parseInt(Context.getAdministrationService() 
    23                     .getGlobalProperty("gmapsimageviewer.bufferSize")); 
    24         } catch (NumberFormatException e) { 
    25             log 
    26                     .error("Could not load global property gmapsimageviewer.bufferSize"); 
    27             bufSize = 1; 
    28        
    29         buffer = new CircularBuffer<Obs>(bufSize); 
    30    
     27               try { 
     28                       bufSize = Integer.parseInt(Context.getAdministrationService() 
     29                                       .getGlobalProperty("gmapsimageviewer.bufferSize")); 
     30               } catch (NumberFormatException e) { 
     31                       log 
     32                                       .error("Could not load global property gmapsimageviewer.bufferSize"); 
     33                       bufSize = 1; 
     34               
     35               buffer = new CircularBuffer<Obs>(bufSize); 
     36       
    3137 
    32     public static synchronized ImageBuffer getInstance() { 
    33         if (instance == null) { 
    34             instance = new ImageBuffer(); 
    35        
    36         return instance; 
    37    
     38       public static synchronized ImageBuffer getInstance() { 
     39               if (instance == null) { 
     40                       instance = new ImageBuffer(); 
     41               
     42               return instance; 
     43       
    3844 
    39     /** 
    40     * Retrieve an obs from the static buffer. If the obs is not in the buffer, 
    41     * it will be retrieved and stored in the buffer. 
    42     *  
    43     * @param obsId 
    44     * @return an obs with its complex data loaded, or null if the obs failed to 
    45      *         load. 
    46     */ 
    47     public synchronized Obs getObs(Integer obsId) { 
    48         for (Iterator<Obs> iterator = buffer.iterator(); iterator.hasNext();) { 
    49             Obs obs = iterator.next(); 
    50             if (obs.getObsId().equals(obsId)) { 
    51                 log.info("gmapsimageviewer: Loading obs " + obsId 
    52                         + " from buffer"); 
    53                 return obs; 
    54            
    55        
     45       /** 
     46        * Retrieve an obs from the static buffer. If the obs is not in the buffer, 
     47        * it will be retrieved and stored in the buffer. 
     48        *  
     49        * @param obsId 
     50        * @return an obs with its complex data loaded, or null if the obs failed to 
     51        *      load. 
     52        */ 
     53       public synchronized Obs getObs(Integer obsId) { 
     54               for (Iterator<Obs> iterator = buffer.iterator(); iterator.hasNext();) { 
     55                       Obs obs = iterator.next(); 
     56                       if (obs.getObsId().equals(obsId)) { 
     57                               log.info("gmapsimageviewer: Loading obs " + obsId 
     58                                               + " from buffer"); 
     59                               return obs; 
     60                       
     61               
    5662 
    57         log.info("gmapsimageviewer: Obs " + obsId 
    58                 + " not found in buffer, loading from ObsService"); 
    59         Obs obs = null; 
     63               log.info("gmapsimageviewer: Obs " + obsId 
     64                               + " not found in buffer, loading from ObsService"); 
     65               Obs obs = null; 
    6066 
    61         try { 
    62             obs = Context.getObsService().getComplexObs(obsId, null); 
    63         } catch (OutOfMemoryError e) { 
    64             log.info("gmapsimageviewer: Out of memory. " 
    65                     + "Clearing buffer and retrying"); 
    66             log.info("Free memory before clearing buffer: " 
    67                     + Runtime.getRuntime().freeMemory()); 
    68             buffer.clear(); 
    69             System.gc(); 
    70             log.info("Free memory after clearing buffer: " 
    71                     + Runtime.getRuntime().freeMemory()); 
     67               try { 
     68                       obs = Context.getObsService().getComplexObs(obsId, null); 
     69               } catch (OutOfMemoryError e) { 
     70                       log.info("gmapsimageviewer: Out of memory. " 
     71                                       + "Clearing buffer and retrying"); 
     72                       log.info("Free memory before clearing buffer: " 
     73                                       + Runtime.getRuntime().freeMemory()); 
     74                       buffer.clear(); 
     75                       System.gc(); 
     76                       log.info("Free memory after clearing buffer: " 
     77                                       + Runtime.getRuntime().freeMemory()); 
    7278 
    73             try { 
    74                 obs = Context.getObsService().getComplexObs(obsId, null); 
    75             } catch (Exception e2) { 
    76                 log.error("Failed to load obs even after clearing buffer. " 
    77                         + "Complex obs may be too large for the VM heap space", 
    78                         e2); 
    79            
    80        
    81         if (buffer.add(obs)) 
    82             log.info("gmapsimageviewer: Obs " + obsId + " added to buffer"); 
    83         else 
    84             log.info("gmapsimageviewer: Failed to add obs" + obsId 
    85                     + " to the buffer"); 
    86         return obs; 
    87    
     79                       try { 
     80                               obs = Context.getObsService().getComplexObs(obsId, null); 
     81                       } catch (Exception e2) { 
     82                               log.error("Failed to load obs even after clearing buffer. " 
     83                                               + "Complex obs may be too large for the VM heap space", 
     84                                               e2); 
     85                       
     86               
     87               if (buffer.add(obs)) 
     88                       log.info("gmapsimageviewer: Obs " + obsId + " added to buffer"); 
     89               else 
     90                       log.info("gmapsimageviewer: Failed to add obs" + obsId 
     91                                       + " to the buffer"); 
     92               return obs; 
     93       
    8894}