Coverage Report - org.openpermis.editor.policy.adapter.BasicAdapterTrader
 
Classes in this File Line Coverage Branch Coverage Complexity
BasicAdapterTrader
0%
0/26
0%
0/6
3
 
 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.Role;
 18  
 import org.openpermis.policy.bean.ActionBean;
 19  
 import org.openpermis.policy.bean.AuthorityBean;
 20  
 import org.openpermis.policy.bean.DomainBean;
 21  
 import org.openpermis.policy.bean.ObligationBean;
 22  
 import org.openpermis.policy.bean.PartBean;
 23  
 import org.openpermis.policy.bean.RoleAssignmentRuleBean;
 24  
 import org.openpermis.policy.bean.RoleHierarchyBean;
 25  
 import org.openpermis.policy.bean.TargetAccessRuleBean;
 26  
 import org.openpermis.policy.bean.TargetBean;
 27  
 
 28  
 
 29  
 /**
 30  
  * A basic implementation of an {@link AdapterTrader}.
 31  
  * @since 0.3.0
 32  
  */
 33  
 public class BasicAdapterTrader implements AdapterTrader {
 34  
 
 35  
         //---- Static
 36  
         
 37  
         //---- State
 38  
         
 39  
         private final MutablePicoContainer pico;
 40  
         
 41  
         //---- Constructors
 42  
         
 43  
         /**
 44  
          * Creates a basic adapter trader.
 45  
          * @since 0.3.0
 46  
          */
 47  0
         public BasicAdapterTrader () {
 48  0
                 this.pico = createContainer(new PicoBuilder().build());
 49  0
         }
 50  
         
 51  
         //---- Methods
 52  
         
 53  
         /**
 54  
          * Creates a pico container and fill it with all combinations of objects and adaptee types.
 55  
          * @since 0.3.0
 56  
          */
 57  
         private static final MutablePicoContainer createContainer (PicoContainer parent) {
 58  0
                 final MutablePicoContainer container = new PicoBuilder(parent).withCaching().build();
 59  0
                 container.change(NO_CACHE);
 60  
                 
 61  0
                 final Class<?>[] objectTypes = {
 62  
                         ActionBean.class, 
 63  
                         DomainBean.class, 
 64  
                         TargetBean.class, 
 65  
                         TargetAccessRuleBean.class,
 66  
                         ObligationBean.class, 
 67  
                         AuthorityBean.class, 
 68  
                         Role.class,
 69  
                         RoleHierarchyBean.class,
 70  
                         RoleAssignmentRuleBean.class
 71  
                 };
 72  0
                 final Class<?>[] adapteeTypes = {Overview.class};
 73  
                 
 74  0
                 for (Class<?> adapteeType : adapteeTypes) {
 75  0
                         for (Class<?> objectType : objectTypes) {
 76  
                                 
 77  
                                 Class<?> adaptee;
 78  
                                 try {
 79  0
                                         adaptee = Class.forName(
 80  
                                                         adapteeType.getPackage().getName() + 
 81  
                                                         "." + 
 82  
                                                         objectType.getSimpleName() + 
 83  
                                                         adapteeType.getSimpleName()
 84  
                                                 );
 85  0
                                 } catch (ClassNotFoundException e) {
 86  0
                                         throw new IllegalStateException(
 87  
                                                 "No adaptee class found for object class <" + objectType.getSimpleName() + 
 88  
                                                 "> and adaptee type <" + adapteeType.getSimpleName() + ">."
 89  
                                         );
 90  0
                                 }
 91  0
                                 final Object key = createKey(adapteeType, objectType);
 92  0
                                 container.addComponent(key, adaptee);
 93  
                         }
 94  
                 }
 95  0
                 return container;
 96  
         }
 97  
         
 98  
         /**
 99  
          * Creates a unique key that identifies an objecgt/adapter type pair.
 100  
          * @param adapteeType the type of adapter for which to retrieve the key.
 101  
          * @param objectType the object type, may be <code>null</code> for default type.
 102  
          * @return the key requested, never {@code null}.
 103  
          * @since 0.3.0
 104  
          */
 105  
         private static String createKey (Class<?> adapteeType, Class<?> objectType) {
 106  0
                 return adapteeType.getCanonicalName() + ":" + objectType.getCanonicalName();
 107  
         }
 108  
         
 109  
         //---- AdapterTrader
 110  
         
 111  
         /**
 112  
          * @since 0.3.0
 113  
          */
 114  
         @SuppressWarnings("unchecked")
 115  
         public <T extends Adaptee<?>> T adaptTo (Object object, Class<T> adapteeType) {
 116  0
                 final Class<?> exactObjectClass = object.getClass();
 117  0
                 this.pico.addComponent(exactObjectClass, object);
 118  
                 
 119  
                 final Class<?> objectType;
 120  0
                 if (object instanceof PartBean) {
 121  0
                         objectType = ((PartBean) object).getPartBeanType();
 122  
                 } else {
 123  0
                         objectType = object.getClass();
 124  
                 }
 125  
                 
 126  
                 try {
 127  0
                         return (T) this.pico.getComponent(createKey(adapteeType, objectType));
 128  0
                 } catch (Exception e) {
 129  0
                         throw new IllegalStateException(
 130  
                                 "Adapter trader doesn't provide an adapter for type <" + 
 131  
                                 objectType.getSimpleName() + 
 132  
                                 "> and adaptee type <" + adapteeType.getSimpleName() + ">."
 133  
                         );
 134  
                 } finally {
 135  0
                         this.pico.removeComponent(exactObjectClass); 
 136  
                 }
 137  
         }
 138  
 
 139  
 }