alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Apache CXF example source code file (AbstractAtomBean.java)

This example Apache CXF source code file (AbstractAtomBean.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Apache CXF tags/keywords

abstractatombean, arraylist, arraylist, bus, handler, illegalstateexception, info, info, list, log, loggerlevel, loggerlevel, logging, string, string, stringtokenizer, util

The Apache CXF AbstractAtomBean.java source code

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.cxf.management.web.logging.atom;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Handler;
import java.util.logging.Logger;

import org.apache.commons.lang.Validate;
import org.apache.cxf.Bus;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.management.web.logging.LogLevel;


public abstract class AbstractAtomBean {

    private List<LoggerLevel> loggers = new ArrayList();
    private boolean initialized;
    private Bus bus;
    
    /**
     * Creates unconfigured and uninitialized bean. To configure setters must be used, then {@link #init()}
     * must be called.
     */
    public AbstractAtomBean() {
        initSingleLogger();
    }

    private void initSingleLogger() {
        loggers = new ArrayList<LoggerLevel>();
        loggers.add(new LoggerLevel("", "INFO"));
    }

    public void setBus(Bus bus) {
        this.bus = bus;
    }
    
    public Bus getBus() {
        return bus;
    }

    /**
     * Set one or more loggers and levels descriptor. <br>
     * Parsed input syntax is:
     * 
     * <pre>
     * loggers   := <logger>(<separator><logger>)*
     * logger    := <name>[":"<level>]
     * separator := "," | " " | "\n"
     * </pre>
     * 
     * Examples:
     * <p>
     * Two loggers and two levels: <br>
     * <tt>org.apache.cxf:INFO, org.apache.cxf.jaxrs:DEBUG
     * <p>
     * Three loggers, first with default "INFO" level: <br>
     * <tt>org.apache.cxf, org.apache.cxf.jaxrs:DEBUG, namedLogger:ERROR
* <p> * One logger with default "INFO" level: <br> * <tt>org.apache.cxf
*/ public void setLoggers(String loggers) { checkInit(); Validate.notNull(loggers, "loggers is null"); parseLoggers(loggers); } /** * Name of logger to associate with ATOM push handler; empty string for root logger. */ public void setLogger(String logger) { checkInit(); Validate.notNull(logger, "logger is null"); if (loggers.size() != 1) { initSingleLogger(); } loggers.get(0).setLogger(logger); } /** * Name of level that logger will use publishing log events to ATOM push handler; empty string for default * "INFO" level. */ public void setLevel(String level) { checkInit(); Validate.notNull(level, "level is null"); if (loggers.size() != 1) { initSingleLogger(); } loggers.get(0).setLevel(level); } /** * Initializes bean; creates ATOM handler based on current properties state, and attaches handler to * logger(s). */ public void init() { checkInit(); initialized = true; Handler h = createHandler(); for (int i = 0; i < loggers.size(); i++) { Logger l = LogUtils.getL7dLogger(AbstractAtomBean.class, null, loggers.get(i).getLogger()); l.addHandler(h); l.setLevel(LogLevel.toJUL(LogLevel.valueOf(loggers.get(i).getLevel()))); } } protected abstract Handler createHandler(); protected void checkInit() { if (initialized) { throw new IllegalStateException("Bean is already initialized"); } } private void parseLoggers(String param) { loggers = new ArrayList<LoggerLevel>(); StringTokenizer st1 = new StringTokenizer(param, ", \t\n\r\f"); while (st1.hasMoreTokens()) { String tok = st1.nextToken(); int idx = tok.indexOf(":"); if (idx != -1) { loggers.add(new LoggerLevel(tok.substring(0, idx), tok.substring(idx + 1, tok.length()))); } else { loggers.add(new LoggerLevel(tok, "INFO")); } } } protected List<LoggerLevel> getLoggers() { return loggers; } protected static class LoggerLevel { private String logger; private String level; public LoggerLevel(String logger, String level) { this.logger = logger; this.level = level; } public String getLogger() { return logger; } public void setLogger(String logger) { this.logger = logger; } public String getLevel() { return level; } public void setLevel(String level) { this.level = level; } } }

Other Apache CXF examples (source code examples)

Here is a short list of links related to this Apache CXF AbstractAtomBean.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.