Skip to content

Commit

Permalink
fix: Update to JDK21 - EXO-71474 - Meeds-io/MIPs#91
Browse files Browse the repository at this point in the history
Remove usage of SecurityManager as it is deprecated for removal in jdk21
Remove also usage of classes
- SecurityHelper
- PrivilegedSystemHelper
- PrivilegedFileHelper
- SecureList
- SecureSet
- SecureCollections

These classes are here only to use securityManager, and as it is removed, it is no more necessary
Resolves Meeds-io/MIPs#91
  • Loading branch information
rdenarie authored May 22, 2024
1 parent bc74993 commit fb50e20
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@

import java.io.File;
import java.lang.ref.WeakReference;
import java.security.PrivilegedAction;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.*;

import org.exoplatform.commons.utils.PrivilegedFileHelper;
import org.exoplatform.commons.utils.SecurityHelper;
import org.exoplatform.container.ExoContainerContext;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
Expand Down Expand Up @@ -64,7 +61,7 @@ public void run() {
}
while ((file =
files.poll()) != null) {
PrivilegedFileHelper.delete(file);
file.delete();
}
}
};
Expand Down Expand Up @@ -102,13 +99,7 @@ public FileCleaner(String id, long timeout, boolean start) {
if (start)
start();

PrivilegedAction<Object> action = new PrivilegedAction<Object>() {
public Object run() {
registerShutdownHook();
return null;
}
};
SecurityHelper.doPrivilegedAction(action);
registerShutdownHook();

if (LOG.isDebugEnabled()) {
LOG.debug("FileCleaner instantiated name= " + getName() + " timeout= " + timeout);
Expand All @@ -119,7 +110,7 @@ public Object run() {
* @param file
*/
public void addFile(File file) {
if (PrivilegedFileHelper.exists(file)) {
if (file.exists()) {
files.offer(file);
}
}
Expand All @@ -141,18 +132,8 @@ public void halt() {
}
}
// Remove the hook for final cleaning up
SecurityHelper.doPrivilegedAction(new PrivilegedAction<Object>() {
public Void run() {
try {
Runtime.getRuntime().removeShutdownHook(hook);
} catch (IllegalStateException e) {
if (LOG.isTraceEnabled()) {
LOG.trace("An exception occurred: " + e.getMessage());
}
}
return null;
}
});
Runtime.getRuntime().removeShutdownHook(hook);

if (files != null && files.size() > 0)
LOG.warn("There are uncleared files: " + files.size());

Expand All @@ -167,16 +148,16 @@ protected void callPeriodically() throws Exception {
File file = null;
Set<File> notRemovedFiles = new HashSet<File>();
while ((file = files.poll()) != null) {
if (PrivilegedFileHelper.exists(file)) {
if (!PrivilegedFileHelper.delete(file)) {
if (file.exists()) {
if (!file.delete()) {
notRemovedFiles.add(file);

if (LOG.isDebugEnabled())
LOG.debug("Could not delete " + (file.isDirectory() ? "directory" : "file")
+ ". Will try next time: " + PrivilegedFileHelper.getAbsolutePath(file));
+ ". Will try next time: " + file.getAbsolutePath());
} else if (LOG.isDebugEnabled()) {
LOG.debug((file.isDirectory() ? "Directory" : "File") + " deleted : "
+ PrivilegedFileHelper.getAbsolutePath(file));
+ file.getAbsolutePath());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@
import org.apache.commons.logging.LogFactory;
import org.exoplatform.commons.file.model.FileInfo;
import org.exoplatform.commons.file.model.FileItem;
import org.exoplatform.commons.utils.SecurityHelper;
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.container.xml.ValueParam;

import java.io.*;
import java.net.URL;
import java.security.PrivilegedAction;

/**
* This class provide file system implementation of the File RDBMS API.
Expand Down Expand Up @@ -163,17 +161,7 @@ public String getFilePath(String name) throws IOException {
}

public void remove(String name) throws IOException {
PrivilegedAction<Boolean> action = new PrivilegedAction<Boolean>() {
public Boolean run() {
try {
((TreeFile) getFile(name)).delete();
} catch (IOException e) {
return false;
}
return true;
}
};
SecurityHelper.doPrivilegedAction(action);
((TreeFile) getFile(name)).delete();
}

@Override
Expand All @@ -182,17 +170,12 @@ public boolean remove(FileInfo fileInfo) throws IOException {
if (file == null || !file.exists()) {
throw new FileNotFoundException("Cannot delete file " + getFilePath(fileInfo) + " since it does not exist");
} else {
PrivilegedAction<Boolean> action = new PrivilegedAction<Boolean>() {
public Boolean run() {
try {
((TreeFile) getFile(fileInfo.getChecksum())).delete();
} catch (IOException e) {
return false;
}
return true;
}
};
return SecurityHelper.doPrivilegedAction(action);
try {
((TreeFile) getFile(fileInfo.getChecksum())).delete();
} catch (IOException e) {
return false;
}
return true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;

import org.exoplatform.commons.utils.SecurityHelper;

/**
* This is an utility class : File operation Created by The eXo Platform SAS
Expand Down Expand Up @@ -65,24 +60,7 @@ private static byte[] createBuffer(int preferredSize) {
* occurred.
*/
public static boolean createNewFile(final File file) throws IOException {
PrivilegedExceptionAction<Boolean> action = new PrivilegedExceptionAction<Boolean>() {
public Boolean run() throws Exception {
return file.createNewFile();
}
};
try {
return SecurityHelper.doPrivilegedExceptionAction(action);
} catch (PrivilegedActionException pae) {
Throwable cause = pae.getCause();

if (cause instanceof IOException) {
throw (IOException) cause;
} else if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else {
throw new RuntimeException(cause);
}
}
return file.createNewFile();
}

/**
Expand All @@ -93,12 +71,7 @@ public Boolean run() throws Exception {
* @return boolean
*/
public static boolean exists(final File file) {
PrivilegedAction<Boolean> action = new PrivilegedAction<Boolean>() {
public Boolean run() {
return file.exists();
}
};
return SecurityHelper.doPrivilegedAction(action);
return file.exists();
}

/**
Expand All @@ -108,12 +81,7 @@ public Boolean run() {
* @return boolean
*/
public static boolean mkdirs(final File file) {
PrivilegedAction<Boolean> action = new PrivilegedAction<Boolean>() {
public Boolean run() {
return file.mkdirs();
}
};
return SecurityHelper.doPrivilegedAction(action);
return file.mkdirs();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.exoplatform.commons.file.services.job.FileStorageCleanJob;
import org.exoplatform.commons.file.storage.DataStorage;
import org.exoplatform.commons.utils.PropertyManager;
import org.exoplatform.commons.utils.SecurityHelper;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.picocontainer.Startable;
Expand All @@ -33,8 +32,6 @@
import org.exoplatform.management.jmx.annotations.Property;

import java.io.*;
import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -74,57 +71,52 @@ public String checkFileStorage() {
if (FileStorageCleanJob.isEnabled().get()) {
FileStorageCleanJob.setEnabled(false);
}
SecurityHelper.doPrivilegedAction(new PrivilegedAction<Boolean>() {
public Boolean run() {
try {
LOG.info("Start File Storage Check Consistency");
boolean isConsistent = true;
int offset = 0;
boolean hasNext = true;
while (hasNext) {
List<FileInfo> list = dataStorage.getAllFilesInfo(offset, pageSize);

if (list == null || list.isEmpty()) {
break;
}
if (list.size() < pageSize) {
hasNext = false;
}
for (FileInfo fileInfo : list) {
String checksum = fileInfo.getChecksum();
if (checksum != null && !checksum.isEmpty()) {
if (!binaryProvider.exists(checksum)) {
isConsistent = false;
report.writeLine("File not exist in file storage File ID : " + fileInfo.getId() + " File name : "
+ fileInfo.getName() + " , Path : " + binaryProvider.getFilePath(fileInfo.getChecksum()));
}
} else {
isConsistent = false;
report.writeLine("File metaData with empty checksum File ID : " + fileInfo.getId() + " File name : "
+ fileInfo.getName() + " , Path : ");
}
try {
LOG.info("Start File Storage Check Consistency");
boolean isConsistent = true;
int offset = 0;
boolean hasNext = true;
while (hasNext) {
List<FileInfo> list = dataStorage.getAllFilesInfo(offset, pageSize);

if (list == null || list.isEmpty()) {
break;
}
if (list.size() < pageSize) {
hasNext = false;
}
for (FileInfo fileInfo : list) {
String checksum = fileInfo.getChecksum();
if (checksum != null && !checksum.isEmpty()) {
if (!binaryProvider.exists(checksum)) {
isConsistent = false;
report.writeLine("File not exist in file storage File ID : " + fileInfo.getId() + " File name : "
+ fileInfo.getName() + " , Path : " + binaryProvider.getFilePath(fileInfo.getChecksum()));
}
offset += pageSize;
}
if (isConsistent) {
report.writeLine(REPORT_CONSISTENT_MESSAGE);
LOG.info("Finish File Storage Check Consistency : " + REPORT_CONSISTENT_MESSAGE);
} else {
report.writeLine(REPORT_NOT_CONSISTENT_MESSAGE);
LOG.info("Finish File Storage Check Consistency : " + REPORT_NOT_CONSISTENT_MESSAGE);
isConsistent = false;
report.writeLine("File metaData with empty checksum File ID : " + fileInfo.getId() + " File name : "
+ fileInfo.getName() + " , Path : ");
}
} catch (Exception e) {
try {
report.writeLine("Processing File Storage Check Consistency Error ");
report.writeStackTrace(e);
} catch (IOException e1) {
LOG.error(e1.getMessage());
}
LOG.error(e.getMessage());
}
return true;
offset += pageSize;
}
if (isConsistent) {
report.writeLine(REPORT_CONSISTENT_MESSAGE);
LOG.info("Finish File Storage Check Consistency : " + REPORT_CONSISTENT_MESSAGE);
} else {
report.writeLine(REPORT_NOT_CONSISTENT_MESSAGE);
LOG.info("Finish File Storage Check Consistency : " + REPORT_NOT_CONSISTENT_MESSAGE);
}
} catch (Exception e) {
try {
report.writeLine("Processing File Storage Check Consistency Error ");
report.writeStackTrace(e);
} catch (IOException e1) {
LOG.error(e1.getMessage());
}
});
LOG.error(e.getMessage());
}
} catch (Exception ex) {
LOG.error(ex.getMessage());
return "Failed Operation";
Expand Down Expand Up @@ -169,14 +161,9 @@ public Report() throws IOException {
"report-filesStorage-" + new SimpleDateFormat("dd-MMM-yy-HH-mm").format(new Date())
+ ".txt");

SecurityHelper.doPrivilegedIOExceptionAction(new PrivilegedExceptionAction<Void>() {
public Void run() throws IOException {
reportPath = reportFile.getAbsolutePath();
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(reportPath)));
reportPath = reportFile.getAbsolutePath();
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(reportPath)));

return null;
}
});

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
import org.hibernate.cfg.Configuration;

import org.exoplatform.commons.exception.ObjectNotFoundException;
import org.exoplatform.commons.utils.PrivilegedSystemHelper;
import org.exoplatform.commons.utils.SecurityHelper;
import org.exoplatform.container.ExoContainer;
import org.exoplatform.container.component.ComponentRequestLifecycle;
import org.exoplatform.container.xml.InitParams;
Expand All @@ -62,11 +60,7 @@ public class CustomHibernateServiceImpl implements HibernateService, ComponentRe
public CustomHibernateServiceImpl(InitParams initParams) {
threadLocal_ = new ThreadLocal<Session>();
PropertiesParam param = initParams.getPropertiesParam("hibernate.properties");
conf_ = SecurityHelper.doPrivilegedAction(new PrivilegedAction<IdmHibernateConfiguration>() {
public IdmHibernateConfiguration run() {
return new IdmHibernateConfiguration();
}
});
conf_ = new IdmHibernateConfiguration();
Iterator<?> properties = param.getPropertyIterator();
while (properties.hasNext()) {
Property p = (Property) properties.next();
Expand All @@ -76,7 +70,7 @@ public IdmHibernateConfiguration run() {
// Replace the potential "java.io.tmpdir" variable in the connection URL
String connectionURL = conf_.getProperty("hibernate.connection.url");
if (connectionURL != null) {
connectionURL = connectionURL.replace("${java.io.tmpdir}", PrivilegedSystemHelper.getProperty("java.io.tmpdir"));
connectionURL = connectionURL.replace("${java.io.tmpdir}", System.getProperty("java.io.tmpdir"));
conf_.setProperty("hibernate.connection.url", connectionURL);
}
}
Expand Down
Loading

0 comments on commit fb50e20

Please sign in to comment.