001    /*******************************************************************************
002     * Copyright (C) PicoContainer Organization. All rights reserved. 
003     * ---------------------------------------------------------------------------
004     * The software in this package is published under the terms of the BSD style
005     * license a copy of which has been included with this distribution in the
006     * LICENSE.txt file. 
007     ******************************************************************************/
008    package org.picocontainer.classname;
009    
010    import java.io.Serializable;
011    import java.net.URL;
012    import java.security.Permission;
013    import java.security.Permissions;
014    import java.util.ArrayList;
015    import java.util.List;
016    
017    /**
018     * ClassPathElement denotes an element in a classpath allowing to grant permissions.
019     * 
020     * @author Paul Hammant
021     */
022    @SuppressWarnings("serial")
023    public class ClassPathElement implements Serializable {
024    
025        private final URL url;
026        private Permissions permissionCollection;
027        private final List<Permission> permissions = new ArrayList<Permission>();
028        
029        public ClassPathElement(URL url) {
030            this.url = url;
031        }
032    
033        public Permission grantPermission(Permission permission) {
034            if (permission == null) {
035                throw new NullPointerException();
036            }
037            permissions.add(permission);
038            return permission;
039        }
040    
041        public URL getUrl() {
042            return url;
043        }
044    
045        public Permissions getPermissionCollection() {
046            if (permissionCollection == null) {
047                permissionCollection = new Permissions();
048                for (Permission permission : permissions) {
049                    permissionCollection.add(permission);
050                }
051            }
052            return permissionCollection;
053        }
054    
055        public String toString() {
056            return "[" + System.identityHashCode(this) + " " + url + " " + permissions.size() +"]";
057        }
058    
059    }