<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.aries.blueprint.container;

import org.apache.aries.blueprint.ComponentDefinitionRegistryProcessor;
import org.apache.aries.blueprint.ExtendedBeanMetadata;
import org.apache.aries.blueprint.Processor;
import org.apache.aries.blueprint.di.Recipe;
import org.apache.aries.blueprint.di.Repository;
import org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl;
import org.apache.aries.blueprint.parser.NamespaceHandlerSet;
import org.apache.aries.blueprint.parser.Parser;
import org.apache.aries.blueprint.reflect.MetadataUtil;
import org.apache.aries.blueprint.reflect.PassThroughMetadataImpl;
import org.apache.aries.blueprint.services.ExtendedBlueprintContainer;
import org.osgi.service.blueprint.container.ComponentDefinitionException;
import org.osgi.service.blueprint.container.Converter;
import org.osgi.service.blueprint.container.NoSuchComponentException;
import org.osgi.service.blueprint.reflect.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.net.URL;
import java.security.AccessControlContext;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

public class BlueprintContainerImpl implements ExtendedBlueprintContainer {

    private static final Logger LOGGER = LoggerFactory.getLogger(BlueprintContainerImpl.class);

    private final ClassLoader loader;
    private final List<URL> resources;
    private final AggregateConverter converter;
    private final ComponentDefinitionRegistryImpl componentDefinitionRegistry;
    private final AtomicBoolean destroyed = new AtomicBoolean(false);
    private final IdSpace tempRecipeIdSpace = new IdSpace();
    private BlueprintRepository repository;
    private List<Processor> processors = new ArrayList<Processor>();
    private Map<String, String> properties;

    public BlueprintContainerImpl(ClassLoader loader, List<URL> resources) throws Exception {
        this(loader, resources, null, true);
    }

    public BlueprintContainerImpl(ClassLoader loader, List<URL> resources, boolean init) throws Exception {
        this(loader, resources, null, init);
    }

    public BlueprintContainerImpl(ClassLoader loader, List<URL> resources, Map<String, String> properties, boolean init) throws Exception {
        this.loader = loader;
        this.converter = new AggregateConverter(this);
        this.componentDefinitionRegistry = new ComponentDefinitionRegistryImpl();
        this.resources = resources;
        this.properties = properties;
        if (init) {
            init();
        }
    }

    public String getProperty(String key) {
        if (properties != null && properties.containsKey(key)) {
            return properties.get(key);
        }
        return System.getProperty(key);
    }

    protected NamespaceHandlerSet createNamespaceHandlerSet(Set<URI> namespaces) {
        NamespaceHandlerSet handlerSet = new SimpleNamespaceHandlerSet();
        // Check namespaces
        Set<URI> unsupported = new LinkedHashSet<URI>();
        for (URI ns : namespaces) {
            if (!handlerSet.getNamespaces().contains(ns)) {
                unsupported.add(ns);
            }
        }
        if (unsupported.size() > 0) {
            throw new IllegalArgumentException("Unsupported namespaces: " + unsupported.toString());
        }
        return handlerSet;
    }

    public void init() throws Exception {
        init(true);
    }
    public void init(boolean validate) throws Exception {
        // Parse xml resources
        Parser parser = new Parser();
        parser.parse(getResources());
        // Check namespaces
        Set<URI> namespaces = parser.getNamespaces();
        // Create handler set
        NamespaceHandlerSet handlerSet = createNamespaceHandlerSet(namespaces);
        // Add predefined beans
        componentDefinitionRegistry.registerComponentDefinition(new PassThroughMetadataImpl("blueprintContainer", this));
        if (validate) {
            // Validate
            parser.validate(handlerSet.getSchema());
        }<commit_msg>[ARIES-1300] Minor updates to noosgi BlueprintContainerImpl and BlueprintContextListener to make it easier to provide custom namespace handler sets<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.aries.blueprint.container;

import java.net.URI;
import java.net.URL;
import java.security.AccessControlContext;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.aries.blueprint.ComponentDefinitionRegistryProcessor;
import org.apache.aries.blueprint.ExtendedBeanMetadata;
import org.apache.aries.blueprint.Processor;
import org.apache.aries.blueprint.di.Recipe;
import org.apache.aries.blueprint.di.Repository;
import org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl;
import org.apache.aries.blueprint.parser.NamespaceHandlerSet;
import org.apache.aries.blueprint.parser.Parser;
import org.apache.aries.blueprint.reflect.MetadataUtil;
import org.apache.aries.blueprint.reflect.PassThroughMetadataImpl;
import org.apache.aries.blueprint.services.ExtendedBlueprintContainer;
import org.osgi.service.blueprint.container.ComponentDefinitionException;
import org.osgi.service.blueprint.container.Converter;
import org.osgi.service.blueprint.container.NoSuchComponentException;
import org.osgi.service.blueprint.reflect.BeanArgument;
import org.osgi.service.blueprint.reflect.BeanMetadata;
import org.osgi.service.blueprint.reflect.BeanProperty;
import org.osgi.service.blueprint.reflect.CollectionMetadata;
import org.osgi.service.blueprint.reflect.ComponentMetadata;
import org.osgi.service.blueprint.reflect.MapEntry;
import org.osgi.service.blueprint.reflect.MapMetadata;
import org.osgi.service.blueprint.reflect.Metadata;
import org.osgi.service.blueprint.reflect.PropsMetadata;
import org.osgi.service.blueprint.reflect.RefMetadata;
import org.osgi.service.blueprint.reflect.ReferenceListener;
import org.osgi.service.blueprint.reflect.RegistrationListener;
import org.osgi.service.blueprint.reflect.ServiceMetadata;
import org.osgi.service.blueprint.reflect.ServiceReferenceMetadata;
import org.osgi.service.blueprint.reflect.Target;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BlueprintContainerImpl implements ExtendedBlueprintContainer {

    private static final Logger LOGGER = LoggerFactory.getLogger(BlueprintContainerImpl.class);

    private final ClassLoader loader;
    private final List<URL> resources;
    private final AggregateConverter converter;
    private final ComponentDefinitionRegistryImpl componentDefinitionRegistry;
    private final AtomicBoolean destroyed = new AtomicBoolean(false);
    private final IdSpace tempRecipeIdSpace = new IdSpace();
    private BlueprintRepository repository;
    private List<Processor> processors = new ArrayList<Processor>();
    private Map<String, String> properties;
    private NamespaceHandlerSet nsHandlerSet;
    
    public BlueprintContainerImpl(ClassLoader loader, List<URL> resources) throws Exception {
        this(loader, resources, null, true);
    }

    public BlueprintContainerImpl(ClassLoader loader, List<URL> resources, boolean init) throws Exception {
        this(loader, resources, null, init);
    }

    public BlueprintContainerImpl(ClassLoader loader, List<URL> resources, Map<String, String> properties, boolean init) throws Exception {
        this(loader, resources, properties, null, init);
    }
    public BlueprintContainerImpl(ClassLoader loader, List<URL> resources, Map<String, String> properties, 
                                  NamespaceHandlerSet nsHandlerSet, boolean init) throws Exception {
        this.loader = loader;
        this.converter = new AggregateConverter(this);
        this.componentDefinitionRegistry = new ComponentDefinitionRegistryImpl();
        this.resources = resources;
        this.properties = properties;
        this.nsHandlerSet = nsHandlerSet;
        if (init) {
            init();
        }
    }
    public String getProperty(String key) {
        if (properties != null && properties.containsKey(key)) {
            return properties.get(key);
        }
        return System.getProperty(key);
    }

    protected NamespaceHandlerSet createNamespaceHandlerSet(Set<URI> namespaces) {
        NamespaceHandlerSet handlerSet = createNamespaceHandlerSet();
        // Check namespaces
        Set<URI> unsupported = new LinkedHashSet<URI>();
        for (URI ns : namespaces) {
            if (!handlerSet.getNamespaces().contains(ns)) {
                unsupported.add(ns);
            }
        }
        if (unsupported.size() > 0) {
            throw new IllegalArgumentException("Unsupported namespaces: " + unsupported.toString());
        }
        return handlerSet;
    }

    protected NamespaceHandlerSet createNamespaceHandlerSet() {
        return nsHandlerSet == null ? new SimpleNamespaceHandlerSet() : nsHandlerSet;
    } 
    
    public void init() throws Exception {
        init(true);
    }
    public void init(boolean validate) throws Exception {
        // Parse xml resources
        Parser parser = new Parser();
        parser.parse(getResources());
        // Check namespaces
        Set<URI> namespaces = parser.getNamespaces();
        // Create handler set
        NamespaceHandlerSet handlerSet = createNamespaceHandlerSet(namespaces);
        // Add predefined beans
        componentDefinitionRegistry.registerComponentDefinition(new PassThroughMetadataImpl("blueprintContainer", this));
        if (validate) {
            // Validate
            parser.validate(handlerSet.getSchema());
        }
{'fraction_non_alphanumeric': 0.08420222553228775, 'fraction_numerical': 0.0015771488653290107, 'mean_word_length': 3.8694539249146755, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6278075', 'n_tokens_mistral': 2869, 'n_tokens_neox': 2819, 'n_words': 781}