Coverage Report - sk.baka.webvm.Configure
 
Classes in this File Line Coverage Branch Coverage Complexity
Configure
0%
0/25
0%
0/16
2.75
Configure$ConfigForm
0%
0/8
N/A
2.75
Configure$JabberForm
0%
0/3
N/A
2.75
Configure$JabberForm$1
0%
0/11
0%
0/2
2.75
Configure$MailForm
0%
0/3
N/A
2.75
Configure$MailForm$1
0%
0/11
0%
0/2
2.75
 
 1  
 /**
 2  
  * Copyright 2009 Martin Vysny.
 3  
  *
 4  
  * This file is part of WebVM.
 5  
  *
 6  
  * WebVM is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License, or
 9  
  * (at your option) any later version.
 10  
  *
 11  
  * WebVM is distributed in the hope that it will be useful,
 12  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14  
  * GNU General Public License for more details.
 15  
  *
 16  
  * You should have received a copy of the GNU General Public License
 17  
  * along with WebVM.  If not, see <http://www.gnu.org/licenses/>.
 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  
  * The WebVM configuration. The configuration is not persistent.
 42  
  * @author Martin Vysny
 43  
  */
 44  0
 public final class Configure extends WebVMPage {
 45  
 
 46  
     private static final long serialVersionUID = 1L;
 47  
 
 48  
     /**
 49  
      * Creates the configure page.
 50  
      */
 51  0
     public Configure() {
 52  
         // submitting forms in a page with multiple forms fails: http://issues.apache.org/jira/browse/WICKET-2134
 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  
                     // we know there will be only ints annotated
 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  
      * A basic configuration form, handles retrieval of config object.
 90  
      */
 91  
     protected static class ConfigForm extends Form<Config> {
 92  
 
 93  
         private static final long serialVersionUID = 1L;
 94  
         /**
 95  
          * The configuration object.
 96  
          */
 97  0
         protected final Config config = new Config(WicketApplication.getConfig());
 98  
 
 99  
         /**
 100  
          * Creates new form.
 101  
          * @param componentName wicket id
 102  
          * @param group bind only this group to the form, -1 to all groups
 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  
      * A mail form, supports sending of a test mail.
 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  
      * A Jabber configuration form, supports sending a simple notification Jabber message.
 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