1 /*
2  * Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 import java.security.Principal;
25 import java.util.ArrayList;
26 import java.util.List;
27 
28 import javax.management.remote.JMXServiceURL ;
29 import javax.management.MBeanRegistration;
30 import javax.management.MBeanServer;
31 import javax.management.ObjectName;
32 import javax.management.StandardMBean;
33 
34 /**
35  * This class defines an MBean that can be registered and used on client side
36  * to handle informations or properties of the remote server.
37  *
38  * For example, this MBean can store IOR addresses
39  * of RMI/IIOP connector(s) used in a test.
40  *
41  * That MBean might not be used for testing purpose itself.
42  */
43 public class ServerDelegate implements ServerDelegateMBean, MBeanRegistration {
44 
45     private MBeanServer mbeanServer = null;
46     private List<JMXServiceURL> addresses  = null;
47     private String port;
48     private static String javaVersion = System.getProperty("java.version");
49     private int sqeJmxwsCredentialsProviderCallCount = 0;
50     private String jmxwsCredentialsProviderUrl = null;
51     private int testJMXAuthenticatorCallCount = 0;
52     private Principal testJMXAuthenticatorPrincipal = null;
53 
54     @SqeDescriptorKey("NO PARAMETER CONSTRUCTOR ServerDelegate")
ServerDelegate()55     public ServerDelegate() {
56         addresses = new ArrayList<JMXServiceURL>();
57     }
58 
preRegister(MBeanServer server, ObjectName name)59     public ObjectName preRegister(MBeanServer server, ObjectName name)
60     throws Exception {
61         // Initialize MBeanServer attribute
62         mbeanServer = server;
63         return name;
64     }
postRegister(Boolean registrationDone)65     public void postRegister(Boolean registrationDone) {
66     }
preDeregister()67     public void preDeregister() throws Exception {
68     }
postDeregister()69     public void postDeregister() {
70     }
71 
addAddress(JMXServiceURL url)72     public void addAddress(JMXServiceURL url) {
73         addresses.add(url) ;
74     }
75 
getAddresses()76     public List<JMXServiceURL> getAddresses() {
77         return addresses ;
78     }
79 
setPort(String p)80     public void setPort(String p) {
81         port = p ;
82     }
83 
getPort()84     public String getPort() {
85         return port ;
86     }
87 
getJavaVersion()88     public String getJavaVersion() {
89         return javaVersion;
90     }
91 
sqeJmxwsCredentialsProviderCalled()92     public void sqeJmxwsCredentialsProviderCalled() {
93         sqeJmxwsCredentialsProviderCallCount++;
94     }
95 
getSqeJmxwsCredentialsProviderCallCount()96     public int getSqeJmxwsCredentialsProviderCallCount() {
97         return sqeJmxwsCredentialsProviderCallCount;
98     }
99 
setJmxwsCredentialsProviderUrl(String url)100     public void setJmxwsCredentialsProviderUrl(String url) {
101         jmxwsCredentialsProviderUrl = url;
102     }
103 
getJmxwsCredentialsProviderUrl()104     public String getJmxwsCredentialsProviderUrl() {
105         return jmxwsCredentialsProviderUrl;
106     }
107 
testJMXAuthenticatorCalled()108     public void testJMXAuthenticatorCalled() {
109         testJMXAuthenticatorCallCount++;
110     }
111 
getTestJMXAuthenticatorCallCount()112     public int getTestJMXAuthenticatorCallCount() {
113         return testJMXAuthenticatorCallCount;
114     }
115 
setTestJMXAuthenticatorPrincipal(Principal principal)116     public void setTestJMXAuthenticatorPrincipal(Principal principal) {
117         testJMXAuthenticatorPrincipal = principal;
118     }
119 
getTestJMXAuthenticatorPrincipalString()120     public String getTestJMXAuthenticatorPrincipalString() {
121         if ( testJMXAuthenticatorPrincipal != null ) {
122             return testJMXAuthenticatorPrincipal.toString();
123         }
124 
125         return null;
126     }
127 
128    /**
129      * Instantiates and registers a StandardMBean in the MBean server.
130      *
131      * @param implementationClassName
132      *      The implementation class name of the MBean.
133      * @param interfaceClassName
134      *      The management interface class name of the MBean.
135      * @param isMXBean
136      *      If true, the resultant MBean is an MXBean.
137      * @param name
138      *      The object name of the StandardMBean.
139      */
140     @SuppressWarnings("unchecked")
createStandardMBean( String implementationClassName, String interfaceClassName, boolean isMXBean, ObjectName name)141     public void createStandardMBean(
142             String implementationClassName,
143             String interfaceClassName,
144             boolean isMXBean,
145             ObjectName name)
146             throws Exception {
147 
148         Object implementation =
149                 Class.forName(implementationClassName).newInstance();
150         Class<Object> interfaceClass = interfaceClassName == null ? null :
151             (Class<Object>)Class.forName(interfaceClassName);
152 
153         // Create the StandardMBean
154         StandardMBean standardMBean = new StandardMBean(
155                 implementation,
156                 interfaceClass,
157                 isMXBean);
158 
159         // Register the StandardMBean
160         mbeanServer.registerMBean(standardMBean, name);
161     }
162 
163     /**
164      * Instantiates and registers a StandardMBean in the MBean server.
165      * The object will use standard JMX design pattern to determine
166      * the management interface associated with the given implementation.
167      */
168     @SuppressWarnings("unchecked")
createStandardMBean( String implementationClassName, boolean isMXBean, ObjectName name)169     public void createStandardMBean(
170             String implementationClassName,
171             boolean isMXBean,
172             ObjectName name)
173             throws Exception {
174 
175         createStandardMBean(implementationClassName, null, isMXBean, name);
176     }
177 }
178