001    /*****************************************************************************
002     * Copyright (C) NanoContainer Organization. All rights reserved.            *
003     * ------------------------------------------------------------------------- *
004     * The software in this package is published under the terms of the BSD      *
005     * style license a copy of which has been included with this distribution in *
006     * the LICENSE.txt file.                                                     *
007     *****************************************************************************/
008    package org.nanocontainer;
009    
010    import java.io.Serializable;
011    import java.net.URL;
012    import java.security.Permission;
013    import java.security.PermissionCollection;
014    import java.security.Permissions;
015    import java.util.ArrayList;
016    import java.util.List;
017    
018    /**
019     * ClassPathElement denotes an element in a classpath allowing to grant permissions.
020     * 
021     * @author Paul Hammant
022     */
023    public class ClassPathElement implements Serializable {
024    
025        private final URL url;
026        private PermissionCollection permissionCollection;
027        private final List permissions = new ArrayList();
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 PermissionCollection getPermissionCollection() {
046            if (permissionCollection == null) {
047                permissionCollection = new Permissions();
048                for (int i = 0; i < permissions.size(); i++) {
049                    Permission permission = (Permission) permissions.get(i);
050                    permissionCollection.add(permission);
051                }
052            }
053            return permissionCollection;
054        }
055    
056        public String toString() {
057            return "[" + System.identityHashCode(this) + " " + url + " " + permissions.size() +"]";
058        }
059    
060    }