KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > planetamessenger > mos > engine > JProxySettings


1 /*
2     =========================================================================
3     Package engine - Implements the engine package.
4
5     This module is developed and maintained by PlanetaMessenger.org.
6     Specs, New and updated versions can be found in
7     http://www.planetamessenger.org
8     If you want contact the Team please send a email to Project Manager
9     Leidson Campos Alves Ferreira at leidson@planetamessenger.org
10
11     Copyright (C) since 2001 by PlanetaMessenger.org
12
13     This program is free software; you can redistribute it and/or modify
14     it under the terms of the GNU General Public License as published by
15     the Free Software Foundation; either version 2 of the License, or
16     (at your option) any later version.
17
18     This program is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21     GNU General Public License for more details.
22
23     You should have received a copy of the GNU General Public License
24     along with this program; if not, write to the Free Software
25     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26
27     =========================================================================
28 */

29 /**
30  *
31  * $Id: JProxySettings.java,v 1.11 2007/02/04 01:06:13 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.11 $
35  * $State: Exp $
36  *
37  */

38
39 package org.planetamessenger.mos.engine;
40
41 import java.sql.Connection JavaDoc;
42 import java.sql.PreparedStatement JavaDoc;
43 import java.sql.ResultSet JavaDoc;
44 import java.sql.SQLException JavaDoc;
45 import org.planetamessenger.system.*;
46
47
48 public class JProxySettings {
49   
50   boolean bUseProxy;
51   boolean bRequestAuth;
52   
53   JProxy proxySet;
54   String JavaDoc strSocksProxyHost;
55   String JavaDoc strHTTPProxyHost;
56   String JavaDoc strUserName;
57   String JavaDoc strUserPassword;
58   int nSocksProxyPort;
59   int nHTTPProxyPort;
60   
61   
62   /**
63    * Creates and initializes a JProxySettings
64    * object.
65    * Calling this constructor, all PM proxy
66    * settings stored in database are loaded
67    * to the object created.
68    */

69   public JProxySettings() {
70
71     proxySet = new JProxy();
72   }
73   
74   /**
75    * Update all proxy settings object.
76    * Calling this method the PM database
77    * will updated with values supplied as
78    * parameters and JVM proxy properties too.
79    * @param bUseProxy Use proxy flag;
80    * @param strSocksProxyHost Proxy host name;
81    * @param nSocksProxyPort Proxy port;
82    * @param strHTTPProxyHost HTTP Proxy host name;
83    * @param nHTTPProxyPort HTTP proxy port;
84    * @param bReqAuth Require proxy authentication flag;
85    * @param strUserName The proxy user name;
86    * @param strUserPasswd The proxy user password;
87    */

88   public void update( boolean bUseProxy,
89                       String JavaDoc strSocksProxyHost,
90                       int nSocksProxyPort,
91                       String JavaDoc strHTTPProxyHost,
92                       int nHTTPProxyPort,
93                       boolean bRequestAuth,
94                       String JavaDoc strUserName,
95                       String JavaDoc strUserPassword ) {
96
97     int nUseProxy = ( bUseProxy ? 1 : 0 );
98     int nRequestAuth = ( bRequestAuth ? 1 : 0 );
99     Connection JavaDoc conn = null;
100     PreparedStatement JavaDoc st = null;
101     
102     this.bUseProxy = bUseProxy;
103     this.strSocksProxyHost = strSocksProxyHost;
104     this.nSocksProxyPort = nSocksProxyPort;
105     this.strHTTPProxyHost = strHTTPProxyHost;
106     this.nHTTPProxyPort = nHTTPProxyPort;
107     this.bRequestAuth = bRequestAuth;
108     this.strUserName = strUserName;
109     this.strUserPassword = strUserPassword;
110
111     try {
112       conn = JSharedObjects.getDatabase().getConnection();
113       st = conn.prepareStatement( "UPDATE profile SET use_proxy= ?, socks_proxy_host = ?, socks_proxy_port = ?, " +
114                                     "http_proxy_host = ?, http_proxy_port = ?, requires_auth = ?, " +
115                                     "proxy_user_name = ?, proxy_user_passwd = ? WHERE profile_id = ?" );
116
117       st.setInt( 1, nUseProxy );
118       st.setString( 2, strSocksProxyHost );
119       st.setInt( 3, nSocksProxyPort );
120       st.setString( 4, strHTTPProxyHost );
121       st.setInt( 5, nHTTPProxyPort );
122       st.setInt( 6, nRequestAuth );
123       st.setString( 7, strUserName );
124       st.setString( 8, strUserPassword );
125       st.setLong( 9, JSharedObjects.getProfileManager().getProfileId() );
126       st.executeUpdate();
127
128     } catch( SQLException JavaDoc e ) {
129       System.err.println( "JProxySettings.updateSettings() - " + e );
130     } finally {
131       if( st != null )
132         try {
133           st.close();
134         } catch( SQLException JavaDoc e ) {
135           System.err.println( "JProxySettings.updateSettings() - " + e );
136         }
137
138       apply();
139     }
140   }
141   
142   /**
143    * Load all proxy settings stored
144    * in PM databse for a specified profile.
145    * @param nProfileId The profile id to load proxy settings;
146    */

147   public void load( long nProfileId ) {
148     
149     Connection JavaDoc conn = null;
150     PreparedStatement JavaDoc st = null;
151     ResultSet JavaDoc resultSet = null;
152
153     try{
154       conn = JSharedObjects.getDatabase().getConnection();
155       st = conn.prepareStatement( "SELECT use_proxy, socks_proxy_host, socks_proxy_port, http_proxy_host, http_proxy_port, requires_auth, proxy_user_name, proxy_user_passwd FROM profile WHERE profile_id = ?" );
156       st.setLong( 1, nProfileId );
157       resultSet = st.executeQuery();
158   
159       if( resultSet == null )
160         return;
161
162       if( resultSet.next() ) {
163         this.bUseProxy = ( resultSet.getInt( "use_proxy" ) == 1 ? true : false );
164         this.strSocksProxyHost = resultSet.getString( "socks_proxy_host" );
165         this.nSocksProxyPort = resultSet.getInt( "socks_proxy_port" );
166         this.strHTTPProxyHost = resultSet.getString( "http_proxy_host" );
167         this.nHTTPProxyPort = resultSet.getInt( "http_proxy_port" );
168         this.bRequestAuth = ( resultSet.getInt( "requires_auth" ) == 1 ? true : false );
169         this.strUserName = resultSet.getString( "proxy_user_name" );
170         this.strUserPassword = resultSet.getString( "proxy_user_passwd" );
171       }
172       else {
173         this.bUseProxy = false;
174         this.bRequestAuth = false;
175         this.strSocksProxyHost = "";
176         this.nSocksProxyPort = 0;
177         this.strHTTPProxyHost = "";
178         this.nHTTPProxyPort = 0;
179         this.strUserName = "";
180         this.strUserPassword = "";
181       }
182
183       resultSet.close();
184
185     } catch( java.sql.SQLException JavaDoc e ) {
186       System.err.println( "JProxySettings.loadSettings() - SQLException " + e );
187     } finally {
188       if( resultSet != null )
189         try {
190           resultSet.close();
191         } catch ( SQLException JavaDoc e ) {}
192
193       if( st != null ) {
194         try {
195           st.close();
196         } catch ( SQLException JavaDoc e ) {}
197       }
198
199       apply();
200     }
201   }
202
203   /**
204    * Apply the Proxy settings into
205    * JVM properties.
206    */

207   void apply() {
208     
209     if( bUseProxy ) {
210       proxySet.setUserName( strUserName );
211       proxySet.setPassword( strUserPassword );
212       
213       // Use HTTP proxy ???
214
if( !strHTTPProxyHost.trim().equals( "" ) )
215         proxySet.setHttpProxy( strHTTPProxyHost, nHTTPProxyPort );
216       else
217         proxySet.clearHttpProxy();
218       
219       // Use SOCKS proxy ???
220
if( !strSocksProxyHost.trim().equals( "" ) )
221         proxySet.setSocksProxy( strSocksProxyHost, nSocksProxyPort );
222       else
223         proxySet.clearSocksProxy();
224     }
225     else
226       proxySet.clearAll();
227   }
228   
229   /**
230    * Get the use proxy property.
231    */

232   public boolean getUseProxy() {
233    
234     return bUseProxy;
235   }
236   
237   /**
238    * Get the proxy host property.
239    */

240   public String JavaDoc getSocksProxyHost() {
241    
242     return strSocksProxyHost;
243   }
244   
245   /**
246    * Get the proxy port property.
247    */

248   public int getSocksProxyPort() {
249    
250     return nSocksProxyPort;
251   }
252   
253   /**
254    * Get the HTTP proxy host property.
255    */

256   public String JavaDoc getHTTPProxyHost() {
257    
258     return strHTTPProxyHost;
259   }
260   
261   /**
262    * Get the HTTP proxy port property.
263    */

264   public int getHTTPProxyPort() {
265     
266     return nHTTPProxyPort;
267   }
268   
269   /**
270    * Get the RequestAuth property.
271    */

272   public boolean getRequestAuth() {
273    
274     return bRequestAuth;
275   }
276   
277   /**
278    * Get the user name property.
279    */

280   public String JavaDoc getUserName() {
281    
282     return strUserName;
283   }
284   
285   /**
286    * Get the user password property.
287    */

288   public String JavaDoc getUserPassword() {
289    
290     return strUserPassword;
291   }
292 }
293
294 // JProxySettings class
Popular Tags