下面列出了org.apache.log4j.helpers.OptionConverter#toLevel ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
Convert <code>val</code> a String parameter to an object of a
given type.
*/
protected
Object convertArg(String val, Class type) {
if(val == null)
return null;
String v = val.trim();
if (String.class.isAssignableFrom(type)) {
return val;
} else if (Integer.TYPE.isAssignableFrom(type)) {
return new Integer(v);
} else if (Long.TYPE.isAssignableFrom(type)) {
return new Long(v);
} else if (Boolean.TYPE.isAssignableFrom(type)) {
if ("true".equalsIgnoreCase(v)) {
return Boolean.TRUE;
} else if ("false".equalsIgnoreCase(v)) {
return Boolean.FALSE;
}
} else if (Priority.class.isAssignableFrom(type)) {
return OptionConverter.toLevel(v, (Level) Level.DEBUG);
}
return null;
}
public
void setAttribute(Attribute attribute) throws AttributeNotFoundException,
InvalidAttributeValueException,
MBeanException,
ReflectionException {
// Check attribute is not null to avoid NullPointerException later on
if (attribute == null) {
throw new RuntimeOperationsException(
new IllegalArgumentException("Attribute cannot be null"),
"Cannot invoke a setter of "+dClassName+" with null attribute");
}
String name = attribute.getName();
Object value = attribute.getValue();
if (name == null) {
throw new RuntimeOperationsException(
new IllegalArgumentException("Attribute name cannot be null"),
"Cannot invoke the setter of "+dClassName+
" with null attribute name");
}
if(name.equals(THRESHOLD)) {
Level l = OptionConverter.toLevel((String) value,
hierarchy.getThreshold());
hierarchy.setThreshold(l);
}
}
public
void setLevelToMatch(String level) {
levelToMatch = OptionConverter.toLevel(level, null);
}
public
void setAttribute(Attribute attribute) throws AttributeNotFoundException,
InvalidAttributeValueException,
MBeanException,
ReflectionException {
// Check attribute is not null to avoid NullPointerException later on
if (attribute == null) {
throw new RuntimeOperationsException(
new IllegalArgumentException("Attribute cannot be null"),
"Cannot invoke a setter of " + dClassName +
" with null attribute");
}
String name = attribute.getName();
Object value = attribute.getValue();
if (name == null) {
throw new RuntimeOperationsException(
new IllegalArgumentException("Attribute name cannot be null"),
"Cannot invoke the setter of "+dClassName+
" with null attribute name");
}
MethodUnion mu = (MethodUnion) dynamicProps.get(name);
if(mu != null && mu.writeMethod != null) {
Object[] o = new Object[1];
Class[] params = mu.writeMethod.getParameterTypes();
if(params[0] == org.apache.log4j.Priority.class) {
value = OptionConverter.toLevel((String) value,
(Level) getAttribute(name));
}
o[0] = value;
try {
mu.writeMethod.invoke(layout, o);
} catch(Exception e) {
cat.error("FIXME", e);
}
} else {
throw(new AttributeNotFoundException("Attribute " + name +
" not found in " +
this.getClass().getName()));
}
}
public
void setAttribute(Attribute attribute) throws AttributeNotFoundException,
InvalidAttributeValueException,
MBeanException,
ReflectionException {
// Check attribute is not null to avoid NullPointerException later on
if (attribute == null) {
throw new RuntimeOperationsException(
new IllegalArgumentException("Attribute cannot be null"),
"Cannot invoke a setter of " + dClassName +
" with null attribute");
}
String name = attribute.getName();
Object value = attribute.getValue();
if (name == null) {
throw new RuntimeOperationsException(
new IllegalArgumentException("Attribute name cannot be null"),
"Cannot invoke the setter of "+dClassName+
" with null attribute name");
}
if(name.equals("priority")) {
if (value instanceof String) {
String s = (String) value;
Level p = logger.getLevel();
if(s.equalsIgnoreCase("NULL")) {
p = null;
} else {
p = OptionConverter.toLevel(s, p);
}
logger.setLevel(p);
}
} else {
throw(new AttributeNotFoundException("Attribute " + name +
" not found in " +
this.getClass().getName()));
}
}
public
void setAttribute(Attribute attribute) throws AttributeNotFoundException,
InvalidAttributeValueException,
MBeanException,
ReflectionException {
// Check attribute is not null to avoid NullPointerException later on
if (attribute == null) {
throw new RuntimeOperationsException(
new IllegalArgumentException("Attribute cannot be null"),
"Cannot invoke a setter of " + dClassName +
" with null attribute");
}
String name = attribute.getName();
Object value = attribute.getValue();
if (name == null) {
throw new RuntimeOperationsException(
new IllegalArgumentException("Attribute name cannot be null"),
"Cannot invoke the setter of "+dClassName+
" with null attribute name");
}
MethodUnion mu = (MethodUnion) dynamicProps.get(name);
if(mu != null && mu.writeMethod != null) {
Object[] o = new Object[1];
Class[] params = mu.writeMethod.getParameterTypes();
if(params[0] == org.apache.log4j.Priority.class) {
value = OptionConverter.toLevel((String) value,
(Level) getAttribute(name));
}
o[0] = value;
try {
mu.writeMethod.invoke(appender, o);
} catch(Exception e) {
cat.error("FIXME", e);
}
} else if(name.endsWith(".layout")) {
} else {
throw(new AttributeNotFoundException("Attribute " + name +
" not found in " +
this.getClass().getName()));
}
}
public
void toLevelTest1() {
String val = "INFO";
Level p = OptionConverter.toLevel(val, null);
assertEquals(p, Level.INFO);
}
public
void toLevelTest2() {
String val = "INFO#org.apache.log4j.xml.XLevel";
Level p = OptionConverter.toLevel(val, null);
assertEquals(p, Level.INFO);
}
public
void toLevelTest3() {
String val = "TRACE#org.apache.log4j.xml.XLevel";
Level p = OptionConverter.toLevel(val, null);
assertEquals(p, XLevel.TRACE);
}
public
void toLevelTest4() {
String val = "TR#org.apache.log4j.xml.XLevel";
Level p = OptionConverter.toLevel(val, null);
assertEquals(p, null);
}
public
void toLevelTest5() {
String val = "INFO#org.apache.log4j.xml.TOTO";
Level p = OptionConverter.toLevel(val, null);
assertEquals(p, null);
}