1 17 18 package org.apache.avalon.fortress.impl.lookup; 19 20 import org.apache.avalon.fortress.Container; 21 import org.apache.avalon.fortress.impl.AbstractContainer; 22 import org.apache.avalon.fortress.impl.handler.ComponentHandler; 23 import org.apache.avalon.framework.component.ComponentSelector; 24 import org.apache.avalon.framework.service.ServiceException; 25 import org.apache.avalon.framework.service.ServiceManager; 26 import org.apache.avalon.framework.service.ServiceSelector; 27 import org.apache.avalon.framework.service.WrapperServiceSelector; 28 29 import java.util.Collections ; 30 import java.util.HashMap ; 31 import java.util.Map ; 32 33 41 public class FortressServiceManager 42 implements ServiceManager 43 { 44 private final Container m_container; 45 private final Map m_used; 46 private final ServiceManager m_parent; 47 48 55 public FortressServiceManager( final Container container, 56 final ServiceManager parent ) throws NullPointerException 57 { 58 if ( null == container ) 59 { 60 throw new NullPointerException ( "impl" ); 61 } 62 63 m_parent = parent; 64 m_container = container; 65 m_used = Collections.synchronizedMap(new HashMap ()); 66 } 67 68 public Object lookup( final String role ) 69 throws ServiceException 70 { 71 final Lookup lookup = parseRole( role ); 72 73 if ( !m_container.has( lookup.m_role, lookup.m_hint ) ) 74 { 75 return m_parent.lookup( role ); 76 } 77 78 final Object result = m_container.get( lookup.m_role, lookup.m_hint ); 79 if ( result instanceof ServiceSelector ) 80 { 81 return result; 82 } 83 84 if ( result instanceof ComponentSelector ) 85 { 86 return new WrapperServiceSelector( lookup.m_role, (ComponentSelector) result ); 87 } 88 89 if ( !( result instanceof ComponentHandler ) ) 90 { 91 final String message = "Invalid entry in component manager"; 92 throw new ServiceException( role, message ); 93 } 94 95 try 96 { 97 final ComponentHandler handler = (ComponentHandler) result; 98 final Object component = handler.get(); 99 100 m_used.put( new ComponentKey( component ), handler ); 101 return component; 102 } 103 catch ( final ServiceException ce ) 104 { 105 throw ce; } 107 catch ( final Exception e ) 108 { 109 final String message = 110 "Could not return a reference to the Component"; 111 throw new ServiceException( role, message, e ); 112 } 113 } 114 115 public boolean hasService( final String role ) 116 { 117 final Lookup lookup = parseRole( role ); 118 119 if ( m_container.has( lookup.m_role, lookup.m_hint ) ) 120 { 121 return true; 122 } 123 else 124 { 125 return null != m_parent ? m_parent.hasService( role ) : false; 126 } 127 } 128 129 public void release( final Object component ) 130 { 131 final ComponentHandler handler = (ComponentHandler) m_used.remove( new ComponentKey( component ) ); 132 if ( null == handler ) 133 { 134 if ( null == m_parent ) 135 { 136 142 } 146 else 147 { 148 m_parent.release( component ); 149 } 150 } 151 else 152 { 153 handler.put( component ); 154 } 155 } 156 157 private Lookup parseRole( final String role ) 158 { 159 final Lookup lookup = new Lookup(); 160 lookup.m_role = role; 161 lookup.m_hint = AbstractContainer.DEFAULT_ENTRY; 162 163 if ( role.endsWith( "Selector" ) ) 164 { 165 lookup.m_role = role.substring( 0, role.length() - "Selector".length() ); 166 lookup.m_hint = AbstractContainer.SELECTOR_ENTRY; 167 } 168 169 final int index = role.lastIndexOf( "/" ); 170 171 if ( index > 0 ) 173 { 174 lookup.m_role = role.substring( 0, index ); 175 lookup.m_hint = role.substring( index + 1 ); 176 } 177 178 return lookup; 179 } 180 181 private final static class Lookup 182 { 183 String m_role; 184 String m_hint; 185 } 186 } 187 | Popular Tags |