|
Glassfish example source code file (Demo.java)
The Glassfish Demo.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 amxtest; import java.util.Properties; import java.util.Set; import java.util.HashSet; import java.util.List; import java.util.ArrayList; import java.util.Collections; import java.util.Map; import java.net.MalformedURLException; import java.io.IOException; import javax.management.ObjectName; import javax.management.Attribute; import javax.management.AttributeList; import javax.management.Notification; import javax.management.NotificationListener; import javax.management.MBeanServerDelegateMBean; import javax.management.MBeanServerNotification; import javax.management.MBeanServerConnection; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; import org.glassfish.admin.amx.base.*; import org.glassfish.admin.amx.core.*; import org.glassfish.admin.amx.core.proxy.ProxyFactory; import org.glassfish.admin.amx.config.AMXConfigProxy; import org.glassfish.admin.amx.intf.config.*; import org.glassfish.admin.amx.util.TimingDelta; import org.glassfish.admin.amx.util.ListUtil; import org.glassfish.admin.amx.util.ExceptionUtil; import org.glassfish.admin.amx.util.jmx.JMXUtil; import org.glassfish.external.amx.AMXGlassfish; /** Demonstration AMX java client */ public final class Demo { final String mAdminUser; final String mAdminPassword; final String mHost; final int mPort; final boolean mDebug; private final MBeanServerConnection mMBeanServerConnection; private final ProxyFactory mProxyFactory; private final DomainRoot mDomainRoot; private final Domain mDomainConfig; private final MBeansListener mListener; private final boolean mPrintln; private final Query mQueryMgr; /** Get a connection to the server, and setup various high-level MBean proxies. */ Demo( final boolean emitMessages, final String host, final String port, final String user, final String password ) throws Exception { mPrintln = emitMessages; mDebug = true; mHost = host; mPort = Integer.parseInt(port); mAdminUser = user; mAdminPassword = password; mMBeanServerConnection = _getMBeanServerConnection(mHost, mPort); mProxyFactory = ProxyFactory.getInstance(mMBeanServerConnection); mDomainRoot = mProxyFactory.getDomainRootProxy(); mQueryMgr = mDomainRoot.getQueryMgr(); mDomainConfig = mDomainRoot.child(Domain.class); enableMonitoring(); mListener = new MBeansListener(mMBeanServerConnection, mDomainRoot.objectName().getDomain()); mListener.startListening(); println( "Demo: " + host + ":" + port + " as {" + user + "," + password + "}" ); } private static String getArg(final String name, final String defaultValue, final String[] args ) { String value = defaultValue; for( final String arg : args ) { if ( arg == null ) continue; if ( arg.startsWith(name) || arg.startsWith("--" + name) ) { final int idx = arg.indexOf("="); value = arg.substring(idx+1); break; } } return value; } /** Run the demo. */ public static void runDemo( final boolean emitMessages, final String[] args ) { final String host = getArg("host", "localhost", args); final String port = getArg("port", "8686", args); final String user = getArg("user", "admin", args); final String password = getArg("password", "changeit", args); try { final Demo demo = new Demo(emitMessages, host, port, user, password); demo.run(); } catch( final Throwable t ) { t.printStackTrace(); } } public static void main( final String[] args ) { runDemo( true, args ); } private void run() { final Set<AMXProxy> amxMBeans = mQueryMgr.queryAll(); println( "AMX MBeans: " + amxMBeans.size() ); println( "AMX config MBeans: " + getAllConfig().size() ); // set an Attribute via getter/setter String locale = mDomainConfig.getLocale(); mDomainConfig.setLocale("GreekSpeak"); println( "Changed locale from " + locale + " to " + mDomainConfig.getLocale() + ", then back."); mDomainConfig.setLocale(locale); // this is how to do the same attribute generically try { locale = (String)mDomainConfig.extra().getAttribute("Locale"); mDomainConfig.extra().setAttribute( new Attribute("Locale", "GreekSpeak")); mDomainConfig.extra().setAttribute( new Attribute("Locale", locale)); // restore it } catch( Exception e ) { e.printStackTrace(); } } /** enable monitoring to HIGH for all available systems so that MBeans in those systems get tested */ protected synchronized void enableMonitoring() throws Exception { for( final Config config : mDomainConfig.getConfigs().getConfig().values() ) { final MonitoringService mon = config.getMonitoringService(); if ( mon == null ) { // this can happen for temporary configs assert ! config.getName().equals("server-config"); //debug( "No MonitoringService in Config: " + config.objectName() ); continue; } final AttributeList prevLevels = ModuleMonitoringLevels.Helper.setAllMonitoringLevel( mon.getModuleMonitoringLevels(), ModuleMonitoringLevels.HIGH ); } } /** get all AMX MBeans that were found when the test started Caller should use the QueryMgr if a fresh set is needed */ protected Set<AMXProxy> getAllAMX() { final Set<AMXProxy> allAMX = mQueryMgr.queryAll(); assert allAMX.size() >= 30; return allAMX; } /** get all AMX MBeans with the specified interface */ protected <T> Set Other Glassfish examples (source code examples)Here is a short list of links related to this Glassfish Demo.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
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.