1 /*
2  * Copyright (c) 2005, 2018, 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 package nsk.monitoring.share;
25 
26 import nsk.share.test.*;
27 import nsk.share.runner.*;
28 import nsk.share.log.Log;
29 import java.lang.management.*;
30 import javax.management.*;
31 import nsk.monitoring.share.thread.RunningThread;
32 import nsk.monitoring.share.thread.SleepingThread;
33 import nsk.monitoring.share.thread.WaitingThread;
34 import nsk.monitoring.share.thread.BlockedThread;
35 import nsk.monitoring.share.thread.*;
36 import nsk.monitoring.share.direct.DirectMonitoringFactory;
37 import nsk.monitoring.share.server.ServerMonitoringFactory;
38 import nsk.monitoring.share.proxy.ProxyMonitoringFactory;
39 import nsk.share.TestBug;
40 import nsk.share.Failure;
41 import java.util.Collection;
42 import java.util.List;
43 import java.util.ArrayList;
44 import java.util.Set;
45 
46 public class Monitoring extends nsk.share.test.Tests {
Monitoring()47         private Monitoring() {
48         }
49 
50         /**
51          * Convert monitoring exception.
52          *
53          * @param e exception
54          * @return converted exception
55          */
convertException(Throwable e)56         public static RuntimeException convertException(Throwable e) {
57                 //e.printStackTrace(logger.getOutStream());
58                 return new Failure(e);
59         }
60 
61         protected static class MonitoringTestRunner extends TestRunner {
62                 private ArgumentHandler argHandler;
63 
MonitoringTestRunner(Test test, String[] args)64                 public MonitoringTestRunner(Test test, String[] args) {
65                         super(test, args);
66                 }
67 
getArgumentHandler(String[] args)68                 public synchronized ArgumentHandler getArgumentHandler(String[] args) {
69                         if (argHandler == null)
70                                 argHandler = new ArgumentHandler(args);
71                         return argHandler;
72                 }
73 
getMonitoringFactory(String testMode, String serverType)74                 public synchronized MonitoringFactory getMonitoringFactory(String testMode, String serverType) {
75                         if (testMode.equals(ArgumentHandler.DIRECTLY_MODE))
76                                 return new DirectMonitoringFactory();
77                         else if (testMode.equals(ArgumentHandler.SERVER_MODE))
78                                 return new ServerMonitoringFactory(getMBeanServer(serverType));
79                         else if (testMode.equals(ArgumentHandler.PROXY_MODE))
80                                 return new ProxyMonitoringFactory(getMBeanServer(serverType));
81                         else
82                                 throw new TestBug("Unknown test mode" + testMode);
83                 }
84 
getMBeanServer(String serverType)85                 public synchronized MBeanServer getMBeanServer(String serverType) {
86                         if (serverType.equals(ArgumentHandler.DEFAULT_TYPE))
87                                 return ManagementFactory.getPlatformMBeanServer();
88                         else {
89                                 System.setProperty(CustomMBeanServer.SERVER_BUILDER_PROPERTY, CustomMBeanServer.CUSTOM_SERVER_BUILDER);
90                                 return ManagementFactory.getPlatformMBeanServer();
91                         }
92                 }
93 
94 
configure(Object o)95                 public void configure(Object o) {
96                         super.configure(o);
97                         if (o instanceof ArgumentHandlerAware)
98                                 ((ArgumentHandlerAware) o).setArgumentHandler(getArgumentHandler(args));
99                         if (o instanceof MonitoringFactoryAware) {
100                                 ArgumentHandler argHandler = getArgumentHandler(args);
101                                 MonitoringFactory mfactory = getMonitoringFactory(argHandler.getTestMode(), argHandler.getServerType());
102                                 ((MonitoringFactoryAware) o).setMonitoringFactory(mfactory);
103                         }
104                         if (o instanceof ScenarioTypeAware) {
105                                 ArgumentHandler argHandler = getArgumentHandler(args);
106                                 ((ScenarioTypeAware) o).setScenarioType(argHandler.getScenarioType());
107                         }
108                 }
109         }
110 
runTest(Test test, String[] args)111         public static void runTest(Test test, String[] args) {
112                 new MonitoringTestRunner(test, args).run();
113         }
114 
queryNamesByStart(MBeanServer mbeanServer, String name)115         public static Collection<ObjectName> queryNamesByStart(MBeanServer mbeanServer, String name) {
116                 try {
117                         ObjectName pattern = ObjectName.getInstance(name + "*");
118                         Set<ObjectName> query = mbeanServer.queryNames(pattern, null);
119                         List<ObjectName> list = new ArrayList<ObjectName>(query.size());
120                         for (ObjectName oname : query) {
121                             list.add(oname);
122                         }
123                         return list;
124                 } catch (Exception e) {
125                         throw convertException(e);
126                 }
127         }
128 }
129