Skip to content

Commit

Permalink
added copyright info, cleanup, refactoring, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Luigi R. Viggiano committed Nov 27, 2014
1 parent d9d27ce commit 907fb6a
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 258 deletions.
30 changes: 0 additions & 30 deletions owner/src/main/java/org/aeonbits/owner/JMXBean.java

This file was deleted.

168 changes: 69 additions & 99 deletions owner/src/main/java/org/aeonbits/owner/PropertiesManager.java
Expand Up @@ -8,7 +8,6 @@

package org.aeonbits.owner;


import org.aeonbits.owner.event.ReloadEvent;
import org.aeonbits.owner.event.ReloadListener;
import org.aeonbits.owner.event.RollbackBatchException;
Expand All @@ -17,6 +16,16 @@
import org.aeonbits.owner.event.TransactionalPropertyChangeListener;
import org.aeonbits.owner.event.TransactionalReloadListener;

import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.InvalidAttributeValueException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanParameterInfo;
import javax.management.ReflectionException;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
Expand Down Expand Up @@ -47,18 +56,6 @@
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;

import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.DynamicMBean;
import javax.management.InvalidAttributeValueException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanParameterInfo;
import javax.management.ReflectionException;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static java.util.Collections.synchronizedList;
Expand All @@ -75,7 +72,7 @@
*
* @author Luigi R. Viggiano
*/
class PropertiesManager implements Reloadable, Accessible, Mutable, JMXBean {
class PropertiesManager implements Reloadable, Accessible, Mutable {

private final Class<? extends Config> clazz;
private final Map<?, ?>[] imports;
Expand Down Expand Up @@ -553,17 +550,17 @@ private void firePropertyChange(PropertyChangeEvent event) {
@Delegate
@Override
public boolean equals(Object obj) {
if (! (obj instanceof Proxy)) return false;
if (!(obj instanceof Proxy)) return false;
InvocationHandler handler = Proxy.getInvocationHandler(obj);
if (! (handler instanceof PropertiesInvocationHandler))
if (!(handler instanceof PropertiesInvocationHandler))
return false;
PropertiesInvocationHandler propsInvocationHandler = (PropertiesInvocationHandler)handler;
PropertiesInvocationHandler propsInvocationHandler = (PropertiesInvocationHandler) handler;
PropertiesManager that = propsInvocationHandler.propertiesManager;
return this.equals(that);
}

private boolean equals(PropertiesManager that) {
if (! this.isAssignationCompatibleWith(that))
if (!this.isAssignationCompatibleWith(that))
return false;
this.readLock.lock();
try {
Expand Down Expand Up @@ -594,98 +591,71 @@ public int hashCode() {
}

@Delegate
public Object getAttribute(String attribute)
throws AttributeNotFoundException, MBeanException,
ReflectionException {
return getProperty(attribute);
public Object getAttribute(String attribute)
throws AttributeNotFoundException, MBeanException,
ReflectionException {
return getProperty(attribute);
}

@Delegate
public void setAttribute(Attribute attribute)
throws AttributeNotFoundException, InvalidAttributeValueException,
MBeanException, ReflectionException {
setProperty(attribute.getName(), (String) attribute.getValue());
}
public void setAttribute(Attribute attribute)
throws AttributeNotFoundException, InvalidAttributeValueException,
MBeanException, ReflectionException {
setProperty(attribute.getName(), (String) attribute.getValue());
}

@Delegate
public AttributeList getAttributes(String[] attributes) {
List<Attribute> attrList = new LinkedList<Attribute>();
for (String propertyName : attributes) {
attrList.add(new Attribute(propertyName, properties.getProperty(propertyName)));
}
return new AttributeList(attrList);
}
public AttributeList getAttributes(String[] attributes) {
List<Attribute> attrList = new LinkedList<Attribute>();
for (String propertyName : attributes)
attrList.add(new Attribute(propertyName, getProperty(propertyName)));
return new AttributeList(attrList);
}

@Delegate
public AttributeList setAttributes(AttributeList attributes) {
for (Attribute attr : attributes.asList()) {
properties.setProperty(attr.getName(), (String) attr.getValue());
}
return attributes;
}
public AttributeList setAttributes(AttributeList attributes) {
for (Attribute attr : attributes.asList())
setProperty(attr.getName(), (String) attr.getValue());
return attributes;
}

@Delegate
public Object invoke(String actionName, Object[] params, String[] signature)
throws MBeanException, ReflectionException {
if (actionName.equals("getProperty") &&
params != null && params.length == 1) {
return properties.getProperty((String) params[0]);
}
else if (actionName.equals("setProperty") &&
params != null && params.length == 2) {
properties.setProperty((String) params[0], (String) params[1]);
public Object invoke(String actionName, Object[] params, String[] signature)
throws MBeanException, ReflectionException {
if (actionName.equals("getProperty") && params != null && params.length == 1) {
return getProperty((String) params[0]);
} else if (actionName.equals("setProperty") && params != null && params.length == 2) {
setProperty((String) params[0], (String) params[1]);
return null;
} else if (actionName.equals("reload") && (params == null || params.length == 0)) {
reload();
return null;
}
else if (actionName.equals("reload") &&
(params == null || params.length == 0) &&
(params == null || params.length == 0)) {
reload();
return null;
}
throw new ReflectionException(new NoSuchMethodException(actionName));
}

@Delegate
public MBeanInfo getMBeanInfo() {
List<MBeanAttributeInfo> attrList = new ArrayList<MBeanAttributeInfo>();
Enumeration<?> propertyEnum = properties.propertyNames();
while (propertyEnum.hasMoreElements()) {
String key = (String) propertyEnum.nextElement();
attrList.add(new MBeanAttributeInfo(key, "String", key, true, true,
false));
}
MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[] {};
attributes = attrList.toArray(attributes);

MBeanParameterInfo paramsKey = new MBeanParameterInfo("Propertykey",
"java.lang.String", "Key of the property");
MBeanParameterInfo paramsValue = new MBeanParameterInfo(
"Propertyvalue", "java.lang.String", "Value of the property");


MBeanOperationInfo getOperation = new MBeanOperationInfo("getProperty", "getProperties",
new MBeanParameterInfo[] { paramsKey }, "java.lang.String", MBeanOperationInfo.INFO);
MBeanOperationInfo setOperation = new MBeanOperationInfo("setProperty", "setProperties",
new MBeanParameterInfo[] { paramsKey, paramsValue },"void", MBeanOperationInfo.ACTION);
MBeanOperationInfo reloadOperation = new MBeanOperationInfo("reload", "Reload properties",
null, // no parameters
"void", MBeanOperationInfo.ACTION);

List<MBeanOperationInfo> operationsList = new ArrayList<MBeanOperationInfo>();
if(Accessible.class.isAssignableFrom(clazz)){
operationsList.add(getOperation);
}
if(Mutable.class.isAssignableFrom(clazz)){
operationsList.add(setOperation);
}
if(Reloadable.class.isAssignableFrom(clazz)){
operationsList.add(reloadOperation);
}
MBeanOperationInfo[] operations = new MBeanOperationInfo[operationsList.size()];
operations = operationsList.toArray(operations);

return new MBeanInfo(clazz.getName(), "Owner MBean", attributes, null,
operations, null);
}
}

@Delegate
public MBeanInfo getMBeanInfo() {
List<MBeanAttributeInfo> attributesInfo = new ArrayList<MBeanAttributeInfo>();
Set<String> propertyNames = propertyNames();
for (String name : propertyNames)
attributesInfo.add(new MBeanAttributeInfo(name, "java.lang.String", name, true, true, false));

MBeanAttributeInfo[] attributes = attributesInfo.toArray(new MBeanAttributeInfo[propertyNames.size()]);

MBeanParameterInfo key = new MBeanParameterInfo("key", "java.lang.String", "Key of the property");
MBeanParameterInfo value = new MBeanParameterInfo("value", "java.lang.String", "Value of the property");

MBeanOperationInfo[] operations = new MBeanOperationInfo[] {
new MBeanOperationInfo("getProperty", "Gets value for a property",
new MBeanParameterInfo[] { key }, "java.lang.String", MBeanOperationInfo.INFO),
new MBeanOperationInfo("setProperty", "Sets the value for a property",
new MBeanParameterInfo[] { key, value }, "void", MBeanOperationInfo.ACTION),
new MBeanOperationInfo("reload", "Reload properties", null, "void", MBeanOperationInfo.ACTION)
};

return new MBeanInfo(clazz.getName(), clazz.getSimpleName() + " OWNER MBean",
attributes, null, operations, null);
}

}
75 changes: 75 additions & 0 deletions owner/src/test/java/org/aeonbits/owner/examples/JMXExample.java
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2012-2014, Luigi R. Viggiano
* All rights reserved.
*
* This software is distributable under the BSD license.
* See the terms of the BSD license in the documentation provided with this software.
*/

package org.aeonbits.owner.examples;

import org.aeonbits.owner.Accessible;
import org.aeonbits.owner.ConfigFactory;
import org.aeonbits.owner.Mutable;
import org.aeonbits.owner.Reloadable;
import org.aeonbits.owner.event.ReloadEvent;
import org.aeonbits.owner.event.ReloadListener;

import javax.management.DynamicMBean;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.lang.management.ManagementFactory;

/**
* @author Luigi R. Viggiano
*/
public class JMXExample {

public interface MyConfig extends DynamicMBean, Mutable, Accessible, Reloadable {

@Key("server.port.number")
@DefaultValue("80")
int port();

@Key("server.host.name")
@DefaultValue("localhost")
String hostname();

}

public static void main(String[] args) throws InterruptedException, MalformedObjectNameException,
InstanceAlreadyExistsException, NotCompliantMBeanException, MBeanRegistrationException {
MyConfig cfg = ConfigFactory.create(MyConfig.class);

cfg.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
System.out.printf("prop change event [%s = %s -> %s]%n", evt.getPropertyName(), evt.getOldValue(), evt.getNewValue());
}
});

cfg.addReloadListener(new ReloadListener() {
public void reloadPerformed(ReloadEvent event) {
System.out.printf("reload event detected%n");
}
});

MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

mbs.registerMBean(cfg, new ObjectName("org.aeonbits.owner.examples:type=MyConfig"));

System.out.println("Now, launch jconsole and attach to the java process. Set a negative port number to exit.");

while (cfg.port() >= 0) {
System.out.printf("\rport: %d hostname: %s \t\t\t\t", cfg.port(), cfg.hostname());
Thread.sleep(500);
}

}

}
19 changes: 13 additions & 6 deletions owner/src/test/java/org/aeonbits/owner/jmx/InvalidJMXBeanTest.java
@@ -1,18 +1,25 @@
/*
* Copyright (c) 2012-2014, Luigi R. Viggiano
* All rights reserved.
*
* This software is distributable under the BSD license.
* See the terms of the BSD license in the documentation provided with this software.
*/

package org.aeonbits.owner.jmx;

import java.lang.management.ManagementFactory;
import java.util.Properties;
import org.aeonbits.owner.Config;
import org.aeonbits.owner.ConfigFactory;
import org.junit.Test;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;

import org.aeonbits.owner.Config;
import org.aeonbits.owner.ConfigFactory;
import org.junit.Test;
import java.lang.management.ManagementFactory;
import java.util.Properties;

public class InvalidJMXBeanTest {

Expand Down

0 comments on commit 907fb6a

Please sign in to comment.