AgentUtils { public static void injectConfigurations( String karafEtc, String applicationName, String scopedInstancePath, String domain, String ipAddress ) { File injectionDir = new File( karafEtc, INJECTED_CONFIGS_DIR ); if( injectionDir.isDirectory()) { for( File source : Utils.listAllFiles( injectionDir, ".cfg.tpl" )) { try { File target = new File( karafEtc, source.getName().replaceFirst( "\\.tpl$", "" )); if( Constants.KARAF_CFG_FILE_AGENT.equalsIgnoreCase( target.getName())) continue; String content = Utils.readFileContent( source ); content = content.replace( "<domain>", domain ); content = content.replace( "<application-name>", applicationName ); content = content.replace( "<scoped-instance-path>", scopedInstancePath ); content = content.replace( "<ip-address>", ipAddress ); Utils.writeStringInto( content, target ); } catch( IOException e ) { Logger logger = Logger.getLogger( AgentUtils.class.getName()); logger.severe( "A configuration file could not be injected from " + source.getName()); Utils.logException( logger, e ); } } } } private  AgentUtils(); static boolean isValidIP( String ip ); static void copyInstanceResources( Instance instance, Map<String,byte[]> fileNameToFileContent ); static void executeScriptResources( File scriptsDir ); static void deleteInstanceResources( Instance instance ); static void changeRoboconfLogLevel( String logLevel, String etcDir ); static Map<String,byte[]> collectLogs( String karafData ); static String findIpAddress( String networkInterface ); static void injectConfigurations(
			String karafEtc,
			String applicationName,
			String scopedInstancePath,
			String domain,
			String ipAddress ); static final String INJECTED_CONFIGS_DIR; }
@Test public void testInjectConfigurations_invalidEtc() throws Exception { File karafEtc = this.folder.newFile(); AgentUtils.injectConfigurations( karafEtc.getAbsolutePath(), "app", "/vm", "default", "127.0.0.1" ); }
@Test public void testInjectConfigurations_emptyEtc() throws Exception { File karafEtc = this.folder.newFolder(); Assert.assertEquals( 0, karafEtc.listFiles().length ); AgentUtils.injectConfigurations( karafEtc.getAbsolutePath(), "app", "/vm", "default", "127.0.0.1" ); Assert.assertEquals( 0, karafEtc.listFiles().length ); }
@Test public void testInjectConfigurations_fullSample() throws Exception { File karafEtc = this.folder.newFolder(); File injectionDir = new File( karafEtc, AgentUtils.INJECTED_CONFIGS_DIR ); Assert.assertTrue( injectionDir.mkdirs()); Utils.writeStringInto( "ip = <ip-address>\napp = <application-name>\n", new File( injectionDir, "valid1.cfg.tpl" )); Utils.writeStringInto( "I am <scoped-instance-path>", new File( injectionDir, "valid2.cfg.tpl" )); Utils.writeStringInto( "ip = <ip-address>\napp = <application-name>", new File( injectionDir, "not-a-template.cfg" )); Utils.writeStringInto( "I am <scoped-instance-path>", new File( injectionDir, Constants.KARAF_CFG_FILE_AGENT + ".tpl" )); Assert.assertEquals( 1, karafEtc.listFiles().length ); AgentUtils.injectConfigurations( karafEtc.getAbsolutePath(), "app", "/vm", "default", "127.0.0.1" ); Assert.assertEquals( 3, karafEtc.listFiles().length ); File f = new File( karafEtc, "valid1.cfg" ); Assert.assertTrue( f.isFile()); Assert.assertEquals( "ip = 127.0.0.1\napp = app\n", Utils.readFileContent( f )); f = new File( karafEtc, "valid2.cfg" ); Assert.assertTrue( f.isFile()); Assert.assertEquals( "I am /vm", Utils.readFileContent( f )); }
@Test public void testInjectConfigurations_exceptionOnConflict() throws Exception { File karafEtc = this.folder.newFolder(); File injectionDir = new File( karafEtc, AgentUtils.INJECTED_CONFIGS_DIR ); Assert.assertTrue( injectionDir.mkdirs()); File conflict = new File( karafEtc, "valid1.cfg" ); Assert.assertTrue( conflict.mkdir()); Utils.writeStringInto( "ip = <ip-address>\napp = <application-name>\n", new File( injectionDir, "valid1.cfg.tpl" )); Assert.assertEquals( 2, karafEtc.listFiles().length ); AgentUtils.injectConfigurations( karafEtc.getAbsolutePath(), "app", "/vm", "default", "127.0.0.1" ); Assert.assertEquals( 2, karafEtc.listFiles().length ); File f = new File( karafEtc, "valid1.cfg" ); Assert.assertTrue( f.isDirectory()); }