Skip to content

Commit b6c7b24

Browse files
authored
Support for interface selection (#32)
* Improvement: Get the provider list from zk, the ui directly selects the interface to reduce the input error probability * modify comments * add jar file to dist directory
1 parent 9181d8a commit b6c7b24

10 files changed

+721
-30
lines changed
Binary file not shown.

dist/jmeter-plugins-dubbo-1.3.7.jar

56.8 KB
Binary file not shown.

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
<modelVersion>4.0.0</modelVersion>
2020
<groupId>io.github.ningyu</groupId>
2121
<artifactId>jmeter-plugins-dubbo</artifactId>
22-
<version>1.3.6</version>
22+
<version>1.3.7</version>
2323

2424
<properties>
2525
<maven.compiler.source>1.7</maven.compiler.source>
2626
<maven.compiler.target>1.7</maven.compiler.target>
2727
<maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
28-
<dubbo_version>2.6.2</dubbo_version>
28+
<dubbo_version>2.6.4</dubbo_version>
2929
<gson_version>2.8.2</gson_version>
3030
<zookeeper_version>3.4.9</zookeeper_version>
3131
<zkclient_version>0.2</zkclient_version>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package io.github.ningyu.jmeter.plugin.dubbo.gui;
19+
20+
import javax.swing.*;
21+
import java.awt.event.ItemListener;
22+
import java.awt.event.KeyEvent;
23+
import java.awt.event.KeyListener;
24+
import java.util.List;
25+
import java.util.Vector;
26+
27+
/**
28+
* AutoCompleter
29+
*/
30+
public class AutoCompleter implements KeyListener {
31+
32+
private JComboBox owner = null;
33+
private JTextField editor = null;
34+
35+
private ComboBoxModel model = null;
36+
37+
public AutoCompleter(JComboBox comboBox) {
38+
owner = comboBox;
39+
editor = (JTextField) comboBox.getEditor().getEditorComponent();
40+
editor.addKeyListener(this);
41+
model = comboBox.getModel();
42+
// owner.addItemListener(this);
43+
}
44+
45+
public AutoCompleter(JComboBox comboBox, ItemListener itemListener) {
46+
owner = comboBox;
47+
editor = (JTextField) comboBox.getEditor().getEditorComponent();
48+
editor.addKeyListener(this);
49+
model = comboBox.getModel();
50+
owner.addItemListener(itemListener);
51+
}
52+
53+
public void keyTyped(KeyEvent e) {
54+
}
55+
56+
public void keyPressed(KeyEvent e) {
57+
// char ch = e.getKeyChar();
58+
// if (ch == KeyEvent.VK_ENTER) {
59+
// return;
60+
// }
61+
// editor.setText("");
62+
}
63+
64+
public void keyReleased(KeyEvent e) {
65+
char ch = e.getKeyChar();
66+
if (ch == KeyEvent.VK_ENTER) {
67+
int caretPosition = editor.getCaretPosition();
68+
String str = editor.getText();
69+
if (str.length() == 0)
70+
return;
71+
autoComplete(str, caretPosition);
72+
}
73+
// if (ch == KeyEvent.CHAR_UNDEFINED || Character.isISOControl(ch)
74+
// || ch == KeyEvent.VK_DELETE)
75+
// return;
76+
}
77+
78+
/**
79+
* 自动完成。根据输入的内容,在列表中找到相似的项目.
80+
*/
81+
protected void autoComplete(String strf, int caretPosition) {
82+
Object[] opts;
83+
opts = getMatchingOptions(strf.substring(0, caretPosition));
84+
if (owner != null) {
85+
model = new DefaultComboBoxModel(opts);
86+
owner.setModel(model);
87+
}
88+
if (opts.length > 0) {
89+
String str = opts[0].toString();
90+
// editor.setCaretPosition(caretPosition);
91+
if (owner != null) {
92+
try {
93+
owner.showPopup();
94+
} catch (Exception ex) {
95+
ex.printStackTrace();
96+
}
97+
}
98+
}
99+
}
100+
101+
/**
102+
* 找到相似的项目, 并且将之排列到数组的最前面。
103+
*
104+
* @param str
105+
* @return 返回所有项目的列表。
106+
*/
107+
protected Object[] getMatchingOptions(String str) {
108+
List v = new Vector();
109+
List v1 = new Vector();
110+
model = owner.getModel();
111+
for (int k = 0; k < model.getSize(); k++) {
112+
Object itemObj = model.getElementAt(k);
113+
if (itemObj != null) {
114+
String item = itemObj.toString();
115+
if (item.toUpperCase().indexOf(str.toUpperCase()) != -1)
116+
v.add(model.getElementAt(k));
117+
else
118+
v1.add(model.getElementAt(k));
119+
} else
120+
v1.add(model.getElementAt(k));
121+
}
122+
for (int i = 0; i < v1.size(); i++) {
123+
v.add(v1.get(i));
124+
}
125+
if (v.isEmpty())
126+
v.add(str);
127+
return v.toArray();
128+
}
129+
}

src/main/java/io/github/ningyu/jmeter/plugin/dubbo/gui/DubboSampleGui.java

+110-24
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,34 @@
1616
*/
1717
package io.github.ningyu.jmeter.plugin.dubbo.gui;
1818

19+
import com.alibaba.dubbo.common.URL;
20+
1921
import io.github.ningyu.jmeter.plugin.dubbo.sample.DubboSample;
2022
import io.github.ningyu.jmeter.plugin.dubbo.sample.MethodArgument;
23+
import io.github.ningyu.jmeter.plugin.dubbo.sample.ProviderService;
24+
import org.apache.commons.lang3.StringUtils;
25+
import org.apache.jmeter.gui.util.HorizontalPanel;
26+
import org.apache.jmeter.gui.util.VerticalPanel;
27+
import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
28+
import org.apache.jmeter.testelement.TestElement;
29+
import org.apache.jorphan.logging.LoggingManager;
30+
import org.apache.log.Logger;
2131

22-
import java.awt.BorderLayout;
23-
import java.awt.Container;
32+
import javax.swing.*;
33+
import javax.swing.table.DefaultTableModel;
34+
import java.awt.*;
2435
import java.awt.event.ActionEvent;
2536
import java.awt.event.ActionListener;
37+
import java.awt.event.ItemEvent;
38+
import java.awt.event.ItemListener;
39+
import java.beans.PropertyChangeEvent;
40+
import java.beans.PropertyChangeListener;
2641
import java.util.ArrayList;
2742
import java.util.Iterator;
2843
import java.util.List;
44+
import java.util.Map;
2945
import java.util.Vector;
3046

31-
import javax.swing.BorderFactory;
32-
import javax.swing.ImageIcon;
33-
import javax.swing.JButton;
34-
import javax.swing.JComboBox;
35-
import javax.swing.JLabel;
36-
import javax.swing.JPanel;
37-
import javax.swing.JScrollPane;
38-
import javax.swing.JTable;
39-
import javax.swing.JTextField;
40-
import javax.swing.SwingConstants;
41-
import javax.swing.table.DefaultTableModel;
42-
43-
import org.apache.jmeter.gui.util.HorizontalPanel;
44-
import org.apache.jmeter.gui.util.VerticalPanel;
45-
import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
46-
import org.apache.jmeter.testelement.TestElement;
47-
import org.apache.jorphan.logging.LoggingManager;
48-
import org.apache.log.Logger;
49-
5047
/**
5148
* DubboSampleGui </br>
5249
* invoke sequence**clearGui()->createTestElement()->modifyTestElement()->configure()**
@@ -58,7 +55,7 @@ public class DubboSampleGui extends AbstractSamplerGui {
5855
/**
5956
*/
6057
private static final long serialVersionUID = -3248204995359935007L;
61-
58+
6259
private JComboBox<String> registryProtocolText;
6360
private JComboBox<String> rpcProtocolText;
6461
private JTextField addressText;
@@ -76,7 +73,8 @@ public class DubboSampleGui extends AbstractSamplerGui {
7673
private String[] columnNames = {"paramType", "paramValue"};
7774
private String[] tmpRow = {"", ""};
7875
private int textColumns = 2;
79-
76+
private JAutoCompleteComboBox<String> interfaceList;
77+
private JAutoCompleteComboBox<String> methodList;
8078

8179
public DubboSampleGui() {
8280
super();
@@ -117,7 +115,51 @@ private void init() {
117115
ah.add(addressText);
118116
ah.add(addressHelpLable);
119117
registrySettings.add(ah);
120-
118+
//Selection Interface
119+
JPanel sh = new HorizontalPanel();
120+
JButton jButton = new JButton("Get Provider List");
121+
interfaceList = new JAutoCompleteComboBox<String>(new DefaultComboBoxModel<String>(new String[]{}), new ItemListener() {
122+
@Override
123+
public void itemStateChanged(ItemEvent e) {
124+
if (e.getStateChange() == ItemEvent.SELECTED) {
125+
doChange(e.getItem().toString());
126+
}
127+
}
128+
});
129+
interfaceList.addPropertyChangeListener("model", new PropertyChangeListener() {
130+
@Override
131+
public void propertyChange(PropertyChangeEvent evt) {
132+
doChange(interfaceList.getSelectedItem().toString());
133+
}
134+
});
135+
jButton.addActionListener(new ActionListener() {
136+
@Override
137+
public void actionPerformed(ActionEvent e) {
138+
doConfirm(e, interfaceList);
139+
}
140+
});
141+
sh.add(jButton);
142+
sh.add(new JLabel("Interfaces:", SwingConstants.RIGHT));
143+
sh.add(interfaceList);
144+
sh.add(new JLabel("Methods:", SwingConstants.RIGHT));
145+
methodList = new JAutoCompleteComboBox<String>(new DefaultComboBoxModel<String>(new String[]{}), new ItemListener() {
146+
@Override
147+
public void itemStateChanged(ItemEvent e) {
148+
if (e.getStateChange() == ItemEvent.SELECTED) {
149+
methodText.setText(e.getItem().toString());
150+
}
151+
}
152+
});
153+
methodList.addPropertyChangeListener("model", new PropertyChangeListener() {
154+
@Override
155+
public void propertyChange(PropertyChangeEvent evt) {
156+
methodText.setText(methodList.getSelectedItem().toString());
157+
}
158+
});
159+
sh.add(methodList);
160+
registrySettings.add(sh);
161+
162+
121163
//RPC Protocol Settings
122164
JPanel protocolSettings = new VerticalPanel();
123165
protocolSettings.setBorder(BorderFactory.createTitledBorder("RPC Protocol Settings"));
@@ -391,6 +433,50 @@ public void clearGui() {
391433
model.setDataVector(null, columnNames);
392434
}
393435

436+
private void doChange(String key) {
437+
ProviderService providerService = ProviderService.get(addressText.getText());
438+
Map<String, URL> provider = providerService.findByService(key);
439+
if (provider != null && !provider.isEmpty()) {
440+
URL url = new ArrayList<URL>(provider.values()).get(0);
441+
String group = url.getParameter(com.alibaba.dubbo.common.Constants.GROUP_KEY);
442+
String version = url.getParameter(com.alibaba.dubbo.common.Constants.VERSION_KEY);
443+
String timeout = url.getParameter(com.alibaba.dubbo.common.Constants.TIMEOUT_KEY);
444+
String protocol = url.getProtocol() + "://";
445+
String interfaceName = url.getServiceInterface();
446+
String method = url.getParameter(com.alibaba.dubbo.common.Constants.METHODS_KEY);
447+
groupText.setText(group);
448+
versionText.setText(version);
449+
timeoutText.setText(timeout);
450+
rpcProtocolText.setSelectedItem(protocol);
451+
interfaceText.setText(interfaceName);
452+
//set method
453+
String[] items = method.split(",");
454+
methodList.setModel(new DefaultComboBoxModel<String>(items));
455+
}
456+
}
457+
458+
private void doConfirm(ActionEvent event, JAutoCompleteComboBox<String> interfaceList) {
459+
String protocol = registryProtocolText.getSelectedItem().toString();
460+
String address = addressText.getText();
461+
if (StringUtils.isBlank(address)) {
462+
JOptionPane.showMessageDialog(this.getParent(), "Address can't be empty!", "error", JOptionPane.ERROR_MESSAGE);
463+
return;
464+
}
465+
int result = JOptionPane.showConfirmDialog(this.getParent(), "Obtaining all the providers lists may cause jmeter to stop responding for a few seconds. Do you want to continue?", "warn", JOptionPane.YES_NO_CANCEL_OPTION);
466+
if (result == JOptionPane.YES_OPTION) {
467+
List<String> list = new ArrayList<String>();
468+
try {
469+
list = ProviderService.get(address).getProviders(protocol, address);
470+
JOptionPane.showMessageDialog(this.getParent(), "Get provider list to finish! Check if the log has errors.", "info", JOptionPane.INFORMATION_MESSAGE);
471+
} catch (Exception e) {
472+
JOptionPane.showMessageDialog(this.getParent(), e.getMessage(), "error", JOptionPane.ERROR_MESSAGE);
473+
return;
474+
}
475+
String[] items = list.toArray(new String[]{});
476+
interfaceList.setModel(new DefaultComboBoxModel<String>(items));
477+
}
478+
}
479+
394480
}
395481

396482

0 commit comments

Comments
 (0)