List of usage examples for java.security AccessController doPrivileged
@CallerSensitive public static <T> T doPrivileged(PrivilegedExceptionAction<T> action) throws PrivilegedActionException
From source file:Main.java
/** * Extracts a field from a class using reflection. * * @param clazz from which to get the field object * @param name the name of the field object * @return the field object.//from www . j ava2 s . co m * @throws PrivilegedActionException */ static Field getField(final Class<?> clazz, final String name) throws PrivilegedActionException { final PrivilegedExceptionAction<Field> action = new PrivilegedExceptionAction<Field>() { public Field run() throws Exception { Field field = clazz.getDeclaredField(name); field.setAccessible(true); return field; } }; return AccessController.doPrivileged(action); }
From source file:Main.java
/** * Gets the value of a static field.// w w w. ja va 2 s . c o m * * @param clazz from which to get the field value * @param name the name of the field * @return the value of the field. * @throws PrivilegedActionException */ static <T> T getStaticFieldValue(final Class<?> clazz, final String name) throws PrivilegedActionException { final PrivilegedExceptionAction<T> action = new PrivilegedExceptionAction<T>() { @SuppressWarnings("unchecked") public T run() throws Exception { Field field = clazz.getDeclaredField(name); field.setAccessible(true); return (T) field.get(null); } }; return AccessController.doPrivileged(action); }
From source file:ReflectionExecutor.java
/** * Execution of reflection with {@link ReflectionAction}. * /* w w w . ja v a 2s . c o m*/ * @param <T> * @param action * @return */ public static <T> T doSafeAction(final ReflectionAction<T> action) { final SecurityManager securityManager = System.getSecurityManager(); if (securityManager == null) { try { return action.run(); } catch (Exception e) { throw new RuntimeException(e); } } try { return AccessController.doPrivileged(new PrivilegedExceptionAction<T>() { @Override public T run() throws Exception { return action.run(); } }); } catch (PrivilegedActionException e) { Throwable cause = e.getCause(); if (InvocationTargetException.class.isInstance(cause)) { InvocationTargetException e1 = (InvocationTargetException) cause; throw new Exception(e1.getTargetException()); } throw new Exception(cause); } }
From source file:groovyx.osgi.test.LocalFileSystemIvyRepository.java
public static File getFileFromPath(String path) { // use privileged action to get user home String userHome = AccessController.doPrivileged(new PrivilegedAction<String>() { public String run() { return System.getProperty(USER_HOME_PROPERTY); }//from w w w.jav a 2 s. c om }); File home = new File(userHome); return new File(home, path); }
From source file:com.paulwithers.bp106.DemoUtils.java
public static String convertObjectToString(final Object o, final ToStringStyle style) { String retVal_ = ""; try {/* ww w .j ava2s .c o m*/ retVal_ = AccessController.doPrivileged(new PrivilegedExceptionAction<String>() { public String run() throws Exception { return ReflectionToStringBuilder.toString(o, style); } }); } catch (AccessControlException e) { e.printStackTrace(); } catch (PrivilegedActionException e) { e.printStackTrace(); } return retVal_; }
From source file:SecuritySupport.java
static ClassLoader getSystemClassLoader() { return (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() { public Object run() { ClassLoader cl = null; try { cl = ClassLoader.getSystemClassLoader(); } catch (SecurityException ex) { }// w w w . ja v a 2s .c o m return cl; } }); }
From source file:com.mgreau.jboss.as7.cli.CliLauncher.java
public static void main(String[] args) throws Exception { int exitCode = 0; CommandContext cmdCtx = null;//from w ww.j a va 2 s .c o m boolean gui = false; String appName = ""; try { String argError = null; List<String> commands = null; File file = null; boolean connect = false; String defaultControllerProtocol = "http-remoting"; String defaultControllerHost = null; int defaultControllerPort = -1; boolean version = false; String username = null; char[] password = null; int connectionTimeout = -1; //App deployment boolean isAppDeployment = false; final Properties props = new Properties(); for (String arg : args) { if (arg.startsWith("--controller=") || arg.startsWith("controller=")) { final String fullValue; final String value; if (arg.startsWith("--")) { fullValue = arg.substring(13); } else { fullValue = arg.substring(11); } final int protocolEnd = fullValue.lastIndexOf("://"); if (protocolEnd == -1) { value = fullValue; } else { value = fullValue.substring(protocolEnd + 3); defaultControllerProtocol = fullValue.substring(0, protocolEnd); } String portStr = null; int colonIndex = value.lastIndexOf(':'); if (colonIndex < 0) { // default port defaultControllerHost = value; } else if (colonIndex == 0) { // default host portStr = value.substring(1); } else { final boolean hasPort; int closeBracket = value.lastIndexOf(']'); if (closeBracket != -1) { //possible ip v6 if (closeBracket > colonIndex) { hasPort = false; } else { hasPort = true; } } else { //probably ip v4 hasPort = true; } if (hasPort) { defaultControllerHost = value.substring(0, colonIndex).trim(); portStr = value.substring(colonIndex + 1).trim(); } else { defaultControllerHost = value; } } if (portStr != null) { int port = -1; try { port = Integer.parseInt(portStr); if (port < 0) { argError = "The port must be a valid non-negative integer: '" + args + "'"; } else { defaultControllerPort = port; } } catch (NumberFormatException e) { argError = "The port must be a valid non-negative integer: '" + arg + "'"; } } } else if ("--connect".equals(arg) || "-c".equals(arg)) { connect = true; } else if ("--version".equals(arg)) { version = true; } else if ("--gui".equals(arg)) { gui = true; } else if (arg.startsWith("--appDeployment=") || arg.startsWith("appDeployment=")) { isAppDeployment = true; appName = arg.startsWith("--") ? arg.substring(16) : arg.substring(14); } else if (arg.startsWith("--file=") || arg.startsWith("file=")) { if (file != null) { argError = "Duplicate argument '--file'."; break; } if (commands != null) { argError = "Only one of '--file', '--commands' or '--command' can appear as the argument at a time."; break; } final String fileName = arg.startsWith("--") ? arg.substring(7) : arg.substring(5); if (!fileName.isEmpty()) { file = new File(fileName); if (!file.exists()) { argError = "File " + file.getAbsolutePath() + " doesn't exist."; break; } } else { argError = "Argument '--file' is missing value."; break; } } else if (arg.startsWith("--commands=") || arg.startsWith("commands=")) { if (file != null) { argError = "Only one of '--file', '--commands' or '--command' can appear as the argument at a time."; break; } if (commands != null) { argError = "Duplicate argument '--command'/'--commands'."; break; } final String value = arg.startsWith("--") ? arg.substring(11) : arg.substring(9); commands = Util.splitCommands(value); } else if (arg.startsWith("--command=") || arg.startsWith("command=")) { if (file != null) { argError = "Only one of '--file', '--commands' or '--command' can appear as the argument at a time."; break; } if (commands != null) { argError = "Duplicate argument '--command'/'--commands'."; break; } final String value = arg.startsWith("--") ? arg.substring(10) : arg.substring(8); commands = Collections.singletonList(value); } else if (arg.startsWith("--user=")) { username = arg.startsWith("--") ? arg.substring(7) : arg.substring(5); } else if (arg.startsWith("--password=")) { password = (arg.startsWith("--") ? arg.substring(11) : arg.substring(9)).toCharArray(); } else if (arg.startsWith("--timeout=")) { if (connectionTimeout > 0) { argError = "Duplicate argument '--timeout'"; break; } final String value = arg.substring(10); try { connectionTimeout = Integer.parseInt(value); } catch (final NumberFormatException e) { // } if (connectionTimeout <= 0) { argError = "The timeout must be a valid positive integer: '" + value + "'"; } } else if (arg.equals("--help") || arg.equals("-h")) { commands = Collections.singletonList("help"); } else if (arg.startsWith("--properties=")) { final String value = arg.substring(13); final File propertiesFile = new File(value); if (!propertiesFile.exists()) { argError = "File doesn't exist: " + propertiesFile.getAbsolutePath(); break; } FileInputStream fis = null; try { fis = new FileInputStream(propertiesFile); props.load(fis); } catch (FileNotFoundException e) { argError = e.getLocalizedMessage(); break; } catch (java.io.IOException e) { argError = "Failed to load properties from " + propertiesFile.getAbsolutePath() + ": " + e.getLocalizedMessage(); break; } finally { if (fis != null) { try { fis.close(); } catch (java.io.IOException e) { } } } for (final Object prop : props.keySet()) { AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { System.setProperty((String) prop, (String) props.get(prop)); return null; } }); } } else if (!(arg.startsWith("-D") || arg.equals("-XX:"))) {// skip system properties and jvm options // assume it's commands if (file != null) { argError = "Only one of '--file', '--commands' or '--command' can appear as the argument at a time: " + arg; break; } if (commands != null) { argError = "Duplicate argument '--command'/'--commands'."; break; } commands = Util.splitCommands(arg); } } if (argError != null) { System.err.println(argError); exitCode = 1; return; } if (version) { cmdCtx = initCommandContext(defaultControllerProtocol, defaultControllerHost, defaultControllerPort, username, password, false, connect, connectionTimeout); VersionHandler.INSTANCE.handle(cmdCtx); return; } if (file != null) { cmdCtx = initCommandContext(defaultControllerProtocol, defaultControllerHost, defaultControllerPort, username, password, false, connect, connectionTimeout); processFile(file, cmdCtx, isAppDeployment, appName, props); return; } if (commands != null) { cmdCtx = initCommandContext(defaultControllerProtocol, defaultControllerHost, defaultControllerPort, username, password, false, connect, connectionTimeout); processCommands(commands, cmdCtx); return; } // Interactive mode cmdCtx = initCommandContext(defaultControllerProtocol, defaultControllerHost, defaultControllerPort, username, password, true, connect, connectionTimeout); cmdCtx.interact(); } catch (Throwable t) { t.printStackTrace(); exitCode = 1; } finally { if (cmdCtx != null && cmdCtx.getExitCode() != 0) { exitCode = cmdCtx.getExitCode(); } if (!gui) { System.exit(exitCode); } } System.exit(exitCode); }
From source file:SecurityActions.java
static ClassLoader getClassLoader(final Class<?> clazz) { if (System.getSecurityManager() == null) { return clazz.getClassLoader(); } else {/*from ww w. j a v a 2 s . com*/ return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { public ClassLoader run() { return clazz.getClassLoader(); } }); } }
From source file:org.eclipse.gemini.blueprint.util.LogUtils.java
/** * Set the TCCL of the bundle before creating the logger. This helps if commons-logging is used since it looks at * the existing TCCL before associating a LogFactory with it and since the TCCL can be the * BundleDelegatingClassLoader, loading a LogFactory using the BundleDelegatingClassLoader will result in an * infinite cycle or chained failures that would be swallowed. * /*from w w w. ja v a 2 s . c o m*/ * <p/> Create the logger using LogFactory but use a simple implementation if something goes wrong. * * @param logName log name * @return logger implementation */ public static Log createLogger(final Class<?> logName) { if (System.getSecurityManager() != null) { return AccessController.doPrivileged(new PrivilegedAction<Log>() { public Log run() { return doCreateLogger(logName); } }); } return doCreateLogger(logName); }
From source file:edu.ku.brc.af.core.RecordSetFactory.java
/** * Returns the instance to the singleton * @return the instance to the singleton *//* www . j a v a 2 s .co m*/ public static RecordSetFactory getInstance() { if (instance != null) { return instance; } // else String factoryNameStr = AccessController.doPrivileged(new java.security.PrivilegedAction<String>() { public String run() { return System.getProperty(factoryName); } }); if (isNotEmpty(factoryNameStr)) { try { return instance = (RecordSetFactory) Class.forName(factoryNameStr).newInstance(); } catch (Exception e) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(RecordSetFactory.class, e); InternalError error = new InternalError("Can't instantiate RecordSet factory " + factoryNameStr); //$NON-NLS-1$ error.initCause(e); throw error; } } return null; }