<commit_before>/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2011, Red Hat, Inc., 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.webservices.service;

import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ADD;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.REMOVE;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SUBSYSTEM;
import static org.jboss.as.server.Services.JBOSS_SERVER_CONTROLLER;
import static org.jboss.as.webservices.dmr.Constants.ENDPOINT;
import static org.jboss.as.webservices.dmr.Constants.ENDPOINT_CLASS;
import static org.jboss.as.webservices.dmr.Constants.ENDPOINT_CONTEXT;
import static org.jboss.as.webservices.dmr.Constants.ENDPOINT_NAME;
import static org.jboss.as.webservices.dmr.Constants.ENDPOINT_TYPE;
import static org.jboss.as.webservices.dmr.Constants.ENDPOINT_WSDL;
import static org.jboss.msc.service.ServiceBuilder.DependencyType.OPTIONAL;

import java.io.IOException;
import java.util.concurrent.Executors;

import org.jboss.as.controller.NewModelController;
import org.jboss.as.controller.client.NewModelControllerClient;
import org.jboss.as.protocol.old.StreamUtils;
import org.jboss.as.webservices.dmr.WSExtension;
import org.jboss.as.webservices.util.WSServices;
import org.jboss.dmr.ModelNode;
import org.jboss.msc.inject.Injector;
import org.jboss.msc.service.AbstractService;
import org.jboss.msc.service.ServiceBuilder;
import org.jboss.msc.service.ServiceController;
import org.jboss.msc.service.ServiceController.Mode;
import org.jboss.msc.service.ServiceListener;
import org.jboss.msc.service.ServiceTarget;
import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StartException;
import org.jboss.msc.service.StopContext;
import org.jboss.msc.value.InjectedValue;
import org.jboss.wsf.spi.deployment.Endpoint;

/**
 * Exposes public {@code #add(Endpoint)} & {@code #remove(Endpoint)} methods so
 * WS subsystem can send udpate events to WS DMR Model.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public final class ModelUpdateService extends AbstractService<Void> {

    private final InjectedValue<NewModelController> controllerValue = new InjectedValue<NewModelController>();
    private static final ModelUpdateService INSTANCE = new ModelUpdateService();
    private volatile NewModelControllerClient client;

    private ModelUpdateService() {
        super();
    }

    private InjectedValue<NewModelController> getServerControllerInjector() {
        return controllerValue;
    }

    public static ModelUpdateService getInstance() {
        return INSTANCE;
    }

    @Override
    public void start(StartContext context) throws StartException {
        super.start(context);
        client = controllerValue.getValue().createClient(Executors.newCachedThreadPool());

    }

    @Override
    public void stop(StopContext context) {
        StreamUtils.safeClose(client);
        client = null;
        super.stop(context);
    }

    public static ServiceController<?> install(final ServiceTarget serviceTarget, final ServiceListener<Object>... listeners) {
        final Injector<NewModelController> controllerInjector = INSTANCE.getServerControllerInjector();
        final ServiceBuilder<Void> builder = serviceTarget.addService(WSServices.MODEL_SERVICE, INSTANCE);
        builder.addDependency(OPTIONAL, JBOSS_SERVER_CONTROLLER, NewModelController.class, controllerInjector);
        builder.addListener(listeners);
        builder.setInitialMode(Mode.ACTIVE);
        return builder.install();
    }

    public void add(final Endpoint endpoint) {
        final NewModelController controller = controllerValue.getOptionalValue();
        if (controller != null) {
            final ModelNode addOperation = newAddOperation(endpoint);
            try {
                client.execute(addOperation);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public void remove(final Endpoint endpoint) {
        final NewModelController controller = controllerValue.getOptionalValue();
        if (controller != null) {
            final ModelNode removeOperation = newRemoveOperation(endpoint);
            try {
                client.execute(removeOperation);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private ModelNode newAddOperation(final Endpoint endpoint) {
        final ModelNode op = new ModelNode();
        op.get(OP).set(ADD);
        final ModelNode address = op.get(OP_ADDR);
        address.add(SUBSYSTEM, WSExtension.SUBSYSTEM_NAME);
        address.add(ENDPOINT, getId(endpoint));
        op.get(ENDPOINT_NAME).set(getName(endpoint));
        op.get(ENDPOINT_CONTEXT).set(getContext(endpoint));<commit_msg>[AS7-855] Disable illegal deployer calls to management layer<commit_after>/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2011, Red Hat, Inc., 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.webservices.service;

import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ADD;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.REMOVE;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SUBSYSTEM;
import static org.jboss.as.server.Services.JBOSS_SERVER_CONTROLLER;
import static org.jboss.as.webservices.dmr.Constants.ENDPOINT;
import static org.jboss.as.webservices.dmr.Constants.ENDPOINT_CLASS;
import static org.jboss.as.webservices.dmr.Constants.ENDPOINT_CONTEXT;
import static org.jboss.as.webservices.dmr.Constants.ENDPOINT_NAME;
import static org.jboss.as.webservices.dmr.Constants.ENDPOINT_TYPE;
import static org.jboss.as.webservices.dmr.Constants.ENDPOINT_WSDL;
import static org.jboss.msc.service.ServiceBuilder.DependencyType.OPTIONAL;

import java.io.IOException;
import java.util.concurrent.Executors;

import org.jboss.as.controller.NewModelController;
import org.jboss.as.controller.client.NewModelControllerClient;
import org.jboss.as.protocol.old.StreamUtils;
import org.jboss.as.webservices.dmr.WSExtension;
import org.jboss.as.webservices.util.WSServices;
import org.jboss.dmr.ModelNode;
import org.jboss.logging.Logger;
import org.jboss.msc.inject.Injector;
import org.jboss.msc.service.AbstractService;
import org.jboss.msc.service.ServiceBuilder;
import org.jboss.msc.service.ServiceController;
import org.jboss.msc.service.ServiceController.Mode;
import org.jboss.msc.service.ServiceListener;
import org.jboss.msc.service.ServiceTarget;
import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StartException;
import org.jboss.msc.service.StopContext;
import org.jboss.msc.value.InjectedValue;
import org.jboss.wsf.spi.deployment.Endpoint;

/**
 * Exposes public {@code #add(Endpoint)} & {@code #remove(Endpoint)} methods so
 * WS subsystem can send udpate events to WS DMR Model.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public final class ModelUpdateService extends AbstractService<Void> {

    private static final Logger log = Logger.getLogger(ModelUpdateService.class);

    private final InjectedValue<NewModelController> controllerValue = new InjectedValue<NewModelController>();
    private static final ModelUpdateService INSTANCE = new ModelUpdateService();
    private volatile NewModelControllerClient client;

    private ModelUpdateService() {
        super();
    }

    private InjectedValue<NewModelController> getServerControllerInjector() {
        return controllerValue;
    }

    public static ModelUpdateService getInstance() {
        return INSTANCE;
    }

    @Override
    public void start(StartContext context) throws StartException {
        super.start(context);
        client = controllerValue.getValue().createClient(Executors.newCachedThreadPool());

    }

    @Override
    public void stop(StopContext context) {
        StreamUtils.safeClose(client);
        client = null;
        super.stop(context);
    }

    public static ServiceController<?> install(final ServiceTarget serviceTarget, final ServiceListener<Object>... listeners) {
        final Injector<NewModelController> controllerInjector = INSTANCE.getServerControllerInjector();
        final ServiceBuilder<Void> builder = serviceTarget.addService(WSServices.MODEL_SERVICE, INSTANCE);
        builder.addDependency(OPTIONAL, JBOSS_SERVER_CONTROLLER, NewModelController.class, controllerInjector);
        builder.addListener(listeners);
        builder.setInitialMode(Mode.ACTIVE);
        return builder.install();
    }

    public void add(final Endpoint endpoint) {
        final NewModelController controller = controllerValue.getOptionalValue();
        if (controller != null) {
            // TODO AS7-855
            log.warn("Registering webservice endpoints in the management model is temporarily disabled (AS7-855)");
//            final ModelNode addOperation = newAddOperation(endpoint);
//            try {
//                client.execute(addOperation);
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            }
        }
    }

    public void remove(final Endpoint endpoint) {
        final NewModelController controller = controllerValue.getOptionalValue();
        if (controller != null) {
            // TODO AS7-855
            log.warn("Registering webservice endpoints in the management model is temporarily disabled (AS7-855)");
//            final ModelNode removeOperation = newRemoveOperation(endpoint);
//            try {
//                client.execute(removeOperation);
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            }
        }
    }

    private ModelNode newAddOperation(final Endpoint endpoint) {
        final ModelNode op = new ModelNode();
        op.get(OP).set(ADD);
        final ModelNode address = op.get(OP_ADDR);
        address.add(SUBSYSTEM, WSExtension.SUBSYSTEM_NAME);
        address.add(ENDPOINT, getId(endpoint));
        op.get(ENDPOINT_NAME).set(getName(endpoint));
        op.get(ENDPOINT_CONTEXT).set(getContext(endpoint));
{'fraction_non_alphanumeric': 0.09132161089052751, 'fraction_numerical': 0.004375658374523945, 'mean_word_length': 3.855232100708104, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '6445589', 'n_tokens_mistral': 3457, 'n_tokens_neox': 3351, 'n_words': 858}