001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.com
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package org.huihoo.jfox.jmx;
009:
010: import java.util.Iterator;
011: import java.util.Set;
012: import java.util.HashSet; //import java.util.Hashtable;
013: //import java.util.Map;
014: //import java.util.List;
015: //import java.util.ArrayList;
016:
017: import javax.management.ObjectName;
018: import javax.management.InstanceNotFoundException;
019: import javax.management.InstanceAlreadyExistsException;
020: import javax.management.RuntimeOperationsException;
021: import javax.management.MalformedObjectNameException;
022: import javax.management.QueryExp;
023: import javax.management.ObjectInstance;
024: import javax.management.MBeanServer;
025:
026: /**
027: *
028: * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
029: */
030:
031: public class MBeanServerRepositoryController {
032:
033: // private List domains = new ArrayList();
034: private MBeanServerRepository repo = new MBeanServerRepositorySupport();
035:
036: // private String domain;
037:
038: private MBeanServer server = null;
039:
040: public MBeanServerRepositoryController(MBeanServer server) {
041: this .server = server;
042: }
043:
044: public String getDomain() {
045: return server.getDefaultDomain();
046: }
047:
048: // public void setDomain(String domain) {
049: // this.domain = domain;
050: // domains.add(domain);
051: // }
052:
053: public MBeanMetaData retrieve(ObjectName name)
054: throws InstanceNotFoundException {
055: if (name.isPattern())
056: throw new RuntimeOperationsException(
057: new IllegalArgumentException("ObjectName "
058: + name.toString()
059: + " is a pattern object name"));
060: name = this .getCompleteObjectName(name);
061: if (!contains(name))
062: throw new InstanceNotFoundException(name.toString());
063: return repo.get(name);
064: }
065:
066: public void store(ObjectName name, MBeanMetaData metadata)
067: throws InstanceAlreadyExistsException {
068: if (name.isPattern())
069: throw new RuntimeOperationsException(
070: new IllegalArgumentException("ObjectName "
071: + name.toString()
072: + " is a pattern object name"));
073: name = this .getCompleteObjectName(name);
074: if (contains(name))
075: throw new InstanceAlreadyExistsException(name.toString());
076: repo.put(name, metadata);
077: }
078:
079: public void remove(ObjectName name)
080: throws InstanceNotFoundException {
081: if (name.isPattern())
082: throw new RuntimeOperationsException(
083: new IllegalArgumentException("ObjectName "
084: + name.toString()
085: + " is a pattern object name"));
086: name = this .getCompleteObjectName(name);
087: if (!contains(name))
088: throw new InstanceNotFoundException(name.toString());
089: repo.remove(name);
090: }
091:
092: public int getRepoSize() {
093: return repo.size();
094: }
095:
096: public boolean contains(ObjectName name) {
097: if (name.isPattern())
098: throw new RuntimeOperationsException(
099: new IllegalArgumentException("ObjectName "
100: + name.toString()
101: + " is a pattern object name"));
102: name = this .getCompleteObjectName(name);
103: return repo.contains(name);
104: }
105:
106: public Iterator iterator() {
107: return repo.iterator();
108: }
109:
110: public Set queryMBeans(ObjectName pattern, QueryExp query) {
111: HashSet set = new HashSet();
112: if (pattern == null) {
113: try {
114: pattern = new ObjectName("*:*");
115: } catch (MalformedObjectNameException malformedobjectnameexception) {
116: }
117: }
118: pattern = getCompleteObjectName(pattern);
119:
120: // not pattern ,single mbean
121: if (!pattern.isPattern()) {
122: try {
123: MBeanMetaData meta = retrieve(pattern);
124: if (meta != null)
125: set.add(meta.getObjectInstance());
126: } catch (InstanceNotFoundException ignore) {
127: } catch (RuntimeOperationsException e) {
128: e.printStackTrace();
129: }
130: } else {
131: // String p_domain = pattern.getDomain();
132: Iterator it = this .iterator();
133: while (it.hasNext()) {
134: MBeanMetaData meta = (MBeanMetaData) it.next();
135: ObjectInstance instance = meta.getObjectInstance();
136: ObjectName objectName = instance.getObjectName();
137: // String domain = objectName.getDomain();
138: // if(matchDomain(domain,p_domain) && matchKeys(objectName,pattern)){ // domain & keys all matched
139: // set.add(instance);
140: // }
141: // changed from jmx1.2
142: try {
143: if (pattern.apply(objectName)) {
144: set.add(instance);
145: }
146: } catch (Exception e) {
147: e.printStackTrace();
148: }
149: }
150: }
151: return set;
152:
153: }
154:
155: /*
156: // domain match
157: private boolean matchDomain(String domain, String pattern) {
158: int s_index = 0; // string index
159: int s_length = domain.length(); // string length
160: int p_length = pattern.length();
161: for (int p_index = 0; p_index < p_length; ++p_index) {
162: char c = pattern.charAt(p_index);
163: if (c == '*') {
164: while (s_index < s_length) {
165: if (matchDomain(domain.substring(s_index),pattern.substring(p_index + 1))) return true;
166: s_index++;
167: }
168: }
169: else if (c == '?') {
170: s_index++;
171: if (s_index > s_length) return false;
172: }
173: else {
174: if (s_index >= s_length || c != domain.charAt(s_index)) return false;
175: s_index++;
176: }
177: }
178: return s_index == s_length;
179: }
180:
181: // keys match
182: private boolean matchKeys(ObjectName objectName, ObjectName pattern){
183: Hashtable nameTable = objectName.getKeyPropertyList();
184: Hashtable patternTable = pattern.getKeyPropertyList();
185: boolean matches = true;
186: if(!pattern.isPropertyPattern()){ // not pattern,must be equal
187: matches = nameTable.equals(patternTable);
188: }
189: else{ // is pattern, so nameTable must containsAll patterTable
190: Iterator it = patternTable.entrySet().iterator();
191: while(it.hasNext()){
192: Map.Entry entry = (Map.Entry)it.next();
193: String key = (String)entry.getKey();
194: String value = (String)entry.getValue();
195: if(!nameTable.containsKey(key) || !nameTable.get(key).equals(value)){
196: matches = false;
197: break;
198: }
199: }
200: }
201:
202: return matches;
203: }
204: */
205:
206: ObjectName getCompleteObjectName(ObjectName objectName) {
207: if (objectName.getDomain().trim().length() == 0) {// use defaultdomain
208: try {
209: objectName = new ObjectName(getDomain(), objectName
210: .getKeyPropertyList());
211: } catch (MalformedObjectNameException e) {
212: e.printStackTrace();
213: }
214: }
215: return objectName;
216: }
217:
218: String[] getDomains() {
219: Set domains = new HashSet();
220: for (Iterator iter = repo.keys().iterator(); iter.hasNext();) {
221: ObjectName objName = (ObjectName) iter.next();
222: domains.add(objName.getDomain());
223: }
224: return (String[]) domains.toArray(new String[0]);
225: }
226:
227: /*
228: public String[] getDomains() {
229: /* Permission check
230: SecurityManager sm = System.getSecurityManager();
231: if (sm != null) {
232: // Check if the caller has the right to invoke 'getDomains'
233: //
234: checkMBeanPermission(null, null, null, "getDomains");
235:
236: // Return domains
237: //
238: String[] domains = repository.getDomains();
239:
240: // Check if the caller has the right to invoke 'getDomains'
241: // on each specific domain in the list.
242: //
243: ArrayList result = new ArrayList(domains.length);
244: for (int i = 0; i < domains.length; i++) {
245: try {
246: ObjectName domain = new ObjectName(domains[i] + ":x=x");
247: checkMBeanPermission(null, null, domain, "getDomains");
248: result.add(domains[i]);
249: } catch (MalformedObjectNameException e) {
250: // Should never occur...
251: } catch (SecurityException e) {
252: // Do not add this domain to the list
253: }
254: }
255:
256: // Make an array from result.
257: //
258: return (String[]) result.toArray(new String[result.size()]);
259: } else {
260: return repository.getDomains();
261: }
262: }
263: */
264: }
|