<commit_before>/*
* JBoss, Home of Professional Open Source.
* Copyright 2011, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.jsr77.ejb;

import static org.jboss.as.jsr77.JSR77Messages.MESSAGES;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.ejb.EJBObject;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.QueryExp;
import javax.management.j2ee.Management;

import org.jboss.as.ee.utils.DescriptorUtils;
import org.jboss.invocation.InterceptorContext;

/**
 *
 * @author <a href="<EMAIL>"><NAME></a>
 */
public class ManagementRemoteEjbComponentView extends BaseManagementEjbComponentView {

    private final MBeanServer server;

    private volatile Method queryNames;
    private volatile Method isRegistered;
    private volatile Method getMBeanCount;
    private volatile Method getMBeanInfo;
    private volatile Method getAttribute;
    private volatile Method getAttributes;
    private volatile Method setAttribute;
    private volatile Method setAttributes;
    private volatile Method invoke;
    private volatile Method getDefaultDomain;
    private volatile Method getListenerRegistry;

    private volatile Method remove;

    public ManagementRemoteEjbComponentView(MBeanServer server) {
        this.server = server;
    }


    @Override
    public Object invoke(InterceptorContext interceptorContext) throws Exception {
        final Method method = interceptorContext.getMethod();
        Object[] params = interceptorContext.getParameters();

        if (method == queryNames) {
            return server.queryNames(
                    getParameter(ObjectName.class, params, 0),
                    getParameter(QueryExp.class, params, 1));
        } else if (method == isRegistered) {
            return server.isRegistered(getParameter(ObjectName.class, params, 0));
        } else if (method == getMBeanCount) {
            return server.getMBeanCount();
        } else if (method == getMBeanInfo) {
            return server.getMBeanInfo(getParameter(ObjectName.class, params, 0));
        } else if (method == getAttribute) {
            return server.getAttribute(
                    getParameter(ObjectName.class, params, 0),
                    getParameter(String.class, params, 1));
        } else if (method == getAttributes) {<commit_msg>Fix up security mnager issue with JSR-77<commit_after>/*
* JBoss, Home of Professional Open Source.
* Copyright 2011, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.jsr77.ejb;

import static org.jboss.as.jsr77.JSR77Messages.MESSAGES;

import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.ejb.EJBObject;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.InvalidAttributeValueException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.QueryExp;
import javax.management.ReflectionException;
import javax.management.j2ee.Management;

import org.jboss.as.ee.utils.DescriptorUtils;
import org.jboss.invocation.InterceptorContext;
import org.wildfly.security.manager.WildFlySecurityManager;

/**
 *
 * @author <a href="<EMAIL>"><NAME></a>
 */
public class ManagementRemoteEjbComponentView extends BaseManagementEjbComponentView {

    private final MBeanServer server;

    private volatile Method queryNames;
    private volatile Method isRegistered;
    private volatile Method getMBeanCount;
    private volatile Method getMBeanInfo;
    private volatile Method getAttribute;
    private volatile Method getAttributes;
    private volatile Method setAttribute;
    private volatile Method setAttributes;
    private volatile Method invoke;
    private volatile Method getDefaultDomain;
    private volatile Method getListenerRegistry;

    private volatile Method remove;

    public ManagementRemoteEjbComponentView(MBeanServer server) {
        this.server = server;
    }


    @Override
    public Object invoke(InterceptorContext interceptorContext) throws Exception {
        final Method method = interceptorContext.getMethod();
        final Object[] params = interceptorContext.getParameters();
        if(WildFlySecurityManager.isChecking()) {
            try {
            return AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                @Override
                public Object run() throws Exception {
                    return invokeInternal(method, params);
                }
            });
            } catch (PrivilegedActionException e) {
                Throwable cause = e.getCause();
                if (cause != null) {
                    if(cause instanceof Exception) {
                        throw (Exception)cause;
                    } else {
                        throw new RuntimeException(e);
                    }
                } else {
                    throw e;
                }
            }
        } else {
            return invokeInternal(method, params);
        }
    }

    private Object invokeInternal(Method method, Object[] params) throws InstanceNotFoundException, IntrospectionException, ReflectionException, MBeanException, AttributeNotFoundException, InvalidAttributeValueException {
        if (method == queryNames) {
            return server.queryNames(
                    getParameter(ObjectName.class, params, 0),
                    getParameter(QueryExp.class, params, 1));
        } else if (method == isRegistered) {
            return server.isRegistered(getParameter(ObjectName.class, params, 0));
        } else if (method == getMBeanCount) {
            return server.getMBeanCount();
        } else if (method == getMBeanInfo) {
            return server.getMBeanInfo(getParameter(ObjectName.class, params, 0));
        } else if (method == getAttribute) {
            return server.getAttribute(
                    getParameter(ObjectName.class, params, 0),
                    getParameter(String.class, params, 1));
        } else if (method == getAttributes) {
{'fraction_non_alphanumeric': 0.07578469984485021, 'fraction_numerical': 0.007399451008473565, 'mean_word_length': 3.271151885830785, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2280248', 'n_tokens_mistral': 2141, 'n_tokens_neox': 2036, 'n_words': 711}