1 17 18 package org.apache.avalon.repository.meta; 19 20 import javax.naming.directory.Attributes ; 21 import javax.naming.directory.Attribute ; 22 import javax.naming.NamingException ; 23 24 import org.apache.avalon.repository.Artifact; 25 import org.apache.avalon.repository.RepositoryRuntimeException; 26 27 35 public class FactoryDescriptor extends ArtifactDescriptor 36 { 37 41 public static final String API_KEY = 42 "avalon.artifact.dependency.api"; 43 44 public static final String SPI_KEY = 45 "avalon.artifact.dependency.spi"; 46 47 public static final String IMP_KEY = 48 "avalon.artifact.dependency"; 49 50 public static final String FACTORY_KEY = 51 "avalon.artifact.factory"; 52 53 public static final String EXPORT_KEY = 54 "avalon.artifact.export"; 55 56 60 private final Artifact[] c_api; 61 62 private final Artifact[] c_spi; 63 64 private final Artifact[] c_imp; 65 66 private final String m_factory; 67 68 private final String m_interface; 69 70 74 79 public FactoryDescriptor( final Attributes attributes ) 80 throws MetaException 81 { 82 super( attributes ); 83 84 c_api = buildDependents( attributes, API_KEY ); 85 c_spi = buildDependents( attributes, SPI_KEY ); 86 c_imp = buildDependents( attributes, IMP_KEY ); 87 88 m_factory = getFactory( attributes ); 89 m_interface = getInterface( attributes ); 90 } 91 92 96 97 101 public String getFactory() 102 { 103 return m_factory; 104 } 105 106 110 public String getInterface() 111 { 112 return m_interface; 113 } 114 115 119 public Artifact[] getDependencies( String key ) 120 { 121 if( key == API_KEY ) 122 { 123 return c_api; 124 } 125 else if( key == SPI_KEY ) 126 { 127 return c_spi; 128 } 129 else if( key == IMP_KEY ) 130 { 131 return c_imp; 132 } 133 else 134 { 135 final String error = 136 "Invalid dependency key: " + key; 137 throw new IllegalArgumentException ( error ); 138 } 139 } 140 141 public Artifact[] getDependencies() 142 { 143 int j = c_api.length + c_spi.length + c_imp.length; 144 Artifact[] all = new Artifact[ j ]; 145 int q = 0; 146 for( int i=0; i<c_api.length; i++ ) 147 { 148 all[q] = c_api[i]; 149 q++; 150 } 151 for( int i=0; i<c_spi.length; i++ ) 152 { 153 all[q] = c_spi[i]; 154 q++; 155 } 156 for( int i=0; i<c_imp.length; i++ ) 157 { 158 all[q] = c_imp[i]; 159 q++; 160 } 161 return all; 162 } 163 164 public String toString() 165 { 166 return "[factory:" + m_factory + "]"; 167 } 168 169 173 private Artifact[] buildDependents( 174 Attributes attributes, String key ) 175 { 176 try 177 { 178 Attribute attribute = attributes.get( key ) ; 179 if( null == attribute ) return new Artifact[0]; 180 181 Artifact[] dependencies = 182 new Artifact[ attribute.size() ] ; 183 for ( int i = 0; i < dependencies.length; i++ ) 184 { 185 final String spec = (String ) attribute.get( i ); 186 dependencies[i] = Artifact.createArtifact( spec ) ; 187 } 188 return dependencies; 189 } 190 catch ( NamingException e ) 191 { 192 throw new RepositoryRuntimeException( 193 "Failed to resolve dependencies for [" + key 194 + "] on the attribute set [" + attributes + "].", e ) ; 195 } 196 } 197 198 private String getFactory( Attributes attributes ) 199 { 200 try 201 { 202 return getValue( attributes, FACTORY_KEY ); 203 } 204 catch( Throwable e ) 205 { 206 return null; 207 } 208 } 209 210 private String getInterface( Attributes attributes ) 211 { 212 try 213 { 214 return getValue( attributes, EXPORT_KEY ); 215 } 216 catch( Throwable e ) 217 { 218 return null; 219 } 220 } 221 } 222 | Popular Tags |