1 50 51 package org.apache.avalon.meta.info; 52 53 import java.util.ArrayList ; 54 import java.util.Arrays ; 55 import java.util.List ; 56 import java.util.Properties ; 57 58 75 public class ContextDescriptor extends Descriptor 76 { 77 81 84 public static final String NAME_KEY = 85 "urn:avalon:name"; 86 87 90 public static final String PARTITION_KEY = 91 "urn:avalon:partition"; 92 93 96 public static final String HOME_KEY = 97 "urn:avalon:home"; 98 99 102 public static final String TEMP_KEY = 103 "urn:avalon:temp"; 104 105 108 public static final String CLASSLOADER_KEY = 109 "urn:avalon:classloader"; 110 111 115 public static final String STRATEGY_KEY = 116 "urn:avalon:context.strategy"; 117 118 121 public static final String AVALON_CONTEXT_CLASSNAME = 122 "org.apache.avalon.framework.context.Context"; 123 124 128 private final String m_classname; 129 130 private final EntryDescriptor[] m_entries; 131 132 136 140 public ContextDescriptor( final EntryDescriptor[] entries ) 141 { 142 this( null, entries, null ); 143 } 144 145 150 public ContextDescriptor( final String classname, 151 final EntryDescriptor[] entries ) 152 { 153 this( classname, entries, null ); 154 } 155 156 163 public ContextDescriptor( final String classname, 164 final EntryDescriptor[] entries, 165 final Properties attributes ) 166 throws NullPointerException , IllegalArgumentException 167 { 168 super( attributes ); 169 170 if ( null == entries ) 171 { 172 throw new NullPointerException ( "entries" ); 173 } 174 175 if ( null == classname ) 176 { 177 m_classname = AVALON_CONTEXT_CLASSNAME; 178 } 179 else 180 { 181 m_classname = classname; 182 } 183 m_entries = entries; 184 } 185 186 190 197 public String getContextInterfaceClassname() 198 { 199 return m_classname; 200 } 201 202 207 public EntryDescriptor[] getEntries() 208 { 209 return m_entries; 210 } 211 212 220 public EntryDescriptor getEntry( final String alias ) 221 { 222 if ( null == alias ) 223 { 224 throw new NullPointerException ( "alias" ); 225 } 226 227 for ( int i = 0; i < m_entries.length; i++ ) 228 { 229 final EntryDescriptor entry = m_entries[i]; 230 if( entry.getAlias().equals( alias ) ) 231 { 232 return entry; 233 } 234 } 235 236 for ( int i = 0; i < m_entries.length; i++ ) 237 { 238 final EntryDescriptor entry = m_entries[i]; 239 if( entry.getKey().equals( alias ) ) 240 { 241 return entry; 242 } 243 } 244 245 return null; 246 } 247 248 256 public EntryDescriptor[] merge( EntryDescriptor[] entries ) 257 throws IllegalArgumentException 258 { 259 for ( int i = 0; i < entries.length; i++ ) 260 { 261 EntryDescriptor entry = entries[i]; 262 final String key = entry.getKey(); 263 EntryDescriptor local = getEntry( entry.getKey() ); 264 if ( local != null ) 265 { 266 if ( !entry.getClassname().equals( local.getClassname() ) ) 267 { 268 final String error = 269 "Conflicting entry type for key: " + key; 270 throw new IllegalArgumentException ( error ); 271 } 272 } 273 } 274 275 return join( entries, getEntries() ); 276 } 277 278 private EntryDescriptor[] join( EntryDescriptor[] primary, EntryDescriptor[] secondary ) 279 { 280 List list = new ArrayList ( primary.length + secondary.length ); 281 list.addAll( Arrays.asList( primary ) ); 282 list.addAll( Arrays.asList( secondary ) ); 283 return (EntryDescriptor[]) list.toArray( new EntryDescriptor[0] ); 284 } 285 286 290 public boolean equals( Object other ) 291 { 292 boolean isEqual = super.equals( other ); 293 if( isEqual ) isEqual = other instanceof ContextDescriptor; 294 if( isEqual ) 295 { 296 ContextDescriptor entity = (ContextDescriptor) other; 297 isEqual = isEqual && m_classname.equals( entity.m_classname ); 298 for( int i=0; i<m_entries.length; i++ ) 299 { 300 isEqual = isEqual && m_entries[i].equals( entity.m_entries[i] ); 301 } 302 } 303 return isEqual; 304 } 305 306 310 public int hashCode() 311 { 312 int hash = super.hashCode(); 313 hash >>>= 7; 314 hash ^= m_classname.hashCode(); 315 for( int i=0; i<m_entries.length; i++ ) 316 { 317 hash >>>= 7; 318 hash ^= m_entries[i].hashCode(); 319 } 320 return hash; 321 } 322 } 323 | Popular Tags |