Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved rendering of arrays. #22

Merged
merged 1 commit into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@
<site>
<id>projects.doc</id>
<name>projects.doc</name>
<url>file:\\projects\doc\${pom.groupId}\${pom.version}</url>
<url>file:\\projects\doc\${project.groupId}\${project.version}</url>
</site>
<repository>
<id>maven2-post</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public final Map<String, Object> getModel()
{
HashMap<String,Object> result = new HashMap<String,Object>();

result.put("items", getList());
result.put(ITEMS_MODEL_ATTRIBUTE, getList());

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public abstract class AbstractTemplateResource extends ServerResource
public String a;

private final static String VELOCITY_ENGINE_CONTEX_KEY = "template.resource.velocity.engine";
protected final static String ATTRIBUTE_MODEL_ATTRIBUTE = "attribute";
protected final static String VALUE_MODEL_ATTRIBUTE = "value";
protected final static String ITEMS_MODEL_ATTRIBUTE = "items";
protected final EncoderBean encoder = new EncoderBean();

@Override
Expand Down Expand Up @@ -102,7 +105,7 @@ public Representation toRepresentation(Object source, Variant variant)
Map<String, Object> enrichedModel = new HashMap<String, Object>(model);

String templateName = getTemplateName();
Object resultObject = enrichedModel.get("value");
Object resultObject = enrichedModel.get(VALUE_MODEL_ATTRIBUTE);

if (resultObject instanceof InputStreamContent) {
return new InputRepresentation((InputStreamContent) resultObject, MediaType.APPLICATION_OCTET_STREAM);
Expand Down Expand Up @@ -187,9 +190,9 @@ else if (MediaType.APPLICATION_JSON.equals(variant.getMediaType()))
getRequest().getOriginalRef().getSegments().size() - 3) : null;
boolean leaf = "attributes".equals(beforeLast) || "operations".equals(beforeLast);

if (model.containsKey("items") && !leaf)
if (model.containsKey(ITEMS_MODEL_ATTRIBUTE) && !leaf)
{
Object items = model.get("items");
Object items = model.get(ITEMS_MODEL_ATTRIBUTE);

Collection<Object> itemCollection = null;
if (items instanceof Collection)
Expand Down Expand Up @@ -224,17 +227,17 @@ else if (item instanceof Map && ((Map) item).containsKey("declaration"))
}
else
{
if (model.containsKey("value"))
if (model.containsKey(VALUE_MODEL_ATTRIBUTE))
{
if(model.get("value") instanceof HtmlContent) {
if(model.get(VALUE_MODEL_ATTRIBUTE) instanceof HtmlContent) {
result.put("value", "...");
} else {
result.put("value", model.get("value").toString());
}
}
else if (model.containsKey("items"))
else if (model.containsKey(ITEMS_MODEL_ATTRIBUTE))
{
Object items = model.get("items");
Object items = model.get(ITEMS_MODEL_ATTRIBUTE);
String value = null;
if(items.getClass().isArray()) {
value = Arrays.deepToString(Arrays.asList(items).toArray());
Expand Down
71 changes: 34 additions & 37 deletions src/main/java/org/jminix/console/resource/AttributeResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@

package org.jminix.console.resource;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.management.Attribute;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.InvalidAttributeValueException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.RuntimeErrorException;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.TabularData;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jminix.type.AttributeFilter;
Expand All @@ -25,14 +44,6 @@
import org.restlet.resource.Post;
import org.restlet.resource.ResourceException;

import javax.management.*;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.TabularData;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class AttributeResource extends AbstractTemplateResource
{

Expand Down Expand Up @@ -71,47 +82,33 @@ public Map<String, Object> getModel()

Object value = server.getAttribute(new ObjectName(domain+":"+mbean), attribute);

model.put("attribute", info);
model.put(ATTRIBUTE_MODEL_ATTRIBUTE, info);

if((value instanceof CompositeData) && getAttribute("item")!=null) {
String item = getDecodedAttribute("item");
log.debug("Reading key " + item + " from " + value);
value = ((CompositeData) value).get(item);
}

if(value==null) {
model.put("value", "<null>");
} else if(value.getClass().isArray()) {
templateName = "array-attribute";
if(value != null) {
value = filter(value);
} else {
value = "<null>";
}

if(value.getClass().isArray()) {
if(value.getClass().getComponentType().isAssignableFrom(CompositeData.class)) {
CompositeData[] data = (CompositeData[])value;
String[] values = new String[data.length];
for(int i=0; i<data.length; i++) {
Set<String> keys = data[i].getCompositeType().keySet();
StringBuilder sb = new StringBuilder("{");
for(String key: keys) {
if (sb.length() > 1) {
sb.append(", ");
}
sb.append(key);
sb.append(": ");
sb.append(data[i].get(key));
}
sb.append("}");
values[i]=sb.toString();
}
model.put("items", values);
templateName = "composite-array-attribute";
} else {
model.put("items", value);
templateName = "array-attribute";
}
model.put(ITEMS_MODEL_ATTRIBUTE, value);
} else if(value instanceof CompositeData){
templateName = "composite-attribute";
model.put("attribute", filter(value));
model.put(VALUE_MODEL_ATTRIBUTE, value);
} else if(value instanceof TabularData){
templateName = "tabular-attribute";
model.put("attribute", filter(value));
} else {
model.put("value", filter(value));
model.put(VALUE_MODEL_ATTRIBUTE, value);
}

return model;
Expand Down Expand Up @@ -139,7 +136,7 @@ public Map<String, Object> getModel()
throw new RuntimeException(targetException.getCause());
}
log.warn("Error accessing attribute", e);
model.put("value", e.getTargetException().getCause().getMessage());
model.put(VALUE_MODEL_ATTRIBUTE, e.getTargetException().getCause().getMessage());
return model;
}
catch (ReflectionException e)
Expand All @@ -151,7 +148,7 @@ public Map<String, Object> getModel()
throw new RuntimeException(e);
}
catch(RuntimeException e) {
model.put("value", e.getMessage());
model.put(VALUE_MODEL_ATTRIBUTE, e.getMessage());
log.warn("Error accessing attribute", e);
return model;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public StandaloneMiniConsole(int port) {

/**
* @param port the listening HTTP port
* @param the application object, if you want to create it by yourself.
* @param application the application object, if you want to create it by yourself.
*/
public StandaloneMiniConsole(int port, MiniConsoleApplication application) {
// Create a new Component.
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/jminix/templates/array-attribute.vm
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ $!{attribute.description}
$i
</li>
#end
</ul>
#parse('jminix/templates/footer.vm')
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#foreach( $i in $items)
$i
#foreach( $key in $i.compositeType.keySet() )
$key : $i.get( $key )
#end
#end
18 changes: 18 additions & 0 deletions src/main/resources/jminix/templates/composite-array-attribute.vm
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#set( $title="Server $request.attributes.get('server') > $encoder.decode($request.attributes.get('domain')) > $encoder.decode($request.attributes.get('mbean')) > $encoder.decode($request.attributes.get('attribute'))" )
#set( $name="$encoder.decode($request.attributes.get('attribute'))" )
#parse('jminix/templates/header.vm')
#if($desc!="off")
$!{attribute.description}
#end
<ul>
#foreach( $i in $items)
<li>
<dl>
#foreach( $key in $i.compositeType.keySet() )
<dt>$key : $i.get( $key ) <dd> $i.compositeType.getDescription( $key )
#end
</dl>
</li>
#end
</ul>
#parse('jminix/templates/footer.vm')
8 changes: 5 additions & 3 deletions src/main/resources/jminix/templates/composite-attribute.vm
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#set( $title="Server $request.attributes.get('server') > $encoder.decode($request.attributes.get('domain')) > $encoder.decode($request.attributes.get('mbean')) > $encoder.decode($request.attributes.get('attribute'))" )
#set( $name="$encoder.decode($request.attributes.get('attribute'))" )
#parse('jminix/templates/header.vm')

#if($desc!="off")
$!{attribute.description}
#end
<dl>
#foreach( $key in $attribute.compositeType.keySet() )
<dt>$key : $attribute.get( $key ) <dd> $attribute.compositeType.getDescription( $key )
#foreach( $key in $value.compositeType.keySet() )
<dt>$key : $value.get( $key ) <dd> $value.compositeType.getDescription( $key )
#end
</dl>
#parse('jminix/templates/footer.vm')
10 changes: 5 additions & 5 deletions src/main/resources/jminix/templates/tabular-attribute.vm
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#set( $title="Server $request.attributes.get('server') > $encoder.decode($request.attributes.get('domain')) > $encoder.decode($request.attributes.get('mbean')) > $encoder.decode($request.attributes.get('attribute'))" )
#set( $name="$encoder.decode($request.attributes.get('attribute'))" )
#parse('jminix/templates/header.vm')
<html>
<h3></h3>

#if($desc!="off")
$!{attribute.description}
#end
<table>
<tr>
#foreach( $key in $attribute.tabularType.rowType.keySet() )
#foreach( $key in $value.tabularType.rowType.keySet() )
<th>$key</th>
#end
</tr>
#foreach ($row in $attribute.values())
#foreach ($row in $value.values())
<tr>
#foreach( $item in $row.compositeType.keySet() )
<td>$row.get($item)</td>
Expand Down