Skip to content

Commit

Permalink
added a helper method to parse object name entries
Browse files Browse the repository at this point in the history
  • Loading branch information
jstrachan committed May 9, 2013
1 parent 893be4e commit c50fb3a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
23 changes: 23 additions & 0 deletions hawtio-web/src/main/webapp/app/core/js/helpers.ts
Expand Up @@ -732,4 +732,27 @@ module Core {
var pluralWord = (count === 1) ? word : word.pluralize();
return "" + count + " " + pluralWord;
}

/**
* given a JMX ObjectName of the form <code>domain:key=value,another=something</code> then return the object
* <code>{key: "value", another: "something"}</code>
* @param name
*/
export function objectNameProperties(objectName: string) {
var entries = {};
if (objectName) {
var idx = objectName.indexOf(":");
if (idx > 0) {
var path = objectName.substring(idx + 1);
var items = path.split(',');
angular.forEach(items, (item) => {
var kv = item.split('=');
var key = kv[0];
var value = kv[1] || key;
entries[key] = value;
});
}
}
return entries;
}
}
20 changes: 20 additions & 0 deletions hawtio-web/src/test/specs/spec/CoreSpec.js
Expand Up @@ -76,4 +76,24 @@ describe("Core", function () {
expect(creds[0]).toEqual("foo");
expect(creds[1]).toEqual("bar");
});


assertObjectNameKeyValue("org.apache.activemq", { "dummy": null });

assertObjectNameKeyValue("org.apache.activemq:brokerName=foo", { "dummy": null });

assertObjectNameKeyValue("org.apache.activemq:brokerName=broker1,clientId=dejan,type=Broker", {
"brokerName": "broker1",
"clientId": "dejan",
"type": "Broker"
});

function assertObjectNameKeyValue(objectName, values) {
var entries = Core.objectNameProperties(objectName);
angular.forEach(values, function (value, key) {
it("objectName " + objectName + " should have key " + key, function () {
expect(entries[key]).toEqual(value);
});
});
}
});

0 comments on commit c50fb3a

Please sign in to comment.