Skip to content

Commit

Permalink
[BASE-64] Applying code modernization #resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgrosso committed Dec 29, 2024
1 parent 953a831 commit 3c206c3
Show file tree
Hide file tree
Showing 14 changed files with 177 additions and 201 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
*/
package net.tirasa.connid.commons.scripted;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import org.identityconnectors.common.logging.Log;
import org.identityconnectors.framework.spi.AbstractConfiguration;
import org.identityconnectors.framework.spi.ConfigurationProperty;
Expand Down Expand Up @@ -380,9 +381,9 @@ private void checkFileIsReadable(final String type, final String fileName) {
if (fileName == null) {
LOG.ok("{0} Script Filename is null", type);
} else {
File file = new File(AbstractScriptedConnector.resolveVariables(fileName));
Path file = Path.of(AbstractScriptedConnector.resolveVariables(fileName));
try {
if (file.canRead()) {
if (Files.isReadable(file)) {
LOG.ok("{0} is readable", fileName);
} else {
throw new IllegalArgumentException("Can't read " + fileName);
Expand All @@ -392,5 +393,4 @@ private void checkFileIsReadable(final String type, final String fileName) {
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import static net.tirasa.connid.commons.scripted.Constants.MSG_BLANK_RESULT_HANDLER;
import static net.tirasa.connid.commons.scripted.Constants.MSG_INVALID_SCRIPT;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -158,7 +158,7 @@ private ScriptExecutor getScriptExecutor(String script, String scriptFileName) {

try {
if (scriptFileName != null) {
scriptCode = IOUtil.readFileUTF8(new File(resolveVariables(scriptFileName)));
scriptCode = IOUtil.readFileUTF8(Path.of(resolveVariables(scriptFileName)));
}
if (scriptCode.length() > 0) {
scriptExec = factory.newScriptExecutor(getClass().getClassLoader(), scriptCode, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@
import groovy.util.ConfigObject;
import groovy.util.ConfigSlurper;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -56,7 +59,6 @@
import org.identityconnectors.test.common.TestHelpers;

/**
* <p>
* Default implementation of {@link DataProvider}. It uses ConfigSlurper from
* Groovy to parse the property file.
* The groovy files are read as classpath resources using following paths :
Expand Down Expand Up @@ -248,7 +250,7 @@ private void initQueriedPropsDump() {
String pOut = System.getProperty(PARAM_QUERIED_PROPERTY_OUT_FILE);
if (StringUtil.isNotBlank(pOut)) {
try {
_queriedPropsOutFile = new File(pOut);
_queriedPropsOutFile = Path.of(pOut).toFile();
if (!_queriedPropsOutFile.exists()) {
_queriedPropsOutFile.createNewFile();
}
Expand All @@ -274,7 +276,7 @@ private void initSnapshot() {
String pOut = System.getProperty(PARAM_PROPERTY_OUT_FILE);
if (StringUtil.isNotBlank(pOut)) {
try {
_propertyOutFile = new File(pOut);
_propertyOutFile = Path.of(pOut).toFile();
if (!_propertyOutFile.exists()) {
_propertyOutFile.createNewFile();
}
Expand Down Expand Up @@ -884,7 +886,11 @@ Object writeDataToFile() {
// parse configuration information to flatten
String result = flatten(configObject);

try (FileWriter fw = new FileWriter(_propertyOutFile, StandardCharsets.UTF_8, true)) {
try (Writer fw = Files.newBufferedWriter(
_propertyOutFile.toPath(),
StandardCharsets.UTF_8,
StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {

fw.append("\n\n\n ============================ NEW TEST ==================== \n\n\n");
fw.append(result);
} catch (IOException e) {
Expand All @@ -903,7 +909,11 @@ private String writeQueriedDumpToFile() {
return null;
}

try (FileWriter fw = new FileWriter(_queriedPropsOutFile, StandardCharsets.UTF_8, true)) {
try (Writer fw = Files.newBufferedWriter(
_queriedPropsOutFile.toPath(),
StandardCharsets.UTF_8,
StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {

fw.append("<dumpSummary>\n");
// MISSING PROPS
fw.append(" <missingProperties>\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.File;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -1093,10 +1094,10 @@ private static ConnectorInfoManager getLocalManager(final DataProvider dataProvi
ConnectorInfoManager manager = null;

// try to load bundleJar property (which should be set by ant)
File bundleJar = new File(((String) dataProvider.getTestSuiteAttribute("bundleJar")).trim());
assertTrue(bundleJar.isFile(), "BundleJar does not exist: " + bundleJar.getAbsolutePath());
Path bundleJar = Path.of(((String) dataProvider.getTestSuiteAttribute("bundleJar")).trim());
assertTrue(Files.isRegularFile(bundleJar), "BundleJar does not exist: " + bundleJar.toString());
try {
manager = fact.getLocalManager(bundleJar.toURI().toURL());
manager = fact.getLocalManager(bundleJar.toUri().toURL());
} catch (MalformedURLException ex) {
throw ContractException.wrap(ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@
* "Portions Copyrighted [year] [name of copyright owner]"
* ====================
* Portions Copyrighted 2010-2014 ForgeRock AS.
* Portions Copyrighted 2010-2018 ConnId
* Portions Copyrighted 2010 ConnId
*/
package org.identityconnectors.framework.impl.api.local;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -90,8 +91,8 @@ private static List<WorkingBundleInfo> expandBundles(final List<URL> bundleURLs)
WorkingBundleInfo info = null;
try {
if ("file".equals(url.getProtocol())) {
final File file = new File(url.toURI());
if (file.isDirectory()) {
Path file = Path.of(url.toURI());
if (Files.isDirectory(file)) {
info = processDirectory(file);
}
}
Expand All @@ -106,32 +107,27 @@ private static List<WorkingBundleInfo> expandBundles(final List<URL> bundleURLs)
return rv;
}

private static WorkingBundleInfo processDirectory(final File dir) throws ConfigurationException {
final WorkingBundleInfo info = new WorkingBundleInfo(dir.getAbsolutePath());
private static WorkingBundleInfo processDirectory(final Path dir) throws ConfigurationException {
final WorkingBundleInfo info = new WorkingBundleInfo(dir.toAbsolutePath().toString());
try {
// easy case - nothing needs to be copied
final File manifest = new File(dir, "META-INF/MANIFEST.MF");
InputStream in = null;
try {
in = new FileInputStream(manifest);
Path manifest = dir.resolve("META-INF/MANIFEST.MF");
try (InputStream in = Files.newInputStream(manifest)) {
Manifest rawManifest = new Manifest(in);
ConnectorBundleManifestParser parser =
new ConnectorBundleManifestParser(info.getOriginalLocation(), rawManifest);
info.setManifest(parser.parse());
} finally {
IOUtil.quietClose(in);
}
info.getImmediateClassPath().add(dir.toURI().toURL());
final List<String> bundleContents = listBundleContents(dir);
info.getImmediateBundleContents().addAll(bundleContents);
final File libDir = new File(dir, "lib");
info.getImmediateClassPath().add(dir.toUri().toURL());
info.getImmediateBundleContents().addAll(listBundleContents(dir.toFile()));
final File libDir = dir.resolve("lib").toFile();
if (libDir.exists()) {
final List<URL> libURLs = BundleLibSorter.getSortedURLs(libDir);
libURLs.forEach((lib) -> {
info.getEmbeddedBundles().add(processURL(lib, false));
});
}
final File nativeDir = new File(dir, "native");
final File nativeDir = dir.resolve("native").toFile();
if (nativeDir.exists()) {
for (File file : BundleLibSorter.getSortedFiles(nativeDir)) {
if (file.isFile()) {
Expand Down Expand Up @@ -328,7 +324,7 @@ public static APIConfigurationImpl createDefaultAPIConfiguration(final LocalConn
try {
final Class<? extends Connector> connectorClass = localInfo.getConnectorClass();
final APIConfigurationImpl rv = new APIConfigurationImpl();
final Configuration config =
final Configuration config =
localInfo.getConnectorConfigurationClass().getDeclaredConstructor().newInstance();
final boolean pooling = PoolableConnector.class.isAssignableFrom(connectorClass);
rv.setConnectorPoolingSupported(pooling);
Expand Down Expand Up @@ -450,16 +446,16 @@ public File copyStreamToFile(final InputStream stream) throws IOException {
final File bundleDir = getBundleTempDir();
File candidate;
do {
candidate = new File(bundleDir, "file-" + nextRandom());
candidate = bundleDir.toPath().resolve("file-" + nextRandom()).toFile();
} while (!candidate.createNewFile());
candidate.deleteOnExit();
copyStream(stream, candidate);
copyStream(stream, candidate.toPath());
return candidate;
}

public File copyStreamToFile(final InputStream stream, final String name) throws IOException {
final File bundleDir = getBundleTempDir();
final File newFile = new File(bundleDir, name);
final File newFile = bundleDir.toPath().resolve(name).toFile();
if (newFile.exists()) {
throw new IOException("File " + newFile + " already exists");
}
Expand All @@ -472,12 +468,12 @@ public File copyStreamToFile(final InputStream stream, final String name) throws
parent = parent.getParentFile();
}
newFile.deleteOnExit();
copyStream(stream, newFile);
copyStream(stream, newFile.toPath());
return newFile;
}

private void copyStream(final InputStream stream, final File toFile) throws IOException {
try (FileOutputStream out = new FileOutputStream(toFile)) {
private static void copyStream(final InputStream stream, final Path toFile) throws IOException {
try (OutputStream out = Files.newOutputStream(toFile)) {
IOUtil.copyFile(stream, out);
}
}
Expand All @@ -486,13 +482,13 @@ private File getBundleTempDir() throws IOException {
if (_bundleTempDir != null) {
return _bundleTempDir;
}
final File tempDir = new File(System.getProperty("java.io.tmpdir"));
final File tempDir = Path.of(System.getProperty("java.io.tmpdir")).toFile();
if (!tempDir.exists()) {
throw new IOException("Temporary directory " + tempDir + " does not exist");
}
File candidate;
do {
candidate = new File(tempDir, "bundle-" + nextRandom());
candidate = tempDir.toPath().resolve("bundle-" + nextRandom()).toFile();
} while (!candidate.mkdir());
candidate.deleteOnExit();
_bundleTempDir = candidate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -260,7 +261,7 @@ public void serialize(final Object object, final ObjectEncoder encoder) {
@Override
public Object deserialize(final ObjectDecoder decoder) {
final String val = decoder.readStringContents();
return new File(val);
return Path.of(val).toFile();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,16 @@
package org.identityconnectors.framework.server;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.logging.LogManager;

import org.identityconnectors.common.IOUtil;
import org.identityconnectors.common.logging.Log;
import org.identityconnectors.common.security.SecurityUtil;
Expand Down Expand Up @@ -112,13 +109,13 @@ public static void main(String[] args) throws Exception {
}
Properties properties = IOUtil.loadPropertiesFile(propertiesFileName);
properties.put(PROP_KEY, SecurityUtil.computeBase64SHA1Hash(key.toCharArray()));
IOUtil.storePropertiesFile(new File(propertiesFileName), properties);
IOUtil.storePropertiesFile(Path.of(propertiesFileName), properties);
} else if (cmd.equalsIgnoreCase("-setDefaults")) {
if (propertiesFileName == null || key != null) {
usage();
return;
}
IOUtil.extractResourceToFile(Main.class, "connectorserver.properties", new File(propertiesFileName));
IOUtil.extractResourceToFile(Main.class, "connectorserver.properties", Path.of(propertiesFileName));
} else {
usage();
}
Expand Down Expand Up @@ -151,7 +148,6 @@ private static void run(Properties properties) throws Exception {

if (loggerClass == null) {


System.out.println("Logger Class is null");
loggerClass = DEFAULT_LOG_SPI;
}
Expand All @@ -173,9 +169,9 @@ private static void run(Properties properties) throws Exception {

connectorServer = ConnectorServer.newInstance();
connectorServer.setPort(port);
connectorServer.setBundleURLs(buildBundleURLs(new File(bundleDirStr)));
connectorServer.setBundleURLs(buildBundleURLs(Path.of(bundleDirStr)));
if (libDirStr != null) {
ClassLoader cl = buildLibClassLoader(new File(libDirStr));
ClassLoader cl = buildLibClassLoader(Path.of(libDirStr));
connectorServer.setBundleParentClassLoader(cl);

}
Expand Down Expand Up @@ -226,29 +222,28 @@ private static void ensureLoggingNotInitialized() throws Exception {
}
}

private static List<URL> buildBundleURLs(File dir) throws MalformedURLException {
List<URL> rv = getJarFiles(dir);
private static List<URL> buildBundleURLs(final Path dir) throws MalformedURLException {
List<URL> rv = getJarFiles(dir.toFile());
if (rv.isEmpty()) {
getLog().warn("No bundles found in the bundles directory.");
}
return rv;
}

private static ClassLoader buildLibClassLoader(File dir) throws MalformedURLException {
List<URL> jars = getJarFiles(dir);
private static ClassLoader buildLibClassLoader(final Path dir) throws MalformedURLException {
List<URL> jars = getJarFiles(dir.toFile());
if (!jars.isEmpty()) {
return new URLClassLoader(jars.toArray(new URL[jars.size()]),
ConnectorInfoManagerFactory.class.getClassLoader());
return new URLClassLoader(jars.toArray(URL[]::new), ConnectorInfoManagerFactory.class.getClassLoader());
}
return null;

}

private static List<URL> getJarFiles(File dir) throws MalformedURLException {
private static List<URL> getJarFiles(final File dir) throws MalformedURLException {
if (!dir.isDirectory()) {
throw new ConnectorException(dir.getPath() + " does not exist");
}
List<URL> rv = new ArrayList<URL>();
List<URL> rv = new ArrayList<>();
for (File bundle : dir.listFiles()) {
if (bundle.getName().endsWith(".jar")) {
rv.add(bundle.toURI().toURL());
Expand Down
Loading

0 comments on commit 3c206c3

Please sign in to comment.