/** * A convenience method to add an imported variable. * @param var a non-null variable */ public void addImportedVariable( ImportedVariable var ) { this.importedVariables.put( var.getName(), var ); } }
/** * Finds the component and facet names that prefix the variables an instance imports. * @param instance an instance * @return a non-null set with all the component and facet names this instance imports */ public static Set<String> findPrefixesForImportedVariables( Instance instance ) { Set<String> result = new HashSet<> (); for( ImportedVariable var : ComponentHelpers.findAllImportedVariables( instance.getComponent()).values()) result.add( VariableHelpers.parseVariableName( var.getName()).getKey()); return result; }
/** * Finds the component dependencies for a given component. * @param component a component * @return a non-null map (key = component name, value = true if the dependency is optional, false otherwise) */ public static Map<String,Boolean> findComponentDependenciesFor( Component component ) { Map<String,Boolean> map = new HashMap<> (); for( ImportedVariable var : findAllImportedVariables( component ).values()) { String componentOrFacet = VariableHelpers.parseVariableName( var.getName()).getKey(); Boolean b = map.get( componentOrFacet ); if( b == null || b ) map.put( componentOrFacet, var.isOptional()); } return map; }
/** * Finds the component and facet names that prefix the variables an instance requires. * <p> * Only the mandatory variables are returned. Optional imports are not considered by this method. * </p> * * @param instance an instance * @return a non-null set with all the component and facet names this instance imports */ public static Set<String> findPrefixesForMandatoryImportedVariables( Instance instance ) { Set<String> result = new HashSet<> (); for( ImportedVariable var : ComponentHelpers.findAllImportedVariables( instance.getComponent()).values()) { if( ! var.isOptional()) result.add( VariableHelpers.parseVariableName( var.getName()).getKey()); } return result; }
/** * Converts imports to a human-readable text. * @param importedVariables a non-null set of imported variables * @return a non-null list of string, one entry per import */ private List<String> convertImports( Collection<ImportedVariable> importedVariables ) { List<String> result = new ArrayList<> (); for( ImportedVariable var : importedVariables ) { String componentOrFacet = VariableHelpers.parseVariableName( var.getName()).getKey(); String s = applyLink( var.getName(), componentOrFacet ); s += var.isOptional() ? this.messages.get( "optional" ) : this.messages.get( "required" ); //$NON-NLS-1$ //$NON-NLS-2$ if( var.isExternal()) s += this.messages.get( "external" ); //$NON-NLS-1$ result.add( s ); } return result; }
/** * Finds the prefixes of the external variables for a given application or template. * <p> * If this list is not empty, it means this application depends on * other ones. * </p> * * @param app an application or an application template * @return a non-null application */ public static Set<String> findPrefixesForExternalImports( AbstractApplication app ) { Set<String> result = new TreeSet<> (); for( Component c : ComponentHelpers.findAllComponents( app )) { for( ImportedVariable var : ComponentHelpers.findAllImportedVariables( c ).values()) { if( ! var.isExternal()) continue; String prefix = VariableHelpers.parseVariableName( var.getName()).getKey(); result.add( prefix ); } } return result; }
/** * Builds a list of messaging contexts. * @param domain the domain * @param applicationName the name of the agent's application * @param instance the current instance * @param thoseThat whether we target "those that import" or "those that export" * @return a non-null list */ public static Collection<MessagingContext> forImportedVariables( String domain, String applicationName, Instance instance, ThoseThat thoseThat ) { Map<String,MessagingContext> result = new HashMap<> (); for( ImportedVariable var : ComponentHelpers.findAllImportedVariables( instance.getComponent()).values()) { String componentOrApplicationTemplateName = VariableHelpers.parseVariableName( var.getName()).getKey(); if( result.containsKey( componentOrApplicationTemplateName )) continue; // When we import a variable, it is either internal or external, but not both! RecipientKind kind = var.isExternal() ? RecipientKind.INTER_APP : RecipientKind.AGENTS; MessagingContext ctx = new MessagingContext( kind, domain, componentOrApplicationTemplateName, thoseThat, applicationName ); result.put( componentOrApplicationTemplateName, ctx ); } return result.values(); }
if( var.getName().endsWith( "." + Constants.WILDCARD )) { result.add( new ModelError( ErrorCode.REC_PUPPET_DISLIKES_WILDCARD_IMPORTS, component, component( component ))); break;
/** * Builds a list of messaging contexts. * @param domain the domain * @param applicationName the name of the agent's application * @param instance the current instance * @param thoseThat whether we target "those that import" or "those that export" * @return a non-null list */ public static Collection<MessagingContext> forImportedVariables( String domain, String applicationName, Instance instance, ThoseThat thoseThat ) { Map<String,MessagingContext> result = new HashMap<> (); for( ImportedVariable var : ComponentHelpers.findAllImportedVariables( instance.getComponent()).values()) { String componentOrApplicationTemplateName = VariableHelpers.parseVariableName( var.getName()).getKey(); if( result.containsKey( componentOrApplicationTemplateName )) continue; // When we import a variable, it is either internal or external, but not both! RecipientKind kind = var.isExternal() ? RecipientKind.INTER_APP : RecipientKind.AGENTS; MessagingContext ctx = new MessagingContext( kind, domain, componentOrApplicationTemplateName, thoseThat, applicationName ); result.put( componentOrApplicationTemplateName, ctx ); } return result.values(); }
/** * Converts imports to a human-readable text. * @param importedVariables a non-null set of imported variables * @return a non-null list of string, one entry per import */ private List<String> convertImports( Collection<ImportedVariable> importedVariables ) { List<String> result = new ArrayList<> (); for( ImportedVariable var : importedVariables ) { String componentOrFacet = VariableHelpers.parseVariableName( var.getName()).getKey(); String s = applyLink( var.getName(), componentOrFacet ); s += var.isOptional() ? this.messages.get( "optional" ) : this.messages.get( "required" ); //$NON-NLS-1$ //$NON-NLS-2$ if( var.isExternal()) s += this.messages.get( "external" ); //$NON-NLS-1$ result.add( s ); } return result; }
String importedVariableName = var.getName(); if( ! importedVariableNameToExported.containsKey( importedVariableName )) importedVariableNameToExported.put( importedVariableName, Boolean.FALSE );
sb.append( var.getName()); if( var.isOptional()) { sb.append( " " );
@Test public void testFindPrefixesForExternalImports() { TestApplication app = new TestApplication(); ImportedVariable var1 = new ImportedVariable( "something.else", true, true ); ImportedVariable var2 = new ImportedVariable( "other.stuff", true, true ); app.getWar().getComponent().importedVariables.put( var1.getName(), var1 ); app.getWar().getComponent().importedVariables.put( var2.getName(), var2 ); Set<String> prefixes = VariableHelpers.findPrefixesForExternalImports( app ); Assert.assertEquals( 2, prefixes.size()); Assert.assertTrue( prefixes.contains( "other" )); Assert.assertTrue( prefixes.contains( "something" )); }
String varName = var.getName(); String patternForImports = ParsingConstants.PATTERN_ID; patternForImports += "(\\.\\*)?";
ImportedVariable originalVar = originalComponent.importedVariables.get( var.getName()); Assert.assertNotNull( "Imports did not match for " + junitMsg, originalVar ); Assert.assertEquals( junitMsg, originalVar.isOptional(), var.isOptional());
app1.getTomcat().getComponent().importedVariables.put( var.getName(), var );
app1.getTomcat().getComponent().importedVariables.put( var.getName(), var );