Coverage Report

##### Coverage Report - org.webmacro.resource.ReloadDelayDecorator

Classes in this File | Line Coverage | Branch Coverage | Complexity
ReloadDelayDecorator |
84% | 16/19
50% | 2/4
| 2.25;2.25
ReloadDelayDecorator$1 |
20% | 1/5
0% | 0/4
| 2.25;2.25

1 |  |
     /*
2 |  |
      * Copyright (C) 1998-2000 Semiotek Inc.  All Rights Reserved.
3 |  |
      *
4 |  |
      * Redistribution and use in source and binary forms, with or without
5 |  |
      * modification, are permitted under the terms of either of the following
6 |  |
      * Open Source licenses:
7 |  |
      *
8 |  |
      * The GNU General Public License, version 2, or any later version, as
9 |  |
      * published by the Free Software Foundation
10 |  |
      * (http://www.fsf.org/copyleft/gpl.html);
11 |  |
      *
12 |  |
      *  or
13 |  |
      *
14 |  |
      * The Semiotek Public License (http://webmacro.org/LICENSE.)
15 |  |
      *
16 |  |
      * This software is provided "as is", with NO WARRANTY, not even the
17 |  |
      * implied warranties of fitness to purpose, or merchantability. You
18 |  |
      * assume all risks and liabilities associated with its use.
19 |  |
      *
20 |  |
      * See www.webmacro.org for more information on the WebMacro project.
21 |  |
      */
22 |  |
     package org.webmacro.resource;
23 |  |
24 |  |
     import org.slf4j.Logger;
25 |  |
     import org.slf4j.LoggerFactory;
26 |  |
     import org.webmacro.Broker;
27 |  |
     import org.webmacro.InitException;
28 |  |
     import org.webmacro.util.Settings;
29 |  |
30 |  |
     import java.util.HashMap;
31 |  |
     import java.util.Map;
32 |  |
33 |  |
     /**
34 |  |
      * Utility class to handle creation of TimedReloadContext.<br>
35 |  |
      * TimedReloadContext objects are used to prevent a cached resource
36 |  |
      * of being checked for modification all the time, but only checks
37 |  |
      * for in given intervals.
38 |  |
      * <br>
39 |  |
      * This class stores a mapping of protocol types to delay values.
40 |  |
      * When a provider requests to decorate a reload context it looks up
41 |  |
      * the delay for this protocol and eventually creates a suitable
42 |  |
      * TimedReloadContext.
43 |  |
      * @author Sebastian Kanthak <sebastian.kanthak@muehlheim.de>
44 |  |
      */
45 | 0 |
     public class ReloadDelayDecorator
46 |  |
     {
47 |  |
48 | 2 |
         static Logger _log =  LoggerFactory.getLogger(ReloadDelayDecorator.class);
49 |  |
50 |  |
              /** maps protocol types to Long objects */
51 |  |
         private Map reloadDelays;
52 |  |
         private long defaultDelay;
53 |  |
54 |  |
         public ReloadDelayDecorator ()
55 | 12 |
         {
56 | 12 |
             reloadDelays = new HashMap(11);
57 | 12 |
         }
58 |  |
59 |  |
         /**
60 |  |
          * Initialize object.
61 |  |
          * Reads config settings from key "CheckForReloadDelay"
62 |  |
          */
63 |  |
         public void init (Broker b, Settings config) throws InitException
64 |  |
         {
65 | 12 |
             defaultDelay = 0; // no delay
66 | 12 |
             synchronized (reloadDelays)
67 |  |
             {
68 | 12 |
                 config.processListSetting("CheckForReloadDelay",
69 |  |
                         new Settings.ListSettingHandler()
70 | 12 |
                         {
71 |  |
                             public void processSetting (String key, String value)
72 |  |
                             {
73 | 0 |
                                 if (key == null || key.length() == 0)
74 |  |
                                 {
75 |  |
                                     // default reloadDelay
76 | 0 |
                                     defaultDelay = Long.parseLong(value);
77 |  |
                                 }
78 |  |
                                 else
79 |  |
                                 {
80 | 0 |
                                     reloadDelays.put(key, Long.valueOf(value));
81 |  |
                                 }
82 | 0 |
                             }
83 |  |
                         });
84 | 12 |
             }
85 | 12 |
         }
86 |  |
87 |  |
         /**
88 |  |
          * Looks up the "check for reload delay" for protocol and creates
89 |  |
          * a suitable TimedReloadContext or passes back the original
90 |  |
          * reload context if delay <= 0
91 |  |
          * @param protocol protocol to look up delay for
92 |  |
          * @param reloadContext reloadContext to decorate
93 |  |
          * @return eventually decorated reload context
94 |  |
          */
95 |  |
         public CacheReloadContext decorate (String protocol,
96 |  |
                                             CacheReloadContext reloadContext)
97 |  |
         {
98 |  |
             long delay;
99 |  |
             Long l;
100 | 224 |
             synchronized (reloadDelays)
101 |  |
             {
102 | 224 |
                 l = (Long) reloadDelays.get(protocol);
103 | 224 |
             }
104 | 224 |
             delay = (l != null) ? l.longValue() : defaultDelay;
105 | 224 |
             if (delay > 0)
106 |  |
             {
107 | 0 |
                 _log.debug("Returning timed reload context with delay " + delay);
108 | 0 |
                 return new TimedReloadContext(reloadContext, delay);
109 |  |
             }
110 |  |
             else
111 |  |
             {
112 | 224 |
                 _log.debug("Returning unmodified reload context");
113 | 224 |
                 return reloadContext;
114 |  |
             }
115 |  |
         }
116 |  |
     }