Skip to content

Instantly share code, notes, and snippets.

@jarrodhroberson
Last active August 29, 2015 13:56
Show Gist options
  • Save jarrodhroberson/8994351 to your computer and use it in GitHub Desktop.
Save jarrodhroberson/8994351 to your computer and use it in GitHub Desktop.
ObjectName Serializer for Jackson, recursively serializes to JSON
package com.vertigrated.mbean.jackson;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import weblogic.health.HealthState;
import java.io.IOException;
public class HealthStateSerializer extends JsonSerializer<HealthState>
{
@Override
public void serialize(final HealthState value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException
{
jgen.writeStartObject();
jgen.writeStringField("MBeanName", value.getMBeanName());
jgen.writeStringField("MBeanType", value.getMBeanType());
jgen.writeStringField("SubSystemName", value.getSubsystemName());
jgen.writeNumberField("State", value.getState());
jgen.writeBooleanField("IsCritical", value.isCritical());
jgen.writeArrayFieldStart("ReasonCode");
for (final String s : value.getReasonCode())
{
jgen.writeString(s);
}
jgen.writeEndArray();
jgen.writeEndObject();
}
}
package com.vertigrated.mbean.jackson;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import javax.annotation.Nonnull;
import javax.management.*;
import java.io.IOException;
import java.util.*;
public class ObjectNameSerializer extends JsonSerializer<ObjectName>
{
private static final List<String> BANNED_NAMES;
static
{
BANNED_NAMES = Arrays.asList("Credential", "NodeManagerPassword", "JavaStandardTrustKeyStorePassPhrase", "CustomIdentityKeyStorePassPhrase",
"CustomTrustKeyStorePassPhrase", "Password", "ServerPrivateKeyPassPhrase", "ClientCertPrivateKeyPassPhrase",
"DefaultIIOPPassword", "SigningKeyPassPhrase", "SSLClientIdentityPassPhrase", "BasicAuthPassword", "TransportLayerSecurityKeyPassPhrase",
"SSOSigningKeyPassPhrase", "DefaultTGIOPPassword");
}
private final Set<ObjectName> seen;
private final MBeanServerConnection connection;
/**
* Serializer to render ObjectName objects to JSON
* @param connection MBeanServerConnection to use to look up the attributes of the MBean that an ObjectName refers to.
*/
public ObjectNameSerializer(@Nonnull final MBeanServerConnection connection)
{
this.seen = new HashSet<ObjectName>();
this.connection = connection;
}
/**
* Serialize an ObjectName with all its attributes or only its String representation if it is a circular reference.
* @param on ObjectName to serialize
* @param jgen JsonGenerator to build the output
* @param provider SerializerProvider
* @throws IOException
* @throws JsonProcessingException
*/
@Override
public void serialize(@Nonnull final ObjectName on, @Nonnull final JsonGenerator jgen, @Nonnull final SerializerProvider provider) throws IOException, JsonProcessingException
{
if (this.seen.contains(on))
{
jgen.writeString(on.toString());
}
else
{
this.seen.add(on);
jgen.writeStartObject();
final List<MBeanAttributeInfo> ais = this.getAttributeInfos(on);
for (final MBeanAttributeInfo ai : ais)
{
final Object attribute = this.getAttribute(on, ai.getName());
jgen.writeObjectField(ai.getName(), attribute);
}
jgen.writeEndObject();
}
}
/**
* Get an attribute by name
*
* @param objectName ObjectName of the context the attribute is in
* @param name name of the attribute
* @return untyped Object that is the value of the attribute
*/
private Object getAttribute(@Nonnull final ObjectName objectName, @Nonnull final String name)
{
try
{
if (!BANNED_NAMES.contains(name))
{
return this.connection.getAttribute(objectName, name);
}
else
{
return null;
}
}
catch (MBeanException e)
{
throw new RuntimeException(e);
}
catch (AttributeNotFoundException e)
{
throw new RuntimeException(e);
}
catch (InstanceNotFoundException e)
{
return null;
}
catch (ReflectionException e)
{
throw new RuntimeException(e);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
/**
* Get all the metadata for all the attributes associated with an ObjectName
*
* @param name ObjectName to get the attributes for
* @return List of MBeanAttributeInfo
*/
private List<MBeanAttributeInfo> getAttributeInfos(@Nonnull final ObjectName name)
{
try
{
return Arrays.asList(ObjectNameSerializer.this.connection.getMBeanInfo(name).getAttributes());
}
catch (InstanceNotFoundException e)
{
return new ArrayList<MBeanAttributeInfo>(0);
}
catch (IntrospectionException e)
{
throw new RuntimeException(e);
}
catch (ReflectionException e)
{
throw new RuntimeException(e);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}
package com.vertigrated.mbean;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.vertigrated.mbean.jackson.HealthStateSerializer;
import com.vertigrated.mbean.jackson.ObjectNameSerializer;
import weblogic.health.HealthState;
import javax.management.*;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.*;
public class TestServiceLookup
{
private static final ObjectName service;
private static final JMXConnector connector;
private static final MBeanServerConnection connection;
private static final Set<ObjectName> seen;
static
{
try
{
service = new ObjectName("com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean");
final JMXServiceURL url = new JMXServiceURL("t3", "localhost", 7001, "/jndi/weblogic.management.mbeanservers.runtime");
final HashMap<String, String> h = new HashMap<String, String>();
h.put(Context.SECURITY_PRINCIPAL, "weblogic");
h.put(Context.SECURITY_CREDENTIALS, "weblogic1");
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");
connector = JMXConnectorFactory.connect(url, h);
connection = connector.getMBeanServerConnection();
seen = new HashSet<ObjectName>();
}
catch (MalformedObjectNameException e)
{
throw new RuntimeException(e);
}
catch (MalformedURLException e)
{
throw new RuntimeException(e);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
public static void main(final String[] args)
{
final SimpleModule module = new SimpleModule("mbeans");
module.addSerializer(HealthState.class, new HealthStateSerializer());
module.addSerializer(ObjectName.class, new ObjectNameSerializer(connection));
final ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(module);
final File f = new File(System.getProperty("user.home") + "/" + service.getKeyProperty("Name") + ".json");
final FileOutputStream fos;
try
{
fos = new FileOutputStream(f);
try
{
mapper.writerWithDefaultPrettyPrinter().writeValue(fos, service);
}
catch (IOException e)
{
System.err.println(e.getMessage());
}
finally
{
try
{
fos.close();
}
catch (IOException e)
{
System.err.println(e.getMessage());
}
}
}
catch (IOException e)
{
System.err.println(e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment