Coverage Report - com.jcabi.log.DecorsManager
 
Classes in this File Line Coverage Branch Coverage Complexity
DecorsManager
0%
0/40
0%
0/10
4.25
 
 1  
 /**
 2  
  * Copyright (c) 2012-2015, jcabi.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the jcabi.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.jcabi.log;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import java.lang.reflect.Constructor;
 34  
 import java.lang.reflect.InvocationTargetException;
 35  
 import java.util.Formattable;
 36  
 import java.util.concurrent.ConcurrentHashMap;
 37  
 import java.util.concurrent.ConcurrentMap;
 38  
 import lombok.EqualsAndHashCode;
 39  
 import lombok.ToString;
 40  
 
 41  
 /**
 42  
  * Manager of all decors.
 43  
  *
 44  
  * @author Marina Kosenko (marina.kosenko@gmail.com)
 45  
  * @author Yegor Bugayenko (yegor@teamed.io)
 46  
  * @version $Id: f3d5fa0c0bf41e63afd4fb70ccb77c06db741923 $
 47  
  * @since 0.1
 48  
  */
 49  
 @Immutable
 50  0
 @ToString
 51  0
 @EqualsAndHashCode
 52  
 final class DecorsManager {
 53  
 
 54  
     /**
 55  
      * Storage of all found decors.
 56  
      * @checkstyle LineLength (2 lines)
 57  
      */
 58  0
     private static final ConcurrentMap<String, Class<? extends Formattable>> DECORS =
 59  
         new ConcurrentHashMap<String, Class<? extends Formattable>>();
 60  
 
 61  
     static {
 62  0
         DecorsManager.DECORS.put("dom", DomDecor.class);
 63  0
         DecorsManager.DECORS.put("exception", ExceptionDecor.class);
 64  0
         DecorsManager.DECORS.put("list", ListDecor.class);
 65  0
         DecorsManager.DECORS.put("ms", MsDecor.class);
 66  0
         DecorsManager.DECORS.put("nano", NanoDecor.class);
 67  0
         DecorsManager.DECORS.put("object", ObjectDecor.class);
 68  0
         DecorsManager.DECORS.put("size", SizeDecor.class);
 69  0
         DecorsManager.DECORS.put("secret", SecretDecor.class);
 70  0
         DecorsManager.DECORS.put("text", TextDecor.class);
 71  0
         DecorsManager.DECORS.put("type", TypeDecor.class);
 72  0
     }
 73  
 
 74  
     /**
 75  
      * Private ctor.
 76  
      */
 77  0
     private DecorsManager() {
 78  
         // empty
 79  0
     }
 80  
 
 81  
     /**
 82  
      * Get decor by key.
 83  
      * @param key Key for the formatter to be used to fmt the arguments
 84  
      * @param arg The arbument to supply
 85  
      * @return The decor
 86  
      * @throws DecorException If some problem
 87  
      */
 88  
     public static Formattable decor(final String key, final Object arg)
 89  
         throws DecorException {
 90  0
         final Class<? extends Formattable> type = DecorsManager.find(key);
 91  
         final Formattable decor;
 92  
         try {
 93  0
             decor = (Formattable) DecorsManager.ctor(type).newInstance(arg);
 94  0
         } catch (final InstantiationException ex) {
 95  0
             throw new DecorException(
 96  
                 ex,
 97  
                 "Can't instantiate %s(%s)",
 98  
                 type.getName(),
 99  
                 arg.getClass().getName()
 100  
             );
 101  0
         } catch (final IllegalAccessException ex) {
 102  0
             throw new DecorException(
 103  
                 ex,
 104  
                 "Can't access %s(%s)",
 105  
                 type.getName(),
 106  
                 arg.getClass().getName()
 107  
             );
 108  0
         } catch (final InvocationTargetException ex) {
 109  0
             throw new DecorException(
 110  
                 ex,
 111  
                 "Can't invoke %s(%s)",
 112  
                 type.getName(),
 113  
                 arg.getClass().getName()
 114  
             );
 115  0
         }
 116  0
         return decor;
 117  
     }
 118  
 
 119  
     /**
 120  
      * Find decor.
 121  
      * @param key Key for the formatter to be used to fmt the arguments
 122  
      * @return The type of decor found
 123  
      * @throws DecorException If some problem
 124  
      */
 125  
     @SuppressWarnings("unchecked")
 126  
     private static Class<? extends Formattable> find(final String key)
 127  
         throws DecorException {
 128  
         final Class<? extends Formattable> type;
 129  0
         if (DecorsManager.DECORS.containsKey(key)) {
 130  0
             type = DecorsManager.DECORS.get(key);
 131  
         } else {
 132  
             try {
 133  0
                 type = (Class<Formattable>) Class.forName(key);
 134  0
             } catch (final ClassNotFoundException ex) {
 135  0
                 throw new DecorException(
 136  
                     ex,
 137  
                     "Decor '%s' not found and class can't be instantiated",
 138  
                     key
 139  
                 );
 140  0
             }
 141  
         }
 142  0
         return type;
 143  
     }
 144  
 
 145  
     /**
 146  
      * Get ctor of the type.
 147  
      * @param type The type
 148  
      * @return The ctor
 149  
      * @throws DecorException If some problem
 150  
      */
 151  
     private static Constructor<?> ctor(final Class<? extends Formattable> type)
 152  
         throws DecorException {
 153  0
         final Constructor<?>[] ctors = type.getDeclaredConstructors();
 154  0
         if (ctors.length != 1) {
 155  0
             throw new DecorException(
 156  
                 "%s should have just one one-arg ctor, but there are %d",
 157  
                 type.getName(),
 158  
                 ctors.length
 159  
             );
 160  
         }
 161  0
         final Constructor<?> ctor = ctors[0];
 162  0
         if (ctor.getParameterTypes().length != 1) {
 163  0
             throw new DecorException(
 164  
                 "%s public ctor should have just once parameter",
 165  
                 type.getName()
 166  
             );
 167  
         }
 168  0
         return ctor;
 169  
     }
 170  
 
 171  
 }