mirror of https://github.com/sipwise/jitsi.git
- Using an external library to manage the connection with the server - Enhanced account registration wizardcusax-fix
parent
5417c1891a
commit
46ef85f493
Binary file not shown.
|
After Width: | Height: | Size: 687 B |
@ -1,654 +0,0 @@
|
||||
/*
|
||||
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
package net.java.sip.communicator.impl.protocol.dict;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Layer abstraction of a dict server
|
||||
*
|
||||
* @author LITZELMANN Cedric
|
||||
* @author ROTH Damien
|
||||
*/
|
||||
public class DictAdapter
|
||||
{
|
||||
/**
|
||||
* The host name of the server: i.e. "dict.org"
|
||||
*/
|
||||
private String host;
|
||||
|
||||
/**
|
||||
* The port used by the server. The default one for the DICT protocol is
|
||||
* 2628.
|
||||
*/
|
||||
private int port;
|
||||
|
||||
/**
|
||||
* The name of the strategy used for searching words with command MATCH:
|
||||
* i.e. the strategie can de "prefix", "suffix", "soundex", "levenshtein", etc.
|
||||
*/
|
||||
private String strategy;
|
||||
|
||||
/**
|
||||
* A string representation used to identify the client to the serveur. In
|
||||
* our case we will use the "SIP Communicator" string for the client name.
|
||||
*/
|
||||
private String clientName = "";
|
||||
|
||||
// Status
|
||||
/**
|
||||
* The socket used to connect to the DICT server.
|
||||
*/
|
||||
private Socket socket;
|
||||
|
||||
/**
|
||||
* A output stream piped to the socket in order to send command to the server.
|
||||
*/
|
||||
private PrintWriter out;
|
||||
|
||||
/**
|
||||
* A input stream piped to the socket in order to receive messages from the server.
|
||||
*/
|
||||
private BufferedReader in;
|
||||
|
||||
/**
|
||||
* A boolean telling if we are currently connected to the DICT server.
|
||||
*/
|
||||
private boolean connected;
|
||||
|
||||
/**
|
||||
* The list of all the databases hosted by the server. Each database
|
||||
* correspond to a dictionnary.
|
||||
*/
|
||||
Vector<String> databasesList;
|
||||
|
||||
/**
|
||||
* Initialize a basic instance with predefined settings
|
||||
*/
|
||||
public DictAdapter()
|
||||
{
|
||||
this.host = "dict.org";
|
||||
this.port = 2628;
|
||||
this.strategy = "prefix";
|
||||
this.connected = false;
|
||||
this.socket = null;
|
||||
this.out = null;
|
||||
this.in = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize a basic instance and set th host
|
||||
* @param host Host
|
||||
*/
|
||||
public DictAdapter(String host)
|
||||
{
|
||||
this.host = host;
|
||||
this.port = 2628;
|
||||
this.strategy = "prefix";
|
||||
this.connected = false;
|
||||
this.socket = null;
|
||||
this.out = null;
|
||||
this.in = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize an instance and set the host and the port
|
||||
* @param host Host
|
||||
* @param port Port
|
||||
*/
|
||||
public DictAdapter(String host, int port)
|
||||
{
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.strategy = "prefix";
|
||||
this.connected = false;
|
||||
this.socket = null;
|
||||
this.out = null;
|
||||
this.in = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize an instance and set the host, port and strategy
|
||||
* @param host Host
|
||||
* @param port Port
|
||||
* @param strategy Match strategy
|
||||
*/
|
||||
public DictAdapter(String host, int port, String strategy)
|
||||
{
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.strategy = strategy;
|
||||
this.connected = false;
|
||||
this.socket = null;
|
||||
this.out = null;
|
||||
this.in = null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Establish a connexion to the dict server
|
||||
* @throws Exception
|
||||
* @return DictResultset containing the error - null otherwise
|
||||
*/
|
||||
private void connect() throws Exception
|
||||
{
|
||||
String fromServer;
|
||||
|
||||
if (this.isConnected())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
this.socket = new Socket(this.host, this.port);
|
||||
this.out = new PrintWriter(new OutputStreamWriter(this.socket.getOutputStream(),
|
||||
"UTF-8"), true);
|
||||
this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream(),
|
||||
"UTF-8"));
|
||||
|
||||
fromServer = this.in.readLine(); // Server banner
|
||||
|
||||
if (fromServer.startsWith("220"))
|
||||
{ // 220 = connect ok
|
||||
this.connected = true;
|
||||
this.client("SIP Communicator");
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new DictException(fromServer.substring(0, 3));
|
||||
}
|
||||
}
|
||||
catch(UnknownHostException uhe)
|
||||
{
|
||||
throw new DictException(uhe);
|
||||
}
|
||||
catch(IOException ioe)
|
||||
{
|
||||
throw new DictException(ioe);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the actual connexion
|
||||
* @throws Exception
|
||||
*/
|
||||
public void close() throws Exception
|
||||
{
|
||||
String fromServer;
|
||||
boolean quit = false;
|
||||
|
||||
if (!this.isConnected())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
this.out.println("QUIT");
|
||||
|
||||
// Clean the socket buffer
|
||||
while (quit == false && (fromServer = this.in.readLine()) != null)
|
||||
{
|
||||
if (fromServer.startsWith("221"))
|
||||
{ // Quit response
|
||||
quit = true;
|
||||
}
|
||||
}
|
||||
|
||||
this.out.close();
|
||||
this.in.close();
|
||||
this.socket.close();
|
||||
|
||||
this.connected = false;
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
throw new DictException(ioe);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the database list from the server
|
||||
* @throws Exception
|
||||
* @return a DictResultset containing the database list - otherwise the error code
|
||||
*/
|
||||
public DictResultset showDB() throws Exception
|
||||
{
|
||||
String fromServer;
|
||||
boolean quit = false;
|
||||
DictResultset result = new DictResultset();
|
||||
this.connect();
|
||||
|
||||
try
|
||||
{
|
||||
fromServer = this.query("SHOW DB");
|
||||
|
||||
if (fromServer.startsWith("110"))
|
||||
{ // OK - getting responses from the server
|
||||
result.newResultset();
|
||||
while (quit == false && (fromServer = this.in.readLine()) != null)
|
||||
{
|
||||
if (fromServer.startsWith("250"))
|
||||
{
|
||||
quit = true;
|
||||
}
|
||||
else if (!fromServer.equals("."))
|
||||
{
|
||||
result.addResult(fromServer);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new DictException(fromServer.substring(0,3));
|
||||
}
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
throw new DictException(ioe);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the strategies allowed by the server for the MATCH command
|
||||
* @throws Exception
|
||||
* @return a DictResultset containing the database list - otherwise the error code
|
||||
*/
|
||||
public DictResultset showStrat() throws Exception
|
||||
{
|
||||
String fromServer;
|
||||
boolean quit = false;
|
||||
DictResultset result = new DictResultset();
|
||||
this.connect();
|
||||
|
||||
try
|
||||
{
|
||||
fromServer = this.query("SHOW STRAT");
|
||||
|
||||
if (fromServer.startsWith("111"))
|
||||
{ // OK - getting responses from the server
|
||||
result.newResultset();
|
||||
while (quit == false && (fromServer = this.in.readLine()) != null)
|
||||
{
|
||||
if (fromServer.startsWith("250"))
|
||||
{
|
||||
quit = true;
|
||||
}
|
||||
else if (!fromServer.equals("."))
|
||||
{
|
||||
result.addResult(fromServer);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new DictException(fromServer.substring(0,3));
|
||||
}
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
throw new DictException(ioe);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the definition of a word
|
||||
* @param database the database in which the word will be searched
|
||||
* @param word the search word
|
||||
* @throws Exception
|
||||
* @return a DictResultset containing the database list - otherwise the error code
|
||||
*/
|
||||
public DictResultset define(String database, String word) throws Exception
|
||||
{
|
||||
String fromServer;
|
||||
boolean quit = false;
|
||||
DictResultset result = new DictResultset();
|
||||
String[] test;
|
||||
this.connect();
|
||||
|
||||
try
|
||||
{
|
||||
fromServer = this.query("DEFINE " + database + " " + word);
|
||||
|
||||
if (fromServer.startsWith("150"))
|
||||
{
|
||||
while (quit == false && (fromServer = this.in.readLine()) != null)
|
||||
{
|
||||
if (fromServer.startsWith("151"))
|
||||
{ // First line - Contains the DB Name
|
||||
test = fromServer.split(" ", 4);
|
||||
result.newResultset(test[3].substring(1, test[3].length() - 1));
|
||||
continue;
|
||||
}
|
||||
else if (fromServer.startsWith("250"))
|
||||
{ // End of the request
|
||||
quit = true;
|
||||
}
|
||||
else if (!fromServer.equals("."))
|
||||
{
|
||||
result.addResult(fromServer);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new DictException(fromServer.substring(0,3));
|
||||
}
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
throw new DictException(ioe);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get words that match with a strategie form a word with the stored strategy
|
||||
* @param database The database in which the words will be searched
|
||||
* @param word The base word
|
||||
* @return a DictResultset containing the words list - otherwise throw an exception
|
||||
* @throws Exception
|
||||
*/
|
||||
public DictResultset match(String database, String word) throws Exception
|
||||
{
|
||||
return this.match(database, this.strategy, word);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get words that match with a strategie from a word
|
||||
* @param database the database in which the words will be searched
|
||||
* @param strat the strategies used
|
||||
* @param word the base word
|
||||
* @throws Exception
|
||||
* @return a DictResultset containing the words list - otherwise the error code
|
||||
*/
|
||||
public DictResultset match(String database, String strat, String word) throws Exception
|
||||
{
|
||||
String fromServer;
|
||||
boolean quit = false;
|
||||
DictResultset result = new DictResultset();
|
||||
this.connect();
|
||||
|
||||
try
|
||||
{
|
||||
fromServer = this.query("MATCH " + database + " " + strat + " " + word);
|
||||
|
||||
if (fromServer.startsWith("152"))
|
||||
{
|
||||
result.newResultset();
|
||||
while (quit == false && (fromServer = this.in.readLine()) != null)
|
||||
{
|
||||
if (fromServer.startsWith("250"))
|
||||
{
|
||||
quit = true;
|
||||
}
|
||||
else if (!fromServer.equals("."))
|
||||
{
|
||||
result.addResult(fromServer);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new DictException(fromServer.substring(0,3));
|
||||
}
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
throw new DictException(ioe);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide information to the server about the clientname, for logging and statistical purposes
|
||||
* @param clientname Client name
|
||||
* @throws Exception
|
||||
*/
|
||||
public void client(String clientname) throws Exception
|
||||
{
|
||||
String fromServer;
|
||||
this.connect();
|
||||
|
||||
fromServer = this.query("CLIENT " + clientname);
|
||||
|
||||
// 250 code is the only possible answer
|
||||
if (!fromServer.startsWith("250"))
|
||||
{
|
||||
throw new DictException(fromServer.substring(0, 3));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the host
|
||||
* @param newHost host address
|
||||
*/
|
||||
public void setHost(String newHost) throws Exception
|
||||
{
|
||||
if (isUrl(newHost))
|
||||
{
|
||||
this.host = newHost;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new DictException(900, "Host URL is incorrect");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the host port
|
||||
* @param newPort Port
|
||||
*/
|
||||
public void setPort(int newPort)
|
||||
{
|
||||
this.port = newPort;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the strategy
|
||||
* @param newStrat Strategy
|
||||
*/
|
||||
public void setStrategy(String newStrat)
|
||||
{
|
||||
this.strategy = newStrat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the client name which is communicated to the server
|
||||
* @param cn Client name
|
||||
*/
|
||||
public void setClientName(String cn)
|
||||
{
|
||||
this.clientName = cn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the host
|
||||
* @return return the host
|
||||
*/
|
||||
public String getHost()
|
||||
{
|
||||
return this.host;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the port
|
||||
* @return return the port
|
||||
*/
|
||||
public int getPort()
|
||||
{
|
||||
return this.port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the strategy
|
||||
* @return return the strategy
|
||||
*/
|
||||
public String getStrategy()
|
||||
{
|
||||
return this.strategy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the client name
|
||||
* @return return the client name
|
||||
*/
|
||||
public String getClientName()
|
||||
{
|
||||
return this.clientName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the database's list from the server
|
||||
* @return List of the databases
|
||||
* @throws Exception
|
||||
*/
|
||||
public Vector<String> getDatabases() throws Exception
|
||||
{
|
||||
if (this.databasesList == null)
|
||||
{
|
||||
DictResultset drs = this.showDB();
|
||||
DictResult list = drs.getResultset(0);
|
||||
|
||||
this.databasesList = new Vector<String>();
|
||||
|
||||
while(list.hasNext())
|
||||
{
|
||||
this.databasesList.add(list.next());
|
||||
}
|
||||
}
|
||||
return this.databasesList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the dictionary name from the databases list
|
||||
* @param code Dictionary code
|
||||
* @return the dictionary name
|
||||
* @throws Exception
|
||||
*/
|
||||
public String getDictionaryName(String code) throws Exception
|
||||
{
|
||||
int dictionary_id_and_description_separator;
|
||||
String dictionary_id_and_description;
|
||||
String dictionary_id;
|
||||
String dictionary_description;
|
||||
|
||||
// First, we check if the code is a special code
|
||||
// Checks the RFC-2229 for more details
|
||||
if (code.equals("*"))
|
||||
{
|
||||
return "Any dictionary";
|
||||
}
|
||||
else if (code.equals("!"))
|
||||
{
|
||||
return "First match";
|
||||
}
|
||||
|
||||
// Gets the databases list
|
||||
if (this.databasesList == null)
|
||||
{
|
||||
getDatabases();
|
||||
|
||||
}
|
||||
|
||||
// Look down the databases list to get the name
|
||||
for (int i=0; i<this.databasesList.size(); i++)
|
||||
{
|
||||
dictionary_id_and_description = this.databasesList.get(i);
|
||||
dictionary_id_and_description_separator = dictionary_id_and_description.indexOf(' ');
|
||||
dictionary_id = dictionary_id_and_description.substring(0, dictionary_id_and_description_separator);
|
||||
|
||||
if (dictionary_id.equals(code))
|
||||
{
|
||||
dictionary_description = dictionary_id_and_description.substring(dictionary_id_and_description_separator + 1);
|
||||
return dictionary_description.replace("\"", "");
|
||||
}
|
||||
}
|
||||
|
||||
// If the name isn't in the list, return null
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if we are connected to the server
|
||||
* @return true if we are connected - false otherwise
|
||||
*/
|
||||
public boolean isConnected()
|
||||
{
|
||||
return this.connected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the URL is correct and a server exists
|
||||
* @param url an Url
|
||||
* @return true if everything is ok - false otherwise
|
||||
*/
|
||||
public static boolean isUrl(String url)
|
||||
{
|
||||
boolean ok;
|
||||
if (url == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
InetAddress.getByName(url);
|
||||
ok = true;
|
||||
}
|
||||
catch (UnknownHostException uhex)
|
||||
{
|
||||
ok = false;
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a query and deals with the automatic deconnexion
|
||||
* @param query A query to send to the server
|
||||
* @return The first ligne of the response from the server
|
||||
* @throws Exception IOException and DictException
|
||||
*/
|
||||
private String query(String query) throws Exception
|
||||
{
|
||||
String result = null;
|
||||
|
||||
this.out.println(query);
|
||||
result = in.readLine();
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
// The connexion may be close, reconnexion
|
||||
this.connected = false;
|
||||
this.connect();
|
||||
|
||||
this.out.println(query);
|
||||
result = in.readLine();
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
// If result is still equal to null, the server is unavailable
|
||||
// We send the appropriate exception
|
||||
throw new DictException(420);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@ -1,287 +0,0 @@
|
||||
/*
|
||||
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
package net.java.sip.communicator.impl.protocol.dict;
|
||||
|
||||
/**
|
||||
* Exception class managing the basics dict server errors.
|
||||
*
|
||||
* @author LITZELMANN Cedric
|
||||
* @author ROTH Damien
|
||||
*/
|
||||
public class DictException
|
||||
extends Exception
|
||||
{
|
||||
// Error number
|
||||
private int error;
|
||||
|
||||
// Error message
|
||||
private String errorMessage;
|
||||
|
||||
/**
|
||||
* Create an exception from the dict error code
|
||||
* @param error Error code returned by the server
|
||||
*/
|
||||
public DictException(int error)
|
||||
{
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an exception with a custom message
|
||||
* @param error Error code returned by the server
|
||||
* @param message Custom message
|
||||
*/
|
||||
public DictException(int error, String message)
|
||||
{
|
||||
this.error = error;
|
||||
this.errorMessage = message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as the first constructor but with a string (converted to an int)
|
||||
* @param error Error code returned by the server
|
||||
*/
|
||||
public DictException(String error)
|
||||
{
|
||||
this.error = Integer.parseInt(error);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an exception from a java exception
|
||||
* @param e Java Exception
|
||||
*/
|
||||
public DictException(Exception e)
|
||||
{
|
||||
String className = e.getClass().toString();
|
||||
|
||||
if (className.endsWith("IOException"))
|
||||
{
|
||||
this.error = 901;
|
||||
this.errorMessage = "IOException";
|
||||
}
|
||||
else if (className.endsWith("UnknownHostException"))
|
||||
{
|
||||
this.error = 902;
|
||||
this.errorMessage = "UnknownHostException";
|
||||
}
|
||||
else if (className.endsWith("SecurityException"))
|
||||
{
|
||||
this.error = 903;
|
||||
this.errorMessage = "SecurityException";
|
||||
}
|
||||
else if (className.endsWith("SocketTimeoutException"))
|
||||
{
|
||||
this.error = 904;
|
||||
this.errorMessage = "SocketTimeoutException";
|
||||
}
|
||||
else
|
||||
{
|
||||
this.error = 900;
|
||||
this.errorMessage = "Unknown error [" + className + "]";
|
||||
}
|
||||
|
||||
this.errorMessage += ": " + e.getMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the error code
|
||||
* @return the error code
|
||||
*/
|
||||
public int getErrorCode()
|
||||
{
|
||||
return this.error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the error message
|
||||
* @return the error message
|
||||
*/
|
||||
public String getMessage()
|
||||
{
|
||||
return this.errorMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an explanation of the error
|
||||
* @return Returns an explanation corresponding to the current error
|
||||
* (this.error).
|
||||
*/
|
||||
public String getErrorMessage()
|
||||
{
|
||||
String result;
|
||||
switch(this.error)
|
||||
{
|
||||
case 110 :
|
||||
result = "n databases present";
|
||||
break;
|
||||
case 111 :
|
||||
result = "n strategies available";
|
||||
break;
|
||||
case 112 :
|
||||
result = "database information follows";
|
||||
break;
|
||||
case 113 :
|
||||
result = "help text follows";
|
||||
break;
|
||||
case 114 :
|
||||
result = "server information follows";
|
||||
break;
|
||||
case 130 :
|
||||
result = "challenge follows";
|
||||
break;
|
||||
case 150 :
|
||||
result = "n definitions retrieved";
|
||||
break;
|
||||
case 151 :
|
||||
result = "word database name";
|
||||
break;
|
||||
case 152 :
|
||||
result = "n matches found";
|
||||
break;
|
||||
case 210 :
|
||||
result = "optional timing";
|
||||
break;
|
||||
case 220 :
|
||||
result = "Connection OK";
|
||||
break;
|
||||
case 221 :
|
||||
result = "Closing Connection";
|
||||
break;
|
||||
case 230 :
|
||||
result = "Authentication successful";
|
||||
break;
|
||||
case 250 :
|
||||
result = "OK";
|
||||
break;
|
||||
case 330 :
|
||||
result = "send response";
|
||||
break;
|
||||
case 420 :
|
||||
result = "Server temporarily unavailable";
|
||||
break;
|
||||
case 421 :
|
||||
result = "Server shutting down at operator request";
|
||||
break;
|
||||
case 500 :
|
||||
result = "Syntax error, command not recognized";
|
||||
break;
|
||||
case 501 :
|
||||
result = "Syntax error, illegal parameters";
|
||||
break;
|
||||
case 502 :
|
||||
result = "Command not implemented";
|
||||
break;
|
||||
case 503 :
|
||||
result = "Command parameter not implemented";
|
||||
break;
|
||||
case 530 :
|
||||
result = "Access denied";
|
||||
break;
|
||||
case 531 :
|
||||
result = "Access denied, use SHOW INFO for server information";
|
||||
break;
|
||||
case 532 :
|
||||
result = "Access denied, unknown mechanism";
|
||||
break;
|
||||
case 550 :
|
||||
result = "Invalid database, use SHOW DB for list of databases";
|
||||
break;
|
||||
case 551 :
|
||||
result = "Invalid strategy, use SHOW STRAT for a list of strategies";
|
||||
break;
|
||||
case 552 :
|
||||
result = "No match";
|
||||
break;
|
||||
case 554 :
|
||||
result = "No databases present";
|
||||
break;
|
||||
case 555 :
|
||||
result = "No strategies available";
|
||||
break;
|
||||
default :
|
||||
if (error >= 900)
|
||||
{
|
||||
result = this.errorMessage;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = this.errorGen(error);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get informations about unknowns errors
|
||||
* @param err Error number
|
||||
* @return Error definition
|
||||
*/
|
||||
private String errorGen(int err)
|
||||
{
|
||||
String error_type = Integer.toString(err);
|
||||
|
||||
String result = new String();
|
||||
|
||||
if(error_type.startsWith("1"))
|
||||
{// test on digit one
|
||||
result = "Positive Preliminary reply : ";
|
||||
}
|
||||
else if(error_type.startsWith("2"))
|
||||
{
|
||||
result = "Positive Completion reply : " ;
|
||||
}
|
||||
else if(error_type.startsWith("3"))
|
||||
{
|
||||
result = "Positive Intermediate reply : ";
|
||||
}
|
||||
else if(error_type.startsWith("4"))
|
||||
{
|
||||
result = "Transient Negative Completion reply : ";
|
||||
}
|
||||
else if(error_type.startsWith("5"))
|
||||
{
|
||||
result = "Permanent Negative Completion reply : ";
|
||||
}
|
||||
else
|
||||
{
|
||||
return "Unknown error";
|
||||
}
|
||||
|
||||
//test on digit two
|
||||
if(error_type.charAt(1) == '0')
|
||||
{
|
||||
result += "Syntax";
|
||||
}
|
||||
else if ( error_type.charAt(1) == '1')
|
||||
{
|
||||
result += "Information";
|
||||
}
|
||||
else if ( error_type.charAt(1) == '2')
|
||||
{
|
||||
result += "Connections";
|
||||
}
|
||||
else if ( error_type.charAt(1) == '3')
|
||||
{
|
||||
result += "Authentication";
|
||||
}
|
||||
else if ( error_type.charAt(1) == '4')
|
||||
{
|
||||
result += "Unspecified as yet";
|
||||
}
|
||||
else if ( error_type.charAt(1) == '5')
|
||||
{
|
||||
result += "DICT System";
|
||||
}
|
||||
else if ( error_type.charAt(1) == '8')
|
||||
{
|
||||
result += "Nonstandard (private implementation) extensions";
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@ -1,64 +0,0 @@
|
||||
/*
|
||||
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
package net.java.sip.communicator.impl.protocol.dict;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Static registry storing the connexions to dict servers
|
||||
* @author ROTH Damien
|
||||
* @author LITZELMANN Cedric
|
||||
*/
|
||||
public class DictRegistry
|
||||
{
|
||||
private static HashMap<String, DictAdapter> adapters = new HashMap<String,DictAdapter>();
|
||||
|
||||
/**
|
||||
* Checks if an adapter associated with the given key is stored in the regitry
|
||||
* @param key Key to the adapter
|
||||
* @return true, if an adapter exists - false otherwise
|
||||
*/
|
||||
public static boolean has(String key)
|
||||
{
|
||||
return adapters.containsKey(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores a new adapter in the registry
|
||||
* @param key Key
|
||||
* @param value DictAdapter class
|
||||
*/
|
||||
public static void put(String key, DictAdapter value)
|
||||
{
|
||||
adapters.put(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the adapter associated with the given key
|
||||
* @param key Key
|
||||
* @return the adapter associated with the given key - null otherwise
|
||||
*/
|
||||
public static DictAdapter get(String key)
|
||||
{
|
||||
if (DictRegistry.has(key))
|
||||
{
|
||||
return adapters.get(key);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the adapter associated with the given key from the registry
|
||||
* @param key
|
||||
*/
|
||||
public static void remove(String key)
|
||||
{
|
||||
if (DictRegistry.has(key)) {
|
||||
adapters.remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,97 +0,0 @@
|
||||
/*
|
||||
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
package net.java.sip.communicator.impl.protocol.dict;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Representation of the results of a response to a query
|
||||
*
|
||||
* @author ROTH Damien
|
||||
* @author LITZEMANN Cedric
|
||||
*/
|
||||
public class DictResult
|
||||
implements Iterator<String>
|
||||
{
|
||||
private String databaseName;
|
||||
private int index;
|
||||
private ArrayList<String> data;
|
||||
|
||||
/**
|
||||
* Basic construct initializing the result
|
||||
*/
|
||||
public DictResult()
|
||||
{
|
||||
this.index = 0;
|
||||
this.data = new ArrayList<String>();
|
||||
this.databaseName = "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the result and save the database name
|
||||
* @param dbn Database name
|
||||
*/
|
||||
public DictResult(String dbn)
|
||||
{
|
||||
this.index = 0;
|
||||
this.data = new ArrayList<String>();
|
||||
this.databaseName = dbn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a result
|
||||
* @param s result
|
||||
*/
|
||||
public void add(String s)
|
||||
{
|
||||
this.data.add(s);
|
||||
}
|
||||
|
||||
/**
|
||||
* From the Iterator implementation, return the next part of the result
|
||||
* @return the next part of the result
|
||||
*/
|
||||
public String next()
|
||||
{
|
||||
return (String) this.data.get(this.index++);
|
||||
}
|
||||
|
||||
/**
|
||||
* From the Iterator implementation, return true if the iteration has more elements
|
||||
* @return true if the iteration has more elements - false otherwise
|
||||
*/
|
||||
public boolean hasNext()
|
||||
{
|
||||
return (this.index < this.data.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* From the Iterator implementation but unsupported
|
||||
*/
|
||||
public void remove()
|
||||
{
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the database name
|
||||
* @param dbn Database name
|
||||
*/
|
||||
public void setDatabaseName(String dbn)
|
||||
{
|
||||
this.databaseName = dbn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the database name
|
||||
* @return the database name
|
||||
*/
|
||||
public String getDatabaseName()
|
||||
{
|
||||
return this.databaseName;
|
||||
}
|
||||
}
|
||||
@ -1,107 +0,0 @@
|
||||
/*
|
||||
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
package net.java.sip.communicator.impl.protocol.dict;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Class managing the results of a dict query
|
||||
*
|
||||
* @author ROTH Damien
|
||||
* @author LITZELMANN Cedric
|
||||
*/
|
||||
public class DictResultset
|
||||
{
|
||||
/**
|
||||
* The index number of the last resultset for this DictResultset. This
|
||||
* parametre is set to "-1" if there is no resultset.
|
||||
*/
|
||||
private int cursor;
|
||||
|
||||
/**
|
||||
* The list containing all the resultsets for this DictResultset.
|
||||
*/
|
||||
private ArrayList<DictResult> data;
|
||||
|
||||
/**
|
||||
* Initialize a resultset list
|
||||
*/
|
||||
public DictResultset()
|
||||
{
|
||||
this.cursor = -1;
|
||||
this.data = new ArrayList<DictResult>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new resultset
|
||||
*/
|
||||
public void newResultset()
|
||||
{
|
||||
this.cursor++;
|
||||
this.data.add(new DictResult());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new resultset and save the database name
|
||||
* @param dbn Database name
|
||||
*/
|
||||
public void newResultset(String dbn)
|
||||
{
|
||||
this.cursor++;
|
||||
this.data.add(new DictResult(dbn));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the database name for the current resultset
|
||||
* @param dbn Database name
|
||||
*/
|
||||
public void setDatabaseName(String dbn)
|
||||
{
|
||||
this.data.get(this.cursor).setDatabaseName(dbn);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a result in the current resultset
|
||||
* @param res a result line from a dict query
|
||||
*/
|
||||
public void addResult(String res)
|
||||
{
|
||||
this.data.get(this.cursor).add(res);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if there is a resultset
|
||||
* @return return true if there is a resultset - false otherwise
|
||||
*/
|
||||
public boolean hasResult()
|
||||
{
|
||||
return this.data.size() > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the resultset at the given index
|
||||
* @param index Index of the wished resultset
|
||||
* @return a DictResult - null otherwise
|
||||
*/
|
||||
public DictResult getResultset(int index)
|
||||
{
|
||||
if (index < this.data.size())
|
||||
{
|
||||
return (DictResult) this.data.get(index);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of resultsets
|
||||
* @return the number of resultsets
|
||||
*/
|
||||
public int getNbResults()
|
||||
{
|
||||
return this.data.size();
|
||||
}
|
||||
}
|
||||
@ -1,183 +0,0 @@
|
||||
/*
|
||||
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
|
||||
*
|
||||
* Distributable under LGPL license. See terms of license at gnu.org.
|
||||
*/
|
||||
package net.java.sip.communicator.plugin.dictaccregwizz;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
|
||||
import net.java.sip.communicator.util.Logger;
|
||||
|
||||
/**
|
||||
* Copy of DictAdapter class to solve the import problem
|
||||
* @author ROTH Damien
|
||||
* @author LITZELMANN Cedric
|
||||
*/
|
||||
|
||||
public class DictAdapter
|
||||
{
|
||||
private static Logger logger = Logger.getLogger(DictAdapter.class);
|
||||
|
||||
/**
|
||||
* The socket used to connect to the DICT server.
|
||||
*/
|
||||
private Socket socket = null;
|
||||
|
||||
/**
|
||||
* A output stream piped to the socket in order to send command to the server.
|
||||
*/
|
||||
private PrintWriter out = null;
|
||||
|
||||
/**
|
||||
* A input stream piped to the socket in order to receive messages from the server.
|
||||
*/
|
||||
private BufferedReader in = null;
|
||||
|
||||
/**
|
||||
* A boolean telling if we are currently connected to the DICT server.
|
||||
*/
|
||||
private boolean connected = false;
|
||||
|
||||
/**
|
||||
* Get the strategies allowed by the server for the MATCH command
|
||||
* @return a HashMap containing the database list - otherwise null
|
||||
*/
|
||||
public ArrayList<String> getStrategies()
|
||||
{
|
||||
String fromServer;
|
||||
boolean quit = false;
|
||||
ArrayList<String> result = null;
|
||||
|
||||
// Connexion
|
||||
if (!this.connected)
|
||||
{
|
||||
// Not connected
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
this.out.println("SHOW STRAT");
|
||||
fromServer = this.in.readLine();
|
||||
|
||||
if (fromServer.startsWith("111"))
|
||||
{ // OK - getting responses from the server
|
||||
result = new ArrayList<String>();
|
||||
while (quit == false && (fromServer = this.in.readLine()) != null)
|
||||
{
|
||||
if (fromServer.startsWith("250"))
|
||||
{
|
||||
quit = true;
|
||||
}
|
||||
else if (!fromServer.equals("."))
|
||||
{
|
||||
result.add(fromServer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
logger.trace("Cannot get the strategies : " + ioe.getMessage());
|
||||
result = null;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Open a connection to the given host on the given port
|
||||
* @param host The hostname of the server.
|
||||
* @param port The port used by the server.
|
||||
* @return true, if a connection is open - false otherwise
|
||||
*/
|
||||
public boolean connect(String host, int port)
|
||||
{
|
||||
this.connected = false;
|
||||
String fromServer;
|
||||
|
||||
try
|
||||
{
|
||||
this.socket = new Socket(host, port);
|
||||
this.out = new PrintWriter(new OutputStreamWriter(this.socket.getOutputStream(),
|
||||
"UTF-8"), true);
|
||||
this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream(),
|
||||
"UTF-8"));
|
||||
|
||||
fromServer = this.in.readLine(); // Server banner
|
||||
|
||||
if (fromServer.startsWith("220"))
|
||||
{ // 220 = connect ok
|
||||
this.connected = true;
|
||||
}
|
||||
}
|
||||
catch(Exception ex)
|
||||
{ // If an exception is throw == connexion impossible
|
||||
logger.trace("Cannot establish a connexion to the server ("
|
||||
+ host + ":" + port + ")", ex);
|
||||
}
|
||||
|
||||
return this.connected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the connexion to the server
|
||||
*/
|
||||
public void close()
|
||||
{
|
||||
String fromServer;
|
||||
|
||||
try
|
||||
{
|
||||
this.out.println("QUIT");
|
||||
|
||||
// Clean the socket buffer
|
||||
while ((fromServer = this.in.readLine()) != null)
|
||||
{
|
||||
if (fromServer.startsWith("221"))
|
||||
{ // Quit response
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
this.out.close();
|
||||
this.in.close();
|
||||
this.socket.close();
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
logger.info("Cannot close the connextion to the server", ioe);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given url is correct and exists
|
||||
* @param host The url that we have to test if it is correct and if it
|
||||
* exists.
|
||||
* @return true if the url exists - false otherwise
|
||||
*/
|
||||
public static boolean isUrl(String host)
|
||||
{
|
||||
boolean ok = false;
|
||||
|
||||
if (host == null || host.length() == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// If an exception is throw, the host format isn't correct or isn't recheable
|
||||
try
|
||||
{
|
||||
InetAddress.getByName(host);
|
||||
ok = true;
|
||||
}
|
||||
catch (UnknownHostException uhex)
|
||||
{
|
||||
logger.trace("Test URL ("+host+") : " + uhex.getMessage(), uhex);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,137 @@
|
||||
/*
|
||||
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
|
||||
*
|
||||
* Distributable under LGPL license. See terms of license at gnu.org.
|
||||
*/
|
||||
package net.java.sip.communicator.plugin.dictaccregwizz;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
/**
|
||||
* Panel showing the current status of the search of the strategies
|
||||
*
|
||||
* @author ROTH Damien
|
||||
*/
|
||||
public class ProgressPanel
|
||||
extends JPanel
|
||||
implements ActionListener
|
||||
{
|
||||
private JPanel rightPanel;
|
||||
|
||||
private JLabel messageLabel;
|
||||
private JLabel progressLabel;
|
||||
private JButton cancelButton;
|
||||
|
||||
private int currentStep;
|
||||
private int totalSteps;
|
||||
|
||||
private boolean isBuild;
|
||||
|
||||
private ThreadManager searchThread;
|
||||
|
||||
/**
|
||||
* Create an instance of <tt>ProgressPanel</tt>
|
||||
* @param searchThread The thread manager
|
||||
*/
|
||||
public ProgressPanel(ThreadManager searchThread)
|
||||
{
|
||||
super(new BorderLayout());
|
||||
|
||||
// Element creation
|
||||
this.messageLabel = new JLabel(" ");
|
||||
this.progressLabel = new JLabel(" ");
|
||||
this.cancelButton = new JButton(Resources.getString("cancel"));
|
||||
this.cancelButton.addActionListener(this);
|
||||
|
||||
// Right panel init
|
||||
this.rightPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
|
||||
this.rightPanel.add(this.progressLabel);
|
||||
this.rightPanel.add(this.cancelButton);
|
||||
|
||||
this.searchThread = searchThread;
|
||||
|
||||
init();
|
||||
this.totalSteps = ThreadManager.NB_STEPS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Init the values
|
||||
*/
|
||||
private void init()
|
||||
{
|
||||
this.isBuild = false;
|
||||
this.currentStep = 1;
|
||||
|
||||
this.add(this.messageLabel, BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the UI
|
||||
*/
|
||||
private void build()
|
||||
{
|
||||
if (this.isBuild)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.add(this.messageLabel, BorderLayout.CENTER);
|
||||
this.add(this.rightPanel, BorderLayout.EAST);
|
||||
|
||||
this.isBuild = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Move to the next step without updating the message
|
||||
*/
|
||||
public void nextStep()
|
||||
{
|
||||
nextStep(this.messageLabel.getText());
|
||||
}
|
||||
|
||||
/**
|
||||
* Mode to the next step with a new message
|
||||
* @param message Message
|
||||
*/
|
||||
public void nextStep(String message)
|
||||
{
|
||||
if (this.currentStep > this.totalSteps)
|
||||
{
|
||||
finish();
|
||||
}
|
||||
|
||||
build();
|
||||
this.messageLabel.setText(message);
|
||||
this.progressLabel.setText(currentStep + "/" + totalSteps);
|
||||
|
||||
this.currentStep++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Informs the end of the progress. Remove all the components and
|
||||
* reset the values
|
||||
*/
|
||||
public void finish()
|
||||
{
|
||||
// Remove all elements
|
||||
this.removeAll();
|
||||
|
||||
// Re-init the panel
|
||||
this.messageLabel.setText(" ");
|
||||
this.progressLabel.setText(" ");
|
||||
init();
|
||||
|
||||
this.repaint();
|
||||
this.validate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent arg0)
|
||||
{
|
||||
this.searchThread.cancel();
|
||||
this.finish();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,213 @@
|
||||
/*
|
||||
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
package net.java.sip.communicator.plugin.dictaccregwizz;
|
||||
|
||||
import java.awt.Component;
|
||||
import java.util.*;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
import net.java.dict4j.*;
|
||||
|
||||
/**
|
||||
* Class managing the list of strategies
|
||||
*
|
||||
* @author ROTH Damien
|
||||
*/
|
||||
public class StrategiesList
|
||||
extends JList
|
||||
{
|
||||
private ListModel model;
|
||||
private CellRenderer renderer;
|
||||
|
||||
/**
|
||||
* Create an instance of the <tt>StrategiesList</tt>
|
||||
*/
|
||||
public StrategiesList()
|
||||
{
|
||||
super();
|
||||
|
||||
this.model = new ListModel();
|
||||
this.renderer = new CellRenderer();
|
||||
|
||||
this.setCellRenderer(this.renderer);
|
||||
this.setModel(model);
|
||||
this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||
this.setVisibleRowCount(6);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores a new set of strategies
|
||||
* @param strategies List of strategies
|
||||
*/
|
||||
public void setStrategies(List<Strategy> strategies)
|
||||
{
|
||||
this.model.setStrategies(strategies);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all the strategies of the list
|
||||
*/
|
||||
public void clear()
|
||||
{
|
||||
this.model.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Automatic selection of strategies
|
||||
* @param initStrategy
|
||||
*/
|
||||
public void autoSelectStrategy(String initStrategy)
|
||||
{
|
||||
int index = -1;
|
||||
|
||||
if (initStrategy.length() > 0)
|
||||
{ // saved strategy
|
||||
index = this.model.indexOf(initStrategy);
|
||||
}
|
||||
if (index < 0)
|
||||
{
|
||||
// First case : levenstein distance
|
||||
index = this.model.indexOf("lev");
|
||||
}
|
||||
if (index < 0)
|
||||
{
|
||||
// Second case : soundex
|
||||
index = this.model.indexOf("soundex");
|
||||
}
|
||||
if (index < 0)
|
||||
{
|
||||
// Last case : prefix
|
||||
index = this.model.indexOf("prefix");
|
||||
}
|
||||
|
||||
// If the index is still < 0, we select the first index
|
||||
if (index < 0)
|
||||
{
|
||||
index = 0;
|
||||
}
|
||||
if (index < this.getVisibleRowCount())
|
||||
{
|
||||
// If the index is visible row, we don't need to scroll
|
||||
this.setSelectedIndex(index);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Otherwise, we scroll to the selected value
|
||||
this.setSelectedValue(this.model.getElementAt(index), true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class managing the list datas
|
||||
*
|
||||
* @author ROTH Damien
|
||||
*/
|
||||
class ListModel
|
||||
extends AbstractListModel
|
||||
{
|
||||
List<Strategy> data;
|
||||
|
||||
/**
|
||||
* Create an instance of <tt>ListModel</tt>
|
||||
*/
|
||||
public ListModel()
|
||||
{
|
||||
this.data = new ArrayList<Strategy>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores the strategies into this model
|
||||
* @param data the strategies list
|
||||
*/
|
||||
public void setStrategies(List<Strategy> strategies)
|
||||
{
|
||||
this.data = strategies;
|
||||
fireContentsChanged(this, 0, this.data.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all the strategies of the list
|
||||
*/
|
||||
public void clear()
|
||||
{
|
||||
this.data.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements <tt>ListModel.getElementAt</tt>
|
||||
*/
|
||||
@Override
|
||||
public Object getElementAt(int row)
|
||||
{
|
||||
return this.data.get(row);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements <tt>ListModel.getSize</tt>
|
||||
*/
|
||||
@Override
|
||||
public int getSize()
|
||||
{
|
||||
return this.data.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the index of a strategie
|
||||
* @param strategyCode the code of the strategy
|
||||
* @return the index of the strategy
|
||||
*/
|
||||
public int indexOf(String strategyCode)
|
||||
{
|
||||
for (int i=0; i<this.data.size(); i++)
|
||||
{
|
||||
if (this.data.get(i).getCode().equals(strategyCode))
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class managing the cell rendering
|
||||
*
|
||||
* @author ROTH Damien
|
||||
*/
|
||||
class CellRenderer
|
||||
extends JLabel
|
||||
implements ListCellRenderer
|
||||
{
|
||||
@Override
|
||||
/**
|
||||
* implements <tt>ListCellRenderer.getListCellRendererComponent</tt>
|
||||
*/
|
||||
public Component getListCellRendererComponent(JList list, Object value,
|
||||
int index, boolean isSelected, boolean cellHasFocus)
|
||||
{
|
||||
Strategy strategy = (Strategy) value;
|
||||
this.setText(strategy.getName());
|
||||
|
||||
if (isSelected)
|
||||
{
|
||||
setBackground(list.getSelectionBackground());
|
||||
setForeground(list.getSelectionForeground());
|
||||
}
|
||||
else
|
||||
{
|
||||
setBackground(list.getBackground());
|
||||
setForeground(list.getForeground());
|
||||
}
|
||||
setEnabled(list.isEnabled());
|
||||
setFont(list.getFont());
|
||||
setOpaque(true);
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,207 +0,0 @@
|
||||
/*
|
||||
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
|
||||
*
|
||||
* Distributable under LGPL license. See terms of license at gnu.org.
|
||||
*/
|
||||
package net.java.sip.communicator.plugin.dictaccregwizz;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import net.java.sip.communicator.util.*;
|
||||
|
||||
/**
|
||||
* The <tt>StrategyThread</tt> is the thread called by the wizzard to populate
|
||||
* the strategies list.
|
||||
*
|
||||
* @author ROTH Damien
|
||||
* @author LITZELMANN Cedric
|
||||
*/
|
||||
public class StrategyThread
|
||||
extends Thread
|
||||
{
|
||||
private static Logger logger = Logger.getLogger(StrategyThread.class);
|
||||
|
||||
/**
|
||||
* The hostname of the DICT server.
|
||||
*/
|
||||
private String host;
|
||||
|
||||
/**
|
||||
* The port used by the DICT server.
|
||||
*/
|
||||
private int port;
|
||||
|
||||
/**
|
||||
* True if the thread is running.
|
||||
*/
|
||||
private boolean isRunning = false;
|
||||
|
||||
/**
|
||||
* True if we need to search the strategies handled by the server in order
|
||||
* to populate the list.
|
||||
*/
|
||||
private boolean needProcess = false;
|
||||
|
||||
/**
|
||||
* The first page wizard for the DICT protocole.
|
||||
*/
|
||||
private FirstWizardPage wizard;
|
||||
|
||||
/**
|
||||
* The java abstraction of the DICT server.
|
||||
*/
|
||||
private DictAdapter adapter = null;
|
||||
|
||||
/**
|
||||
* Create a new StrategyThread
|
||||
* @param wizard the wizard for callback methods
|
||||
*/
|
||||
public StrategyThread(FirstWizardPage wizard)
|
||||
{
|
||||
this.wizard = wizard;
|
||||
}
|
||||
|
||||
/**
|
||||
* Thread method running until it's destroy
|
||||
*/
|
||||
public void run()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (this.needProcess())
|
||||
{
|
||||
this.setRunning(true);
|
||||
this.process();
|
||||
this.processDone();
|
||||
this.setRunning(false);
|
||||
}
|
||||
try
|
||||
{
|
||||
this.sleep(500);
|
||||
}
|
||||
catch (InterruptedException ie)
|
||||
{
|
||||
// Action de log
|
||||
logger.info("DICT THREAD : " + ie);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Search the strategies on the server and populate the list
|
||||
*/
|
||||
private void process()
|
||||
{
|
||||
ArrayList<String> strategies = null;
|
||||
String temp[];
|
||||
|
||||
if (adapter == null) {
|
||||
adapter = new DictAdapter();
|
||||
}
|
||||
|
||||
this.wizard.setStrategyButtonEnable(false);
|
||||
|
||||
// Initialize the connexion
|
||||
this.wizard.threadMessage("Trying to connect to server");
|
||||
if (!adapter.connect(this.host, this.port))
|
||||
{
|
||||
// Connexion attempt failed
|
||||
this.wizard.threadMessage("Connexion attempt failed, this isn't a"
|
||||
+ "dict server or the server is offline");
|
||||
return;
|
||||
}
|
||||
|
||||
// Retrieving strategies
|
||||
this.wizard.threadMessage("Retrieving strategies");
|
||||
strategies = adapter.getStrategies();
|
||||
if (strategies == null)
|
||||
{
|
||||
// No strategy found
|
||||
this.wizard.threadMessage("No strategy found on the server");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Insert the strategies in the list
|
||||
for (int i=0; i<strategies.size(); i++)
|
||||
{
|
||||
temp = strategies.get(i).split(" ", 2);
|
||||
|
||||
this.wizard.threadAddStrategy(temp[0], temp[1].replace("\"", ""));
|
||||
}
|
||||
|
||||
this.wizard.autoSelectStrategy();
|
||||
|
||||
// Closing connexion
|
||||
this.wizard.threadMessage("Closing connexion");
|
||||
adapter.close();
|
||||
|
||||
this.wizard.threadRemoveMessage();
|
||||
this.wizard.setStrategyButtonEnable(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the hostname of the dict server
|
||||
* @param host The hostname of the server.
|
||||
* @return The thread for populating strategie list.
|
||||
*/
|
||||
public StrategyThread setHost(String host)
|
||||
{
|
||||
this.host = host;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the port of the dict server
|
||||
* @param port The port of the DICT server.
|
||||
* @return The thread for populating strategie list.
|
||||
*/
|
||||
public StrategyThread setPort(int port)
|
||||
{
|
||||
this.port = port;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the thread is processing a query
|
||||
* @return TRUE if the thread is processing a query - FALSE otherwise
|
||||
*/
|
||||
public synchronized boolean isRunning()
|
||||
{
|
||||
return this.isRunning || this.needProcess;
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks the thread as running or not
|
||||
* @param r Activate or desactivate the strategie thread.
|
||||
*/
|
||||
public synchronized void setRunning(boolean r)
|
||||
{
|
||||
this.isRunning = r;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if we need to process a query
|
||||
* @return Returns true if we need to search and populate the strategie list. False otherwise.
|
||||
*/
|
||||
private synchronized boolean needProcess()
|
||||
{
|
||||
return this.needProcess;
|
||||
}
|
||||
|
||||
/**
|
||||
* Init the request
|
||||
*/
|
||||
public synchronized void sendProcessRequest()
|
||||
{
|
||||
this.needProcess = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines that the thread has done the query
|
||||
*/
|
||||
private synchronized void processDone()
|
||||
{
|
||||
this.needProcess = false;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,182 @@
|
||||
/*
|
||||
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
package net.java.sip.communicator.plugin.dictaccregwizz;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import net.java.sip.communicator.util.*;
|
||||
|
||||
import net.java.dict4j.*;
|
||||
|
||||
/**
|
||||
* Class manager the thread called for searching the strategies' list
|
||||
*
|
||||
* @author ROTH Damien
|
||||
*/
|
||||
public class ThreadManager
|
||||
{
|
||||
protected static Logger logger = Logger.getLogger(ThreadManager.class);
|
||||
public static int NB_STEPS = 4;
|
||||
|
||||
private StrategyThread thread = null;
|
||||
private FirstWizardPage wizard = null;
|
||||
|
||||
/**
|
||||
* Create an instance of <tt>ThreadManager</tt>
|
||||
* @param wiz Wizard
|
||||
*/
|
||||
public ThreadManager(FirstWizardPage wiz)
|
||||
{
|
||||
this.wizard = wiz;
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit a request to launch the thread
|
||||
* @param host Server host
|
||||
* @param port Server port
|
||||
* @return TRUE if the thread is started - FALSE otherwise
|
||||
*/
|
||||
public boolean submitRequest(String host, int port)
|
||||
{
|
||||
if (this.thread != null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
this.thread = new StrategyThread(this.wizard, host, port);
|
||||
this.thread.start();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the thread
|
||||
*/
|
||||
public void cancel()
|
||||
{
|
||||
if (this.thread != null)
|
||||
{
|
||||
this.thread.interrupt();
|
||||
this.thread = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for the searching thread to stop
|
||||
* @return
|
||||
*/
|
||||
public boolean waitThread()
|
||||
{
|
||||
if (this.thread == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
this.thread.join();
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
logger.info(e);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Thread used to search the strategies
|
||||
*
|
||||
* @author ROTH Damien
|
||||
* @author LITZELMANN Cédric
|
||||
*/
|
||||
class StrategyThread
|
||||
extends Thread
|
||||
{
|
||||
private FirstWizardPage wizard;
|
||||
private String host;
|
||||
private int port;
|
||||
|
||||
/**
|
||||
* Informations messages
|
||||
*/
|
||||
private String[] messages = new String[] {
|
||||
Resources.getString("dict.threadConnect"),
|
||||
Resources.getString("dict.threadConnectFailed"),
|
||||
Resources.getString("dict.retrievingStrategies"),
|
||||
Resources.getString("dict.noStrategiesFound"),
|
||||
Resources.getString("dict.populateList"),
|
||||
Resources.getString("dict.closingConnexion")
|
||||
};
|
||||
|
||||
/**
|
||||
* Create an instance of the thread
|
||||
* @param wizard The wizard who started the thread
|
||||
* @param host Server host
|
||||
* @param port Server port
|
||||
*/
|
||||
public StrategyThread(FirstWizardPage wizard, String host, int port)
|
||||
{
|
||||
this.wizard = wizard;
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void run()
|
||||
{
|
||||
List<Strategy> strategies = null;
|
||||
|
||||
DictConnection dictConnection = new DictConnection(host, port);
|
||||
|
||||
// Open the connection to the server
|
||||
this.wizard.progressMessage(messages[0]);
|
||||
try
|
||||
{
|
||||
dictConnection.connect();
|
||||
}
|
||||
catch (DictException e)
|
||||
{
|
||||
this.wizard.strategiesSearchFailure(this.messages[1], e);
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the strategies
|
||||
this.wizard.progressMessage(messages[2]);
|
||||
try
|
||||
{
|
||||
strategies = dictConnection.getStrategies();
|
||||
}
|
||||
catch (DictException e)
|
||||
{
|
||||
this.wizard.strategiesSearchFailure(this.messages[3], e);
|
||||
return;
|
||||
}
|
||||
|
||||
// Store the strategies
|
||||
this.wizard.progressMessage(messages[4]);
|
||||
this.wizard.setStrategies(strategies);
|
||||
this.wizard.autoSelectStrategy();
|
||||
|
||||
// Close the connection
|
||||
this.wizard.progressMessage(messages[5]);
|
||||
try
|
||||
{
|
||||
dictConnection.close();
|
||||
}
|
||||
catch (DictException e)
|
||||
{
|
||||
// An error while closing the connection isn't very important
|
||||
// We just log it
|
||||
ThreadManager.logger.info("DICT search strategies thread : " +
|
||||
"Error while closing connection", e);
|
||||
}
|
||||
|
||||
// End of the search
|
||||
this.wizard.strategiesSearchComplete();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue