public Map < ConfigID , FactoryElement > getFactoryInstancesUsingDefaultId ( String pid , String alias , String defaultId ) throws ConfigMergeException { Map < ConfigID , List < SimpleElement > > defaultFactories = defaultConfigurationFactories ( pid , alias , defaultId ) ; Map < ConfigID , List < SimpleElement > > factories = getAllFactoryElements ( pid , alias , defaultId ) ; // process factory instances Map < ConfigID , FactoryElement > mergedMap = new HashMap < ConfigID , FactoryElement > ( ) ; for ( Map . Entry < ConfigID , List < SimpleElement > > entry : factories . entrySet ( ) ) { ConfigID elementId = entry . getKey ( ) ; // If there are defaults, create a FactoryElement using them and then override with the // specified values. Otherwise, create a FactoryElement from the configured values. List < SimpleElement > defaultElements = defaultFactories . remove ( elementId ) ; if ( defaultElements == null ) { FactoryElement merged = new FactoryElement ( entry . getValue ( ) , pid , elementId . getId ( ) ) ; mergedMap . put ( elementId , merged ) ; } else { // We have factory elements, so first remove any default configuration that is marked "merge if doesn't exist". Iterator < SimpleElement > iter = defaultElements . iterator ( ) ; while ( iter . hasNext ( ) ) { SimpleElement element = iter . next ( ) ; if ( element . mergeBehavior == MergeBehavior . MERGE_WHEN_MISSING ) { iter . remove ( ) ; } } if ( ! defaultElements . isEmpty ( ) ) { FactoryElement merged = new FactoryElement ( defaultElements , pid , elementId . getId ( ) ) ; // Remove the ID from the list of attributes. If it's specified, it will be added back by the configured values. merged . attributes . remove ( XMLConfigConstants . CFG_INSTANCE_ID ) ; merged . merge ( entry . getValue ( ) ) ; mergedMap . put ( elementId , merged ) ; } else { FactoryElement merged = new FactoryElement ( entry . getValue ( ) , pid , elementId . getId ( ) ) ; mergedMap . put ( elementId , merged ) ; } } } // process remaining default factory instances. These instances do not match up to configurations, so don't // create them if the merge behavior is merge_when_exists for ( Map . Entry < ConfigID , List < SimpleElement > > entry : defaultFactories . entrySet ( ) ) { ConfigID elementId = entry . getKey ( ) ; List < SimpleElement > elements = entry . getValue ( ) ; boolean found = false ; for ( SimpleElement element : elements ) { if ( element . mergeBehavior != MergeBehavior . MERGE_WHEN_EXISTS ) found = true ; } if ( found ) { FactoryElement merged = new FactoryElement ( elements , pid , elementId . getId ( ) ) ; mergedMap . put ( elementId , merged ) ; } } return mergedMap ; }
Get a Map of FactoryElements indexed by ConfigID