Skip to content

Commit

Permalink
Removed "AccessController.doPrivileged()" marked as "deprecated for r…
Browse files Browse the repository at this point in the history
…emoval" (#1267) (#1290)

* Removed "AccessController.doPrivileged()" due to marked on Java-side as
"deprecated for removal" (#1267)

* Updated URL class loader and test case class
  • Loading branch information
speckyspooky committed May 25, 2023
1 parent c2f850a commit 15d8a1e
Show file tree
Hide file tree
Showing 41 changed files with 438 additions and 1,225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

import java.io.File;
import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.FileHandler;
Expand Down Expand Up @@ -63,6 +60,11 @@ private void addLogHandler() {
}
}

/**
* Set state directory
*
* @param sStateDir
*/
public static void setStateDir(String sStateDir) {
stateDir = sStateDir;
}
Expand Down Expand Up @@ -91,7 +93,8 @@ public JavaUtilLoggerImpl(String name) {
/**
* The constructor.
*
* @param name
* @param name logger name
* @param verboseLevel set verbose level
*/
public JavaUtilLoggerImpl(String name, int verboseLevel) {
this.logger = Logger.getLogger(name);
Expand Down Expand Up @@ -205,13 +208,20 @@ private static Level toJavaUtilLevel(int chartLevel) {
return Level.SEVERE;
}

/**
* Init file handler for logging
*
* @param sLogFolder log file folder
* @param level log level
* @throws SecurityException security exception
* @throws IOException IO exception
*/
public static void initFileHandler(String sLogFolder, final Level level) throws SecurityException, IOException {
if (sLogFolder == null) {
if (stateDir == null) {
return;
} else {
sLogFolder = stateDir;
}
sLogFolder = stateDir;
}

if (sLogFolder.length() > 0 && sLogFolder.lastIndexOf(File.separator) == sLogFolder.length() - 1) {
Expand All @@ -222,21 +232,12 @@ public static void initFileHandler(String sLogFolder, final Level level) throws
final String sDir = sLogFolder;

try {
fileHandler = AccessController.doPrivileged(new PrivilegedExceptionAction<FileHandler>() {

@Override
public FileHandler run() throws Exception {
Level logLevel = level != null ? level : Level.FINEST;

FileHandler fileHandler = new FileHandler(sDir + File.separator + sName + ".log", true); //$NON-NLS-1$
fileHandler.setFormatter(new SimpleFormatter());
fileHandler.setLevel(logLevel);
return fileHandler;
}
Level logLevel = level != null ? level : Level.FINEST;
fileHandler = new FileHandler(sDir + File.separator + sName + ".log", true); //$NON-NLS-1$
fileHandler.setFormatter(new SimpleFormatter());
fileHandler.setLevel(logLevel);

});
} catch (PrivilegedActionException e) {
Exception typedException = e.getException();
} catch (Exception typedException) {
if (typedException instanceof SecurityException) {
throw (SecurityException) typedException;
} else if (typedException instanceof IOException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.prefs.BackingStoreException;
import java.util.prefs.InvalidPreferencesFormatException;
import java.util.prefs.Preferences;
Expand Down Expand Up @@ -201,19 +197,11 @@ public void write() throws IOException, BackingStoreException {
*/
public void read() throws IOException, InvalidPreferencesFormatException {
try {
pr = AccessController.doPrivileged(new PrivilegedExceptionAction<Preferences>() {

@Override
public Preferences run() throws Exception {
try (FileInputStream fis = new FileInputStream(sLocation)) {
Preferences.importPreferences(fis);
return Preferences.userRoot();
}
}

});
} catch (PrivilegedActionException e) {
Exception typedException = e.getException();
try (FileInputStream fis = new FileInputStream(sLocation)) {
Preferences.importPreferences(fis);
pr = Preferences.userRoot();
}
} catch (Exception typedException) {
if (typedException instanceof IOException) {
throw (IOException) typedException;
} else if (typedException instanceof InvalidPreferencesFormatException) {
Expand All @@ -228,14 +216,6 @@ public Preferences run() throws Exception {
*/
private boolean exists() {
final File f = new File(sLocation);

return AccessController.doPrivileged(new PrivilegedAction<Boolean>() {

@Override
public Boolean run() {
return (f.exists() && f.isFile());
}

});
return (f.exists() && f.isFile());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

package org.eclipse.birt.chart.log;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.logging.Level;
import java.util.logging.SimpleFormatter;
import java.util.logging.StreamHandler;
Expand Down Expand Up @@ -84,19 +82,13 @@ synchronized public static ILogger getLogger(String name) {

private static StreamHandler getTracingHandler() {
if (tracingHandler == null) {
tracingHandler = AccessController.doPrivileged(new PrivilegedAction<StreamHandler>() {

@Override
public StreamHandler run() {
StreamHandler handler = new StreamHandler(System.out, new SimpleFormatter());
try {
tracingHandler.setLevel(Level.ALL);
} catch (SecurityException e) {
e.printStackTrace();
}
return handler;
}
});
StreamHandler handler = new StreamHandler(System.out, new SimpleFormatter());
try {
tracingHandler.setLevel(Level.ALL);
} catch (SecurityException e) {
e.printStackTrace();
}
tracingHandler = handler;
}
return tracingHandler;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -314,54 +310,47 @@ public Chart read(URI uri) throws IOException {
public Chart readEmbedded(final URI uri) throws IOException {
Chart chart = null;
try {
chart = AccessController.doPrivileged(new PrivilegedExceptionAction<Chart>() {

@Override
public Chart run() throws Exception {
// Create and setup local ResourceSet
ResourceSet rsChart = new ResourceSetImpl();
rsChart.getResourceFactoryRegistry().getExtensionToFactoryMap().put("chart", //$NON-NLS-1$
new ModelResourceFactoryImpl());

// Create resources to represent the disk files to be used
// to store the
// models
Resource rChart = null;

StringBuffer sb = null;
InputStream fis = null;
FileWriter writer = null;
try {
fis = new FileInputStream(uri.toFileString());
sb = getChartStringFromStream(fis);

File fTmp = File.createTempFile("_ChartResource", ".chart"); //$NON-NLS-1$ //$NON-NLS-2$
writer = new FileWriter(fTmp);
writer.write(sb.toString());
writer.flush();

URI uriEmbeddedModel = URI.createFileURI(fTmp.getAbsolutePath());
rChart = rsChart.getResource(uriEmbeddedModel, true);

rChart.load(Collections.EMPTY_MAP);

// Delete the temporary file once the model is loaded.
if (fTmp.exists()) {
fTmp.delete();
}
return (Chart) rChart.getContents().get(0);
} finally {
if (writer != null) {
writer.close();
}
if (fis != null) {
fis.close();
}
}
// Create and setup local ResourceSet
ResourceSet rsChart = new ResourceSetImpl();
rsChart.getResourceFactoryRegistry().getExtensionToFactoryMap().put("chart", //$NON-NLS-1$
new ModelResourceFactoryImpl());

// Create resources to represent the disk files to be used
// to store the
// models
Resource rChart = null;

StringBuffer sb = null;
InputStream fis = null;
FileWriter writer = null;
try {
fis = new FileInputStream(uri.toFileString());
sb = getChartStringFromStream(fis);

File fTmp = File.createTempFile("_ChartResource", ".chart"); //$NON-NLS-1$ //$NON-NLS-2$
writer = new FileWriter(fTmp);
writer.write(sb.toString());
writer.flush();

URI uriEmbeddedModel = URI.createFileURI(fTmp.getAbsolutePath());
rChart = rsChart.getResource(uriEmbeddedModel, true);

rChart.load(Collections.EMPTY_MAP);

// Delete the temporary file once the model is loaded.
if (fTmp.exists()) {
fTmp.delete();
}
chart = (Chart) rChart.getContents().get(0);
} finally {
if (writer != null) {
writer.close();
}
});
} catch (PrivilegedActionException e) {
Exception typedException = e.getException();
if (fis != null) {
fis.close();
}
}
} catch (Exception typedException) {
if (typedException instanceof IOException) {
throw (IOException) typedException;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -648,24 +646,19 @@ public IDataPointDefinition getDataPointDefinition(Class<?> cSeries) throws Char
* @return display name of series.
*/
public String getSeriesDisplayName(final String seriesClassName) {
return AccessController.doPrivileged(new PrivilegedAction<String>() {

@Override
public String run() {
String sDisplayName = seriesClassName;
try {
Class<?> seriesClass = Class.forName(seriesClassName);
Method createMethod = seriesClass.getDeclaredMethod("create", new Class[] {}); //$NON-NLS-1$
Series newSeries = (Series) createMethod.invoke(seriesClass, new Object[] {});
Method mDisplayName = seriesClass.getDeclaredMethod("getDisplayName", new Class[] {}); //$NON-NLS-1$
Object oName = mDisplayName.invoke(newSeries, new Object[] {});
sDisplayName = (String) oName;
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
return sDisplayName;
}
});
String sDisplayName = seriesClassName;
try {
Class<?> seriesClass = Class.forName(seriesClassName);
Method createMethod = seriesClass.getDeclaredMethod("create", new Class[] {}); //$NON-NLS-1$
Series newSeries = (Series) createMethod.invoke(seriesClass, new Object[] {});
Method mDisplayName = seriesClass.getDeclaredMethod("getDisplayName", new Class[] {}); //$NON-NLS-1$
Object oName = mDisplayName.invoke(newSeries, new Object[] {});
sDisplayName = (String) oName;
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
e.printStackTrace();
}
return sDisplayName;
}

/**
Expand Down

0 comments on commit 15d8a1e

Please sign in to comment.