|
Java example source code file (NotificationAccessControllerTest.java)
The NotificationAccessControllerTest.java Java example source code
/*
* Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 5106721
* @summary Check the NotificationAccessController methods are properly called.
* @author Luis-Miguel Alventosa
* @run clean NotificationAccessControllerTest
* @run build NotificationAccessControllerTest
* @run main NotificationAccessControllerTest
*/
import com.sun.jmx.remote.security.NotificationAccessController;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerFactory;
import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.remote.JMXAuthenticator;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXPrincipal;
import javax.management.remote.JMXServiceURL;
import javax.security.auth.Subject;
public class NotificationAccessControllerTest {
public class NAC implements NotificationAccessController {
private boolean throwException;
public NAC(boolean throwException) {
this.throwException = throwException;
}
public void addNotificationListener(
String connectionId,
ObjectName name,
Subject subject)
throws SecurityException {
echo("addNotificationListener:");
echo("\tconnectionId: " + connectionId);
echo("\tname: " + name);
echo("\tsubject: " +
(subject == null ? null : subject.getPrincipals()));
if (throwException)
if (name.getCanonicalName().equals("domain:name=1,type=NB")
&&
subject.getPrincipals().contains(new JMXPrincipal("role")))
throw new SecurityException();
}
public void removeNotificationListener(
String connectionId,
ObjectName name,
Subject subject)
throws SecurityException {
echo("removeNotificationListener:");
echo("\tconnectionId: " + connectionId);
echo("\tname: " + name);
echo("\tsubject: " +
(subject == null ? null : subject.getPrincipals()));
if (throwException)
if (name.getCanonicalName().equals("domain:name=2,type=NB")
&&
subject.getPrincipals().contains(new JMXPrincipal("role")))
throw new SecurityException();
}
public void fetchNotification(
String connectionId,
ObjectName name,
Notification notification,
Subject subject)
throws SecurityException {
echo("fetchNotification:");
echo("\tconnectionId: " + connectionId);
echo("\tname: " + name);
echo("\tnotification: " + notification);
echo("\tsubject: " +
(subject == null ? null : subject.getPrincipals()));
if (!throwException)
if (name.getCanonicalName().equals("domain:name=2,type=NB") &&
subject.getPrincipals().contains(new JMXPrincipal("role")))
throw new SecurityException();
}
}
public class CustomJMXAuthenticator implements JMXAuthenticator {
public Subject authenticate(Object credentials) {
String role = ((String[]) credentials)[0];
echo("\nCreate principal with name = " + role);
return new Subject(true,
Collections.singleton(new JMXPrincipal(role)),
Collections.EMPTY_SET,
Collections.EMPTY_SET);
}
}
public interface NBMBean {
public void emitNotification(int seqnum, ObjectName name);
}
public static class NB
extends NotificationBroadcasterSupport
implements NBMBean {
public void emitNotification(int seqnum, ObjectName name) {
if (name == null) {
sendNotification(new Notification("nb", this, seqnum));
} else {
sendNotification(new Notification("nb", name, seqnum));
}
}
}
public class Listener implements NotificationListener {
public List<Notification> notifs = new ArrayList
Other Java examples (source code examples)Here is a short list of links related to this Java NotificationAccessControllerTest.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 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.