KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > apps > ALoginTest


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.apps;
15
16 import java.awt.*;
17 import java.awt.event.*;
18 import javax.swing.*;
19 import java.io.*;
20 import java.net.*;
21 import java.sql.*;
22
23 import org.compiere.plaf.*;
24
25 /**
26  * Connection Test
27  * ** Not Translated **
28  *
29  * @author Jorg Janke
30  * @version $Id: ALoginTest.java,v 1.5 2003/09/29 01:04:42 jjanke Exp $
31  */

32 public class ALoginTest extends JDialog implements ActionListener, Runnable JavaDoc
33 {
34     /**
35      * Consatructor
36      */

37     public ALoginTest (Dialog frame, String JavaDoc host, String JavaDoc sid, String JavaDoc port, String JavaDoc uid, String JavaDoc pwd)
38     {
39         super (frame, "Connect Test: " + host, true);
40         m_host = host;
41         m_sid = sid;
42         m_port = port;
43         m_uid = uid;
44         m_pwd = pwd;
45         try
46         {
47             jbInit();
48             pack();
49         }
50         catch(Exception JavaDoc ex)
51         {
52             inform ("Internal Error = " + ex.getMessage());
53         }
54         // Start Tests
55
try
56         {
57             m_worker = new Thread JavaDoc(this);
58             m_worker.start();
59         }
60         catch (Exception JavaDoc e1)
61         {
62             inform ("Internal Error = " + e1);
63         }
64         AEnv.showCenterScreen(this);
65     } // ALoginTest
66

67     private String JavaDoc m_host;
68     private String JavaDoc m_port;
69     private String JavaDoc m_sid;
70     private String JavaDoc m_uid;
71     private String JavaDoc m_pwd;
72     private Thread JavaDoc m_worker;
73
74     private JPanel mainPanel = new JPanel();
75     private BorderLayout mainLayout = new BorderLayout();
76     private JPanel southPanel = new JPanel();
77     private JButton bOK = new JButton();
78     private JScrollPane infoPane = new JScrollPane();
79     private JTextArea info = new JTextArea();
80     private FlowLayout southLayout = new FlowLayout();
81
82     /**
83      * Static Layout
84      */

85     void jbInit() throws Exception JavaDoc
86     {
87         setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
88         mainPanel.setLayout(mainLayout);
89         bOK.setText("Exit");
90         bOK.addActionListener(this);
91         info.setBackground(CompierePLAF.getFieldBackground_Inactive());
92         southPanel.setLayout(southLayout);
93         southLayout.setAlignment(FlowLayout.RIGHT);
94         infoPane.setPreferredSize(new Dimension(400, 400));
95         getContentPane().add(mainPanel);
96         mainPanel.add(southPanel, BorderLayout.SOUTH);
97         southPanel.add(bOK, null);
98         mainPanel.add(infoPane, BorderLayout.CENTER);
99         infoPane.getViewport().add(info, null);
100     } // jbInit
101

102     /**
103      * Inform
104      */

105     private void inform (String JavaDoc text)
106     {
107         System.out.println(text);
108         info.append(text);
109         info.append("\n");
110         info.setCaretPosition(info.getText().length());
111     } // inform
112

113     /**
114      * Action Listener
115      */

116     public void actionPerformed (ActionEvent e)
117     {
118         if (e.getSource() == bOK)
119         {
120             while (m_worker != null && m_worker.isAlive())
121                 m_worker.interrupt();
122             dispose();
123         }
124     } // actionEvent
125

126     /**
127      * Run individual tests
128      */

129     public void run ()
130     {
131         String JavaDoc vmName = System.getProperty("java.vm.name");
132         String JavaDoc vmVersion = System.getProperty("java.vm.version");
133         inform ("Using Java=" + vmName + " " + vmVersion);
134         inform ("");
135         //
136
boolean found = false;
137         boolean foundJDBC = false;
138         inform("*** Testing connection to Server: " + m_host + " ***");
139         if (m_host == null || m_host.length() == 0)
140         {
141             inform ("ERROR: invalid host name");
142             return;
143         }
144         String JavaDoc host = m_host;
145         inform("Trying Echo - Port 7");
146         found = testHostPort(host, 7);
147
148         inform("Trying FTP - Port 21");
149         if (testHostPort (host, 21) && !found)
150             found = true;
151
152         inform("Trying HTTP - Port 80");
153         if (testHostPort (host, 80) && !found)
154             found = true;
155
156         inform("Trying Kerberos - Port 88");
157         if (testHostPort (host, 88) && !found)
158             found = true;
159
160         inform("Trying NetBios Session - Port 139");
161         if (testHostPort (host, 139) && !found)
162             found = true;
163
164         inform("Trying RMI - Port 1099");
165         if (testHostPort (host, 1099) && !found)
166             found = true;
167
168         inform("Trying Oracle Connection Manager - Port 1630");
169         if (testHostPort (host, 1630) && !found)
170             found = true;
171
172         inform("Trying Oracle JDBC - TCP Port 1521");
173         foundJDBC = testHostPort (host, 1521);
174
175         int jdbcPort = 0;
176         try
177         {
178             jdbcPort = Integer.parseInt(m_port);
179         }
180         catch (Exception JavaDoc e)
181         {
182             inform ("ERROR: Cannot parse port=" + m_port);
183             inform (e.getMessage());
184             return;
185         }
186         if (jdbcPort != 1521)
187         {
188             inform("Trying Oracle JDBC - TCP Port " + jdbcPort);
189             if (testHostPort (host, jdbcPort) && !foundJDBC)
190                 foundJDBC = true;
191         }
192
193         // Test Interrupt
194
if (m_worker != null && m_worker.isInterrupted())
195             return;
196
197         /** Info */
198
199         if (found && foundJDBC)
200         {
201             inform ("*** Server found: " + host + " ***");
202             inform ("");
203         }
204         else if (!found && foundJDBC)
205         {
206             inform ("*** Server found: " + host + " (JDBC only) ***");
207             inform ("");
208         }
209         else if (found && !foundJDBC)
210         {
211             inform ("ERROR: Server found: " + host + " - but no JDBC ***");
212             inform ("Make sure that the Oracle Listener process is active");
213             return;
214         }
215         else
216         {
217             inform ("ERROR: Server NOT found: " + host + "***");
218             inform ("End Test: Make sure that you can ping the Server");
219             return;
220         }
221
222         /*********************************************************************/
223
224         inform ("Connect to SID: " + m_sid);
225
226         inform ("Connect with entered parameters");
227         if (!testJDBC(host, jdbcPort, m_sid, m_uid, m_pwd))
228         {
229             if (m_worker != null && m_worker.isInterrupted())
230                 return;
231
232             if (jdbcPort != 1521)
233             {
234                 inform ("Connect with standard JDBC port 1521");
235                 if (testJDBC(host, 1521, m_sid, m_uid, m_pwd))
236                 {
237                     inform ("Please set port to 1521");
238                     return;
239                 }
240                 if (m_worker != null && m_worker.isInterrupted())
241                     return;
242             }
243
244             inform ("Connect with user system/manager");
245             if (testJDBC(host, 1521, m_sid, "system", "manager"))
246             {
247                 inform ("Please check COMPIERE user id and password");
248                 inform (".... and please change SYSTEM password");
249                 return;
250             }
251         }
252
253         inform ("*** Compiere database found: " + host + ":" + jdbcPort + ":" + m_sid + " ***");
254         if (m_worker != null && m_worker.isInterrupted())
255             return;
256
257         /*********************************************************************/
258
259         inform ("");
260         inform ("Testing available application users:");
261         testCompiereUsers(host, jdbcPort);
262
263         inform ("");
264         inform ("*** Test complete **");
265     } // run
266

267     /**
268      * Test Host Port
269      */

270     private boolean testHostPort (String JavaDoc host, int port)
271     {
272         Socket pingSocket = null;
273         try
274         {
275             /* Resolve address. */
276             InetAddress server = InetAddress.getByName(host);
277             /* Establish socket. */
278             pingSocket = new Socket(server, port);
279         }
280         catch (UnknownHostException e)
281         {
282             inform (" Unknown Host: " + e );
283         }
284         catch (IOException io )
285         {
286             inform (" IO Exception: " + io );
287         }
288
289         if (pingSocket != null)
290         {
291             try
292             {
293                 pingSocket.close();
294             }
295             catch (IOException e)
296             {
297                 inform (" IO close exception: " + e );
298             }
299             inform (" *** success ***");
300             return true;
301         }
302         else
303         {
304             return false;
305         }
306     } // testHostPort
307

308     /**
309      * Test JDBC
310      */

311     private boolean testJDBC (String JavaDoc host, int port, String JavaDoc sid, String JavaDoc uid, String JavaDoc pwd)
312     {
313         try
314         {
315             inform (" Registering Driver: oracle.jdbc.driver.OracleDriver");
316             DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
317             inform (" - driver registered");
318             DriverManager.setLoginTimeout(5);
319             DriverManager.setLogWriter(new PrintWriter(System.out));
320             inform (" - driver initialized");
321         }
322         catch (SQLException e)
323         {
324             inform ("ERROR: " + e.getMessage());
325             return false;
326         }
327
328         boolean ok = false;
329
330         String JavaDoc urlC = "jdbc:oracle:thin:@" + host + ":" + port + ":" + sid;
331         try
332         {
333             inform (" Trying Client connection URL=" + urlC + ", User=" + uid);
334             Connection con = DriverManager.getConnection(urlC, uid, pwd);
335             inform (" - connected");
336             //
337
DatabaseMetaData conMD = con.getMetaData();
338             inform(" - Driver Name:\t" + conMD.getDriverName());
339             inform(" - Driver Version:\t" + conMD.getDriverVersion());
340             inform(" - DB Name:\t" + conMD.getDatabaseProductName());
341             inform(" - DB Version:\t" + conMD.getDatabaseProductVersion());
342             //
343
con.close();
344             inform (" *** success ***");
345             ok = true;
346         }
347         catch (SQLException e)
348         {
349             inform (" ERROR: " + e.getMessage());
350         }
351
352         String JavaDoc urlS = "jdbc:oracle:oci8:@";
353         try
354         {
355             inform (" Trying Server connection URL=" + urlS + ", User=" + uid);
356             Connection con = DriverManager.getConnection(urlS, uid, pwd);
357             inform (" - connected");
358             //
359
DatabaseMetaData conMD = con.getMetaData();
360             inform(" - Driver Name:\t" + conMD.getDriverName());
361             inform(" - Driver Version:\t" + conMD.getDriverVersion());
362             inform(" - DB Name:\t" + conMD.getDatabaseProductName());
363             inform(" - DB Version:\t" + conMD.getDatabaseProductVersion());
364             //
365
con.close();
366             inform (" *** success ***");
367         }
368         catch (SQLException e)
369         {
370             inform (" ERROR: " + e.getMessage());
371         }
372
373         return ok;
374     } // testJDBC
375

376     /**
377      * Test Compiere Users
378      */

379     private void testCompiereUsers(String JavaDoc host, int port)
380     {
381         String JavaDoc sql = "SELECT Name, Password FROM AD_User WHERE IsActive='Y'";
382         String JavaDoc urlC = "jdbc:oracle:thin:@" + host + ":" + port + ":" + m_sid;
383         try
384         {
385             inform (" - Client connection URL=" + urlC + ", User=" + m_uid);
386             Connection con = DriverManager.getConnection(urlC, m_uid, m_pwd);
387             inform (" - connected");
388             Statement stmt = con.createStatement();
389             inform (" - statement created");
390             ResultSet rs = stmt.executeQuery(sql);
391             inform (" - query executed listing active application users:");
392             while (rs.next())
393             {
394                 String JavaDoc user = rs.getString(1);
395                 String JavaDoc password = rs.getString(2);
396                 String JavaDoc answer = ">> User = " + user;
397                 if ((user.equals("System") || user.equals("SuperUser")) && password.equals("System"))
398                     answer += " with standard password (should be changed)";
399                 inform (answer);
400             }
401             rs.close();
402             inform (" - query closed");
403             stmt.close();
404             inform (" - statement closed");
405             con.close();
406             inform (" - connection closed");
407         }
408         catch (SQLException e)
409         {
410             inform (" ERROR: " + e.getMessage());
411         }
412     } // testCompiereUsers
413
} // ALoginTest
414
Popular Tags