/** * Constructor. * @param template */ public Application( ApplicationTemplate template ) { this.template = template; // We must duplicate all the instances if( template != null ) { template.associateApplication( this ); for( Instance rootInstance : template.getRootInstances()) getRootInstances().add( InstanceHelpers.replicateInstance( rootInstance )); } }
sb.append( renderParagraph( this.messages.get( "instances.intro" ))); //$NON-NLS-1$ if( this.applicationTemplate.getRootInstances().isEmpty()) { sb.append( renderParagraph( this.messages.get( "instances.none" ))); //$NON-NLS-1$ sortedRootInstances.addAll( this.applicationTemplate.getRootInstances()); for( Instance inst : sortedRootInstances ) {
sb.append( renderParagraph( this.messages.get( "instances.intro" ))); //$NON-NLS-1$ if( this.applicationTemplate.getRootInstances().isEmpty()) { sb.append( renderParagraph( this.messages.get( "instances.none" ))); //$NON-NLS-1$ sortedRootInstances.addAll( this.applicationTemplate.getRootInstances()); for( Instance inst : sortedRootInstances ) {
result.objectToSource.putAll( ilr.getObjectToSource()); result.loadErrors.addAll( ilr.getLoadErrors()); app.getRootInstances().addAll( ilr.getRootInstances());
/** * Adds an instance to the local model. * @param msg the message to process * @throws IOException if an error occurred with the messaging * @throws PluginException if an error occurred while initializing the plug-in */ void processMsgAddInstance( MsgCmdAddInstance msg ) throws IOException, PluginException { Component instanceComponent; Instance parentInstance = InstanceHelpers.findInstanceByPath( this.scopedInstance, msg.getParentInstancePath()); if( parentInstance == null ) { this.logger.severe( "The parent instance for " + msg.getParentInstancePath() + " was not found. The request to add a new instance is dropped." ); } else if(( instanceComponent = ComponentHelpers.findComponentFrom( parentInstance.getComponent(), msg.getComponentName())) == null ) { this.logger.severe( "The component " + msg.getComponentName() + " was not found in the local graph." ); } else { Instance newInstance = new Instance( msg.getInstanceName()).component( instanceComponent ); newInstance.channels.addAll( msg.getChannels()); if( msg.getData() != null ) newInstance.data.putAll( msg.getData()); if( msg.getOverridenExports() != null ) newInstance.overriddenExports.putAll( msg.getOverridenExports()); ApplicationTemplate tempApp = new ApplicationTemplate( "temp app" ); tempApp.getRootInstances().add( parentInstance ); if( ! InstanceHelpers.tryToInsertChildInstance( tempApp, parentInstance, newInstance )) { this.logger.severe( "The new '" + msg.getInstanceName() + "' instance could not be inserted into the local model." ); } else { this.messagingClient.listenToExportsFromOtherAgents( ListenerCommand.START, newInstance ); this.messagingClient.requestExportsFromOtherAgents( newInstance ); } } }
app.getRootInstances().add( vmInstance1 ); app.getRootInstances().clear(); app.getRootInstances().add( tomcatInstance ); app.getRootInstances().clear(); app.getRootInstances().add( vmInstance1 ); Assert.assertEquals( 0, RuntimeModelValidator.validate( app ).size());
@Test public void testFindInstancesByComponentName() { ApplicationTemplate app = new ApplicationTemplate(); Component tomcat = new Component( "tomcat" ).installerName( "puppet" ); Component other = new Component( "other" ).installerName( "chef" ); Instance i1 = new Instance( "i1" ).component( tomcat ); Instance i2 = new Instance( "i2" ).component( tomcat ); Instance i3 = new Instance( "i3" ).component( other ); Instance i4 = new Instance( "i4" ).component( other ); Graphs graphs = new Graphs(); graphs.getRootComponents().add( other ); graphs.getRootComponents().add( tomcat ); app.setGraphs( graphs ); InstanceHelpers.insertChild( i3, i1 ); app.getRootInstances().add( i2 ); app.getRootInstances().add( i3 ); app.getRootInstances().add( i4 ); List<Instance> tomcatInstances = InstanceHelpers.findInstancesByComponentName( app, tomcat.getName()); Assert.assertEquals( 2, tomcatInstances.size()); Assert.assertTrue( tomcatInstances.contains( i1 )); Assert.assertTrue( tomcatInstances.contains( i2 )); List<Instance> otherInstances = InstanceHelpers.findInstancesByComponentName( app, other.getName()); Assert.assertEquals( 2, otherInstances.size()); Assert.assertTrue( otherInstances.contains( i3 )); Assert.assertTrue( otherInstances.contains( i4 )); Assert.assertEquals( 0, InstanceHelpers.findInstancesByComponentName( app, "whatever" ).size()); }
@Test public void testInvalidChildInstance_2() throws Exception { Component vmComponent = new Component( "VM" ).installerName( "target" ); Component tomcatComponent = new Component( "Tomcat" ).installerName( "puppet" ); Graphs graphs = new Graphs(); graphs.getRootComponents().add( vmComponent ); // We cannot instantiate a VM under a VM Instance vmInstance = new Instance("vm" ).component( vmComponent ); Instance tomcatInstance = new Instance("tomcat" ).component( tomcatComponent ); InstanceHelpers.insertChild( vmInstance, tomcatInstance ); ApplicationTemplate app = new ApplicationTemplate( "app" ).version( "2.4" ).graphs( graphs ); app.getRootInstances().add( vmInstance ); Iterator<ModelError> iterator = RuntimeModelValidator.validate( app ).iterator(); Assert.assertEquals( ErrorCode.RM_INVALID_INSTANCE_PARENT, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); }
Assert.assertEquals( 3, result.applicationTemplate.getRootInstances().size()); for( Instance i : result.applicationTemplate.getRootInstances()) { if( "Apache VM".equals( i.getName())) { Assert.assertEquals( "VM", i.getComponent().getName());
app.getRootInstances().clear(); Assert.assertEquals( 0, InstanceHelpers.getAllInstances( app ).size());