1 /*
2 ** Zabbix
3 ** Copyright (C) 2001-2021 Zabbix SIA
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 **/
19 
20 package com.zabbix.gateway;
21 
22 import java.io.File;
23 import java.net.InetAddress;
24 
25 class ConfigurationParameter
26 {
27 	static final int TYPE_INTEGER = 0;
28 	static final int TYPE_INETADDRESS = 1;
29 	static final int TYPE_FILE = 2;
30 
31 	private String name;
32 	private int type;
33 	private Object value;
34 	private InputValidator validator;
35 	private PostInputValidator postValidator;
36 
ConfigurationParameter(String name, int type, Object defaultValue, InputValidator validator, PostInputValidator postValidator)37 	ConfigurationParameter(String name, int type, Object defaultValue, InputValidator validator, PostInputValidator postValidator)
38 	{
39 		this.name = name;
40 		this.type = type;
41 		this.value = defaultValue;
42 		this.validator = validator;
43 		this.postValidator = postValidator;
44 	}
45 
getName()46 	String getName()
47 	{
48 		return name;
49 	}
50 
getType()51 	int getType()
52 	{
53 		return type;
54 	}
55 
getValue()56 	Object getValue()
57 	{
58 		return value;
59 	}
60 
setValue(String text)61 	void setValue(String text)
62 	{
63 		Object userValue = null;
64 
65 		try
66 		{
67 			switch (type)
68 			{
69 				case TYPE_INTEGER:
70 					userValue = Integer.valueOf(text);
71 					break;
72 				case TYPE_INETADDRESS:
73 					userValue = InetAddress.getByName(text);
74 					break;
75 				case TYPE_FILE:
76 					userValue = new File(text);
77 					break;
78 				default:
79 					throw new IllegalArgumentException(String.format("unknown type %d", type));
80 			}
81 		}
82 		catch (Exception e)
83 		{
84 			throw new IllegalArgumentException(e);
85 		}
86 
87 		if (null != validator && !validator.validate(userValue))
88 			throw new IllegalArgumentException(String.format("bad value for %s parameter: '%s'", name, text));
89 
90 		if (null != postValidator)
91 			postValidator.execute(userValue);
92 
93 		this.value = userValue;
94 	}
95 }
96