<commit_before>/**
 * Part of the CCNx Java Library.
 *
 * Copyright (C) 2008, 2009 Palo Alto Research Center, Inc.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation. 
 * This library 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 library;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301 USA.
 */

package org.ccnx.ccn.profiles.namespace;

import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.ccnx.ccn.CCNHandle;
import org.ccnx.ccn.config.ConfigurationException;
import org.ccnx.ccn.config.SystemConfiguration;
import org.ccnx.ccn.impl.support.Log;
import org.ccnx.ccn.io.ErrorStateException;
import org.ccnx.ccn.io.content.ContentGoneException;
import org.ccnx.ccn.io.content.ContentNotReadyException;
import org.ccnx.ccn.profiles.VersioningProfile;
import org.ccnx.ccn.profiles.namespace.Root.RootObject;
import org.ccnx.ccn.profiles.search.Pathfinder;
import org.ccnx.ccn.profiles.search.Pathfinder.SearchResults;
import org.ccnx.ccn.profiles.security.access.AccessControlManager;
import org.ccnx.ccn.profiles.security.access.AccessControlProfile;
import org.ccnx.ccn.profiles.security.access.group.GroupAccessControlManager;
import org.ccnx.ccn.protocol.ContentName;
import org.ccnx.ccn.protocol.MalformedContentNameStringException;

/**
 *
 **/
public class NamespaceManager {
	
	protected static Map<ContentName, Class<? extends AccessControlManager>> _accessControlManagerTypes = 
				new TreeMap<ContentName, Class<? extends AccessControlManager>>();

	protected static Set<AccessControlManager> _acmList = new HashSet<AccessControlManager>(); 
	protected static Set<ContentName> _searchedPathCache = new HashSet<ContentName>();
	
	static {
		try {
			NamespaceManager.registerAccessControlManagerType(
					ContentName.fromNative(GroupAccessControlManager.PROFILE_NAME), GroupAccessControlManager.class);
		} catch (MalformedContentNameStringException e) {
			throw new ErrorStateException("Cannot parse built-in profile name: " + GroupAccessControlManager.PROFILE_NAME);
		}
	}
	
	public static AccessControlManager createAccessControlManager(RootObject policyInformation, CCNHandle handle) throws ContentNotReadyException, ContentGoneException, ErrorStateException, InstantiationException, IllegalAccessException {
		Class<? extends AccessControlManager> acmClazz = null;
		synchronized(NamespaceManager.class) {
			acmClazz = _accessControlManagerTypes.get(policyInformation.root().profileName());
		}
		if (null != acmClazz) {
			AccessControlManager acm = (AccessControlManager)acmClazz.newInstance();
			acm.initialize(policyInformation, handle);
			return acm;
		}
		return null;
	}
	
	public static synchronized void registerAccessControlManagerType(ContentName profileName, 
																	 Class<? extends AccessControlManager> acmClazz) {
		_accessControlManagerTypes.put(profileName, acmClazz);
	}

	/**
	 * Find an ACM object that covers operations on a specific name. 
	 * If none exists in memory then this searches up the name tree 
	 * looking for the root of a namespace under access control,<commit_msg>Fixed exception in NameSpaceManager<commit_after>/**
 * Part of the CCNx Java Library.
 *
 * Copyright (C) 2008, 2009 Palo Alto Research Center, Inc.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation. 
 * This library 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 library;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301 USA.
 */

package org.ccnx.ccn.profiles.namespace;

import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.ccnx.ccn.CCNHandle;
import org.ccnx.ccn.config.ConfigurationException;
import org.ccnx.ccn.config.SystemConfiguration;
import org.ccnx.ccn.impl.support.Log;
import org.ccnx.ccn.io.ErrorStateException;
import org.ccnx.ccn.io.content.ContentGoneException;
import org.ccnx.ccn.io.content.ContentNotReadyException;
import org.ccnx.ccn.profiles.VersioningProfile;
import org.ccnx.ccn.profiles.namespace.Root.RootObject;
import org.ccnx.ccn.profiles.search.Pathfinder;
import org.ccnx.ccn.profiles.search.Pathfinder.SearchResults;
import org.ccnx.ccn.profiles.security.access.AccessControlManager;
import org.ccnx.ccn.profiles.security.access.AccessControlProfile;
import org.ccnx.ccn.profiles.security.access.group.GroupAccessControlManager;
import org.ccnx.ccn.protocol.ContentName;
import org.ccnx.ccn.protocol.MalformedContentNameStringException;

/**
 *
 **/
public class NamespaceManager {
	
	protected static Map<ContentName, Class<? extends AccessControlManager>> _accessControlManagerTypes = 
				new TreeMap<ContentName, Class<? extends AccessControlManager>>();

	protected static Set<AccessControlManager> _acmList = new HashSet<AccessControlManager>(); 
	protected static Set<ContentName> _searchedPathCache = new HashSet<ContentName>();
	
	static {
		try {
			NamespaceManager.registerAccessControlManagerType(ContentName.fromNative(GroupAccessControlManager.PROFILE_NAME_STRING), 
																GroupAccessControlManager.class);
		} catch (MalformedContentNameStringException e) {
			throw new RuntimeException("Cannot parse built-in profile name: " + GroupAccessControlManager.PROFILE_NAME_STRING);
		}
	}
	
	public static AccessControlManager createAccessControlManager(RootObject policyInformation, CCNHandle handle) throws ContentNotReadyException, ContentGoneException, ErrorStateException, InstantiationException, IllegalAccessException {
		Class<? extends AccessControlManager> acmClazz = null;
		synchronized(NamespaceManager.class) {
			acmClazz = _accessControlManagerTypes.get(policyInformation.root().profileName());
		}
		if (null != acmClazz) {
			AccessControlManager acm = (AccessControlManager)acmClazz.newInstance();
			acm.initialize(policyInformation, handle);
			return acm;
		}
		return null;
	}
	
	public static synchronized void registerAccessControlManagerType(ContentName profileName, 
																	 Class<? extends AccessControlManager> acmClazz) {
		_accessControlManagerTypes.put(profileName, acmClazz);
	}

	/**
	 * Find an ACM object that covers operations on a specific name. 
	 * If none exists in memory then this searches up the name tree 
	 * looking for the root of a namespace under access control,
{'fraction_non_alphanumeric': 0.08200330214639516, 'fraction_numerical': 0.005778756191524491, 'mean_word_length': 5.293506493506493, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3019778', 'n_tokens_mistral': 2171, 'n_tokens_neox': 1989, 'n_words': 573}