📄 allocationpermission.java
字号:
/* * AllocationPermission.java * * Created on October 7, 2003, 10:14 AM */package gov.nist.security.permissions;import java.util.*;/** * Permission checking if the amount of allocated memory in the JVM has been exceeded * @author DERUELLE Jean */public class AllocationPermission extends java.security.Permission{ /**maximum memory to allocate in the JVM*/ int memoryAllowed=0; /**mask of the actions*/ protected int mask; static private int ALLOCATION=0x01; /** * Creates a new instance of AllocationPermission * @param name - name of the permission */ public AllocationPermission(String name) { this(name,"allocation"); try{ memoryAllowed=Integer.parseInt(name); } catch(NumberFormatException nfe){ nfe.printStackTrace(); } } /** * Creates a new instance of AllocationPermission * @param name - name of the permission * @param actions - the actions of the permission */ public AllocationPermission(String name, String actions) { super(name); try{ memoryAllowed=Integer.parseInt(name); } catch(NumberFormatException nfe){ nfe.printStackTrace(); } parse(actions); } /** * Look into the actions String to get the actions * associated with that permission * @param actions - the actions String */ private void parse(String actions){ //Look into the action string for //the words incoming or outgoing or both StringTokenizer st=new StringTokenizer(actions,",\t"); mask=0; while(st.hasMoreTokens()){ String tok=st.nextToken(); if(tok.equals("allocation")) mask|=ALLOCATION; else throw new IllegalArgumentException("Unknown action "+ tok); } } /** * This method return the actions' String * @return the actions of the permission */ public String getActions() { //This method must return the same String, no matter how //the action list was passed to the constructor. if(mask==0) return ""; else if(mask==ALLOCATION) return "allocation"; else throw new IllegalArgumentException("Unknown mask"); } /** * Checks if the specified permission's actions are "implied by" this object's actions. * @param permission - the permission to check against. * @return if the specified permission is implied by this object, false if not. */ public boolean implies(java.security.Permission permission) { if(!(permission instanceof AllocationPermission)) return false; AllocationPermission allocationPermission=(AllocationPermission)permission; //Similarly, the requested actions must macth all match actions //that we've been constructed with if((mask & allocationPermission.mask) !=allocationPermission.mask) return false; int memoryAllocated=0; try{ memoryAllocated=Integer.parseInt(allocationPermission.getName()); } catch(NumberFormatException nfe){ nfe.printStackTrace(); return false; } if(memoryAllocated<=memoryAllowed) return true; return false; } /** * @see java.security.Permission#hashCode() */ public int hashCode(){ //We must always provide the same hashcode for permissions //because the hashes must match if the permissions compare // as equals return getName().hashCode() ^ mask; } /** * @see java.security.Permission#equals(Object obj) */ public boolean equals(Object o){ if(!(o instanceof AllocationPermission)) return false; AllocationPermission allocationPermission=(AllocationPermission)o; //For equality, we check the name and the action mask return ((allocationPermission.getName().equals(getName())) && (allocationPermission.mask == mask)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -