| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package sk.baka.webvm; |
| 20 | |
|
| 21 | |
import java.lang.reflect.Field; |
| 22 | |
import java.util.ArrayList; |
| 23 | |
import java.util.EnumSet; |
| 24 | |
import org.apache.wicket.MarkupContainer; |
| 25 | |
import org.apache.wicket.markup.html.form.Button; |
| 26 | |
import org.apache.wicket.markup.html.form.DropDownChoice; |
| 27 | |
import org.apache.wicket.markup.html.form.Form; |
| 28 | |
import org.apache.wicket.markup.html.form.FormComponent; |
| 29 | |
import org.apache.wicket.markup.html.form.PasswordTextField; |
| 30 | |
import org.apache.wicket.markup.html.form.TextField; |
| 31 | |
import org.apache.wicket.markup.html.panel.FeedbackPanel; |
| 32 | |
import org.apache.wicket.model.PropertyModel; |
| 33 | |
import org.apache.wicket.validation.IValidator; |
| 34 | |
import org.apache.wicket.validation.validator.RangeValidator; |
| 35 | |
import sk.baka.webvm.misc.NotificationDelivery; |
| 36 | |
import sk.baka.webvm.analyzer.ProblemAnalyzer; |
| 37 | |
import sk.baka.webvm.config.Bind; |
| 38 | |
import sk.baka.webvm.config.Config; |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | 0 | public final class Configure extends WebVMPage { |
| 45 | |
|
| 46 | |
private static final long serialVersionUID = 1L; |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | 0 | public Configure() { |
| 52 | |
|
| 53 | 0 | border.add(new ConfigForm("problemsForm", Config.GROUP_PROBLEMS)); |
| 54 | 0 | border.add(new MailForm("mailForm")); |
| 55 | 0 | border.add(new JabberForm("jabberForm")); |
| 56 | 0 | } |
| 57 | |
|
| 58 | |
@SuppressWarnings("unchecked") |
| 59 | |
private static void bind(final MarkupContainer c, final Object bean, final int group) { |
| 60 | 0 | for (final Field field : bean.getClass().getFields()) { |
| 61 | 0 | final Bind annotation = field.getAnnotation(Bind.class); |
| 62 | 0 | if (annotation == null) { |
| 63 | 0 | continue; |
| 64 | |
} |
| 65 | 0 | if (group >= 0 && annotation.group() != group) { |
| 66 | 0 | continue; |
| 67 | |
} |
| 68 | |
final FormComponent<?> f; |
| 69 | 0 | if (Enum.class.isAssignableFrom(field.getType())) { |
| 70 | 0 | final Class<? extends Enum> clazz = field.getType().asSubclass(Enum.class); |
| 71 | 0 | final EnumSet<? extends Enum> allConstants = EnumSet.allOf(clazz); |
| 72 | 0 | f = new DropDownChoice<Enum>(annotation.key(), new PropertyModel<Enum>(bean, field.getName()), new ArrayList<Enum>(allConstants)); |
| 73 | 0 | } else if (annotation.password()) { |
| 74 | 0 | f = new PasswordTextField(annotation.key(), new PropertyModel<String>(bean, field.getName())); |
| 75 | |
} else { |
| 76 | 0 | final TextField<Object> tf = new TextField<Object>(annotation.key(), new PropertyModel<Object>(bean, field.getName())); |
| 77 | 0 | if ((annotation.min() != Integer.MIN_VALUE) || (annotation.max() != Integer.MAX_VALUE)) { |
| 78 | |
|
| 79 | 0 | tf.add((IValidator) new RangeValidator<Integer>(annotation.min(), annotation.max())); |
| 80 | |
} |
| 81 | 0 | f = tf; |
| 82 | |
} |
| 83 | 0 | f.setRequired(annotation.required()); |
| 84 | 0 | c.add(f); |
| 85 | |
} |
| 86 | 0 | } |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
protected static class ConfigForm extends Form<Config> { |
| 92 | |
|
| 93 | |
private static final long serialVersionUID = 1L; |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | 0 | protected final Config config = new Config(WicketApplication.getConfig()); |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public ConfigForm(final String componentName, final int group) { |
| 105 | 0 | super(componentName); |
| 106 | 0 | add(new FeedbackPanel("feedback")); |
| 107 | 0 | bind(this, config, group); |
| 108 | 0 | add(new Button("submit")); |
| 109 | 0 | } |
| 110 | |
|
| 111 | |
@Override |
| 112 | |
public final void onSubmit() { |
| 113 | 0 | WicketApplication.setConfig(config); |
| 114 | 0 | } |
| 115 | |
} |
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
private static class MailForm extends ConfigForm { |
| 121 | |
|
| 122 | |
public MailForm(String componentName) { |
| 123 | 0 | super(componentName, Config.GROUP_MAIL); |
| 124 | 0 | } |
| 125 | |
private static final long serialVersionUID = 1L; |
| 126 | |
{ |
| 127 | 0 | add(new Button("sendTestMail") { |
| 128 | |
|
| 129 | |
private static final long serialVersionUID = 1L; |
| 130 | |
|
| 131 | |
@Override |
| 132 | |
public void onSubmit() { |
| 133 | 0 | if (!NotificationDelivery.isEmailEnabled(config)) { |
| 134 | 0 | error("No SMTP host name is entered, mail notification is disabled"); |
| 135 | 0 | return; |
| 136 | |
} |
| 137 | 0 | final ProblemAnalyzer pa = new ProblemAnalyzer(); |
| 138 | 0 | pa.configure(config); |
| 139 | |
try { |
| 140 | 0 | NotificationDelivery.sendEmail(config, true, pa.getProblems(WicketApplication.getHistory().getVmstatHistory())); |
| 141 | 0 | } catch (Exception ex) { |
| 142 | 0 | error("Failed to send a message: " + ex.toString()); |
| 143 | 0 | } |
| 144 | 0 | } |
| 145 | |
}); |
| 146 | |
} |
| 147 | |
} |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
private static class JabberForm extends ConfigForm { |
| 153 | |
|
| 154 | |
public JabberForm(String componentName) { |
| 155 | 0 | super(componentName, Config.GROUP_JABBER); |
| 156 | 0 | } |
| 157 | |
private static final long serialVersionUID = 1L; |
| 158 | |
{ |
| 159 | 0 | add(new Button("sendTestJabber") { |
| 160 | |
|
| 161 | |
private static final long serialVersionUID = 1L; |
| 162 | |
|
| 163 | |
@Override |
| 164 | |
public void onSubmit() { |
| 165 | 0 | if (!NotificationDelivery.isJabberEnabled(config)) { |
| 166 | 0 | error("No Jabber server is entered, jabber notification is disabled"); |
| 167 | 0 | return; |
| 168 | |
} |
| 169 | 0 | final ProblemAnalyzer pa = new ProblemAnalyzer(); |
| 170 | 0 | pa.configure(config); |
| 171 | |
try { |
| 172 | 0 | NotificationDelivery.sendJabber(config, true, pa.getProblems(WicketApplication.getHistory().getVmstatHistory())); |
| 173 | 0 | } catch (Exception ex) { |
| 174 | 0 | error("Failed to send a message: " + ex.toString()); |
| 175 | 0 | } |
| 176 | 0 | } |
| 177 | |
}); |
| 178 | |
} |
| 179 | |
} |
| 180 | |
} |
| 181 | |
|