Java Code Examples for org.apache.log4j.helpers.OptionConverter#instantiateByClassName()

The following examples show how to use org.apache.log4j.helpers.OptionConverter#instantiateByClassName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AppenderDynamicMBean.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public
Object invoke(String operationName, Object params[], String signature[])
  throws MBeanException,
  ReflectionException {

  if(operationName.equals("activateOptions") &&
                   appender instanceof OptionHandler) {
    OptionHandler oh = (OptionHandler) appender;
    oh.activateOptions();
    return "Options activated.";
  } else if (operationName.equals("setLayout")) {
    Layout layout = (Layout) OptionConverter.instantiateByClassName((String)
						      params[0],
						      Layout.class,
						      null);
    appender.setLayout(layout);
    registerLayoutMBean(layout);
  }
  return null;
}
 
Example 2
Source File: RendererMap.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
    Add a renderer to a hierarchy passed as parameter.
 */
 static
 public
 void addRenderer(RendererSupport repository, String renderedClassName,
	   String renderingClassName) {
   LogLog.debug("Rendering class: ["+renderingClassName+"], Rendered class: ["+
	 renderedClassName+"].");
   ObjectRenderer renderer = (ObjectRenderer)
            OptionConverter.instantiateByClassName(renderingClassName,
					    ObjectRenderer.class,
					    null);
   if(renderer == null) {
     LogLog.error("Could not instantiate renderer ["+renderingClassName+"].");
     return;
   } else {
     try {
Class renderedClass = Loader.loadClass(renderedClassName);
repository.setRenderer(renderedClass, renderer);
     } catch(ClassNotFoundException e) {
LogLog.error("Could not find class ["+renderedClassName+"].", e);
     }
   }
 }
 
Example 3
Source File: XmlConfiguration.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
/**
 * Used internally to parse an {@link ErrorHandler} element.
 */
private void parseErrorHandler(Element element, Appender appender) {
    ErrorHandler eh = (ErrorHandler) OptionConverter.instantiateByClassName(
            subst(element.getAttribute(CLASS_ATTR)),
            ErrorHandler.class,
            null);

    if (eh != null) {
        eh.setAppender(appender);

        PropertySetter propSetter = new PropertySetter(eh);
        forEachElement(element.getChildNodes(), (currentElement) -> {
            String tagName = currentElement.getTagName();
            if (tagName.equals(PARAM_TAG)) {
                setParameter(currentElement, propSetter);
            }
        });
        propSetter.activate();
        appender.setErrorHandler(eh);
    }
}
 
Example 4
Source File: XmlConfigurationFactory.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
/**
 * Used internally to parse an {@link ErrorHandler} element.
 */
private void parseErrorHandler(Element element, Appender appender) {
    ErrorHandler eh = (ErrorHandler) OptionConverter.instantiateByClassName(
            subst(element.getAttribute(CLASS_ATTR)),
            ErrorHandler.class,
            null);

    if (eh != null) {
        eh.setAppender(appender);

        PropertySetter propSetter = new PropertySetter(eh);
        forEachElement(element.getChildNodes(), (currentElement) -> {
            String tagName = currentElement.getTagName();
            if (tagName.equals(PARAM_TAG)) {
                setParameter(currentElement, propSetter);
            }
        });
        propSetter.activate();
        appender.setErrorHandler(eh);
    }
}
 
Example 5
Source File: PropertyConfigurator.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
   Check the provided <code>Properties</code> object for a
   {@link org.apache.log4j.spi.LoggerFactory LoggerFactory}
   entry specified by {@link #LOGGER_FACTORY_KEY}.  If such an entry
   exists, an attempt is made to create an instance using the default
   constructor.  This instance is used for subsequent Category creations
   within this configurator.

   @see #parseCatsAndRenderers
 */
protected void configureLoggerFactory(Properties props) {
  String factoryClassName = OptionConverter.findAndSubst(LOGGER_FACTORY_KEY,
					   props);
  if(factoryClassName != null) {
    LogLog.debug("Setting category factory to ["+factoryClassName+"].");
    loggerFactory = (LoggerFactory)
         OptionConverter.instantiateByClassName(factoryClassName,
					 LoggerFactory.class,
					 loggerFactory);
    PropertySetter.setProperties(loggerFactory, props, FACTORY_PREFIX + ".");
  }
}
 
Example 6
Source File: LoggerDynamicMBean.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
void addAppender(String appenderClass, String appenderName) {
  cat.debug("addAppender called with "+appenderClass+", "+appenderName);
  Appender appender = (Appender)
     OptionConverter.instantiateByClassName(appenderClass,
			      org.apache.log4j.Appender.class,
			      null);
  appender.setName(appenderName);
  logger.addAppender(appender);

  //appenderMBeanRegistration();

}
 
Example 7
Source File: DOMConfigurator.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
    Used internally to parse a filter element.
  */
 protected
 void parseFilters(Element element, Appender appender) {
   String clazz = subst(element.getAttribute(CLASS_ATTR));
   Filter filter = (Filter) OptionConverter.instantiateByClassName(clazz,
                                               Filter.class, null);
   
   if(filter != null) {
     PropertySetter propSetter = new PropertySetter(filter);
     NodeList children = element.getChildNodes();
     final int length 	= children.getLength();

     for (int loop = 0; loop < length; loop++) {
Node currentNode = children.item(loop);
if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
  Element currentElement = (Element) currentNode;
  String tagName = currentElement.getTagName();
  if(tagName.equals(PARAM_TAG)) {
           setParameter(currentElement, propSetter);
  } else {
           quietParseUnrecognizedElement(filter, currentElement, props);
     }
}
     }
     propSetter.activate();
     LogLog.debug("Adding filter of type ["+filter.getClass()
	   +"] to appender named ["+appender.getName()+"].");
     appender.addFilter(filter);
   }    
 }
 
Example 8
Source File: DOMConfigurator.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Creates an object and processes any nested param elements
 * but does not call activateOptions.  If the class also supports
 * UnrecognizedElementParser, the parseUnrecognizedElement method
 * will be call for any child elements other than param.
 *
 * @param element       element, may not be null.
 * @param props         properties
 * @param expectedClass interface or class expected to be implemented
 *                      by created class
 * @return created class or null.
 * @throws Exception thrown if the contain object should be abandoned.
 * @since 1.2.15
 */
public static Object parseElement(final Element element,
                                         final Properties props,
                                         final Class expectedClass) throws Exception {
    String clazz = subst(element.getAttribute("class"), props);
    Object instance = OptionConverter.instantiateByClassName(clazz,
            expectedClass, null);

    if (instance != null) {
        PropertySetter propSetter = new PropertySetter(instance);
        NodeList children = element.getChildNodes();
        final int length = children.getLength();

        for (int loop = 0; loop < length; loop++) {
            Node currentNode = children.item(loop);
            if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                Element currentElement = (Element) currentNode;
                String tagName = currentElement.getTagName();
                if (tagName.equals("param")) {
                    setParameter(currentElement, propSetter, props);
                } else {
                     parseUnrecognizedElement(instance, currentElement, props);
                }
            }
        }
        return instance;
    }
    return null;
}
 
Example 9
Source File: SMTPAppender.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
   The <b>EvaluatorClass</b> option takes a string value
   representing the name of the class implementing the {@link
   TriggeringEventEvaluator} interface. A corresponding object will
   be instantiated and assigned as the triggering event evaluator
   for the SMTPAppender.
 */
public
void setEvaluatorClass(String value) {
    evaluator = (TriggeringEventEvaluator)
              OptionConverter.instantiateByClassName(value,
			   TriggeringEventEvaluator.class,
				       evaluator);
}
 
Example 10
Source File: XmlConfiguration.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an object and processes any nested param elements
 * but does not call activateOptions.  If the class also supports
 * UnrecognizedElementParser, the parseUnrecognizedElement method
 * will be call for any child elements other than param.
 *
 * @param element       element, may not be null.
 * @param props         properties
 * @param expectedClass interface or class expected to be implemented
 *                      by created class
 * @return created class or null.
 * @throws Exception thrown if the contain object should be abandoned.
 * @since 1.2.15
 */
public Object parseElement(final Element element, final Properties props,
        @SuppressWarnings("rawtypes") final Class expectedClass) throws Exception {
    String clazz = subst(element.getAttribute("class"), props);
    Object instance = OptionConverter.instantiateByClassName(clazz,
            expectedClass, null);

    if (instance != null) {
        PropertySetter propSetter = new PropertySetter(instance);
        NodeList children = element.getChildNodes();
        final int length = children.getLength();

        for (int loop = 0; loop < length; loop++) {
            Node currentNode = children.item(loop);
            if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                Element currentElement = (Element) currentNode;
                String tagName = currentElement.getTagName();
                if (tagName.equals("param")) {
                    setParameter(currentElement, propSetter, props);
                } else {
                    parseUnrecognizedElement(instance, currentElement, props);
                }
            }
        }
        return instance;
    }
    return null;
}
 
Example 11
Source File: XmlConfigurationFactory.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an object and processes any nested param elements
 * but does not call activateOptions.  If the class also supports
 * UnrecognizedElementParser, the parseUnrecognizedElement method
 * will be call for any child elements other than param.
 *
 * @param element       element, may not be null.
 * @param props         properties
 * @param expectedClass interface or class expected to be implemented
 *                      by created class
 * @return created class or null.
 * @throws Exception thrown if the contain object should be abandoned.
 * @since 1.2.15
 */
public static Object parseElement(final Element element, final Properties props,
        @SuppressWarnings("rawtypes") final Class expectedClass) throws Exception {
    String clazz = subst(element.getAttribute("class"), props);
    Object instance = OptionConverter.instantiateByClassName(clazz,
            expectedClass, null);

    if (instance != null) {
        PropertySetter propSetter = new PropertySetter(instance);
        NodeList children = element.getChildNodes();
        final int length = children.getLength();

        for (int loop = 0; loop < length; loop++) {
            Node currentNode = children.item(loop);
            if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                Element currentElement = (Element) currentNode;
                String tagName = currentElement.getTagName();
                if (tagName.equals("param")) {
                    setParameter(currentElement, propSetter, props);
                } else {
                    parseUnrecognizedElement(instance, currentElement, props);
                }
            }
        }
        return instance;
    }
    return null;
}
 
Example 12
Source File: DOMConfigurator.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
    Used internally to parse an {@link ErrorHandler} element.
  */
 protected
 void parseErrorHandler(Element element, Appender appender) {
   ErrorHandler eh = (ErrorHandler) OptionConverter.instantiateByClassName(
                                      subst(element.getAttribute(CLASS_ATTR)),
                                      org.apache.log4j.spi.ErrorHandler.class, 
				       null);
   
   if(eh != null) {
     eh.setAppender(appender);

     PropertySetter propSetter = new PropertySetter(eh);
     NodeList children = element.getChildNodes();
     final int length 	= children.getLength();

     for (int loop = 0; loop < length; loop++) {
Node currentNode = children.item(loop);
if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
  Element currentElement = (Element) currentNode;
  String tagName = currentElement.getTagName();
  if(tagName.equals(PARAM_TAG)) {
           setParameter(currentElement, propSetter);
  } else if(tagName.equals(APPENDER_REF_TAG)) {
    eh.setBackupAppender(findAppenderByReference(currentElement));
  } else if(tagName.equals(LOGGER_REF)) {
    String loggerName = currentElement.getAttribute(REF_ATTR);	    
    Logger logger = (catFactory == null) ? repository.getLogger(loggerName)
               : repository.getLogger(loggerName, catFactory);
    eh.setLogger(logger);
  } else if(tagName.equals(ROOT_REF)) {
    Logger root = repository.getRootLogger();
    eh.setLogger(root);
  } else {
         quietParseUnrecognizedElement(eh, currentElement, props);
     }
}
     }
     propSetter.activate();
     appender.setErrorHandler(eh);
   }
 }
 
Example 13
Source File: IMAppender.java    From olat with Apache License 2.0 2 votes vote down vote up
/**
 * The <b>EvaluatorClass </b> option takes a string value representing the name of the class implementing the {@linkTriggeringEventEvaluator} interface. A
 * corresponding object will be instantiated and assigned as the triggering event evaluator for the SMTPAppender.
 */
public void setEvaluatorClass(final String value) {
    evaluator = (TriggeringEventEvaluator) OptionConverter.instantiateByClassName(value, TriggeringEventEvaluator.class, evaluator);
}
 
Example 14
Source File: IMAppender.java    From olat with Apache License 2.0 2 votes vote down vote up
/**
 * The <b>EvaluatorClass </b> option takes a string value representing the name of the class implementing the {@linkTriggeringEventEvaluator} interface. A
 * corresponding object will be instantiated and assigned as the triggering event evaluator for the SMTPAppender.
 */
public void setEvaluatorClass(final String value) {
    evaluator = (TriggeringEventEvaluator) OptionConverter.instantiateByClassName(value, TriggeringEventEvaluator.class, evaluator);
}