Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
package org.apache.karaf.bundle.command;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;

import org.apache.karaf.shell.api.action.Argument;
import org.apache.karaf.shell.api.action.Command;
Expand Down Expand Up @@ -65,12 +64,12 @@ protected Object doExecute(Bundle bundle) throws Exception {
}

private void update(Bundle bundle, URL location) throws IOException, BundleException {
try (InputStream is = location.openStream()) {
try (var is = location.openStream()) {
if (raw) {
bundle.update(is);
} else {
File file = BundleUtils.fixBundleWithUpdateLocation(is, location.toString());
try (FileInputStream fis = new FileInputStream(file)) {
try (var fis = Files.newInputStream(file.toPath())) {
bundle.update(fis);
}
file.delete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
package org.apache.karaf.bundle.core.internal;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -151,7 +150,7 @@ private void updateBundleIfNecessary(File localRepository, List<Bundle> updated,
throws BundleException, IOException {
File location = getBundleExternalLocation(localRepository, bundle);
if (location != null && location.exists() && location.lastModified() > bundle.getLastModified()) {
try (InputStream is = new FileInputStream(location)) {
try (var is = Files.newInputStream(location.toPath())) {
logger.info("[Watch] Updating watched bundle: {} ({})", bundle.getSymbolicName(), bundle.getVersion());
if (bundle.getHeaders().get(Constants.FRAGMENT_HOST) != null) {
logger.info("[Watch] Bundle {} is a fragment, so it's not stopped", bundle.getSymbolicName());
Expand All @@ -162,7 +161,7 @@ private void updateBundleIfNecessary(File localRepository, List<Bundle> updated,
String updateLocation = getLocation(bundle);
if (!updateLocation.equals(bundle.getLocation())) {
File file = BundleUtils.fixBundleWithUpdateLocation(is, updateLocation);
try (FileInputStream fis = new FileInputStream(file)) {
try (var fis = Files.newInputStream(file.toPath())) {
bundle.update(fis);
}
file.delete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package org.apache.karaf.bundle.core.internal;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Dictionary;

import javax.xml.stream.XMLInputFactory;
Expand Down Expand Up @@ -76,20 +76,20 @@ static String getLocalRepoFromConfig(Dictionary<String, Object> dict) {
if (path == null) {
String settings = (String) dict.get("org.ops4j.pax.url.mvn.settings");
if (settings != null) {
path = getLocalRepositoryFromSettings(new File(settings));
path = getLocalRepositoryFromSettings(Path.of(settings));
}
}
}
return path;
}

private static String getLocalRepositoryFromSettings(File file) {
private static String getLocalRepositoryFromSettings(Path file) {
XMLStreamReader reader = null;
try (InputStream fin = new FileInputStream(file)) {
try (var is = Files.newInputStream(file)) {
XMLInputFactory factory = XMLInputFactory.newFactory();
factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
reader = factory.createXMLStreamReader(fin);
reader = factory.createXMLStreamReader(is);
int event;
String elementName = null;
while ((event = reader.next()) != XMLStreamConstants.END_DOCUMENT) {
Expand Down
12 changes: 8 additions & 4 deletions client/src/main/java/org/apache/karaf/client/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.Console;
import java.io.FileInputStream;
import java.io.IOError;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Proxy;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyPair;
import java.time.Duration;
Expand Down Expand Up @@ -76,7 +76,7 @@ public static void main(String[] args) throws Exception {
if (config.getFile() != null) {
StringBuilder sb = new StringBuilder();
sb.setLength(0);
try (Reader reader = new BufferedReader(new InputStreamReader(new FileInputStream(config.getFile())))) {
try (var reader = Files.newBufferedReader(Path.of(config.getFile()))) {
for (int c = reader.read(); c >= 0; c = reader.read()) {
sb.append((char) c);
}
Expand All @@ -85,7 +85,7 @@ public static void main(String[] args) throws Exception {
} else if (config.isBatch()) {
StringBuilder sb = new StringBuilder();
sb.setLength(0);
Reader reader = new BufferedReader(new InputStreamReader(System.in));
var reader = new BufferedReader(new InputStreamReader(System.in));
for (int c = reader.read(); c >= 0; c = reader.read()) {
sb.append((char) c);
}
Expand All @@ -106,6 +106,7 @@ public static void main(String[] args) throws Exception {
public void welcome(ClientSession s, String banner, String lang) {
System.out.println(banner);
}

@Override
public String[] interactive(ClientSession s, String name, String instruction, String lang, String[] prompt, boolean[] echo) {
String[] answers = new String[prompt.length];
Expand All @@ -125,13 +126,16 @@ public String[] interactive(ClientSession s, String name, String instruction, St
return null;
}
}

@Override
public boolean isInteractionAllowed(ClientSession session) {
return true;
}

@Override
public void serverVersionInfo(ClientSession session, List<String> lines) {
}

@Override
public String getUpdatedPassword(ClientSession session, String prompt, String lang) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@
import org.apache.karaf.shell.api.action.Command;
import org.apache.karaf.shell.api.action.Option;
import org.apache.karaf.shell.api.action.lifecycle.Service;
import org.apache.karaf.util.StreamUtils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;

@Command(scope = "config", name = "install", description = "Install a cfg file in the Karaf etc folder.")
@Service
Expand Down Expand Up @@ -60,17 +57,15 @@ public Object execute() throws Exception {
System.out.println("Creating configuration file " + finalname);
}

try (InputStream is = new BufferedInputStream(new URL(url).openStream())) {
try (var is = 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);
}
Files.copy(is, file.toPath());
} catch (RuntimeException | MalformedURLException e) {
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
*/
package org.apache.karaf.config.core.impl;

import java.io.*;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Dictionary;
import java.util.HashMap;
Expand All @@ -31,7 +33,6 @@
import org.apache.felix.utils.properties.TypedProperties;
import org.apache.karaf.config.core.ConfigMBean;
import org.apache.karaf.config.core.ConfigRepository;
import org.apache.karaf.util.StreamUtils;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.service.cm.Configuration;

Expand Down Expand Up @@ -96,17 +97,15 @@ public void install(String url, String finalname, boolean override) throws MBean
}
}

try (InputStream is = new BufferedInputStream(new URL(url).openStream())) {
try (var is = 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);
}
Files.copy(is, file.toPath());
} catch (RuntimeException | MalformedURLException e) {
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.karaf.config.core.impl;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -128,15 +127,15 @@ public boolean exists(String pid) throws Exception {
return true;
}

private File getCfgFileFromProperties(Dictionary<String, Object> properties) throws URISyntaxException, MalformedURLException {
private static File getCfgFileFromProperties(Dictionary<String, Object> properties) throws URISyntaxException, MalformedURLException {
if (properties != null) {
Object val = properties.get(FILEINSTALL_FILE_NAME);
return getCfgFileFromProperty(val);
}
return null;
}

private File getCfgFileFromProperty(Object val) throws URISyntaxException, MalformedURLException {
private static File getCfgFileFromProperty(Object val) throws URISyntaxException, MalformedURLException {
if (val instanceof URL) {
return new File(((URL) val).toURI());
}
Expand Down Expand Up @@ -227,7 +226,9 @@ public ConfigurationAdmin getConfigAdmin() {
static TypedProperties load(File file) throws IOException {
TypedProperties props = new TypedProperties();
if (file.toURI().toString().endsWith(".json")) {
Hashtable<String, Object> configuration = Configurations.buildReader().build(new FileReader(file)).readConfiguration();
Hashtable<String, Object> configuration = Configurations.buildReader()
.build(Files.newBufferedReader(file.toPath()))
.readConfiguration();
for (String key : configuration.keySet()) {
props.put(key, configuration.get(key));
}
Expand All @@ -239,7 +240,7 @@ static TypedProperties load(File file) throws IOException {

void store(TypedProperties properties, File file) throws IOException {
if (file.toURI().toString().endsWith(".json")) {
Configurations.buildWriter().build(new FileWriter(file)).writeConfiguration(new Hashtable<>(properties));
Configurations.buildWriter().build(Files.newBufferedWriter(file.toPath())).writeConfiguration(new Hashtable<>(properties));
} else {
properties.save(file);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Dictionary;
import java.util.Hashtable;
Expand All @@ -61,7 +61,7 @@ public boolean canHandle(File artifact) {
return artifact.getName().endsWith("." + getExtension());
}

private String getExtension() {
private static String getExtension() {
String extension = (System.getenv(EXT_ENV_VAR) != null) ? System.getenv(EXT_ENV_VAR) : null;
extension = (System.getProperty(EXT_SYS_PROP) != null) ? System.getProperty(EXT_SYS_PROP) : extension;
if (extension == null) {
Expand Down Expand Up @@ -91,7 +91,9 @@ private void setConfig(File artifact) throws Exception {
Configuration configuration = getConfiguration(toConfigKey(artifact), configurationPID);
Dictionary<String, Object> props = configuration.getProperties();
Hashtable<String, Object> old = props != null ? new Hashtable<>(new DictionaryAsMap<>(props)) : null;
Hashtable<String, Object> properties = Configurations.buildReader().build(new FileReader(artifact)).readConfiguration();
Hashtable<String, Object> properties = Configurations.buildReader()
.build(Files.newBufferedReader(artifact.toPath()))
.readConfiguration();
if (old != null) {
old.remove(DirectoryWatcher.FILENAME);
old.remove(Constants.SERVICE_PID);
Expand Down Expand Up @@ -162,7 +164,7 @@ public void configurationEvent(ConfigurationEvent event) {
}
}

private File getCfgFileFromProperty(Object val) throws URISyntaxException, MalformedURLException {
private static File getCfgFileFromProperty(Object val) throws URISyntaxException, MalformedURLException {
if (val instanceof URL) {
return new File(((URL) val).toURI());
}
Expand Down Expand Up @@ -210,7 +212,7 @@ Configuration findExistingConfiguration(String configKey) throws Exception {
}
}

private String escapeFilterValue(String s) {
private static String escapeFilterValue(String s) {
return s.replaceAll("[(]", "\\\\(").
replaceAll("[)]", "\\\\)").
replaceAll("[=]", "\\\\=").
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
import org.junit.Assert;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.nio.file.Files;

public class ConfigMBeanImplTest {

Expand All @@ -44,10 +43,11 @@ public void testInstall() throws Exception {
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");
try (var reader = Files.newBufferedReader(output.toPath())) {
String line = null;
while ((line = reader.readLine()) != null) {
builder.append(line).append("\n");
}
}
Assert.assertTrue(builder.toString().contains("foo=bar"));
}
Expand Down
Loading
Loading