KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > standalone > EclipseConnection


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.standalone;
12
13 import java.io.*;
14 import java.net.*;
15 import java.util.Properties JavaDoc;
16
17 import javax.net.ssl.HostnameVerifier;
18 import javax.net.ssl.HttpsURLConnection;
19
20 /**
21  * This program is used to start or stop Eclipse Infocenter application. It
22  * should be launched from command line.
23  */

24 public class EclipseConnection {
25     // help server host
26
private String JavaDoc host;
27     // help server port
28
private String JavaDoc port;
29
30     public EclipseConnection() {
31     }
32
33     public String JavaDoc getPort() {
34         return port;
35     }
36
37     public String JavaDoc getHost() {
38         return host;
39     }
40
41     public void reset() {
42         host = null;
43         port = null;
44     }
45
46     public boolean isValid() {
47         return (host != null && port != null);
48     }
49
50     public void connect(URL url) throws InterruptedException JavaDoc, Exception JavaDoc {
51         try {
52             HttpURLConnection connection = (HttpURLConnection) url
53                     .openConnection();
54             if (connection instanceof HttpsURLConnection) {
55                 HttpsURLConnection secureConnection = (HttpsURLConnection) connection;
56                 // The following allows the connection to
57
// continue even if the default rules for
58
// URL hostname verification fail.
59
secureConnection.setHostnameVerifier(new HostnameVerifier() {
60                     public boolean verify(String JavaDoc urlHostName, javax.net.ssl.SSLSession session) {
61                         if (Options.isDebug()) {
62                             System.out.println("Warning: URL Host: " //$NON-NLS-1$
63
+ urlHostName + " vs. " //$NON-NLS-1$
64
+ session.getPeerHost());
65                         }
66                         return true;
67                     }
68                 });
69             }
70             if (Options.isDebug()) {
71                 System.out.println("Connection to control servlet created."); //$NON-NLS-1$
72
}
73             connection.connect();
74             if (Options.isDebug()) {
75                 System.out.println("Connection to control servlet connected."); //$NON-NLS-1$
76
}
77             int code = connection.getResponseCode();
78             if (Options.isDebug()) {
79                 System.out
80                         .println("Response code from control servlet=" + code); //$NON-NLS-1$
81
}
82             connection.disconnect();
83             if (code == HttpURLConnection.HTTP_MOVED_TEMP) {
84                 // Redirect from server.
85
String JavaDoc redirectLocation = connection.getHeaderField("location"); //$NON-NLS-1$
86
URL redirectURL = new URL(redirectLocation);
87                 if (url.equals(redirectURL)) {
88                     if (Options.isDebug()) {
89                         System.out.println("Redirecting to the same URL! " //$NON-NLS-1$
90
+ redirectLocation);
91                     }
92                     return;
93                 }
94                 if (Options.isDebug()) {
95                     System.out.println("Follows redirect to " + redirectLocation); //$NON_NLS-1$ //$NON-NLS-1$
96
}
97                 connect(redirectURL);
98             }
99             return;
100         } catch (IOException ioe) {
101             if (Options.isDebug()) {
102                 ioe.printStackTrace();
103             }
104         }
105     }
106
107     /**
108      * Obtains host and port from the file. Retries several times if file does
109      * not exists, and help might be starting up.
110      */

111     public void renew() throws Exception JavaDoc {
112         Properties JavaDoc p = new Properties JavaDoc();
113         FileInputStream is = null;
114         try {
115             is = new FileInputStream(Options.getConnectionFile());
116             p.load(is);
117             is.close();
118         } catch (IOException ioe) {
119             // it is ok, eclipse might have just exited
120
throw ioe;
121         } finally {
122             if (is != null) {
123                 try {
124                     is.close();
125                 } catch (IOException ioe2) {
126                 }
127             }
128         }
129         host = (String JavaDoc) p.get("host"); //$NON-NLS-1$
130
port = (String JavaDoc) p.get("port"); //$NON-NLS-1$
131
if (Options.isDebug()) {
132             System.out.println("Help server host=" + host); //$NON-NLS-1$
133
}
134         if (Options.isDebug()) {
135             System.out.println("Help server port=" + port); //$NON-NLS-1$
136
}
137     }
138
139 }
140
Popular Tags