1 55 package org.apache.avalon.framework.service; 56 57 import java.util.HashMap ; 58 import java.util.Map ; 59 60 66 public class DefaultServiceSelector 67 implements ServiceSelector 68 { 69 private final HashMap m_objects = new HashMap (); 70 private boolean m_readOnly; 71 private final String m_role; 72 73 76 public DefaultServiceSelector() 77 { 78 this(""); 79 } 80 81 88 public DefaultServiceSelector(String role) 89 { 90 if ( null==role ) 91 { 92 throw new NullPointerException (role); 93 } 94 95 m_role = role; 96 } 97 98 105 public Object select( Object hint ) 106 throws ServiceException 107 { 108 final Object object = m_objects.get( hint ); 109 110 if( null != object ) 111 { 112 return object; 113 } 114 else 115 { 116 throw new ServiceException( m_role + "/" + hint.toString(), "Unable to provide implementation" ); 117 } 118 } 119 120 125 public boolean isSelectable( final Object hint ) 126 { 127 boolean objectExists = false; 128 129 try 130 { 131 this.release( this.select( hint ) ); 132 objectExists = true; 133 } 134 catch( Throwable t ) 135 { 136 } 138 139 return objectExists; 140 } 141 142 147 public void release( final Object object ) 148 { 149 } 152 153 158 public void put( final Object hint, final Object object ) 159 { 160 checkWriteable(); 161 m_objects.put( hint, object ); 162 } 163 164 169 protected final Map getObjectMap() 170 { 171 return m_objects; 172 } 173 174 178 public void makeReadOnly() 179 { 180 m_readOnly = true; 181 } 182 183 188 protected final void checkWriteable() 189 throws IllegalStateException 190 { 191 if( m_readOnly ) 192 { 193 throw new IllegalStateException 194 ( "ServiceSelector is read only and can not be modified" ); 195 } 196 } 197 } 198 | Popular Tags |