/** * Updates the imports of an instance with new values. * @param instance the instance whose imports must be updated * @param variablePrefixToImports the new imports (can be null) */ public static void updateImports( Instance instance, Map<String,Collection<Import>> variablePrefixToImports ) { instance.getImports().clear(); if( variablePrefixToImports != null ) instance.getImports().putAll( variablePrefixToImports ); }
/** * Adds an import to an instance (provided this import was not already set). * @param instance the instance whose imports must be updated * @param componentOrFacetName the component or facet name associated with the import * @param imp the import to add */ public static void addImport( Instance instance, String componentOrFacetName, Import imp ) { Collection<Import> imports = instance.getImports().get( componentOrFacetName ); if(imports == null) { imports = new LinkedHashSet<Import> (); instance.getImports().put( componentOrFacetName, imports ); } if( ! imports.contains( imp )) imports.add( imp ); }
/** * Constructor. * @param applicationName the application name * @param instance the changed instance */ public MsgNotifInstanceChanged( String applicationName, Instance instance ) { super(); this.instancePath = InstanceHelpers.computeInstancePath( instance ); this.newImports = instance.getImports(); this.newStatus = instance.getStatus(); this.applicationName = applicationName; }
/** * Constructor. * @param applicationName the application name * @param instance the changed instance */ public MsgNotifInstanceChanged( String applicationName, Instance instance ) { super(); this.instancePath = InstanceHelpers.computeInstancePath( instance ); this.newImports = instance.getImports(); this.newStatus = instance.getStatus(); this.applicationName = applicationName; }
/** * Determines whether an instance has all the imports it needs. * <p> * Master of Obvious said:<br> * By definition, optional imports are not considered to be required. * </p> * * @param instance a non-null instance * @param logger a logger (can be null) * @return true if all its (mandatory) imports are resolved, false otherwise */ public static boolean hasAllRequiredImports( Instance instance, Logger logger ) { boolean haveAllImports = true; for( String facetOrComponentName : VariableHelpers.findPrefixesForMandatoryImportedVariables( instance )) { Collection<Import> imports = instance.getImports().get( facetOrComponentName ); if( imports != null && ! imports.isEmpty()) continue; haveAllImports = false; if( logger != null ) logger.fine( InstanceHelpers.computeInstancePath( instance ) + " is still missing dependencies '" + facetOrComponentName + ".*'." ); break; } return haveAllImports; }
sb.append( " => " ); Collection<Import> imports = instance.getImports().get( facetOrComponentName ); if( imports == null || imports.isEmpty()) {
i.getImports().clear();
@Test public void testAddImport() { Map<String,Collection<Import>> prefixToImports = new HashMap<String,Collection<Import>> (); prefixToImports.put( "comp", new ArrayList<Import>( Arrays.asList( new Import( "/root1", "comp1" ), new Import( "/root2", "comp1" )))); Instance inst = new Instance( "inst" ); inst.getImports().putAll( prefixToImports ); Assert.assertEquals( 1, inst.getImports().keySet().size()); Assert.assertTrue( inst.getImports().keySet().contains( "comp" )); ImportHelpers.addImport( inst, "wow", new Import( "/root", "comp1" )); Assert.assertEquals( 2, inst.getImports().keySet().size()); Assert.assertTrue( inst.getImports().keySet().contains( "comp" )); Assert.assertTrue( inst.getImports().keySet().contains( "wow" )); Assert.assertEquals( 2, inst.getImports().get( "comp" ).size()); ImportHelpers.addImport( inst, "comp", new Import( "/root3", "comp1" )); Assert.assertEquals( 3, inst.getImports().get( "comp" ).size()); Assert.assertEquals( 2, inst.getImports().keySet().size()); // We cannot insert the same import twice ImportHelpers.addImport( inst, "comp", new Import( "/root3", "comp1" )); Assert.assertEquals( 3, inst.getImports().get( "comp" ).size()); Assert.assertEquals( 2, inst.getImports().keySet().size()); }
@Test public void testUpdateImports() { Map<String,Collection<Import>> prefixToImports = new HashMap<String,Collection<Import>> (); prefixToImports.put( "comp", Arrays.asList( new Import( "/root1", "comp1" ), new Import( "/root2", "comp1" ))); Instance inst = new Instance( "inst" ); Assert.assertEquals( 0, inst.getImports().size()); ImportHelpers.updateImports( inst, prefixToImports ); Assert.assertEquals( 1, inst.getImports().size()); Iterator<Import> iterator = inst.getImports().get( "comp" ).iterator(); Assert.assertEquals( "/root1", iterator.next().getInstancePath()); Assert.assertEquals( "/root2", iterator.next().getInstancePath()); Assert.assertFalse( iterator.hasNext()); prefixToImports.put( "comp", Arrays.asList( new Import( "/root1", "comp1" ))); ImportHelpers.updateImports( inst, prefixToImports ); Assert.assertEquals( 1, inst.getImports().size()); iterator = inst.getImports().get( "comp" ).iterator(); Assert.assertEquals( "/root1", iterator.next().getInstancePath()); Assert.assertFalse( iterator.hasNext()); ImportHelpers.updateImports( inst, null ); Assert.assertEquals( 0, inst.getImports().size()); }
Collection<Import> imports = instance.getImports().get( msg.getExternalExportsPrefix()); if( imports == null ) continue;
Collection<Import> imports = instance.getImports().get( msg.getComponentOrFacetName()); Import toRemove = ImportHelpers.findImportByExportingInstance( imports, msg.getRemovedInstancePath()); if( toRemove == null ) instance.getImports().remove( msg.getComponentOrFacetName());
@Test public void testDuplicateInstance_singleInstance() { Instance original = new Instance( "inst" ).channel( "chan" ).component( new Component( "comp" )); original.overriddenExports.put( "test", "test" ); original.overriddenExports.put( "A.port", "8012" ); original.data.put( "some", "data" ); original.getImports().put( "facet-name", new ArrayList<Import> ()); original.setStatus( InstanceStatus.DEPLOYED_STARTED ); Instance copy = InstanceHelpers.replicateInstance( original ); Assert.assertEquals( original.getName(), copy.getName()); Assert.assertEquals( original.channels, copy.channels ); Assert.assertEquals( original.overriddenExports.size(), copy.overriddenExports.size()); Assert.assertEquals( "test", copy.overriddenExports.get( "test" )); Assert.assertEquals( "8012", copy.overriddenExports.get( "A.port" )); Assert.assertEquals( 0, copy.getImports().size()); Assert.assertEquals( original.getComponent(), copy.getComponent()); Assert.assertEquals( InstanceStatus.NOT_DEPLOYED, copy.getStatus()); }
@Test public void checkInstanceReplication() { TestApplicationTemplate tpl = new TestApplicationTemplate(); Application app = new Application( tpl ); Assert.assertEquals( 5, InstanceHelpers.getAllInstances( app ).size()); Assert.assertEquals( 5, InstanceHelpers.getAllInstances( tpl ).size()); for( Instance inst : InstanceHelpers.getAllInstances( tpl )) { String instancePath = InstanceHelpers.computeInstancePath( inst ); Instance copiedInstance = InstanceHelpers.findInstanceByPath( app, instancePath ); Assert.assertNotNull( copiedInstance ); Assert.assertEquals( inst.getName(), copiedInstance.getName()); Assert.assertEquals( inst.getComponent(), copiedInstance.getComponent()); Assert.assertEquals( inst.getImports(), copiedInstance.getImports()); Assert.assertEquals( inst.getParent(), copiedInstance.getParent()); Assert.assertEquals( inst.getChildren().size(), copiedInstance.getChildren().size()); // Paths are the same, so the children are equal (even if they are not the same object) Assert.assertEquals( inst.getChildren(), copiedInstance.getChildren()); Assert.assertEquals( inst.channels, copiedInstance.channels ); Assert.assertEquals( inst.overriddenExports, copiedInstance.overriddenExports ); Assert.assertEquals( inst.data, copiedInstance.data ); Assert.assertFalse( inst == copiedInstance ); } }
@Test public void testHasAllRequiredImports_withWildcard() throws Exception { // Same test than "testHasAllRequiredImports_required" // but here, variable imports use a wild card. Component dbComponent = new Component( "database" ).installerName( "whatever" ); dbComponent.addExportedVariable( new ExportedVariable( "database.ip", null )); dbComponent.addExportedVariable( new ExportedVariable( "database.port", "3009" )); dbComponent.addExportedVariable( new ExportedVariable( "database.collection", "whatever" )); Component appServerComponent = new Component( "app-server" ).installerName( "whatever" ); appServerComponent.addExportedVariable( new ExportedVariable( "app-server.ip", null )); appServerComponent.addExportedVariable( new ExportedVariable( "app-server.port", "8009" )); appServerComponent.addImportedVariable( new ImportedVariable( "database.*", false, false )); Instance appServer = new Instance( "app server" ).component( appServerComponent ); appServer.overriddenExports.put( "app-server.ip", "192.168.1.15" ); appServer.setStatus( InstanceStatus.STARTING ); Instance database = new Instance( "database" ).component( dbComponent ); database.overriddenExports.put( "database.ip", "192.168.1.28" ); // The application server does not know about the database Assert.assertFalse( ImportHelpers.hasAllRequiredImports( appServer, Logger.getAnonymousLogger())); // The application server is now aware of the database ImportHelpers.addImport( appServer, "database", new Import( database )); Assert.assertTrue( ImportHelpers.hasAllRequiredImports( appServer, null )); appServer.getImports().clear(); Assert.assertFalse( ImportHelpers.hasAllRequiredImports( appServer, null )); }
@Test public void testHasAllRequiredImports_required() throws Exception { Component dbComponent = new Component( "database" ).installerName( "whatever" ); dbComponent.addExportedVariable( new ExportedVariable( "database.ip", null )); dbComponent.addExportedVariable( new ExportedVariable( "database.port", "3009" )); dbComponent.addExportedVariable( new ExportedVariable( "database.collection", "whatever" )); Component appServerComponent = new Component( "app-server" ).installerName( "whatever" ); appServerComponent.addExportedVariable( new ExportedVariable( "app-server.ip", null )); appServerComponent.addExportedVariable( new ExportedVariable( "app-server.port", "8009" )); appServerComponent.addImportedVariable( new ImportedVariable( "database.ip", false, false )); appServerComponent.addImportedVariable( new ImportedVariable( "database.port", false, false )); appServerComponent.addImportedVariable( new ImportedVariable( "database.collection", true, false )); Instance appServer = new Instance( "app server" ).component( appServerComponent ); appServer.overriddenExports.put( "app-server.ip", "192.168.1.15" ); appServer.setStatus( InstanceStatus.STARTING ); Instance database = new Instance( "database" ).component( dbComponent ); database.overriddenExports.put( "database.ip", "192.168.1.28" ); // The application server does not know about the database Assert.assertFalse( ImportHelpers.hasAllRequiredImports( appServer, Logger.getAnonymousLogger())); // The application server is now aware of the database ImportHelpers.addImport( appServer, "database", new Import( database )); Assert.assertTrue( ImportHelpers.hasAllRequiredImports( appServer, null )); appServer.getImports().clear(); Assert.assertFalse( ImportHelpers.hasAllRequiredImports( appServer, null )); }
@Test public void testHasAllRequiredImports_optional() throws Exception { Component clusterNodeComponent = new Component( "cluster" ).installerName( "whatever" ); clusterNodeComponent.addImportedVariable( new ImportedVariable( "cluster.ip", true, false )); clusterNodeComponent.addImportedVariable( new ImportedVariable( "cluster.port", true, false )); clusterNodeComponent.addExportedVariable( new ExportedVariable( "cluster.ip", null )); clusterNodeComponent.addExportedVariable( new ExportedVariable( "cluster.port", "9007" )); Instance i1 = new Instance( "inst 1" ).component( clusterNodeComponent ); i1.overriddenExports.put( "cluster.ip", "192.168.1.15" ); i1.setStatus( InstanceStatus.STARTING ); Instance i2 = new Instance( "inst 2" ).component( clusterNodeComponent ); i2.overriddenExports.put( "cluster.ip", "192.168.1.28" ); // The cluster node does not know about the other node Assert.assertTrue( ImportHelpers.hasAllRequiredImports( i1, null )); Assert.assertTrue( ImportHelpers.hasAllRequiredImports( i2, null )); // The node is now aware of another node ImportHelpers.addImport( i1, "cluster", new Import( i2 )); i1.setStatus( InstanceStatus.STARTING ); Assert.assertTrue( ImportHelpers.hasAllRequiredImports( i1, null )); Assert.assertTrue( ImportHelpers.hasAllRequiredImports( i2, null )); i1.getImports().clear(); Assert.assertTrue( ImportHelpers.hasAllRequiredImports( i1, null )); Assert.assertTrue( ImportHelpers.hasAllRequiredImports( i2, null )); }