Coverage Report - org.openpermis.editor.policy.adapter.BasicAdapterTrader
 
Classes in this File Line Coverage Branch Coverage Complexity
BasicAdapterTrader
0%
0/23
0%
0/4
2.75
 
 1  
 /*
 2  
  * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch)
 3  
  * All rights reserved.
 4  
  * 
 5  
  * Licensed under the Open Permis License which accompanies this distribution, 
 6  
  * and is available at http://www.openpermis.org/BSDlicenceKent.txt
 7  
  */
 8  
 package org.openpermis.editor.policy.adapter;
 9  
 
 10  
 import static org.picocontainer.Characteristics.NO_CACHE;
 11  
 
 12  
 import org.picocontainer.MutablePicoContainer;
 13  
 import org.picocontainer.PicoBuilder;
 14  
 import org.picocontainer.PicoContainer;
 15  
 
 16  
 import org.openpermis.editor.policy.adapter.overview.Overview;
 17  
 import org.openpermis.policy.bean.ActionBean;
 18  
 import org.openpermis.policy.bean.DomainBean;
 19  
 import org.openpermis.policy.bean.ObligationBean;
 20  
 import org.openpermis.policy.bean.PartBean;
 21  
 import org.openpermis.policy.bean.TargetAccessRuleBean;
 22  
 import org.openpermis.policy.bean.TargetBean;
 23  
 
 24  
 
 25  
 /**
 26  
  * A basic implementation of an {@link AdapterTrader}.
 27  
  * @since 0.3.0
 28  
  */
 29  
 public class BasicAdapterTrader implements AdapterTrader {
 30  
 
 31  
         //---- Static
 32  
         
 33  
         //---- State
 34  
         
 35  
         private final MutablePicoContainer pico;
 36  
         
 37  
         //---- Constructors
 38  
         
 39  
         /**
 40  
          * Creates a basic adapter trader.
 41  
          * @since 0.3.0
 42  
          */
 43  0
         public BasicAdapterTrader () {
 44  0
                 this.pico = createContainer(new PicoBuilder().build());
 45  0
         }
 46  
         
 47  
         //---- Methods
 48  
         
 49  
         /**
 50  
          * Creates a pico container and fill it with all combinations of part beans and adaptee types.
 51  
          * @since 0.3.0
 52  
          */
 53  
         private static final MutablePicoContainer createContainer (PicoContainer parent) {
 54  0
                 final MutablePicoContainer container = new PicoBuilder(parent).withCaching().build();
 55  0
                 container.change(NO_CACHE);
 56  
                 
 57  0
                 final Class<?>[] partBeanTypes = {
 58  
                         ActionBean.class, DomainBean.class, TargetBean.class, TargetAccessRuleBean.class,
 59  
                         ObligationBean.class
 60  
                 };
 61  0
                 final Class<?>[] adapteeTypes = {Overview.class};
 62  
                 
 63  0
                 for (Class<?> adapteeType : adapteeTypes) {
 64  0
                         for (Class<?> partBeanType : partBeanTypes) {
 65  
                                 
 66  
                                 Class<?> adaptee;
 67  
                                 try {
 68  0
                                         adaptee = Class.forName(
 69  
                                                         adapteeType.getPackage().getName() + 
 70  
                                                         "." + 
 71  
                                                         partBeanType.getSimpleName() + 
 72  
                                                         adapteeType.getSimpleName()
 73  
                                                 );
 74  0
                                 } catch (ClassNotFoundException e) {
 75  0
                                         throw new IllegalStateException(
 76  
                                                 "No adaptee class found for part bean <" + partBeanType.getSimpleName() + 
 77  
                                                 "> and adaptee type <" + adapteeType.getSimpleName() + ">."
 78  
                                         );
 79  0
                                 }
 80  0
                                 final Object key = createKey(adapteeType, partBeanType);
 81  0
                                 container.addComponent(key, adaptee);
 82  
                         }
 83  
                 }
 84  
 
 85  0
                 return container;
 86  
         }
 87  
         
 88  
         /**
 89  
          * Creates a unique key that identifies a bean/adapter type pair.
 90  
          * @param adapteeType the type of adapter for which to retrieve the key.
 91  
          * @param partBeanType the part bean type, corresponds to {@code getPartBeanType()} of 
 92  
          * {@link org.openpermis.policy.bean.PartBean}, may be <code>null</code> for default type.
 93  
          * @return the key requested, never {@code null}.
 94  
          * @since 0.3.0
 95  
          */
 96  
         private static String createKey (Class<?> adapteeType, Class<?> partBeanType) {
 97  0
                 return adapteeType.getCanonicalName() + ":" + partBeanType.getCanonicalName();
 98  
         }
 99  
         
 100  
         //---- AdapterTrader
 101  
         
 102  
         /**
 103  
          * @since 0.3.0
 104  
          */
 105  
         @SuppressWarnings("unchecked")
 106  
         public <T extends Adaptee<?>> T adaptTo (PartBean part, Class<T> adapteeType) {
 107  0
                 final Class<?> partClass = part.getClass();
 108  0
                 this.pico.addComponent(partClass, part);
 109  
                 
 110  
                 try {
 111  0
                         return (T) this.pico.getComponent(createKey(adapteeType, part.getPartBeanType()));
 112  0
                 } catch (Exception e) {
 113  0
                         throw new IllegalStateException(
 114  
                                 "Adapter trader doesn't provide an adapter for part bean type <" + 
 115  
                                 part.getPartBeanType().getSimpleName() + 
 116  
                                 "> and adaptee type <" + adapteeType.getSimpleName() + ">."
 117  
                         );
 118  
                 } finally {
 119  0
                         this.pico.removeComponent(partClass); 
 120  
                 }
 121  
         }
 122  
 
 123  
 }