@Test public void testInstall() throws Exception { System.setProperty("karaf.etc", "./target/test-classes"); ConfigMBeanImpl configMBean = new ConfigMBeanImpl(); configMBean.install("file:./target/test-classes/test.cfg", "foo.cfg", true); File output = new File("target/test-classes/foo.cfg"); Assert.assertTrue(output.exists()); StringBuilder builder = new StringBuilder(); BufferedReader reader = new BufferedReader(new FileReader(output)); String line = null; while ((line = reader.readLine()) != null) { builder.append(line).append("\n"); } Assert.assertTrue(builder.toString().contains("foo=bar")); }
@Override public void install(String url, String finalname, boolean override) throws MBeanException { if (finalname.contains("..")) { throw new IllegalArgumentException("For security reason, relative path is not allowed in config file final name"); } try { File etcFolder = new File(System.getProperty("karaf.etc")); File file = new File(etcFolder, finalname); if (file.exists()) { if (!override) { throw new IllegalArgumentException("Configuration file {} already exists " + finalname); } } try (InputStream is = new BufferedInputStream(new URL(url).openStream())) { if (!file.exists()) { File parentFile = file.getParentFile(); if (parentFile != null) { parentFile.mkdirs(); } file.createNewFile(); } try (FileOutputStream fop = new FileOutputStream(file)) { StreamUtils.copy(is, fop); } } catch (RuntimeException | MalformedURLException e) { throw e; } } catch (Exception e) { throw new MBeanException(null, e.toString()); } }
ConfigMBeanImpl extends StandardMBean implements ConfigMBean { @Override public void install(String url, String finalname, boolean override) throws MBeanException { if (finalname.contains("..")) { throw new IllegalArgumentException("For security reason, relative path is not allowed in config file final name"); } try { File etcFolder = new File(System.getProperty("karaf.etc")); File file = new File(etcFolder, finalname); if (file.exists()) { if (!override) { throw new IllegalArgumentException("Configuration file {} already exists " + finalname); } } try (InputStream is = new BufferedInputStream(new URL(url).openStream())) { if (!file.exists()) { File parentFile = file.getParentFile(); if (parentFile != null) { parentFile.mkdirs(); } file.createNewFile(); } try (FileOutputStream fop = new FileOutputStream(file)) { StreamUtils.copy(is, fop); } } catch (RuntimeException | MalformedURLException e) { throw e; } } catch (Exception e) { throw new MBeanException(null, e.toString()); } } }
ConfigMBeanImpl extends StandardMBean implements ConfigMBean { @Override public void install(String url, String finalname, boolean override) throws MBeanException { if (finalname.contains("..")) { throw new IllegalArgumentException("For security reason, relative path is not allowed in config file final name"); } try { File etcFolder = new File(System.getProperty("karaf.etc")); File file = new File(etcFolder, finalname); if (file.exists()) { if (!override) { throw new IllegalArgumentException("Configuration file {} already exists " + finalname); } } try (InputStream is = new BufferedInputStream(new URL(url).openStream())) { if (!file.exists()) { File parentFile = file.getParentFile(); if (parentFile != null) { parentFile.mkdirs(); } file.createNewFile(); } try (FileOutputStream fop = new FileOutputStream(file)) { StreamUtils.copy(is, fop); } } catch (RuntimeException | MalformedURLException e) { throw e; } } catch (Exception e) { throw new MBeanException(null, e.toString()); } } ConfigMBeanImpl(); }
ConfigMBeanImpl extends StandardMBean implements ConfigMBean { @Override public void install(String url, String finalname, boolean override) throws MBeanException { if (finalname.contains("..")) { throw new IllegalArgumentException("For security reason, relative path is not allowed in config file final name"); } try { File etcFolder = new File(System.getProperty("karaf.etc")); File file = new File(etcFolder, finalname); if (file.exists()) { if (!override) { throw new IllegalArgumentException("Configuration file {} already exists " + finalname); } } try (InputStream is = new BufferedInputStream(new URL(url).openStream())) { if (!file.exists()) { File parentFile = file.getParentFile(); if (parentFile != null) { parentFile.mkdirs(); } file.createNewFile(); } try (FileOutputStream fop = new FileOutputStream(file)) { StreamUtils.copy(is, fop); } } catch (RuntimeException | MalformedURLException e) { throw e; } } catch (Exception e) { throw new MBeanException(null, e.toString()); } } ConfigMBeanImpl(); @Override List<String> getConfigs(); @Override void create(String pid); @Override void install(String url, String finalname, boolean override); @Override void delete(String pid); @Override boolean exists(String pid); @Override @SuppressWarnings("rawtypes") Map<String, String> listProperties(String pid); @Override void deleteProperty(String pid, String key); @Override void appendProperty(String pid, String key, String value); @Override void setProperty(String pid, String key, String value); @Override String getProperty(String pid, String key); @Override void update(String pid, Map<String, String> properties); @Override void append(String pid, Map<String, String> properties); @Override void delete(String pid, List<String> properties); void setConfigRepo(ConfigRepository configRepo); @Override String createFactoryConfiguration(String factoryPid); @Override String createFactoryConfiguration(String factoryPid, String alias); @Override String createFactoryConfiguration(String factoryPid, Map<String, String> properties); @Override String createFactoryConfiguration(String factoryPid, String alias, Map<String, String> properties); }
ConfigMBeanImpl extends StandardMBean implements ConfigMBean { @Override public void install(String url, String finalname, boolean override) throws MBeanException { if (finalname.contains("..")) { throw new IllegalArgumentException("For security reason, relative path is not allowed in config file final name"); } try { File etcFolder = new File(System.getProperty("karaf.etc")); File file = new File(etcFolder, finalname); if (file.exists()) { if (!override) { throw new IllegalArgumentException("Configuration file {} already exists " + finalname); } } try (InputStream is = new BufferedInputStream(new URL(url).openStream())) { if (!file.exists()) { File parentFile = file.getParentFile(); if (parentFile != null) { parentFile.mkdirs(); } file.createNewFile(); } try (FileOutputStream fop = new FileOutputStream(file)) { StreamUtils.copy(is, fop); } } catch (RuntimeException | MalformedURLException e) { throw e; } } catch (Exception e) { throw new MBeanException(null, e.toString()); } } ConfigMBeanImpl(); @Override List<String> getConfigs(); @Override void create(String pid); @Override void install(String url, String finalname, boolean override); @Override void delete(String pid); @Override boolean exists(String pid); @Override @SuppressWarnings("rawtypes") Map<String, String> listProperties(String pid); @Override void deleteProperty(String pid, String key); @Override void appendProperty(String pid, String key, String value); @Override void setProperty(String pid, String key, String value); @Override String getProperty(String pid, String key); @Override void update(String pid, Map<String, String> properties); @Override void append(String pid, Map<String, String> properties); @Override void delete(String pid, List<String> properties); void setConfigRepo(ConfigRepository configRepo); @Override String createFactoryConfiguration(String factoryPid); @Override String createFactoryConfiguration(String factoryPid, String alias); @Override String createFactoryConfiguration(String factoryPid, Map<String, String> properties); @Override String createFactoryConfiguration(String factoryPid, String alias, Map<String, String> properties);  }