<commit_before>/**
 * 
 * Copyright 2005-2006 The Apache Software Foundation
 * 
 * Licensed 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.activemq.broker.jmx;

import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArraySet;

import org.apache.activemq.broker.Broker;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.ConnectionContext;
import org.apache.activemq.broker.jmx.OpenTypeSupport.OpenTypeFactory;
import org.apache.activemq.broker.region.Destination;
import org.apache.activemq.broker.region.Queue;
import org.apache.activemq.broker.region.Region;
import org.apache.activemq.broker.region.RegionBroker;
import org.apache.activemq.broker.region.Subscription;
import org.apache.activemq.broker.region.Topic;
import org.apache.activemq.broker.region.TopicSubscription;
import org.apache.activemq.command.ActiveMQDestination;
import org.apache.activemq.command.ActiveMQMessage;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.activemq.command.ConsumerInfo;
import org.apache.activemq.command.Message;
import org.apache.activemq.command.SubscriptionInfo;
import org.apache.activemq.memory.UsageManager;
import org.apache.activemq.store.MessageRecoveryListener;
import org.apache.activemq.store.PersistenceAdapter;
import org.apache.activemq.store.TopicMessageStore;
import org.apache.activemq.thread.TaskRunnerFactory;
import org.apache.activemq.util.JMXSupport;
import org.apache.activemq.util.ServiceStopper;
import org.apache.activemq.util.SubscriptionKey;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.management.InstanceNotFoundException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.CompositeType;
import javax.management.openmbean.OpenDataException;
import javax.management.openmbean.TabularData;
import javax.management.openmbean.TabularDataSupport;
import javax.management.openmbean.TabularType;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class ManagedRegionBroker extends RegionBroker{
    private static final Log log=LogFactory.getLog(ManagedRegionBroker.class);
    private final MBeanServer mbeanServer;
    private final ObjectName brokerObjectName;
    private final Map topics=new ConcurrentHashMap();
    private final Map queues=new ConcurrentHashMap();
    private final Map temporaryQueues=new ConcurrentHashMap();
    private final Map temporaryTopics=new ConcurrentHashMap();
    private final Map queueSubscribers=new ConcurrentHashMap();
    private final Map topicSubscribers=new ConcurrentHashMap();
    private final Map durableTopicSubscribers=new ConcurrentHashMap();
    private final Map inactiveDurableTopicSubscribers=new ConcurrentHashMap();
    private final Map temporaryQueueSubscribers=new ConcurrentHashMap();
    private final Map temporaryTopicSubscribers=new ConcurrentHashMap();
    private final Map subscriptionKeys = new ConcurrentHashMap();
    private final Map subscriptionMap = new ConcurrentHashMap();
    private final Set registeredMBeans = new CopyOnWriteArraySet();
    
    /* This is the first broker in the broker interceptor chain. */
    private Broker contextBroker;

    public ManagedRegionBroker(BrokerService brokerService,MBeanServer mbeanServer,ObjectName brokerObjectName,
                    TaskRunnerFactory taskRunnerFactory,UsageManager memoryManager,PersistenceAdapter adapter) throws IOException{
        super(brokerService,taskRunnerFactory,memoryManager,adapter);
        this.mbeanServer=mbeanServer;
        this.brokerObjectName=brokerObjectName;
    }
    
    public void start() throws Exception {
        super.start();
        //build all existing durable subscriptions
        buildExistingSubscriptions();
        
    }

    
    protected void doStop(ServiceStopper stopper) {
        super.doStop(stopper);
        
        // lets remove any mbeans not yet removed
        for (Iterator iter = registeredMBeans.iterator(); iter.hasNext();) {
            ObjectName name = (ObjectName) iter.next();
            try {
                mbeanServer.unregisterMBean(name);
            }
            catch (Exception e) {
                stopper.onException(this, e);
            }
        }
        registeredMBeans.clear();
    }

    protected Region createQueueRegion(UsageManager memoryManager,TaskRunnerFactory taskRunnerFactory,
                    PersistenceAdapter adapter){
        return new ManagedQueueRegion(this,destinationStatistics,memoryManager,taskRunnerFactory,adapter);
    }

    protected Region createTempQueueRegion(UsageManager memoryManager,TaskRunnerFactory taskRunnerFactory){
        return new ManagedTempQueueRegion(this,destinationStatistics,memoryManager,taskRunnerFactory);
    }

    protected Region createTempTopicRegion(UsageManager memoryManager,TaskRunnerFactory taskRunnerFactory){
        return new ManagedTempTopicRegion(this,destinationStatistics,memoryManager,taskRunnerFactory);
    }<commit_msg>deal with mbeans being already unregistered gracefully<commit_after>/**
 * 
 * Copyright 2005-2006 The Apache Software Foundation
 * 
 * Licensed 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.activemq.broker.jmx;

import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArraySet;

import org.apache.activemq.broker.Broker;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.ConnectionContext;
import org.apache.activemq.broker.jmx.OpenTypeSupport.OpenTypeFactory;
import org.apache.activemq.broker.region.Destination;
import org.apache.activemq.broker.region.Queue;
import org.apache.activemq.broker.region.Region;
import org.apache.activemq.broker.region.RegionBroker;
import org.apache.activemq.broker.region.Subscription;
import org.apache.activemq.broker.region.Topic;
import org.apache.activemq.broker.region.TopicSubscription;
import org.apache.activemq.command.ActiveMQDestination;
import org.apache.activemq.command.ActiveMQMessage;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.activemq.command.ConsumerInfo;
import org.apache.activemq.command.Message;
import org.apache.activemq.command.SubscriptionInfo;
import org.apache.activemq.memory.UsageManager;
import org.apache.activemq.store.MessageRecoveryListener;
import org.apache.activemq.store.PersistenceAdapter;
import org.apache.activemq.store.TopicMessageStore;
import org.apache.activemq.thread.TaskRunnerFactory;
import org.apache.activemq.util.JMXSupport;
import org.apache.activemq.util.ServiceStopper;
import org.apache.activemq.util.SubscriptionKey;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.management.InstanceNotFoundException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.CompositeType;
import javax.management.openmbean.OpenDataException;
import javax.management.openmbean.TabularData;
import javax.management.openmbean.TabularDataSupport;
import javax.management.openmbean.TabularType;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class ManagedRegionBroker extends RegionBroker{
    private static final Log log=LogFactory.getLog(ManagedRegionBroker.class);
    private final MBeanServer mbeanServer;
    private final ObjectName brokerObjectName;
    private final Map topics=new ConcurrentHashMap();
    private final Map queues=new ConcurrentHashMap();
    private final Map temporaryQueues=new ConcurrentHashMap();
    private final Map temporaryTopics=new ConcurrentHashMap();
    private final Map queueSubscribers=new ConcurrentHashMap();
    private final Map topicSubscribers=new ConcurrentHashMap();
    private final Map durableTopicSubscribers=new ConcurrentHashMap();
    private final Map inactiveDurableTopicSubscribers=new ConcurrentHashMap();
    private final Map temporaryQueueSubscribers=new ConcurrentHashMap();
    private final Map temporaryTopicSubscribers=new ConcurrentHashMap();
    private final Map subscriptionKeys = new ConcurrentHashMap();
    private final Map subscriptionMap = new ConcurrentHashMap();
    private final Set registeredMBeans = new CopyOnWriteArraySet();
    
    /* This is the first broker in the broker interceptor chain. */
    private Broker contextBroker;

    public ManagedRegionBroker(BrokerService brokerService,MBeanServer mbeanServer,ObjectName brokerObjectName,
                    TaskRunnerFactory taskRunnerFactory,UsageManager memoryManager,PersistenceAdapter adapter) throws IOException{
        super(brokerService,taskRunnerFactory,memoryManager,adapter);
        this.mbeanServer=mbeanServer;
        this.brokerObjectName=brokerObjectName;
    }
    
    public void start() throws Exception {
        super.start();
        //build all existing durable subscriptions
        buildExistingSubscriptions();
        
    }

    
    protected void doStop(ServiceStopper stopper) {
        super.doStop(stopper);
        
        // lets remove any mbeans not yet removed
        for (Iterator iter = registeredMBeans.iterator(); iter.hasNext();) {
            ObjectName name = (ObjectName) iter.next();
            try {
                mbeanServer.unregisterMBean(name);
            }
            catch (InstanceNotFoundException e) {
                log.warn("The MBean: " + name + " is no longer registered with JMX");
            }
            catch (Exception e) {
                stopper.onException(this, e);
            }
        }
        registeredMBeans.clear();
    }

    protected Region createQueueRegion(UsageManager memoryManager,TaskRunnerFactory taskRunnerFactory,
                    PersistenceAdapter adapter){
        return new ManagedQueueRegion(this,destinationStatistics,memoryManager,taskRunnerFactory,adapter);
    }

    protected Region createTempQueueRegion(UsageManager memoryManager,TaskRunnerFactory taskRunnerFactory){
        return new ManagedTempQueueRegion(this,destinationStatistics,memoryManager,taskRunnerFactory);
    }

    protected Region createTempTopicRegion(UsageManager memoryManager,TaskRunnerFactory taskRunnerFactory){
        return new ManagedTempTopicRegion(this,destinationStatistics,memoryManager,taskRunnerFactory);
    }
{'fraction_non_alphanumeric': 0.07742728390004097, 'fraction_numerical': 0.0019664072101597705, 'mean_word_length': 4.677209302325581, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3406730', 'n_tokens_mistral': 3187, 'n_tokens_neox': 3174, 'n_words': 752}