📄 repository.java
字号:
// Extract the domain name. String dom= name.getDomain().intern(); // Default domain case if (dom.length() == 0) { dom = domain; } Map<String,NamedObject> moiTb = domainTb.get(dom); if (moiTb == null) { return null; // No domain containing registered object names } return moiTb.get(name.getCanonicalKeyPropertyListString()); } // Private methods <============================================= // Protected methods ---------------------------------------------> // Protected methods <============================================= // Public methods ---------------------------------------------> /** * Construct a new repository with the given default domain. */ public Repository(String domain) { domainTb = new HashMap<String,Map<String,NamedObject>>(5); if (domain != null && domain.length() != 0) this.domain = domain; else this.domain = ServiceName.DOMAIN; // Creates an new hastable for the default domain domainTb.put(this.domain.intern(), new HashMap<String,NamedObject>()); } /** * Returns the list of domains in which any MBean is currently * registered. * * @since.unbundled JMX RI 1.2 */ public String[] getDomains() { final List<String> result; synchronized(domainTb) { // Temporary list result = new ArrayList<String>(domainTb.size()); for (Map.Entry<String,Map<String,NamedObject>> entry : domainTb.entrySet()) { // Skip domains that are in the table but have no // MBean registered in them // in particular the default domain may be like this Map<String,NamedObject> t = entry.getValue(); if (t != null && t.size() != 0) result.add(entry.getKey()); } } // Make an array from result. return result.toArray(new String[result.size()]); } /** * Stores an MBean associated with its object name in the repository. * * @param object MBean to be stored in the repository. * @param name MBean object name. */ public void addMBean(final DynamicMBean object, ObjectName name) throws InstanceAlreadyExistsException { if (isTraceOn()) { trace("addMBean", "name=" + name); } // Extract the domain name. String dom = name.getDomain().intern(); boolean to_default_domain = false; // Set domain to default if domain is empty and not already set if (dom.length() == 0) { try { name = new ObjectName(domain + name.toString()); } catch (MalformedObjectNameException e) { if (isDebugOn()) { debug("addMBean", "Unexpected MalformedObjectNameException"); } } } // Do we have default domain ? if (dom == domain) { to_default_domain = true; dom = domain; } else { to_default_domain = false; } // Validate name for an object if (name.isPattern()) { throw new RuntimeOperationsException( new IllegalArgumentException("Repository: cannot add mbean for " + "pattern name " + name.toString())); } // Domain cannot be JMImplementation if entry does not exists if ( !to_default_domain && dom.equals("JMImplementation") && domainTb.containsKey("JMImplementation")) { throw new RuntimeOperationsException( new IllegalArgumentException( "Repository: domain name cannot be JMImplementation")); } // If domain not already exists, add it to the hash table final Map<String,NamedObject> moiTb = domainTb.get(dom); if (moiTb == null) { addNewDomMoi(object, dom, name); return; } else { // Add instance if not already present String cstr = name.getCanonicalKeyPropertyListString(); NamedObject elmt= moiTb.get(cstr); if (elmt != null) { throw new InstanceAlreadyExistsException(name.toString()); } else { nbElements++; moiTb.put(cstr, new NamedObject(name, object)); } } } /** * Checks whether an MBean of the name specified is already stored in * the repository. * * @param name name of the MBean to find. * * @return true if the MBean is stored in the repository, * false otherwise. */ public boolean contains(ObjectName name) { if (isTraceOn()) { trace("contains", "name=" + name); } return (retrieveNamedObject(name) != null); } /** * Retrieves the MBean of the name specified from the repository. The * object name must match exactly. * * @param name name of the MBean to retrieve. * * @return The retrieved MBean if it is contained in the repository, * null otherwise. */ public DynamicMBean retrieve(ObjectName name) { if (isTraceOn()) { trace("retrieve", "name=" + name); } // Calls internal retrieve method to get the named object NamedObject no = retrieveNamedObject(name); if (no == null) return null; else return no.getObject(); } /** * Selects and retrieves the list of MBeans whose names match the specified * object name pattern and which match the specified query expression * (optionally). * * @param pattern The name of the MBean(s) to retrieve - may be a specific * object or a name pattern allowing multiple MBeans to be selected. * @param query query expression to apply when selecting objects - this * parameter will be ignored when the Repository Service does not * support filtering. * * @return The list of MBeans selected. There may be zero, one or many * MBeans returned in the set. */ public Set<NamedObject> query(ObjectName pattern, QueryExp query) { final Set<NamedObject> result = new HashSet<NamedObject>(); // The following filter cases are considered: // null, "", "*:*" : names in all domains // ":*", ":[key=value],*" : names in defaultDomain // "domain:*", "domain:[key=value],*" : names in the specified domain // Surely one of the most frequent case ... query on the whole world ObjectName name = null; if (pattern == null || pattern.getCanonicalName().length() == 0 || pattern.equals(ObjectName.WILDCARD)) name = ObjectName.WILDCARD; else name = pattern; // If pattern is not a pattern, retrieve this mbean ! if (!name.isPattern()) { final NamedObject no = retrieveNamedObject(name); if (no != null) result.add(no); return result; } // All names in all domains if (name == ObjectName.WILDCARD) { synchronized(domainTb) { for (Map<String,NamedObject> moiTb : domainTb.values()) { result.addAll(moiTb.values()); } } return result; } String canonical_key_property_list_string = name.getCanonicalKeyPropertyListString(); // All names in default domain if (name.getDomain().length() == 0) { final Map<String,NamedObject> moiTb = domainTb.get(domain); if (canonical_key_property_list_string.length() == 0) result.addAll(moiTb.values()); else addAllMatching(moiTb, result, new ObjectNamePattern(name)); return result; } // Pattern matching in the domain name (*, ?) synchronized (domainTb) { char[] dom2Match = name.getDomain().toCharArray(); for (String domain : domainTb.keySet()) { char[] theDom = domain.toCharArray(); if (wildmatch(theDom, dom2Match)) { final Map<String,NamedObject> moiTb = domainTb.get(domain); if (canonical_key_property_list_string.length() == 0) result.addAll(moiTb.values()); else addAllMatching(moiTb, result, new ObjectNamePattern(name)); } } } return result; } /** * Removes an MBean from the repository. * * @param name name of the MBean to remove. * * @exception InstanceNotFoundException The MBean does not exist in * the repository. */ public void remove(final ObjectName name) throws InstanceNotFoundException { // Debugging stuff if (isTraceOn()) { trace("remove", "name=" + name); } // Extract domain name. String dom= name.getDomain().intern(); // Default domain case if (dom.length() == 0) dom = domain; // Find the domain subtable Map<String,NamedObject> moiTb = domainTb.get(dom); if (moiTb == null) { throw new InstanceNotFoundException(name.toString()); } // Remove the corresponding element if (moiTb.remove(name.getCanonicalKeyPropertyListString()) == null) { throw new InstanceNotFoundException(name.toString()); } // We removed it ! nbElements--; // No more object for this domain, we remove this domain hashtable if (moiTb.isEmpty()) { domainTb.remove(dom); // set a new default domain table (always present) // need to reinstantiate a hashtable because of possible // big buckets array size inside table, never cleared, // thus the new ! if (dom == domain) domainTb.put(domain, new HashMap<String,NamedObject>()); } } /** * Gets the number of MBeans stored in the repository. * * @return Number of MBeans. */ public Integer getCount() { return new Integer(nbElements); } /** * Gets the name of the domain currently used by default in the * repository. * * @return A string giving the name of the default domain name. */ public String getDefaultDomain() { return domain; } // Public methods <=============================================}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -