1 17 18 package org.apache.avalon.fortress.impl.role; 19 20 import org.apache.avalon.fortress.MetaInfoManager; 21 import org.apache.avalon.fortress.RoleManager; 22 import org.apache.avalon.fortress.util.Service; 23 import org.apache.avalon.framework.activity.Initializable; 24 25 import java.io.BufferedReader; 26 import java.io.IOException; 27 import java.io.InputStream; 28 import java.io.InputStreamReader; 29 import java.net.URL; 30 import java.util.*; 31 32 82 public final class ServiceMetaManager extends AbstractMetaInfoManager implements Initializable 83 { 84 87 public ServiceMetaManager() 88 { 89 super( (MetaInfoManager) null ); 90 } 91 92 97 public ServiceMetaManager( final RoleManager parent ) 98 { 99 super( parent ); 100 } 101 102 107 public ServiceMetaManager( final MetaInfoManager parent ) 108 { 109 super( parent ); 110 } 111 112 119 public ServiceMetaManager( final MetaInfoManager parent, final ClassLoader loader ) 120 { 121 super( parent, loader ); 122 } 123 124 130 public void initialize() throws Exception 131 { 132 final Set services = new HashSet(); 133 134 final Enumeration enum = getLoader().getResources( "services.list" ); 135 while ( enum.hasMoreElements() ) 136 { 137 readEntries( services, (URL) enum.nextElement() ); 138 } 139 140 final Iterator it = services.iterator(); 141 while ( it.hasNext() ) 142 { 143 final String role = (String) it.next(); 144 getLogger().debug( "Adding service: " + role ); 145 try 146 { 147 setupImplementations( role ); 148 } 149 catch ( Exception e ) 150 { 151 getLogger().debug( "Specified service '" + role + "' is not available", e ); 152 } 153 } 154 } 155 156 164 private void setupImplementations( final String role ) 165 throws ClassNotFoundException 166 { 167 final Iterator it = Service.providers( getLoader().loadClass( role ), getLoader() ); 168 169 while ( it.hasNext() ) 170 { 171 final String impl = ( (Class) it.next() ).getName(); 172 getLogger().debug( "Reading meta info for " + impl ); 173 if ( ! isAlreadyAdded( impl ) ) 174 { 175 readMeta( role, impl ); 176 } 177 else 178 { 179 addComponent( role, impl, null, null ); 181 } 182 } 183 } 184 185 191 private void readMeta( final String role, final String implementation ) 192 { 193 final Properties meta = new Properties(); 194 final List deps = new ArrayList(); 195 196 try 197 { 198 final InputStream stream = 199 getLoader().getResourceAsStream( getMetaFile( implementation ) ); 200 201 if ( stream != null ) 202 { 203 meta.load( stream ); 204 } 205 else 206 { 207 getLogger().error( 208 "Meta information for " + implementation + 209 " unavailable, skipping this class." 210 ); 211 return; 212 } 213 } 214 catch ( IOException ioe ) 215 { 216 getLogger().error( "Could not load meta information for " + 217 implementation + ", skipping this class." ); 218 return; 219 } 220 221 try 222 { 223 URL depURL = getLoader().getResource( getDepFile( implementation ) ); 224 225 if ( depURL == null ) 226 { 227 if (getLogger().isDebugEnabled()) 228 { 229 getLogger().debug( "No dependencies for " + implementation + "." ); 230 } 231 } 232 else 233 { 234 HashSet set = new HashSet(); 235 readEntries( set, depURL ); 236 deps.addAll( set ); 237 } 238 } 239 catch ( Exception ioe ) 240 { 241 getLogger().debug( "Could not load dependencies for " + 242 implementation + ".", ioe ); 243 } 244 245 addComponent( role, implementation, meta, deps ); 246 } 247 248 254 private String getMetaFile( final String implementation ) 255 { 256 String entry = implementation.replace( '.', '/' ); 257 entry += ".meta"; 258 return entry; 259 } 260 261 267 private String getDepFile( final String implementation ) 268 { 269 String entry = implementation.replace( '.', '/' ); 270 entry += ".deps"; 271 return entry; 272 } 273 274 282 private void readEntries( final Set entries, final URL url ) 283 throws IOException 284 { 285 final BufferedReader reader = new BufferedReader( new InputStreamReader( url.openStream() ) ); 286 287 try 288 { 289 String entry = reader.readLine(); 290 while ( entry != null ) 291 { 292 entries.add( entry ); 293 entry = reader.readLine(); 294 } 295 } 296 finally 297 { 298 reader.close(); 299 } 300 } 301 } 302 | Popular Tags |