KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > fenyo > gnetwatch > CommandLine


1
2 /*
3  * GNetWatch
4  * Copyright 2006, 2007 Alexandre Fenyo
5  * gnetwatch@fenyo.net
6  *
7  * This file is part of GNetWatch.
8  *
9  * GNetWatch is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * GNetWatch is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with GNetWatch; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  */

23
24 package net.fenyo.gnetwatch;
25
26 import java.io.*;
27 import java.util.*;
28 import javax.net.ssl.*;
29 import java.util.regex.Matcher JavaDoc;
30 import java.util.regex.Pattern JavaDoc;
31 import java.security.*;
32 import java.security.cert.*;
33
34 import net.fenyo.gnetwatch.GUI.*;
35 import net.fenyo.gnetwatch.actions.ExternalCommand;
36 import net.fenyo.gnetwatch.activities.*;
37 import net.fenyo.gnetwatch.data.EventReachable;
38
39 import org.dom4j.*;
40 import org.dom4j.io.*;
41
42 import org.apache.commons.logging.*;
43
44 /**
45  * Manage command line arguments.
46  * @author Alexandre Fenyo
47  * @version $Id: CommandLine.java,v 1.18 2007/03/09 15:42:40 fenyo Exp $
48  *
49  * @todo nothing more.
50  */

51
52 public class CommandLine {
53   private static Log log = LogFactory.getLog(CommandLine.class);
54
55   static public class NoCheckTrustManager implements X509TrustManager {
56     private X509TrustManager myTrustManager;
57
58     public NoCheckTrustManager() throws java.security.NoSuchAlgorithmException JavaDoc {
59         TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");
60         try {
61           trustManagerFactory.init((KeyStore) null);
62           myTrustManager = (X509TrustManager) trustManagerFactory.getTrustManagers()[0];
63         } catch (KeyStoreException ex) {
64           log.error("Exception", ex);
65         }
66     }
67
68     public X509Certificate[] getAcceptedIssuers() {
69       return myTrustManager.getAcceptedIssuers();
70     }
71
72     public void checkClientTrusted(X509Certificate chain[], String JavaDoc authType) throws CertificateException {}
73
74     public void checkServerTrusted(X509Certificate chain[], String JavaDoc authType) throws CertificateException {}
75   }
76
77   /**
78    * General entry point.
79    * @param args command line arguments.
80    * @return void.
81    * @throws IOException io exception.
82    * @throws FileNotFoundException file not found.
83    */

84   public static void main(String JavaDoc[] args)
85     throws IOException, FileNotFoundException, InterruptedException JavaDoc {
86     Config config = null;
87     Synchro synchro = null;
88     Background background = null;
89     GUI gui = null;
90     Main main = null;
91     SNMPManager snmp_manager = null;
92     CaptureManager capture_mgr = null;
93
94     // Get configuration properties.
95
config = new Config();
96
97     // Read general logging rules.
98
GenericTools.initLogEngine(config);
99     log.info(config.getString("log_engine_initialized"));
100     log.info(config.getString("begin"));
101
102     // Initialize Object-Relational mapping
103
// synchro = new Synchro(config);
104

105     // Do not check SSL certificates
106
SSLContext ssl_context = null;
107     try {
108       ssl_context = SSLContext.getInstance("SSL");
109       ssl_context.init(null, new TrustManager [] {new NoCheckTrustManager() }, new SecureRandom());
110     } catch (final NoSuchAlgorithmException ex) {
111       log.error("Exception", ex);
112     } catch (final KeyManagementException ex) {
113       log.error("Exception", ex);
114     }
115     HttpsURLConnection.setDefaultSSLSocketFactory(ssl_context.getSocketFactory());
116     HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier () {
117       public final boolean verify(String JavaDoc hostname, SSLSession session) {
118         return true;
119       }
120     });
121
122     // Initialize background processes management
123
background = new Background(config);
124     background.createBackgroundThread();
125
126     // Initialize packet capture on every interface
127
capture_mgr = new CaptureManager(config);
128
129     // Initialize main processes management
130
main = new Main(config, capture_mgr);
131
132     // Build SNMP Manager
133
snmp_manager = new SNMPManager();
134
135     // Build GUI
136
gui = new GUI(config, background, main, snmp_manager);
137     main.setGUI(gui);
138     capture_mgr.setGUI(gui);
139     gui.waitForCreation();
140
141     // Initial configuration
142
gui.createFromXML(gui.getConfig().getProperty("initialobjects"));
143
144     // Wait for the GUI to terminate
145
gui.join();
146     // The GUI is now closed
147
log.info(config.getString("end"));
148
149     // Stop every application thread
150
config.setEnd();
151     gui.end();
152     background.end();
153     capture_mgr.unRegisterAllListeners();
154   }
155 }
156
Popular Tags