001    /*
002     * Copyright (C) 2014 XStream Committers.
003     * All rights reserved.
004     *
005     * Created on 09. January 2014 by Joerg Schaible
006     */
007    package com.thoughtworks.xstream.security;
008    
009    import java.util.Arrays;
010    import java.util.Collections;
011    import java.util.HashSet;
012    import 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     */
021    public 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    }