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

Glassfish example source code file (ComplianceMonitor.java)

This example Glassfish source code file (ComplianceMonitor.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 - Glassfish tags/keywords

amx, amxvalidator, compliancemonitor, compliancemonitor, domainroot, instance, instance, log, logging, management, mbeanserver, mbeanserver, mbeanservernotification, objectname, objectname, string, threading, threads, util, validatorthread

The Glassfish ComplianceMonitor.java source code

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */
package org.glassfish.admin.amx.impl.mbean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.Set;
import java.util.logging.Level;



import javax.management.MBeanServer;
import javax.management.MBeanServerNotification;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import org.glassfish.admin.amx.base.DomainRoot;
import org.glassfish.admin.amx.core.AMXValidator;
import org.glassfish.admin.amx.impl.util.ImplUtil;
import org.glassfish.admin.amx.util.jmx.JMXUtil;

import com.sun.enterprise.config.serverbeans.AmxPref;
import org.glassfish.admin.amx.impl.util.InjectedValues;

/**
Validates AMX MBeans as they are registered.  Problems are emitted as WARNING to the server log.
 */
public final class ComplianceMonitor implements NotificationListener {

    private static ComplianceMonitor INSTANCE = null;
    private final DomainRoot mDomainRoot;
    private final MBeanServer mServer;
    private volatile boolean mStarted = false;
    private volatile String mValidationLevel;
    private volatile boolean mUnregisterNonCompliant;
    private volatile boolean mLogInaccessibleAttributes;
    /** offloads the validation so as not to block during Notifications */
    private final ValidatorThread mValidatorThread;

    private ComplianceMonitor(final DomainRoot domainRoot) {
        mDomainRoot = domainRoot;

        mServer = (MBeanServer) domainRoot.extra().mbeanServerConnection();

        final AmxPref amxPrefs = InjectedValues.getInstance().getAMXPrefs();
        if (amxPrefs == null) {
            mValidationLevel = AmxPref.VALIDATION_LEVEL_FULL;
            mUnregisterNonCompliant = false;
            mLogInaccessibleAttributes = true;
        } else {
            mValidationLevel = amxPrefs.getValidationLevel();
            mUnregisterNonCompliant = Boolean.valueOf(amxPrefs.getUnregisterNonCompliant());
            mLogInaccessibleAttributes = Boolean.valueOf(amxPrefs.getLogInaccessibleAttributes());
        }

        mValidatorThread = new ValidatorThread(mServer, mValidationLevel, mUnregisterNonCompliant, mLogInaccessibleAttributes);

        ImplUtil.getLogger().info("AMX ComplianceMonitor: ValidationLevel = " + mValidationLevel +
                ", UnregisterNonCompliant = " + mUnregisterNonCompliant +
                ", LogInaccessibleAttributes = " + mLogInaccessibleAttributes);
    }

    public Map<ObjectName, AMXValidator.ProblemList> getComplianceFailures() {
        return mValidatorThread.getComplianceFailures();
    }

    private void listen() {
        try {
            JMXUtil.listenToMBeanServerDelegate(mServer, this, null, null);

        } catch (final Exception e) {
            throw new RuntimeException(e);
        }

        // queue all existing MBeans
        final Set<ObjectName> existing = JMXUtil.queryLocalMBeans(mServer, mDomainRoot.objectName().getDomain(), System.getProperty("com.sun.ass.instanceName"));
        for (final ObjectName objectName : existing) {
            //debug( "Queueing for validation: " + objectName );
            validate(objectName);
        }
    }

    boolean shouldValidate() {
        return !AmxPref.VALIDATION_LEVEL_OFF.equals(mValidationLevel);
    }

    private void validate(final ObjectName objectName) {
        if (shouldValidate()) {
            mValidatorThread.add(objectName);
        }
    }

    public static synchronized ComplianceMonitor getInstance(final DomainRoot domainRoot) {
        if (INSTANCE == null) {
            INSTANCE = new ComplianceMonitor(domainRoot);
            INSTANCE.listen(); // to start queuing immediately
        }
        return INSTANCE;
    }

    public static synchronized void  removeInstance() {
        if(INSTANCE != null) {
            INSTANCE.destroy();
            INSTANCE = null;
        }
    }

    public void start() {
        if (shouldValidate() && !mStarted) {
            mValidatorThread.start();
        }
    }

    public void handleNotification(final Notification notifIn, final Object handback) {
        if ((notifIn instanceof MBeanServerNotification) &&
                notifIn.getType().equals(MBeanServerNotification.REGISTRATION_NOTIFICATION)) {
            final MBeanServerNotification notif = (MBeanServerNotification) notifIn;
            final ObjectName objectName = notif.getMBeanName();
            if (objectName.getDomain().equals(mDomainRoot.objectName().getDomain())) {
                validate(objectName);
            }
        }
    }

    protected void destroy() {
        mValidatorThread.quit();
        mStarted = false;
        mValidationLevel = null;
    }


    private static final class ValidatorThread extends Thread {

        private final MBeanServer mServer;
        private final LinkedBlockingQueue<ObjectName> mMBeans = new LinkedBlockingQueue();
        /** total number of failures */
        private final AtomicInteger mComplianceFailures = new AtomicInteger();
        private final boolean mUnregisterNonCompliant;
        private volatile String mValidationLevel;
        private volatile boolean mLogInaccessibleAttributes;

        ValidatorThread(
                final MBeanServer server,
                final String validationLevel,
                final boolean unregisterNonCompliant,
                final boolean logInaccessibleAttributes) {
            super("ComplianceMonitor.ValidatorThread");
            mServer = server;
            mValidationLevel = validationLevel;
            mUnregisterNonCompliant = unregisterNonCompliant;
            mLogInaccessibleAttributes = logInaccessibleAttributes;

            mFailures = new ConcurrentHashMap<ObjectName, AMXValidator.ProblemList>();
        }
        /** queue poison pill */
        private static final ObjectName QUIT = JMXUtil.newObjectName("quit:type=quit");
        private final ConcurrentHashMap<ObjectName, AMXValidator.ProblemList> mFailures;

        public Map<ObjectName, AMXValidator.ProblemList> getComplianceFailures() {
            return mFailures;
        }

        void quit() {
            add(QUIT);
        }

        public void add(final ObjectName objectName) {
            mMBeans.add(objectName);
        }

        public void run() {
            try {
                doRun();
            } catch (final Throwable t) {
                ImplUtil.getLogger().log(Level.SEVERE, "AMX ComplianceMonitor thread has unexpectedly quit", t);
            }
        }

        protected void doRun() throws InterruptedException {
            //debug( "ValidatorThread.doRun(): started" );                
            while (true) {
                final ObjectName next = mMBeans.take(); // BLOCK until ready
                final List<ObjectName> toValidate = new ArrayList();
                toValidate.add(next);
                mMBeans.drainTo(toValidate);    // efficiently get any additional ones
                if (mMBeans.contains(QUIT)) {
                    break;  // poison, quit;
                }

                // process available MBeans as a group so we can emit summary information as a group.
                final AMXValidator validator = new AMXValidator(mServer, mValidationLevel, mUnregisterNonCompliant, mLogInaccessibleAttributes);
                try {
                    //debug( "VALIDATING MBeans: " + toValidate.size() );
                    final ObjectName[] objectNames = new ObjectName[toValidate.size()];
                    toValidate.toArray(objectNames);
                    final AMXValidator.ValidationResult result = validator.validate(objectNames);
                    if (result.numFailures() != 0) {
                        mFailures.putAll(result.failures());

                        mComplianceFailures.addAndGet(result.numFailures());
                        ImplUtil.getLogger().info(result.toString());
                    }
                } catch (final Throwable t) {
                    ImplUtil.getLogger().log(Level.WARNING, "Exception validating MBean " + next, t);
                }
            }
        }
    }

    private static void debug(final Object o) {
        System.out.println(o.toString());
    }
}






















Other Glassfish examples (source code examples)

Here is a short list of links related to this Glassfish ComplianceMonitor.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.