ApplicationTemplateMngrImpl implements IApplicationTemplateMngr { @Override public ApplicationTemplate loadApplicationTemplate( File applicationFilesDirectory ) throws AlreadyExistingException, InvalidApplicationException, IOException, UnauthorizedActionException { synchronized( INSTALL_LOCK ) { this.logger.info( "Loading an application template from " + applicationFilesDirectory + "..." ); ApplicationLoadResult lr = RuntimeModelIo.loadApplication( applicationFilesDirectory ); if( RoboconfErrorHelpers.containsCriticalErrors( lr.getLoadErrors())) throw new InvalidApplicationException( lr.getLoadErrors()); Collection<RoboconfError> warnings = RoboconfErrorHelpers.findWarnings( lr.getLoadErrors()); for( String warningMsg : RoboconfErrorHelpers.formatErrors( warnings, null, true ).values()) this.logger.warning( warningMsg ); ApplicationTemplate tpl = lr.getApplicationTemplate(); if( this.templates.containsKey( tpl )) throw new AlreadyExistingException( tpl.getName()); Set<String> externExportPrefixes = new HashSet<> (); for( ApplicationTemplate otherTpl : this.templates.keySet()) { if( otherTpl.getExternalExportsPrefix() != null ) externExportPrefixes.add( otherTpl.getExternalExportsPrefix()); } if( externExportPrefixes.contains( tpl.getExternalExportsPrefix())) throw new IOException( "The external exports prefix is already used by another template." ); Set<String> newTargetIds = registerTargets( tpl ); File targetDirectory = ConfigurationUtils.findTemplateDirectory( tpl, this.configurationMngr.getWorkingDirectory()); try { if( ! applicationFilesDirectory.equals( targetDirectory )) { if( Utils.isAncestorFile( targetDirectory, applicationFilesDirectory )) throw new IOException( "Cannot move " + applicationFilesDirectory + " in Roboconf's work directory. Already a child directory." ); else Utils.copyDirectory( applicationFilesDirectory, targetDirectory ); } } catch( IOException e ) { unregisterTargets( newTargetIds ); throw e; } tpl.setDirectory( targetDirectory ); for( File targetDir : ResourceUtils.findScopedInstancesDirectories( tpl ).values()) Utils.deleteFilesRecursivelyAndQuietly( targetDir ); this.templates.put( tpl, Boolean.TRUE ); this.logger.info( "Application template " + tpl.getName() + " was successfully loaded." ); this.notificationMngr.applicationTemplate( tpl, EventType.CREATED ); return tpl; } } ApplicationTemplateMngrImpl(
			INotificationMngr notificationMngr,
			ITargetsMngr targetsMngr,
			IApplicationMngr applicationMngr,
			IConfigurationMngr configurationMngr ); @Override Set<ApplicationTemplate> getApplicationTemplates(); @Override ApplicationTemplate findTemplate( String name, String version ); @Override ApplicationTemplate loadApplicationTemplate( File applicationFilesDirectory ); @Override void deleteApplicationTemplate( String tplName, String tplVersion ); @Override void restoreTemplates();  }
@Test( expected = AlreadyExistingException.class ) public void testLoadApplicationTemplate_conflict() throws Exception { File directory = TestUtils.findApplicationDirectory( "lamp" ); Assert.assertTrue( directory.exists()); try { this.mngr.loadApplicationTemplate( directory ); } catch( Exception e ) { Assert.fail( "Loading the application the first time should not fail." ); } this.mngr.loadApplicationTemplate( directory ); }
@Test( expected = IOException.class ) public void testLoadApplicationTemplate_externalExportsPrefixIsAlreadyUsed() throws Exception { File originalDirectory = TestUtils.findApplicationDirectory( "lamp" ); Assert.assertTrue( originalDirectory.exists()); File directoryCopy = this.folder.newFolder(); Utils.copyDirectory( originalDirectory, directoryCopy ); File descriptorFile = new File( directoryCopy, Constants.PROJECT_DIR_DESC + "/" + Constants.PROJECT_FILE_DESCRIPTOR ); Assert.assertTrue( descriptorFile.exists()); ApplicationTemplateDescriptor desc = ApplicationTemplateDescriptor.load( descriptorFile ); desc.setExternalExportsPrefix( "for-test" ); ApplicationTemplateDescriptor.save( descriptorFile, desc ); try { this.mngr.loadApplicationTemplate( directoryCopy ); } catch( Exception e ) { Assert.fail( "Loading the application the first time should not fail." ); } desc.setVersion( "33.2" ); ApplicationTemplateDescriptor.save( descriptorFile, desc ); this.mngr.loadApplicationTemplate( directoryCopy ); }
@Test( expected = IOException.class ) public void testLoadApplicationTemplate_invalidDirectory() throws Exception { File source = TestUtils.findApplicationDirectory( "lamp" ); Assert.assertTrue( source.exists()); File apps = new File( this.dmDirectory, ConfigurationUtils.TEMPLATES ); File target = new File( apps, "Legacy LAMP - 1.0.1-SNAPSHOT/sub/dir" ); Utils.copyDirectory( source, target ); this.mngr.loadApplicationTemplate( target ); }
@Test( expected = InvalidApplicationException.class ) public void testApplicationTemplate_invalidTemplate() throws Exception { this.mngr.loadApplicationTemplate( this.folder.newFolder()); }