accesslist.java
来自「JXTA™ is a set of open, generalize」· Java 代码 · 共 564 行 · 第 1/2 页
JAVA
564 行
/** * sets the ACL description * * @param description The new description */ public void setDescrption(String description) { this.description = description; } /** * sets the entries list * * @param map The new access map */ protected void setEntries(Map<ID, Entry> map) { accessMap.clear(); accessMap.putAll(map); } /** * Refreshes the access list * * @param acl The access list to refresh from */ private void refresh(AccessList acl) { grantAll = acl.grantAll; description = acl.description; accessMap.clear(); accessMap.putAll(acl.accessMap); } /** * Adds an ACL entry * * @param entry the entry to add */ public void add(Entry entry) { if (!accessMap.containsKey(entry.id)) { accessMap.put(entry.id, entry); } } /** * Removes an ACL entry * * @param entry the entry to remove */ public void remove(Entry entry) { if (accessMap.containsKey(entry.id)) { accessMap.remove(entry.id); } } /** * Determines if an entry has access * * @param id the PeerID * @return ture if access is allowed, always true if grantAll is set */ public boolean isAllowed(ID id) { if (grantAll) { return true; } else if (accessMap.containsKey(id)) { Entry entry = accessMap.get(id); return entry.access; } else { return false; } } /** * gets the entries list * * @return List The List containing Entries */ public Map<ID, Entry> getAccessMap() { return accessMap; } /** * Returns the Document * * @param asMimeType mime type encoding * @return The document value */ public Document getDocument(MimeMediaType asMimeType) { StructuredDocument adv = StructuredDocumentFactory.newStructuredDocument(asMimeType, getAdvertisementType()); if (adv instanceof XMLDocument) { ((XMLDocument) adv).addAttribute("xmlns:jxta", "http://jxta.org"); } Element e; if (grantAll) { e = adv.createElement(GRANTALL_TAG, Boolean.valueOf(grantAll).toString()); adv.appendChild(e); } if (description != null) { e = adv.createElement(DESCRIPTION_TAG, description); adv.appendChild(e); } for (Object o : accessMap.values()) { Entry entry = (Entry) o; if (entry.id == null && entry.name == null) { // skip bad entries continue; } e = adv.createElement(PEER_TAG, entry.id.toString()); adv.appendChild(e); ((Attributable) e).addAttribute(NAME_TAG, entry.name); if (entry.access) { ((Attributable) e).addAttribute(ACCESS_TAG, ACCESS_TAG_GRANT_VALUE); } else { ((Attributable) e).addAttribute(ACCESS_TAG, ACCESS_TAG_DENY_VALUE); } } return adv; } /** * Process an individual element from the document. * * @param doc the element */ protected void initialize(XMLElement doc) { Enumeration elements = doc.getChildren(); while (elements.hasMoreElements()) { XMLElement elem = (XMLElement) elements.nextElement(); if (GRANTALL_TAG.equals(elem.getName())) { grantAll = Boolean.getBoolean(elem.getTextValue()); if (Logging.SHOW_CONFIG && LOG.isLoggable(Level.CONFIG)) { LOG.config("Grant all access = [ " + grantAll + " ]"); } continue; } if (DESCRIPTION_TAG.equals(elem.getName())) { description = elem.getTextValue(); if (Logging.SHOW_CONFIG && LOG.isLoggable(Level.CONFIG)) { LOG.config("Loading [ " + description + " ] access list :"); } continue; } if (PEER_TAG.equals(elem.getName())) { String name = "NA"; Attribute nameAttr = elem.getAttribute(NAME_TAG); if (nameAttr != null) { name = nameAttr.getValue(); } String access = ACCESS_TAG_GRANT_VALUE; Attribute accessAttr = elem.getAttribute(ACCESS_TAG); if (accessAttr != null) { access = accessAttr.getValue(); } boolean acl = ACCESS_TAG_GRANT_VALUE.equalsIgnoreCase(access); ID pid; try { URI id = new URI(elem.getTextValue().trim()); pid = IDFactory.fromURI(id); } catch (URISyntaxException badID) { throw new IllegalArgumentException("unknown ID format in advertisement: " + elem.getTextValue()); } Entry entry = new Entry(pid, name, acl); if (Logging.SHOW_CONFIG && LOG.isLoggable(Level.CONFIG)) { LOG.config("Adding entry to access list :" + entry); } accessMap.put(entry.id, entry); continue; } if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.warning("Unrecognized tag : " + elem.getName()); } } } /** * returns the document string representation of this object * * @return String representation of the of this message type */ @Override public String toString() { try { XMLDocument doc = (XMLDocument) getDocument(MimeMediaType.XMLUTF8); return doc.toString(); } catch (Throwable e) { if (e instanceof Error) { throw (Error) e; } else if (e instanceof RuntimeException) { throw (RuntimeException) e; } else { throw new UndeclaredThrowableException(e); } } } /** * All messages have a type (in xml this is !doctype) which * identifies the message * * @return String "jxta:XACL" */ public static String getAdvertisementType() { return "jxta:XACL"; } /** * Entries class */ public final static class Entry { /** * Entry ID entry id */ public final ID id; /** * Entry name */ public final String name; /** * Entry name */ public final boolean access; /** * Creates a Entry with id and name * * @param id id * @param name node name * @param access access control */ public Entry(ID id, String name, boolean access) { this.id = id; this.name = name; this.access = access; } @Override public String toString() { return "[" + name + " access = " + access + " : " + id.toString() + "]"; } @Override public boolean equals(Object obj) { return this == obj || (obj != null && id.equals(((Entry) obj).id)); } @Override public int hashCode() { return id.hashCode(); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?