Example usage for java.lang System getSecurityManager

List of usage examples for java.lang System getSecurityManager

Introduction

In this page you can find the example usage for java.lang System getSecurityManager.

Prototype

public static SecurityManager getSecurityManager() 

Source Link

Document

Gets the system-wide security manager.

Usage

From source file:org.wso2.carbon.core.init.CarbonServerManager.java

/**
 * Start the CarbonServerManager// ww w  .  jav  a2 s. co  m
 *
 * @param context The CarbonCore BundleContext
 */
public void start(BundleContext context) {
    // Need permissions in order to instantiate CarbonServerManager
    SecurityManager secMan = System.getSecurityManager();
    if (secMan != null) {
        secMan.checkPermission(new ManagementPermission("control"));
        new Timer("JavaSecPolicyUpdateTimer").scheduleAtFixedRate(new TimerTask() {
            public void run() {
                java.security.Policy.getPolicy().refresh();
            }
        }, 120000, 5000);
    }

    if (System.getProperty(CarbonConstants.START_TIME) == null) {
        System.setProperty(CarbonConstants.START_TIME, System.currentTimeMillis() + "");
    }

    this.bundleContext = context;

    //Initializing ConfigItem Listener - Modules and Deployers
    configItemListener = new PreAxis2ConfigItemListener(bundleContext, this);

    //Initializing Required OSGi service listener
    requiredServiceListener = new PreAxis2RequiredServiceListener(bundleContext, this);

    osgiAxis2ServicesListener = new OSGiAxis2ServicesListener(bundleContext, this);

    populateListeners();

    if (configItemListener.registerBundleListener()) {
        configItemListener.start();
    }

    if (requiredServiceListener.registerServiceListener()) {
        requiredServiceListener.start();
    }

    if (osgiAxis2ServicesListener.registerBundleListener()) {
        osgiAxis2ServicesListener.start();
    }

    //check whether pending list is empty, If so initialize Carbon
    if (pendingItemMap.isEmpty()) {
        initializeCarbon();
    } else {
        //Scheduling timer to run if the required items are being delayed.
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                try {
                    if (!pendingItemMap.isEmpty()) {
                        log.warn("Carbon initialization is delayed due to the following unsatisfied items:");
                        for (String configItem : pendingItemMap.keySet()) {
                            log.warn("Waiting for required " + pendingItemMap.get(configItem) + ": "
                                    + configItem);
                        }
                    }
                } catch (Exception ignored) {
                }
            }
        }, 60000, 60000);
    }
}

From source file:org.apache.catalina.core.ApplicationContextFacade.java

public RequestDispatcher getNamedDispatcher(String name) {
    if (System.getSecurityManager() != null) {
        return (RequestDispatcher) doPrivileged("getNamedDispatcher", new Object[] { name });
    } else {//from w ww  . j  a v  a2  s .  c om
        return context.getNamedDispatcher(name);
    }
}

From source file:org.ublog.benchmark.social.SocialBenchmark.java

private GraphInterface getRemoteGraphServer(String serverName, int serverPort) {
    if (System.getSecurityManager() == null) {
        System.setSecurityManager(new RMISecurityManager());
    }// ww w  . ja va2  s .  c o  m
    try {
        if (logger.isInfoEnabled()) {
            logger.info("Trying to connect to graph server");
        }
        Registry reg = LocateRegistry.getRegistry(serverName, serverPort);
        System.out.println("reg " + reg.list().toString());
        return (GraphInterface) reg.lookup("Graph");

    } catch (NotBoundException ex) {
        Logger.getLogger("global").log(null, ex);
        return null;
    } catch (RemoteException ex) {
        ex.printStackTrace();
        Logger.getLogger("global").log(null, ex);
        return null;
    }
}

From source file:org.codice.ddf.configuration.migration.ImportMigrationContextImpl.java

@SuppressWarnings("PMD.DefaultPackage" /* designed to be called from ImportMigrationEntryImpl within this package */)
@VisibleForTesting// w w  w .j  ava  2s .c o  m
InputStream getInputStreamFor(ImportMigrationEntryImpl entry, boolean checkAccess) throws IOException {
    if (checkAccess && files.contains(entry.getName())) {
        // we were asked to check if the migratable that is requesting this stream has read access
        // and the content was exported by the framework and not by the migratable directly, as
        // such we need to check with the security manager if one was installed
        final SecurityManager sm = System.getSecurityManager();

        if (sm != null) {
            sm.checkRead(entry.getFile().getPath());
        }
    }
    final InputStream is = zip.getInputStream(entry.getZipEntry());

    inputStreams.add(is);
    return is;
}

From source file:com.sshtools.daemon.forwarding.ForwardingServer.java

/**
 *
 *
 * @param addressToBind// w  w w .  j a v a2  s . c o m
 * @param portToBind
 *
 * @throws ForwardingConfigurationException
 */
protected void addRemoteForwardingConfiguration(String addressToBind, int portToBind)
        throws ForwardingConfigurationException {
    // Is the server already listening
    Iterator it = remoteForwardings.iterator();
    ForwardingConfiguration config;

    while (it.hasNext()) {
        config = (ForwardingConfiguration) it.next();

        if (config.getAddressToBind().equals(addressToBind) && (config.getPortToBind() == portToBind)) {
            throw new ForwardingConfigurationException("The address and port are already in use!");
        }
    }

    config = new ForwardingConfiguration(addressToBind, portToBind);

    // Check the security mananger
    SecurityManager manager = System.getSecurityManager();

    if (manager != null) {
        try {
            manager.checkPermission(
                    new SocketPermission(addressToBind + ":" + String.valueOf(portToBind), "accept,listen"));
        } catch (SecurityException e) {
            throw new ForwardingConfigurationException("The security manager has denied listen permision on "
                    + addressToBind + ":" + String.valueOf(portToBind));
        }
    }

    try {
        ForwardingListener listener = new ServerForwardingListener(connection, addressToBind, portToBind);
        remoteForwardings.add(listener);
        listener.start();
    } catch (IOException ex) {
        throw new ForwardingConfigurationException(ex.getMessage());
    }
}

From source file:org.apache.catalina.core.ApplicationContextFacade.java

public Servlet getServlet(String name) throws ServletException {
    if (System.getSecurityManager() != null) {
        try {/*  w w  w  .j  ava 2  s .c  om*/
            return (Servlet) invokeMethod(context, "getServlet", new Object[] { name });
        } catch (Throwable t) {
            if (t instanceof ServletException) {
                throw (ServletException) t;
            }
            return null;
        }
    } else {
        return context.getServlet(name);
    }
}

From source file:org.apache.catalina.core.ApplicationContextFacade.java

public Enumeration getServlets() {
    if (System.getSecurityManager() != null) {
        return (Enumeration) doPrivileged("getServlets", null);
    } else {/*  w  w  w .  j  av  a2s  .  co  m*/
        return context.getServlets();
    }
}

From source file:org.eclipse.gemini.blueprint.config.internal.adapter.OsgiServiceLifecycleListenerAdapter.java

public void bind(final Object service, final Map properties) throws Exception {
    boolean trace = log.isTraceEnabled();
    if (trace)//from w ww.j av  a 2 s .c  om
        log.trace("Invoking bind method for service " + ObjectUtils.identityToString(service) + " with props="
                + properties);

    if (!initialized)
        retrieveTarget();

    boolean isSecurityEnabled = (System.getSecurityManager() != null);
    AccessControlContext acc = null;

    if (isSecurityEnabled) {
        acc = SecurityUtils.getAccFrom(beanFactory);
    }

    // first call interface method (if it exists)
    if (isLifecycleListener) {
        if (trace)
            log.trace("Invoking listener interface methods");

        try {
            if (isSecurityEnabled) {
                AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                    public Object run() throws Exception {
                        ((OsgiServiceLifecycleListener) target).bind(service, properties);
                        return null;
                    }
                }, acc);
            } else {
                ((OsgiServiceLifecycleListener) target).bind(service, properties);
            }
        } catch (Exception ex) {
            if (ex instanceof PrivilegedActionException) {
                ex = ((PrivilegedActionException) ex).getException();
            }
            log.warn("standard bind method on [" + target.getClass().getName() + "] threw exception", ex);
        }
    }

    if (isSecurityEnabled) {
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
            public Object run() {
                CustomListenerAdapterUtils.invokeCustomMethods(target, bindMethods, service, properties);
                invokeCustomServiceReferenceMethod(target, bindReference, service);
                return null;
            }
        }, acc);
    } else {
        CustomListenerAdapterUtils.invokeCustomMethods(target, bindMethods, service, properties);
        invokeCustomServiceReferenceMethod(target, bindReference, service);
    }
}

From source file:org.apache.catalina.core.ApplicationContextFacade.java

public Enumeration getServletNames() {
    if (System.getSecurityManager() != null) {
        return (Enumeration) doPrivileged("getServletNames", null);
    } else {/*from w  w  w.  j a  va 2  s. c o m*/
        return context.getServletNames();
    }
}

From source file:org.wso2.carbon.core.ServerManagement.java

/**
 * Wait till all service requests have been serviced. This method will only wait for a maximum
 * of {@link ServerManagement#TIMEOUT}/*from w  w w.  ja va  2  s.c o m*/
 *
 * @throws Exception If an error occurs while trying to connect to the Tomcat MBean
 */
public void waitForRequestCompletion() throws Exception {
    SecurityManager secMan = System.getSecurityManager();
    if (secMan != null) {
        secMan.checkPermission(new ManagementPermission("control"));
    }
    log.info("Waiting for request service completion...");
    /**
     * Get all MBeans with names such as Catalina:type=RequestProcessor,worker=http-9762,name=HttpRequest<n>
     * & Catalina:type=RequestProcessor,worker=http-9762,name=HttpsRequest<n>
     */
    MBeanServer mbs = ManagementFactory.getMBeanServer();
    boolean areRequestsInService;
    long start = System.currentTimeMillis();
    do {
        // Check whether there are any processors which are currently in the SERVICE stage (3)
        QueryExp query = Query.eq(Query.attr("stage"), Query.value(3)); // 3 = org.apache.coyote.Constants.STAGE_SERVICE
        Set set = mbs.queryNames(new ObjectName("Catalina:type=RequestProcessor,*"), query);
        if (set.size() > 0) {
            areRequestsInService = true;
            if (System.currentTimeMillis() - start > TIMEOUT) {
                log.warn("Timeout occurred even though there are active connections.");
                break;
            }
            Thread.sleep(2000);
        } else {
            areRequestsInService = false;
        }
    } while (areRequestsInService);
    log.info("All requests have been served.");
}