Skip to content

Commit

Permalink
Merge pull request #168 from jamezp/LOGMGR-189
Browse files Browse the repository at this point in the history
[LOGMGR-189] Expose the instances created by the configuration API.
  • Loading branch information
jamezp authored Jan 19, 2018
2 parents 5a01c6f + 3111414 commit 3ad451e
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
/**
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
*/
abstract class AbstractPropertyConfiguration<T, C extends AbstractPropertyConfiguration<T, C>> extends AbstractBasicConfiguration<T, C> implements ObjectConfigurable, PropertyConfigurable {
abstract class AbstractPropertyConfiguration<T, C extends AbstractPropertyConfiguration<T, C>> extends AbstractBasicConfiguration<T, C> implements ObjectConfigurable<T>, PropertyConfigurable {
private final Class<? extends T> actualClass;
private final String moduleName;
private final String className;
Expand Down Expand Up @@ -440,6 +440,11 @@ public void rollback() {
return true;
}

@Override
public T getInstance() {
return refs.get(getName());
}

protected final void addPostConfigurationActions() {
addPostConfigurationActions(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@

package org.jboss.logmanager.config;

import java.util.logging.ErrorManager;

/**
* Configuration for an error manager.
*
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
*/
public interface ErrorManagerConfiguration extends ObjectConfigurable, PropertyConfigurable, NamedConfigurable {
public interface ErrorManagerConfiguration extends ObjectConfigurable<ErrorManager>, PropertyConfigurable, NamedConfigurable {
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

package org.jboss.logmanager.config;

import java.util.logging.Filter;

/**
* A configuration for a filter.
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
*/
public interface FilterConfiguration extends ObjectConfigurable, NamedConfigurable, PropertyConfigurable {
public interface FilterConfiguration extends ObjectConfigurable<Filter>, NamedConfigurable, PropertyConfigurable {
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@

package org.jboss.logmanager.config;

import java.util.logging.Formatter;

/**
* A configuration for a logger formatter.
*
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
*/
public interface FormatterConfiguration extends NamedConfigurable, ObjectConfigurable, PropertyConfigurable {
public interface FormatterConfiguration extends NamedConfigurable, ObjectConfigurable<Formatter>, PropertyConfigurable {
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

package org.jboss.logmanager.config;

import java.util.logging.Handler;

/**
* Configuration for a single handler.
*
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
*/
public interface HandlerConfiguration extends HandlerContainingConfigurable, NamedConfigurable, PropertyConfigurable, ObjectConfigurable {
public interface HandlerConfiguration extends HandlerContainingConfigurable, NamedConfigurable, PropertyConfigurable, ObjectConfigurable<Handler> {

/**
* Get the name of the configured formatter for this handler.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
*/
public interface ObjectConfigurable {
public interface ObjectConfigurable<T> {

/**
* Get the module name for this object's configuration, if any. If JBoss Modules is not present
Expand All @@ -40,4 +40,16 @@ public interface ObjectConfigurable {
* @return the class name
*/
String getClassName();

/**
* Returns the instance associated with this configuration or {@code null} if no instance has yet been created.
* <p>
* Any changes to the instance will not be recognized by the configuration API.
* </p>
*
* @return the instance associated with this configuration or {@code null}
*/
default T getInstance() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
/**
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
public interface PojoConfiguration extends NamedConfigurable, PropertyConfigurable, ObjectConfigurable {
public interface PojoConfiguration extends NamedConfigurable, PropertyConfigurable, ObjectConfigurable<Object> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

import org.jboss.logmanager.ExtHandler;
Expand Down Expand Up @@ -147,6 +148,39 @@ public void testRemovePropertiesRollback() throws Exception {
checkPropertiesSet();
}

@Test
public void testInstanceTypes() {
final FilterConfiguration filterConfiguration = logContextConfiguration.addFilterConfiguration(null,
TestFilter.class.getName(), "testFilter");

final PojoConfiguration pojoConfiguration = logContextConfiguration.addPojoConfiguration(null,
TestPojo.class.getName(), "testPojo");
pojoConfiguration.setPropertyValueString("value", "testValue");

logContextConfiguration.commit();

final ErrorManager errorManager = logContextConfiguration.getErrorManagerConfiguration(ERROR_MANAGER_NAME).getInstance();
Assert.assertNotNull("Error manager should not be null", errorManager);
Assert.assertEquals(ErrorManager.class, errorManager.getClass());

final Handler handler = logContextConfiguration.getHandlerConfiguration(HANDLER_NAME).getInstance();
Assert.assertNotNull("Handler should not be null", handler);
Assert.assertEquals(TypeHandler.class, handler.getClass());

final Formatter formatter = logContextConfiguration.getFormatterConfiguration(PATTERN_FORMATTER_NAME).getInstance();
Assert.assertNotNull("Formatter should not be null", formatter);
Assert.assertEquals(PatternFormatter.class, formatter.getClass());

final Filter filter = filterConfiguration.getInstance();
Assert.assertNotNull("Filter should not be null", filter);
Assert.assertEquals(TestFilter.class, filter.getClass());

final Object object = pojoConfiguration.getInstance();
Assert.assertNotNull("POJO instance should not be null", object);
Assert.assertEquals(TestPojo.class, object.getClass());
Assert.assertEquals("testValue", ((TestPojo) object).getValue());
}

private static void checkPropertiesSet() {
Assert.assertEquals("stringValue", TypeHandler.stringValue);
Assert.assertEquals(QueueHandler.class, TypeHandler.handler.getClass());
Expand Down Expand Up @@ -462,4 +496,25 @@ public void setEnumValue(final TestEnum enumValue) {
TypeHandler.enumValue = enumValue;
}
}

public static class TestFilter implements Filter {

@Override
public boolean isLoggable(final LogRecord record) {
return true;
}
}

@SuppressWarnings("unused")
public static class TestPojo {
private String value;

public String getValue() {
return value;
}

public void setValue(final String value) {
this.value = value;
}
}
}

0 comments on commit 3ad451e

Please sign in to comment.