Clover coverage report - Code Coverage for hivemind release 1.0
Coverage timestamp: Wed Sep 22 2004 08:05:25 EDT
file stats: LOC: 237   Methods: 12
NCLOC: 136   Classes: 1
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
ConfigurationPointImpl.java 94.4% 94.1% 83.3% 92.6%
coverage coverage
 1   
 //  Copyright 2004 The Apache Software Foundation
 2   
 //
 3   
 // Licensed under the Apache License, Version 2.0 (the "License");
 4   
 // you may not use this file except in compliance with the License.
 5   
 // You may obtain a copy of the License at
 6   
 //
 7   
 //     http://www.apache.org/licenses/LICENSE-2.0
 8   
 //
 9   
 // Unless required by applicable law or agreed to in writing, software
 10   
 // distributed under the License is distributed on an "AS IS" BASIS,
 11   
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12   
 // See the License for the specific language governing permissions and
 13   
 // limitations under the License.
 14   
 
 15   
 package org.apache.hivemind.impl;
 16   
 
 17   
 import java.util.ArrayList;
 18   
 import java.util.Collections;
 19   
 import java.util.List;
 20   
 
 21   
 import org.apache.commons.logging.Log;
 22   
 import org.apache.commons.logging.LogFactory;
 23   
 import org.apache.hivemind.*;
 24   
 import org.apache.hivemind.ApplicationRuntimeException;
 25   
 import org.apache.hivemind.Occurances;
 26   
 import org.apache.hivemind.internal.ConfigurationPoint;
 27   
 import org.apache.hivemind.internal.Contribution;
 28   
 import org.apache.hivemind.schema.Schema;
 29   
 import org.apache.hivemind.util.ToStringBuilder;
 30   
 
 31   
 /**
 32   
  * Implementation of the {@link org.apache.hivemind.internal.ConfigurationPoint} interface; a container
 33   
  * for {@link org.apache.hivemind.internal.Contribution}s.
 34   
  *
 35   
  * @author Howard Lewis Ship
 36   
  */
 37   
 public final class ConfigurationPointImpl
 38   
     extends AbstractExtensionPoint
 39   
     implements ConfigurationPoint
 40   
 {
 41   
     private static final Log LOG = LogFactory.getLog(ConfigurationPointImpl.class);
 42   
 
 43   
     /**
 44   
      * The cached elements for the extension point (if caching is enabled).
 45   
      */
 46   
     private List _elements;
 47   
     private List _elementsProxy;
 48   
     private Occurances _expectedCount;
 49   
     private List _contributions;
 50   
     private boolean _building;
 51   
     private Schema _contributionsSchema;
 52   
     private ShutdownCoordinator _shutdownCoordinator;
 53   
 
 54  1
     protected void extendDescription(ToStringBuilder builder)
 55   
     {
 56  1
         builder.append("expectedCount", _expectedCount);
 57  1
         builder.append("contributions", _contributions);
 58  1
         builder.append("schema", _contributionsSchema);
 59   
     }
 60   
 
 61   
     /**
 62   
      * Returns the number of contributions; it is expected
 63   
      * that each top-level {@link org.apache.hivemind.Element}
 64   
      * in each {@link Contribution} will convert to one element instance;
 65   
      * the value returned is the total number of top-level elements
 66   
      * in all contributed Extensions. 
 67   
      */
 68  819
     public int getContributionCount()
 69   
     {
 70  819
         if (_contributions == null)
 71  286
             return 0;
 72   
 
 73  533
         int total = 0;
 74   
 
 75  533
         int count = _contributions.size();
 76  533
         for (int i = 0; i < count; i++)
 77   
         {
 78  545
             Contribution c = (Contribution) _contributions.get(i);
 79  545
             total += c.getElements().size();
 80   
         }
 81   
 
 82  533
         return total;
 83   
     }
 84   
 
 85  545
     public void addContribution(Contribution c)
 86   
     {
 87  545
         if (_contributions == null)
 88  533
             _contributions = new ArrayList();
 89   
 
 90  545
         _contributions.add(c);
 91   
     }
 92   
 
 93  819
     public Occurances getExpectedCount()
 94   
     {
 95  819
         return _expectedCount;
 96   
     }
 97   
 
 98  819
     public void setExpectedCount(Occurances occurances)
 99   
     {
 100  819
         _expectedCount = occurances;
 101   
     }
 102   
 
 103   
     /**
 104   
      * Returns the contributed elements as an unmodifiable {@link List}.
 105   
      * Internally, a proxy to the real list is returned, such that the
 106   
      * real list may not be constructed until actually needed.
 107   
      */
 108  525
     public synchronized List getElements()
 109   
     {
 110  525
         if (_elements != null)
 111  1
             return _elements;
 112   
 
 113  524
         if (_elementsProxy == null)
 114   
         {
 115  523
             ElementsProxyList outerProxy = new ElementsProxyList();
 116   
 
 117  523
             new ElementsInnerProxyList(this, outerProxy);
 118   
 
 119  523
             _shutdownCoordinator.addRegistryShutdownListener(outerProxy);
 120   
 
 121  523
             _elementsProxy = outerProxy;
 122   
         }
 123   
 
 124  524
         return _elementsProxy;
 125   
     }
 126   
 
 127   
     /**
 128   
      * Invoked by {@link ElementsInnerProxyList} when the actual list
 129   
      * is needed.  Returns the List (which is modifiable, but
 130   
      * that's OK because ElementsInnerProxyList is unmodifiable) and,
 131   
      * as a side effect, keeps a reference to an unmodifiable
 132   
      * version of the result for future invocations
 133   
      * of {@link #getElements()}.
 134   
      */
 135   
 
 136  519
     synchronized List constructElements()
 137   
     {
 138   
         // It's nice to have this protection, but (unlike services), you
 139   
         // would really have to go out of your way to provoke
 140   
         // a recursive configuration.
 141   
 
 142  519
         if (_building)
 143  0
             throw new ApplicationRuntimeException(
 144   
                 ImplMessages.recursiveConfiguration(getExtensionPointId()));
 145   
 
 146  519
         try
 147   
         {
 148  519
             _building = true;
 149   
 
 150  519
             List result = constructElementsList();
 151   
 
 152   
             // After constructing the result, if the result
 153   
             // will be cached, then there's no need to keep
 154   
             // the schema and extensions (used to build the
 155   
             // result); it can all be released to the GC.
 156   
 
 157  516
             _elements = Collections.unmodifiableList(result);
 158   
 
 159  516
             _contributionsSchema = null;
 160  516
             _contributions = null;
 161   
 
 162   
             // Now that we have the real list, we don't need the proxy
 163   
             // anymore, either.
 164   
 
 165  516
             _elementsProxy = null;
 166   
 
 167  516
             return result;
 168   
         }
 169   
         finally
 170   
         {
 171  519
             _building = false;
 172   
         }
 173   
     }
 174   
 
 175   
     /**
 176   
      * Returns the list of elements assembled from all contributions.
 177   
      * The list is modifiable, and should be wrapped into an unmodifiable
 178   
      * list.  May return the empty list, but won't return null.
 179   
      */
 180  519
     private List constructElementsList()
 181   
     {
 182  519
         if (LOG.isDebugEnabled())
 183  51
             LOG.debug("Constructing extension point " + getExtensionPointId());
 184   
 
 185  519
         if (_contributions == null)
 186  94
             return Collections.EMPTY_LIST;
 187   
 
 188  425
         SchemaProcessorImpl processor =
 189   
             new SchemaProcessorImpl(
 190   
                 getModule().getErrorHandler(),
 191   
                 LogFactory.getLog(getExtensionPointId()),
 192   
                 _contributionsSchema);
 193   
 
 194  425
         int count = _contributions.size();
 195   
 
 196  425
         try
 197   
         {
 198  425
             for (int i = 0; i < count; i++)
 199   
             {
 200  431
                 Contribution extension = (Contribution) _contributions.get(i);
 201   
 
 202  431
                 processor.process(extension.getElements(), extension.getContributingModule());
 203   
             }
 204   
 
 205  422
             return processor.getElements();
 206   
         }
 207   
         catch (Exception ex)
 208   
         {
 209  3
             throw new ApplicationRuntimeException(
 210   
                 ImplMessages.unableToConstructConfiguration(getExtensionPointId(), ex),
 211   
                 ex);
 212   
         }
 213   
 
 214   
     }
 215   
 
 216  0
     public Schema getSchema()
 217   
     {
 218  0
         return _contributionsSchema;
 219   
     }
 220   
 
 221  819
     public void setContributionsSchema(Schema schema)
 222   
     {
 223  819
         _contributionsSchema = schema;
 224   
     }
 225   
 
 226  0
     public Schema getContributionsSchema()
 227   
     {
 228  0
         return _contributionsSchema;
 229   
     }
 230   
 
 231  819
     public void setShutdownCoordinator(ShutdownCoordinator coordinator)
 232   
     {
 233  819
         _shutdownCoordinator = coordinator;
 234   
     }
 235   
 
 236   
 }
 237