1 /* 2 Copyright (C) 2000 - 2007 Grid Systems, S.A. 3 4 This program is free software; you can redistribute it and/or modify 5 it under the terms of the GNU General Public License, version 2, as 6 published by the Free Software Foundation. 7 8 This program is distributed in the hope that it will be useful, 9 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License 14 along with this program; if not, write to the Free Software 15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 16 */ 17 18 /* 19 * Project: WindowsFirewallUtils 20 * Created on 03-ene-2005 21 * 22 * Copyright (c)2004 Grid Systems 23 */ 24 package com.gridsystems.utils.windows; 25 26 import com.gridsystems.utils.SystemUtils; 27 28 /** 29 * Wrapper for the Windows firewall profile. 30 * 31 * @author <a href="mailto:rruiz@gridsystems.com">Rodrigo Ruiz Aguayo</a> 32 * @version 1.0 33 */ 34 public final class Firewall { 35 36 /** 37 * Whether the firewall has been detected. 38 */ 39 private static boolean PRESENT = false; 40 41 /** 42 * Whether some Exception has been thrown. 43 */ 44 private static FirewallException fireExc = null; 45 46 static { 47 if (SystemUtils.isWindows()) { 48 try { 49 // Tries to load libWindowsFirewallUtils.dll 50 SystemUtils.extractDynamicLibrary("WindowsFirewallUtils", Firewall.class); 51 try { 52 initialize(); 53 } catch (Exception e) { 54 // none 55 } 56 } catch (Exception e) { 57 fireExc = new FirewallException("Error extracting dynamic library:" 58 + e.getMessage(), e); 59 } 60 } 61 } 62 63 /** 64 * Hidden constructor. 65 */ 66 private Firewall() { } 67 68 /** 69 * Gets if the windows firewall is installed on this system. 70 * 71 * @return true if the firewall is detected 72 * @throws FirewallException 73 */ 74 public static boolean isPresent() throws FirewallException { 75 if (fireExc != null) { throw fireExc; } 76 return PRESENT; 77 } 78 79 /** 80 * Gets if the windows firewall is enabled. 81 * 82 * @return true if the firewall is enabled 83 * @throws FirewallException 84 */ 85 public static boolean isEnabled() throws FirewallException { 86 if (fireExc != null) { throw fireExc; } 87 return PRESENT && isEnabled0(); 88 } 89 90 /** 91 * Gets if the windows firewall is enabled. 92 * 93 * @param enable true if the firewall is to be enabled 94 * @return true if the operation is performed, false if it was not necessary 95 * @throws FirewallException 96 */ 97 public static boolean setEnabled(boolean enable) throws FirewallException { 98 if (fireExc != null) { throw fireExc; } 99 return PRESENT && setEnabled0(enable); 100 } 101 102 /** 103 * Opens a port in the firewall. 104 * 105 * @param serviceName The name of the port 106 * @param port The port number 107 * @return true if the port is open, false if it already was 108 * @throws FirewallException If an error occurs 109 */ 110 public static boolean openPort(String serviceName, int port) throws FirewallException { 111 if (fireExc != null) { throw fireExc; } 112 return PRESENT && openPort0(serviceName, port); 113 } 114 115 /** 116 * Closes a port in the firewall. 117 * 118 * @param port The port number 119 * @return true if the port was closed, false if it already was 120 * @throws FirewallException If an error occurs 121 */ 122 public static boolean closePort(int port) throws FirewallException { 123 if (fireExc != null) { throw fireExc; } 124 return PRESENT && closePort0(port); 125 } 126 127 /** 128 * Initializes this class. 129 */ 130 private static native void initialize(); 131 132 /** 133 * Gets if the windows firewall is enabled. 134 * 135 * @return true if the firewall is enabled 136 */ 137 private static native boolean isEnabled0(); 138 139 /** 140 * Sets if the windows firewall is enabled. 141 * 142 * @param enable true if the firewall is to be enabled 143 * @return true if the operation is performed, false if it was not necessary 144 */ 145 private static native boolean setEnabled0(boolean enable); 146 147 /** 148 * Opens a port in the firewall. 149 * 150 * @param serviceName The name of the port 151 * @param port The port number 152 * @return true if the port is open, false if it already was 153 * @throws FirewallException If an error occurs 154 */ 155 private static native boolean openPort0(String serviceName, int port) 156 throws FirewallException; 157 158 /** 159 * Closes a port in the firewall. 160 * 161 * @param port The port number 162 * @return true if the port was closed, false if it already was 163 * @throws FirewallException If an error occurs 164 */ 165 private static native boolean closePort0(int port) throws FirewallException; 166 }