Clover coverage report - Code Coverage for hivemind release 1.0-beta-1
Coverage timestamp: Sat Jul 3 2004 09:41:37 EDT
file stats: LOC: 159   Methods: 8
NCLOC: 94   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
MessagesImpl.java 83.3% 92.3% 100% 91.5%
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.io.IOException;
 18   
 import java.io.InputStream;
 19   
 import java.net.URL;
 20   
 import java.text.MessageFormat;
 21   
 import java.util.ArrayList;
 22   
 import java.util.List;
 23   
 import java.util.Locale;
 24   
 import java.util.Properties;
 25   
 
 26   
 import org.apache.commons.logging.Log;
 27   
 import org.apache.commons.logging.LogFactory;
 28   
 import org.apache.hivemind.ApplicationRuntimeException;
 29   
 import org.apache.hivemind.HiveMind;
 30   
 import org.apache.hivemind.Messages;
 31   
 import org.apache.hivemind.Resource;
 32   
 import org.apache.hivemind.util.LocalizedNameGenerator;
 33   
 
 34   
 /**
 35   
  * Implementation of {@link org.apache.hivemind.Messages} for
 36   
  * a {@link org.apache.hivemind.internal.Module}.
 37   
  *
 38   
  * @author Howard Lewis Ship
 39   
  */
 40   
 public final class MessagesImpl implements Messages
 41   
 {
 42   
     private static final Log LOG = LogFactory.getLog(MessagesImpl.class);
 43   
 
 44   
     private Properties _properties;
 45   
     private Locale _locale;
 46   
 
 47  12
     public MessagesImpl(Resource moduleLocation, Locale locale)
 48   
     {
 49  12
         _locale = locale;
 50   
 
 51  12
         initialize(moduleLocation);
 52   
     }
 53   
 
 54  12
     private void initialize(Resource moduleLocation)
 55   
     {
 56  12
         if (LOG.isDebugEnabled())
 57  0
             LOG.debug("Reading message properties for module at " + moduleLocation);
 58   
 
 59  12
         String descriptorName = moduleLocation.getName();
 60  12
         int dotx = descriptorName.lastIndexOf('.');
 61  12
         String baseName = descriptorName.substring(0, dotx);
 62   
 
 63  12
         LocalizedNameGenerator g = new LocalizedNameGenerator(baseName, _locale, ".properties");
 64  12
         List urls = new ArrayList();
 65   
 
 66  12
         while (g.more())
 67   
         {
 68  29
             String name = g.next();
 69  29
             Resource l = moduleLocation.getRelativeResource(name);
 70  29
             URL url = l.getResourceURL();
 71   
 
 72  29
             if (url != null)
 73  11
                 urls.add(url);
 74   
         }
 75   
 
 76   
         // Now read an assemble them, least specific to most specific.
 77   
         // More specific keys overwrite less specific keys.
 78   
 
 79  12
         int count = urls.size();
 80   
 
 81  12
         _properties = new Properties();
 82   
 
 83  12
         for (int i = count - 1; i >= 0; i--)
 84   
         {
 85  11
             URL url = (URL) urls.get(i);
 86   
 
 87  11
             if (LOG.isDebugEnabled())
 88  0
                 LOG.debug("Reading message properties from " + url);
 89   
 
 90  11
             try
 91   
             {
 92  11
                 InputStream stream = url.openStream();
 93   
 
 94  11
                 _properties.load(stream);
 95   
 
 96  11
                 stream.close();
 97   
             }
 98   
             catch (IOException ex)
 99   
             {
 100  0
                 throw new ApplicationRuntimeException(ImplMessages.unabelToReadMessages(url), ex);
 101   
             }
 102   
         }
 103   
 
 104   
     }
 105   
 
 106  10
     public String getMessage(String key)
 107   
     {
 108  10
         String result = _properties.getProperty(key);
 109   
 
 110  10
         if (result == null)
 111  2
             result = "[" + key.toUpperCase() + "]";
 112   
 
 113   
         // Reasons not to write the result back in as a new property:
 114   
         // 1) This is a developer error that should be resolved before an app goes into production
 115   
         // 2) Would have to synchronize getMessage() methods
 116   
         // 3) Would screw up #getMessage(String, String)
 117   
 
 118  10
         return result;
 119   
     }
 120   
 
 121  2
     public String getMessage(String key, String defaultValue)
 122   
     {
 123  2
         return _properties.getProperty(key, defaultValue);
 124   
     }
 125   
 
 126  4
     public String format(String key, Object[] args)
 127   
     {
 128  4
         String pattern = getMessage(key);
 129   
 
 130   
         // This ugliness is mandated for JDK 1.3 compatibility, which has a bug 
 131   
         // in MessageFormat ... the
 132   
         // pattern is applied in the constructor, using the system default Locale,
 133   
         // regardless of what locale is later specified!
 134   
         // It appears that the problem does not exist in JDK 1.4.
 135   
 
 136  4
         MessageFormat messageFormat = new MessageFormat("");
 137  4
         messageFormat.setLocale(_locale);
 138  4
         messageFormat.applyPattern(pattern);
 139   
 
 140  4
         return messageFormat.format(args);
 141   
     }
 142   
 
 143  1
     public String format(String key, Object arg0)
 144   
     {
 145  1
         return format(key, new Object[] { arg0 });
 146   
     }
 147   
 
 148  1
     public String format(String key, Object arg0, Object arg1)
 149   
     {
 150  1
         return format(key, new Object[] { arg0, arg1 });
 151   
     }
 152   
 
 153  1
     public String format(String key, Object arg0, Object arg1, Object arg2)
 154   
     {
 155  1
         return format(key, new Object[] { arg0, arg1, arg2 });
 156   
     }
 157   
 
 158   
 }
 159