ec9d348d7969b62a44baacb3958ca0516c567b3f
[jSite2.git] / src / net / pterodactylus / jsite / core / Verifier.java
1 /*
2  * jSite2 - Verifier.java -
3  * Copyright © 2008 David Roden
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package net.pterodactylus.jsite.core;
21
22 import java.net.InetAddress;
23 import java.net.UnknownHostException;
24
25 /**
26  * Contains verifier for various settings.
27  *
28  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
29  * @version $Id$
30  */
31 public class Verifier {
32
33         /**
34          * Checks whether the given name is a valid node name.
35          *
36          * @param name
37          *            The name to verify
38          * @return <code>true</code> if the name is okay, <code>false</code> if
39          *         there is an error
40          */
41         public static boolean verifyNodeName(String name) {
42                 return (name != null) && (name.trim().length() != 0);
43         }
44
45         /**
46          * Verifies the given hostname by trying to resolve it.
47          *
48          * @param hostname
49          *            The hostname to verify
50          * @return <code>true</code> if the hostname is not empty and can be
51          *         resolved, <code>false</code> otherwise
52          */
53         public static boolean verifyHostname(String hostname) {
54                 if ((hostname == null) || (hostname.trim().length() == 0)) {
55                         return false;
56                 }
57                 try {
58                         InetAddress.getByName(hostname.trim());
59                         return true;
60                 } catch (UnknownHostException uhe1) {
61                         return false;
62                 }
63         }
64
65         /**
66          * Verifies that the port number is numeric and in the range from
67          * <code>0</code> to <code>65535</code>.
68          *
69          * @param portString
70          *            The port number string
71          * @return <code>true</code> if the port number is okay,
72          *         <code>false</code> otherwise
73          */
74         public static boolean verifyPort(String portString) {
75                 if ((portString == null) || (portString.trim().length() == 0)) {
76                         return false;
77                 }
78                 try {
79                         int portNumber = Integer.valueOf(portString.trim());
80                         if ((portNumber > 0) && (portNumber < 65536)) {
81                                 return true;
82                         }
83                 } catch (NumberFormatException nfe1) {
84                         /* ignore. */
85                 }
86                 return false;
87         }
88
89 }