/** * Determines whether an instances is associated with the "target" installer or not. * @param instance an instance (not null) * @return true if it is associated with the "target" installer, false otherwise */ public static boolean isTarget( Instance instance ) { return instance.getComponent() != null && ComponentHelpers.isTarget( instance.getComponent()); }
/** * Finds the resource directory for an instance. * @param applicationFilesDirectory the application's directory * @param instance an instance * @return a non-null file (that may not exist) */ public static File findInstanceResourcesDirectory( File applicationFilesDirectory, Instance instance ) { return findInstanceResourcesDirectory( applicationFilesDirectory, instance.getComponent()); }
/** * Finds the directory where an agent stores the files for a given instance. * @param instance an instance (not null) * @return a file (not null, but may not exist) */ public static File findInstanceDirectoryOnAgent( Instance instance ) { String path = InstanceHelpers.computeInstancePath( instance ); path = path.substring( 1 ).replace( '/', '_' ).replace( ' ', '_' ); String installerName = ComponentHelpers.findComponentInstaller( instance.getComponent()); return new File( Constants.WORK_DIRECTORY_AGENT, installerName + "/" + path ); }
/** * Constructor. * @param instance */ public Import( Instance instance ) { this( InstanceHelpers.computeInstancePath( instance ), instance.getComponent() == null ? null : instance.getComponent().getName(), InstanceHelpers.findAllExportedVariables( instance )); }
/** * Finds instances by component name. * @param application an application (not null) * @param componentName a component name (not null) * @return a non-null list of instances */ public static List<Instance> findInstancesByComponentName( AbstractApplication application, String componentName ) { List<Instance> result = new ArrayList<> (); for( Instance inst : getAllInstances( application )) { if( componentName.equals( inst.getComponent().getName())) result.add( inst ); } return result; }
/** * 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; }
/** * Constructor. * @param instance */ public MsgCmdAddInstance( Instance instance ) { this.parentInstancePath = instance.getParent() == null ? null : InstanceHelpers.computeInstancePath( instance.getParent()); this.instanceName = instance.getName(); this.componentName = instance.getComponent() != null ? instance.getComponent().getName() : null; this.channels = instance.channels; this.data = instance.data; this.overridenExports = instance.overriddenExports; }
/** * Builds a map with the variables defined by this class. * @param instance a non-null instance * @return a non-null map where all the properties here are mapped to their values for this instance */ public static Map<String,String> buildReferenceMap( Instance instance ) { Map<String,String> result = new HashMap<> (); String instancePath = InstanceHelpers.computeInstancePath( instance ); result.put( ROBOCONF_INSTANCE_NAME, instance.getName()); result.put( ROBOCONF_INSTANCE_PATH, instancePath ); result.put( ROBOCONF_COMPONENT_NAME, instance.getComponent().getName()); result.put( ROBOCONF_CLEAN_INSTANCE_PATH, cleanInstancePath( instancePath )); result.put( ROBOCONF_CLEAN_REVERSED_INSTANCE_PATH, cleanReversedInstancePath( instancePath )); return result; }
/** * Constructor. * @param app * @param commandFile */ public Context( AbstractApplication app, File commandFile ) { this.app = app; this.commandFile = commandFile; for( Instance instance : InstanceHelpers.getAllInstances( app )) this.instancePathToComponentName.put( InstanceHelpers.computeInstancePath( instance ), instance.getComponent().getName()); }
/** * Constructor. * @param instance */ public MsgCmdAddInstance( Instance instance ) { this.parentInstancePath = instance.getParent() == null ? null : InstanceHelpers.computeInstancePath( instance.getParent()); this.instanceName = instance.getName(); this.componentName = instance.getComponent() != null ? instance.getComponent().getName() : null; this.channels = instance.channels; this.data = instance.data; this.overridenExports = instance.overriddenExports; }
/** * 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; }
@Override public synchronized void generateRandomValues( Application application, Instance instance ) { // Exported variables that are random will be set a value for( ExportedVariable var : instance.getComponent().exportedVariables.values()) { // Not random? if( ! var.isRandom()) continue; // Port if( var.getRandomKind() == RandomKind.PORT ) { // Acknowledge: verify a random value was not already set on it. // Otherwise, generate a random port and associate it. if( ! acknowledgePort( application, instance, var.getName())) generateRandomPort( application, instance, var.getName()); } } // Save the updated model ConfigurationUtils.saveInstances( application ); }
@Test public void testFindAllChildren_testApp() { TestApplicationTemplate app = new TestApplicationTemplate(); Collection<Component> children = ComponentHelpers.findAllChildren( app.getTomcat().getComponent()); Assert.assertEquals( 1, children.size()); Assert.assertTrue( children.contains( app.getWar().getComponent())); children = ComponentHelpers.findAllChildren( app.getTomcatVm().getComponent()); Assert.assertEquals( 2, children.size()); Assert.assertTrue( children.contains( app.getTomcat().getComponent())); Assert.assertTrue( children.contains( app.getMySql().getComponent())); children = ComponentHelpers.findAllChildren( app.getMySqlVm().getComponent()); Assert.assertEquals( 2, children.size()); Assert.assertTrue( children.contains( app.getTomcat().getComponent())); Assert.assertTrue( children.contains( app.getMySql().getComponent())); Assert.assertEquals( 0, ComponentHelpers.findAllChildren( app.getMySql().getComponent()).size()); Assert.assertEquals( 0, ComponentHelpers.findAllChildren( app.getWar().getComponent()).size()); }
@Test public void testConstructor() { List<Instance> instances = InstanceHelpers.getAllInstances( this.app ); Assert.assertEquals( instances.size(), this.context.instancePathToComponentName.size()); for( Instance instance : instances ) { String path = InstanceHelpers.computeInstancePath( instance ); Assert.assertEquals( instance.getComponent().getName(), this.context.instancePathToComponentName.get( path )); } }
@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" )); }
@Test public void testFindScopedInstancesDirectories() throws Exception { final File appDir = this.folder.newFolder(); TestApplication app = new TestApplication(); app.setDirectory( appDir ); Assert.assertEquals( 0, ResourceUtils.findScopedInstancesDirectories( app ).size()); File vmDir = new File( appDir, Constants.PROJECT_DIR_GRAPH + "/" + app.getMySqlVm().getComponent().getName()); Assert.assertTrue( vmDir.mkdirs()); Map<Component,File> map = ResourceUtils.findScopedInstancesDirectories( app ); Assert.assertEquals( 1, map.size()); Assert.assertEquals( vmDir, map.get( app.getMySqlVm().getComponent())); } }
@Test public void testResolveInstance() { Assert.assertEquals( "vm", this.context.resolveInstance( "/tomcat-vm" ).getComponent().getName()); Assert.assertEquals( "tomcat", this.context.resolveInstance( "/tomcat-vm/tomcat-server" ).getComponent().getName()); Assert.assertNull( this.context.resolveInstance( "/tomcat-vm/invalid" )); this.context.instancePathToComponentName.put( "not a path", "whatever" ); Assert.assertNull( this.context.resolveInstance( "not a path" )); Assert.assertNull( this.context.resolveInstance( "" )); this.context.instancePathToComponentName.put( "", "vm" ); Assert.assertNull( this.context.resolveInstance( "" )); }
@Test public void testFindComponentFrom() { TestApplicationTemplate app = new TestApplicationTemplate(); Component comp = app.getTomcatVm().getComponent(); Assert.assertNull( ComponentHelpers.findComponentFrom( comp, "inexisting" )); Assert.assertNull( ComponentHelpers.findComponentFrom( null, "inexisting" )); Assert.assertEquals( comp, ComponentHelpers.findComponentFrom( comp, comp.getName())); Component targetComp = app.getTomcat().getComponent(); Assert.assertEquals( targetComp, ComponentHelpers.findComponentFrom( comp, targetComp.getName())); targetComp = app.getWar().getComponent(); Assert.assertEquals( targetComp, ComponentHelpers.findComponentFrom( comp, targetComp.getName())); }
@Test public void testChain() { Instance inst = new Instance().name( "ins" ).status( InstanceStatus.DEPLOYING ).component( null ).parent( null ); Assert.assertEquals( 0, inst.channels.size()); Assert.assertEquals( "ins", inst.getName()); Assert.assertEquals( InstanceStatus.DEPLOYING, inst.getStatus()); Assert.assertNull( inst.getComponent()); Assert.assertNull( inst.getParent()); Assert.assertEquals( 1, inst.channel( "woo" ).channels.size()); Assert.assertEquals( 2, inst.channel( "yeah" ).channels.size()); Assert.assertEquals( 2, inst.channel( "woo" ).channels.size()); }
@Test public void testIsTarget() { Instance inst = new Instance( "i" ); Assert.assertFalse( InstanceHelpers.isTarget( inst )); inst.setComponent( new Component( "comp" ).installerName( "whatever" )); Assert.assertFalse( InstanceHelpers.isTarget( inst )); inst.getComponent().setInstallerName( Constants.TARGET_INSTALLER ); Assert.assertTrue( InstanceHelpers.isTarget( inst )); }