<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */


package org.apache.geronimo.kernel.osgi;

import java.net.URL;
import java.io.InputStream;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceReference;
import org.apache.geronimo.kernel.config.ConfigurationUtil;
import org.apache.geronimo.kernel.config.ConfigurationData;
import org.apache.geronimo.kernel.config.ConfigurationManager;
import org.apache.geronimo.kernel.config.Configuration;
import org.apache.geronimo.kernel.Kernel;
import org.apache.geronimo.kernel.GBeanNotFoundException;
import org.apache.geronimo.kernel.InternalKernelException;
import org.apache.geronimo.kernel.repository.Artifact;
import org.apache.geronimo.gbean.AbstractName;

/**
 * @version $Rev$ $Date$
 */
public class ConfigurationActivator implements BundleActivator {
    
    private Artifact id;


    public void start(BundleContext bundleContext) throws Exception {
        ServiceReference kernelReference = bundleContext.getServiceReference(Kernel.class.getName());
        Kernel kernel = (Kernel) bundleContext.getService(kernelReference);
        ConfigurationManager manager = ConfigurationUtil.getConfigurationManager(kernel);
        Bundle bundle = bundleContext.getBundle();
        URL plan = bundle.getEntry("META-INF/config.ser");
        InputStream in = plan.openStream();
        try {
            //TODO there are additional consistency checks in RepositoryConfigurationStore that we should use.
            ConfigurationData data = ConfigurationUtil.readConfigurationData(in);
            data.setBundleContext(bundleContext);
            manager.loadConfiguration(data);
            id = data.getId();
//            manager.startConfiguration(id);
        } finally {
            in.close();
        }
    }

    public void stop(BundleContext bundleContext) throws Exception {
        ServiceReference kernelReference = bundleContext.getServiceReference(Kernel.class.getName());
        if (kernelReference != null) {
            Kernel kernel = (Kernel) bundleContext.getService(kernelReference);
            AbstractName name = Configuration.getConfigurationAbstractName(id);
            //TODO investigate how this is called and whether just stopping/unloading the configuration gbean will
            //leave the configuration model in a consistent state.  We might need a shutdown flag set elsewhere to avoid
            //overwriting the load attribute in config.xml. This code mimics the shutdown hook in KernelConfigurationManager
            //see https://issues.apache.org/jira/browse/GERONIMO-4909
            try {
                kernel.stopGBean(name);
            } catch (GBeanNotFoundException e) {

            } catch (InternalKernelException e) {

            } catch (IllegalStateException e) {

            }
            try {
                kernel.unloadGBean(name);
            } catch (GBeanNotFoundException e) {

            } catch (InternalKernelException e) {

            } catch (IllegalStateException e) {

            }
            //TODO this code is more symmetrical with start, but currently sets the load attribute to false in config.xml,
            //which prevents restarting the server.
//            ConfigurationManager manager = ConfigurationUtil.getConfigurationManager(kernel);
//            manager.unloadConfiguration(id);
        }
    }
}
<commit_msg>No change, code format<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */


package org.apache.geronimo.kernel.osgi;

import java.io.InputStream;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceReference;
import org.apache.geronimo.kernel.config.ConfigurationUtil;
import org.apache.geronimo.kernel.config.ConfigurationData;
import org.apache.geronimo.kernel.config.ConfigurationManager;
import org.apache.geronimo.kernel.config.Configuration;
import org.apache.geronimo.kernel.Kernel;
import org.apache.geronimo.kernel.GBeanNotFoundException;
import org.apache.geronimo.kernel.InternalKernelException;
import org.apache.geronimo.kernel.repository.Artifact;
import org.apache.geronimo.gbean.AbstractName;

/**
 * @version $Rev$ $Date$
 */
public class ConfigurationActivator implements BundleActivator {

    private Artifact id;


    public void start(BundleContext bundleContext) throws Exception {
        ServiceReference kernelReference = null;
        InputStream in = null;
        try {
            kernelReference = bundleContext.getServiceReference(Kernel.class.getName());
            if (kernelReference == null) {
                return;
            }
            Kernel kernel = (Kernel) bundleContext.getService(kernelReference);
            ConfigurationManager manager = ConfigurationUtil.getConfigurationManager(kernel);
            Bundle bundle = bundleContext.getBundle();
            in = bundle.getEntry("META-INF/config.ser").openStream();
            //TODO there are additional consistency checks in RepositoryConfigurationStore that we should use.
            ConfigurationData data = ConfigurationUtil.readConfigurationData(in);
            data.setBundleContext(bundleContext);
            manager.loadConfiguration(data);
            id = data.getId();
            //            manager.startConfiguration(id);
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (Exception e) {
                }
            if (kernelReference != null)
                try {
                    bundleContext.ungetService(kernelReference);
                } catch (Exception e) {
                }
        }
    }

    public void stop(BundleContext bundleContext) throws Exception {
        ServiceReference kernelReference = null;
        try {
            kernelReference = bundleContext.getServiceReference(Kernel.class.getName());
            if (kernelReference == null) {
                return;
            }
            Kernel kernel = (Kernel) bundleContext.getService(kernelReference);
            AbstractName name = Configuration.getConfigurationAbstractName(id);
            //TODO investigate how this is called and whether just stopping/unloading the configuration gbean will
            //leave the configuration model in a consistent state.  We might need a shutdown flag set elsewhere to avoid
            //overwriting the load attribute in config.xml. This code mimics the shutdown hook in KernelConfigurationManager
            //see https://issues.apache.org/jira/browse/GERONIMO-4909
            try {
                kernel.stopGBean(name);
            } catch (GBeanNotFoundException e) {
            } catch (InternalKernelException e) {
            } catch (IllegalStateException e) {
            }
            try {
                kernel.unloadGBean(name);
            } catch (GBeanNotFoundException e) {
            } catch (InternalKernelException e) {
            } catch (IllegalStateException e) {
            }
            //TODO this code is more symmetrical with start, but currently sets the load attribute to false in config.xml,
            //which prevents restarting the server.
            //            ConfigurationManager manager = ConfigurationUtil.getConfigurationManager(kernel);
            //            manager.unloadConfiguration(id);
        } finally {
            if (kernelReference != null)
                try {
                    bundleContext.ungetService(kernelReference);
                } catch (Exception e) {
                }
        }
    }
}

{'fraction_non_alphanumeric': 0.07749918593292088, 'fraction_numerical': 0.0017366764354716162, 'mean_word_length': 2.6636182902584493, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, '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': '6083218', 'n_tokens_mistral': 2213, 'n_tokens_neox': 2151, 'n_words': 674}