001/*
002 * Copyright (C) 2014 XStream Committers.
003 * All rights reserved.
004 *
005 * Created on 09. January 2014 by Joerg Schaible
006 */
007package com.thoughtworks.xstream.security;
008
009import java.util.Arrays;
010import java.util.Collections;
011import java.util.HashSet;
012import java.util.Set;
013
014
015/**
016 * Explicit permission for a type with a name matching one in the provided list.
017 * 
018 * @author Jörg Schaible
019 * @since 1.4.7
020 */
021public class ExplicitTypePermission implements TypePermission {
022
023    final Set names;
024
025    /**
026     * @since 1.4.7
027     */
028    public ExplicitTypePermission(final Class[] types) {
029        this(new Object() {
030            public String[] getNames() {
031                if (types == null)
032                    return null;
033                String[] names = new String[types.length];
034                for (int i = 0; i < types.length; ++i)
035                    names[i] = types[i].getName();
036                return names;
037            }
038        }.getNames());
039    }
040
041    /**
042     * @since 1.4.7
043     */
044    public ExplicitTypePermission(String[] names) {
045        this.names = names == null ? Collections.EMPTY_SET : new HashSet(Arrays.asList(names));
046    }
047
048    public boolean allows(Class type) {
049        if (type == null)
050            return false;
051        return names.contains(type.getName());
052    }
053
054}