/** * Constructor. */ public ManagedApplication( Application application ) { Objects.requireNonNull( application ); Objects.requireNonNull( application.getTemplate()); this.application = application; this.scopedInstanceToAwaitingMessages = new HashMap<> (); }
public File getTemplateDirectory() { return this.application.getTemplate().getDirectory(); }
public Graphs getGraphs() { return this.application.getTemplate().getGraphs(); }
@Override public boolean isTemplateUsed( ApplicationTemplate tpl ) { boolean result = false; for( ManagedApplication ma : this.nameToManagedApplication.values()) { if( tpl.equals( ma.getApplication().getTemplate())) { result = true; break; } } return result; }
/** * Saves an application descriptor. * @param f the file where the properties will be saved * @param app an application (not null) * @throws IOException if the file could not be written */ public static void save( File f, Application app ) throws IOException { Properties properties = new Properties(); if( ! Utils.isEmptyOrWhitespaces( app.getDisplayName())) properties.setProperty( APPLICATION_NAME, app.getDisplayName()); if( ! Utils.isEmptyOrWhitespaces( app.getDescription())) properties.setProperty( APPLICATION_DESCRIPTION, app.getDescription()); if( app.getTemplate() != null ) { if( ! Utils.isEmptyOrWhitespaces( app.getTemplate().getName())) properties.setProperty( APPLICATION_TPL_NAME, app.getTemplate().getName()); if( ! Utils.isEmptyOrWhitespaces( app.getTemplate().getVersion())) properties.setProperty( APPLICATION_TPL_VERSION, app.getTemplate().getVersion()); } Utils.writePropertiesFile( properties, f ); } }
/** * Restores instances and set them in the application. * @param ma the application * @param configurationDirectory the configuration directory */ public static InstancesLoadResult restoreInstances( ManagedApplication ma ) { File sourceFile = new File( ma.getDirectory(), Constants.PROJECT_DIR_INSTANCES + "/" + INSTANCES_FILE ); Graphs graphs = ma.getApplication().getTemplate().getGraphs(); InstancesLoadResult result; if( sourceFile.exists()) result = RuntimeModelIo.loadInstances( sourceFile, sourceFile.getParentFile(), graphs, ma.getApplication().getName()); else result = new InstancesLoadResult(); return result; }
generator.writeStringField( DESC, app.getDescription()); if( app.getTemplate() != null ) { generator.writeStringField( APP_INST_TPL_NAME, app.getTemplate().getName()); if( app.getTemplate().getVersion() != null ) generator.writeStringField( APP_INST_TPL_VERSION, app.getTemplate().getVersion()); if( app.getTemplate().getExternalExportsPrefix() != null ) generator.writeStringField( APP_INST_TPL_EEP, app.getTemplate().getExternalExportsPrefix()); Map<String,String> externalExports = app.getTemplate().externalExports; if( ! externalExports.isEmpty()) generator.writeObjectField( EXT_VARS, new MapWrapper( externalExports ));
@Override public void copyOriginalMapping( Application app ) throws IOException { List<InstanceContext> keys = new ArrayList<> (); // Null <=> The default for the application keys.add( new InstanceContext( app.getTemplate(), (String) null )); // We can search defaults only for the existing instances for( Instance scopedInstance : InstanceHelpers.findAllScopedInstances( app )) keys.add( new InstanceContext( app.getTemplate(), scopedInstance )); // Copy mappings for the components for( Component comp : ComponentHelpers.findAllComponents( app )) { if( ComponentHelpers.isTarget( comp )) keys.add( new InstanceContext( app.getTemplate(), "@" + comp.getName())); } // Copy the associations when they exist for the template for( InstanceContext key : keys ) { String targetId = this.instanceToCachedId.get( key ); try { if( targetId != null ) associateTargetWith( targetId, app, key.getInstancePathOrComponentName()); } catch( UnauthorizedActionException e ) { // This method could be used to reset the mappings / associations. // However, this is not possible if the current instance is already deployed. // So, we just skip it. this.logger.severe( e.getMessage()); Utils.logException( this.logger, e ); } } }
@Override public List<TargetWrapperDescriptor> listPossibleTargets( AbstractApplication app ) { // Find the matching targets based on registered hints String key = new InstanceContext( app ).toString(); String tplKey = null; if( app instanceof Application ) tplKey = new InstanceContext(((Application) app).getTemplate()).toString(); List<File> targetDirectories = new ArrayList<> (); File dir = new File( this.configurationMngr.getWorkingDirectory(), ConfigurationUtils.TARGETS ); for( File f : Utils.listDirectories( dir )) { // If there is no hint for this target, then it is global. // We can list it. File hintsFile = new File( f, TARGETS_HINTS_FILE ); if( ! hintsFile.exists()) { targetDirectories.add( f ); continue; } // Otherwise, the key must exist in the file Properties props = Utils.readPropertiesFileQuietly( hintsFile, this.logger ); if( props.containsKey( key )) targetDirectories.add( f ); else if( tplKey != null && props.containsKey( tplKey )) targetDirectories.add( f ); } // Build the result return buildList( targetDirectories, app ); }
@Test public void testEqualsAndHashCode_3() { Application app1 = new Application( new TestApplicationTemplate()); Application app2 = new Application( "app", app1.getTemplate()); HashSet<Application> set = new HashSet<>( 2 ); set.add( app1 ); set.add( app2 ); Assert.assertEquals( 2, set.size()); }
@Test public void testEqualsAndHashCode_2() { Application app1 = new Application( new TestApplicationTemplate()); app1.setName( "app" ); Application app2 = new Application( app1.getTemplate()); app2.setName( "app" ); HashSet<Application> set = new HashSet<>( 2 ); set.add( app1 ); set.add( app2 ); Assert.assertEquals( 1, set.size()); }
throw new UnauthorizedActionException( "Application " + applicationName + " does not exist." ); if( ! externalExportPrefix.equals( app.getTemplate().getExternalExportsPrefix())) throw new UnauthorizedActionException( "Application " + applicationName + "'s template does not have " + externalExportPrefix + " as external exports prefix." );
@Override public void bindOrUnbindApplication( ManagedApplication ma, String externalExportPrefix, String applicationName, boolean bind ) throws UnauthorizedActionException, IOException { // Checks Application app = findApplicationByName( applicationName ); if( app == null ) throw new UnauthorizedActionException( "Application " + applicationName + " does not exist." ); if( ! externalExportPrefix.equals( app.getTemplate().getExternalExportsPrefix())) throw new UnauthorizedActionException( "Application " + applicationName + "'s template does not have " + externalExportPrefix + " as external exports prefix." ); // Update the model boolean notify = true; if( bind ) ma.getApplication().bindWithApplication( externalExportPrefix, applicationName ); else notify = ma.getApplication().unbindFromApplication( externalExportPrefix, applicationName ); // Update and propagate the modification if( notify ) { // Save the configuration ConfigurationUtils.saveApplicationBindings( ma.getApplication()); this.logger.fine( "External prefix " + externalExportPrefix + " is now bound to application " + applicationName + " in " + ma.getName() + "." ); // Notify the agents for( Instance inst : InstanceHelpers.findAllScopedInstances( ma.getApplication())) { MsgCmdChangeBinding msg = new MsgCmdChangeBinding( externalExportPrefix, ma.getApplication().getApplicationBindings().get( externalExportPrefix )); this.messagingMngr.sendMessageSafely( ma, inst, msg ); } } }
@Test public void testEquals() { Application app = new Application( "app", new ApplicationTemplate()); Assert.assertFalse( app.equals( null )); Assert.assertFalse( app.equals( new Application( app.getTemplate()))); Assert.assertFalse( app.equals( new Object())); Assert.assertEquals( app, app ); Assert.assertEquals( app, new Application( "app", app.getTemplate())); Assert.assertEquals( app, new Application( "app", new ApplicationTemplate( "whatever" ))); }