View Javadoc

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  package com.gridsystems.config.modules.tomcat;
19  
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  import com.gridsystems.utils.windows.Firewall;
25  import com.gridsystems.utils.windows.FirewallException;
26  
27  /**
28   * Manages ports of firewall in Windows OS.
29   *
30   * @author Xmas
31   *
32   */
33  public final class WindowsFirewallManager {
34  
35    /**
36     * Class logger.
37     */
38    private static Log log = LogFactory.getLog(WindowsFirewallManager.class);
39  
40    /**
41     * Hidden constructor.
42     */
43    private WindowsFirewallManager() {
44    }
45  
46    /**
47     * Closes all ports defined in this model on the Windows firewall.
48     * @param connectors List of connectors to close.
49     */
50    public static void closePorts(Connector[] connectors) {
51      if (connectors != null) {
52        for (int i = 0; i < connectors.length; i++) {
53          Connector c = connectors[i];
54          closePort(c.getPort());
55        }
56      }
57    }
58  
59    /**
60     * Opens all ports defined in this model on the Windows firewall.
61     * @param productName Product Name
62     * @param connectors List of connectors to open.
63     */
64    public static void openPorts(String productName, Connector[] connectors) {
65      if (connectors != null) {
66        for (int i = 0; i < connectors.length; i++) {
67          Connector c = connectors[i];
68          openPort(productName, c.getName(), c.getPort());
69        }
70      }
71    }
72  
73    /**
74     * Closes a port in the windows firewall in verbose mode, and without exceptions.
75     *
76     * @param port  The port number
77     */
78    private static void closePort(int port) {
79      try {
80        if (Firewall.closePort(port)) {
81          log.info("Port " + port + " closed on windows firewall");
82        }
83      } catch (FirewallException e) {
84        log.error("Error closing port on windows firewall", e);
85      }
86    }
87  
88    /**
89     * Opens a port in the windows firewall in verbose mode, and without exceptions.
90     * @param productName Product Name
91     * @param name  The service name
92     * @param port  The port number
93     */
94    private static void openPort(String productName, String name, int port) {
95      try {
96        if (Firewall.openPort(productName + " - " + name, port)) {
97          log.info("Port " + port + " opened on windows firewall");
98        }
99      } catch (FirewallException e) {
100       log.error("Error opening port on windows firewall", e);
101     }
102   }
103 
104 }