Below are my service, serviceImpl and async callback interfaces. Appreciate if someone can help me understand why the submitTeam(...) isn't being called (I howeve see that isValidEmail(...) is being invoked when used). At least provide me the approach for debugging this as currently I am unable to use eclipse debugger (eclipse is not stopping at breakpoints) and the sysout/syserr statements are not being logged to the console either :-(.
#RemoteServiceRelativePath("registrationService")
public interface RegistrationService extends RemoteService
{
Boolean isValidEmail(String email);
String submitTeam(String teamName, List<Player> players);
}
public interface RegistrationServiceAsync
{
void isValidEmail(String email, AsyncCallback<Boolean> callback);
void submitTeam(String teamName, List<Player> players, AsyncCallback<String> callback);
}
public class SubmitTeamCallback implements AsyncCallback<String> {
private final Label status;
public SubmitTeamCallback(Label s) {
status = s;
}
public void onFailure(Throwable caught) {
status.setText(caught.getMessage());
}
public void onSuccess(String result) {
System.out.println("" + getClass().getName() + " : " + result);
status.setText(result);
}
}
public class RegistrationCallback implements AsyncCallback<Boolean>
{
private final Label status;
private final PlayerWidget playerWidget;
private Boolean isValidSharedFlag;
public RegistrationCallback(Label s, PlayerWidget pw, Boolean isValid)
{
status = s;
playerWidget = pw;
setIsValidSharedFlag(isValid);
}
public void onFailure(Throwable caught)
{
status.setText(caught.getMessage());
}
public void onSuccess(Boolean result)
{
if (result.equals(Boolean.FALSE))
{
playerWidget.invalidEmail();
String oldText = status.getText();
status.setText(oldText + "Please specify a valid email address for the captain.");
setIsValidSharedFlag(Boolean.FALSE);
} else
{
playerWidget.validEmail();
String newText = status.getText().replace("Please specify a valid email address for the captain.", "");
status.setText(newText);
setIsValidSharedFlag(Boolean.TRUE);
}
}
public void setIsValidSharedFlag(Boolean isValidSharedFlag)
{
this.isValidSharedFlag = isValidSharedFlag;
}
public Boolean getIsValidSharedFlag()
{
return isValidSharedFlag;
}
}
public class RegistrationServiceImpl extends RemoteServiceServlet implements
RegistrationService {
Logger logger = Logger.getLogger("");
private final Emailer emailer = new Emailer();
private final EmailValidator validator = new EmailValidator();
public Boolean isValidEmail(String email) {
return validator.validate(email);
}
public String submitTeam(String teamName, List<Player> players) {
logger.log(Level.SEVERE, "This is a test log");
System.out.println("This is a test log");
boolean emailSent = false;
for (Player p : players) {
System.out.println("Emailing to captain");
if (p instanceof Captain) {
if (!validator.validate(p.getEmail())) {
return "Please specify a valid email";
}
System.out.println("Emailing to captain");
emailSent = emailer.email(p);
}
}
if (emailSent) {
System.out.println("Email sent successfully");
return teamName
+ " has been successfully registered. Please pay the registration fee to confirm registration. Thank you.";
} else {
return "Unable to send email. Please email the team details [Team name, minimum of 6 players, captain's email address and phone number] to funpluscharity#gmail.com";
}
}
}
Below method is going to invoke the RPC calls
private void registerTeam() {
System.out.println("Received request to dubmit team");
StringBuilder statusText = new StringBuilder();
try {
Boolean isValid = true;
RegistrationServiceAsync service = (RegistrationServiceAsync) GWT
.create(RegistrationService.class);
ServiceDefTarget serviceDef = (ServiceDefTarget) service;
System.err.println(".........." + GWT.getModuleBaseURL());
serviceDef
.setServiceEntryPoint("/services/registrationService");
if (teamName.getText() == null
|| teamName.getText().length() == 0) {
isValid = false;
statusText.append("Please specify team name. ");
}
Player captain = getCaptain();
if (!atleast6PlayersAreSpecified()) {
isValid = false;
statusText.append("Please specify atleast 6 players. ");
}
if (captain == null) {
isValid = false;
statusText.append("Please designate a captain. ");
}
System.out.println("Is request valid? " + isValid);
if (isValid.equals(Boolean.TRUE)) {
System.out.println("RPC - submitTeam start ");
System.out.println("" + getPlayers());
SubmitTeamCallback callback = new SubmitTeamCallback(status);
service.submitTeam(teamName.getText(), getPlayers(),
callback);
System.out.println("RPC - submitTeam end");
} else {
status.setText(statusText.toString());
}
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
}
Made some progress after adding Window.alert(...) based on #Pistol suggestion and it is going to onFailure(Throwable t) method of the SubmitTeamCallback class.
unknown.com_google_gwt_user_client_rpc_SerializationException_SerializationException__Ljava_lang_String_2V(Unknown Source)
An alternative method to debug client side code is using Window.alert(...). For example placing it in your callback onFailure() method to see caught messages for a start. Or you can use FireBug or something similar to see if the rpc-call is actually being made?
Edit :
Are you sure your Player class implements IsSerializable and have an empty constructor? Check out Serializable User-Defined Classes.
Figured this out. The POJO's should have a default constructor and the one's I am using didn't have one. GWT compiler didn't complain either. Not sure why.
Related
So I want to add a dialog message to my app. There already an option for other types of error's. I just want to add an error for when there's no mobile data and WiFi. It's an older app, so it's taking me a bit more to understand, but here's what I got.
So here is the status code for the errors. Not sure if the codes are random or not (I didn't make this project)
public class StatusCodeUtil {
public static final int AWS_GATEWAY_ERROR = 1;
public static final int URL_INVALID = 2;
public static final int INTERNAL_SERVER_ERROR = 14;
public static final int ENDPOINT_INFO_STORAGE_INCOMPLETE = 7;
public static final int NO_PERMISSION_GET_DEVICE_ID = 8;
public static final int INVALID_API_FUNCTION = 18;
public static final int INVALID_HTTP_STATUS_CODE = -1;
public static final int NO_NETWORK_ERROR = 3; <- This is the status code I want to work
}
Here is the Callback for the errors
public abstract class ApiCallBack<T> implements Callback<ApiResponse<T>> {
private ParabitSDKBeaconApplication application;
public ApiCallBack(ParabitSDKBeaconApplication application) {
this.application = application;
}
#Override
public void onResponse(Call<ApiResponse<T>> call, Response<ApiResponse<T>> response) {
Long roundTripTime = getRoundTripTime(response);
if (response.isSuccessful()) {
ApiResponse<T> responseBody = response.body();
onApiResponse(responseBody.getMessage(), response.code(), responseBody.getData());
} else {
/**
* error level 1 (HTTP client or gateway error)
* */
String errorBodyJson = getErrorBodyStr(response);
// can not user ApiResponse<T> to catch the json here
// will lead to exception: java.lang.AssertionError: illegal type variable reference
// no good way to solve this (Gson's problem)
ApiErrorResponse errorBody = GsonUtil.jsonStrToObject(errorBodyJson,
new TypeToken<ApiErrorResponse>(){});
if (errorBody.getMessage().equalsIgnoreCase("forbidden")) { // x-api-key invalid
if (getLogControlManager().isLog()) {
Log.e(PARABIT_SDK_LOG, "AWS Gateway Error: " + errorBody.getMessage());
}
onError(new ApiErrorCodeInfo(AWS_GATEWAY_ERROR, response.code(),
errorBody.getMessage()));
} else if (errorBody.getMessage().equalsIgnoreCase(
"missing authentication token")) {
if (getLogControlManager().isLog()) {
Log.e(PARABIT_SDK_LOG, "AWS Gateway Error: " + errorBody.getMessage());
}
onError(new ApiErrorCodeInfo(INVALID_API_FUNCTION, response.code(),
errorBody.getMessage()));
} else {
if (getLogControlManager().isLog()) {
Log.e(PARABIT_SDK_LOG, "Other Error Response: " + errorBody.getMessage());
}
// should never happen for now
onError(new ApiErrorCodeInfo(INTERNAL_SERVER_ERROR, response.code(),
errorBody.getMessage()));
}
}
}
#Override
public void onFailure(Call<ApiResponse<T>> call, Throwable t) {
/**
* error level 1 (HTTP client or gateway error)
* */
if (t instanceof UnknownHostException) { // host of end point is unknown
if (getLogControlManager().isLog()) {
Log.e(PARABIT_SDK_LOG, "onFailure: " + "UnknownHostException");
}
onError(new ApiErrorCodeInfo(URL_INVALID, t.getLocalizedMessage()));
} else {
if (getLogControlManager().isLog()) {
Log.e(PARABIT_SDK_LOG, "onFailure: " + t.getLocalizedMessage());
}
onError(new ApiErrorCodeInfo(INTERNAL_SERVER_ERROR,
t.getLocalizedMessage()));
}
}
public static<T> String getErrorBodyStr(Response<ApiResponse<T>> response) {
if (response.errorBody() == null) {
return "";
}
String errorBodyStr = "";
try {
errorBodyStr = response.errorBody().string();
} catch (IOException e) {
e.printStackTrace();
}
return errorBodyStr;
}
protected Long getRoundTripTime(Response response) {
Long roundTripTime = response.raw().sentRequestAtMillis()
- response.raw().receivedResponseAtMillis();
return roundTripTime;
}
// public abstract void onSuccess(String successMsg, List<T> data);
public abstract void onApiResponse(String ApiMsg, int httpStatusCode, List<T> data);
public abstract void onError(ApiErrorCodeInfo apiErrorCodeInfo);
protected LogControlManager getLogControlManager() {
return SdkApplicationInstance.getSdkLogControlManager(application);
}
}
The code in the Activity that controls which error is shown
loginViewModel.loginStatusInfo.observe(this, loginStatusInfo -> {
if (loginStatusInfo.getStatus() == API_SUCCESS_STATUS){
hideLoadingDialog();
startHomeActivity();
}else if (loginStatusInfo.getStatus() == INTERNAL_SERVER_ERROR) {
hideLoadingDialog();
loginErrorDialog(getString(R.string.fail_to_login_server_error));
}else if(loginStatusInfo.getStatus() == NO_NETWORK_ERROR){<- I added this else if
hideLoadingDialog();
loginErrorDialog(getString(R.string.network_require_msg));
}
else {
hideLoadingDialog();
loginErrorDialog(loginStatusInfo.getMessage());
}
});
Any help will be appreciated, Thank you.
So I actually called ConectivityManager on the loginViewModel.loginStatusInfo method and it worked.
loginViewModel.loginStatusInfo.observe(this, loginStatusInfo -> {
if (loginStatusInfo.getStatus() == API_SUCCESS_STATUS){
hideLoadingDialog();
startHomeActivity();
}else if (loginStatusInfo.getStatus() == INTERNAL_SERVER_ERROR) {
hideLoadingDialog();
loginErrorDialog(getString(R.string.fail_to_login_server_error));
}else if(ConnectivityManager.TYPE_MOBILE == 0){
hideLoadingDialog();
networkErrorDialog(getString(R.string.network_require_msg));
}
else {
hideLoadingDialog();
loginErrorDialog(loginStatusInfo.getMessage());
}
});
I am using a global variables "GlobalVariables" in a separated class and I am try to use it in the following code but it is always gives me the error :
The method getApplication() is undefined for the type UploadPicture
I tried the following but still have error:
((GlobalVariables) this.getApplication()).set_FileUploading(false);
The qustion was already asked here but unfortunatlly all the answors didn't work with me and gave me same error! any suggestion please?
public class UploadPicture extends AsyncTask<Void, Long, Boolean> {
private DropboxAPI<?> mApi;
private String mPath;
private File mFile;
private long mFileLen;
private UploadRequest mRequest;
private Context mContext;
private String mErrorMsg;
private File outFiles;
public UploadPicture(Context context, DropboxAPI<?> api, String dropboxPath, File file) {
mContext = context.getApplicationContext();
mFileLen = file.length();
mApi = api;
mPath = dropboxPath;
mFile = file;
}
#Override
protected Boolean doInBackground(Void... params) {
try {
FileInputStream fis = new FileInputStream(mFile);
String path = mPath + outFiles.getName();
mRequest = mApi.putFileOverwriteRequest(path, fis, mFile.length(),
new ProgressListener() {
#Override
public long progressInterval() {
return 500;
}
#Override
public void onProgress(long bytes, long total) {
//publishProgress(bytes);
}
}
);
if (mRequest != null) {
mRequest.upload();
((GlobalVariables) UploadPicture.this.getApplication()).set_FileUploading(false);
return true;
}
} catch (DropboxUnlinkedException e) {
// This session wasn't authenticated properly or user unlinked
mErrorMsg = "This app wasn't authenticated properly.";
} catch (DropboxFileSizeException e) {
// File size too big to upload via the API
mErrorMsg = "This file is too big to upload";
} catch (DropboxPartialFileException e) {
// We canceled the operation
mErrorMsg = "Upload canceled";
} catch (DropboxServerException e) {
// Server-side exception. These are examples of what could happen,
// but we don't do anything special with them here.
if (e.error == DropboxServerException._401_UNAUTHORIZED) {
// Unauthorized, so we should unlink them. You may want to
// automatically log the user out in this case.
} else if (e.error == DropboxServerException._403_FORBIDDEN) {
// Not allowed to access this
} else if (e.error == DropboxServerException._404_NOT_FOUND) {
// path not found (or if it was the thumbnail, can't be
// thumbnailed)
} else if (e.error == DropboxServerException._507_INSUFFICIENT_STORAGE) {
// user is over quota
} else {
// Something else
}
// This gets the Dropbox error, translated into the user's language
mErrorMsg = e.body.userError;
if (mErrorMsg == null) {
mErrorMsg = e.body.error;
}
} catch (DropboxIOException e) {
// Happens all the time, probably want to retry automatically.
mErrorMsg = "Network error. Try again.";
} catch (DropboxParseException e) {
// Probably due to Dropbox server restarting, should retry
mErrorMsg = "Dropbox error. Try again.";
} catch (DropboxException e) {
// Unknown error
mErrorMsg = "Unknown error. Try again.";
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return false;
}
}
Edit: I am adding now my "VariableGlobales" calss:
public class GlobalVariables extends Application {
private Boolean _IsIOIORunning=false;
private Boolean _FileUploading=false;
public Boolean get_IsIOIORunning()
{
return _IsIOIORunning;
}
public void set_IsIOIORunning(Boolean _IsIOIORunning)
{
this._IsIOIORunning = _IsIOIORunning;
}
public Boolean get_FileUploading()
{
return _FileUploading;
}
public void set_FileUploading(Boolean _FileUploading)
{
this._FileUploading = _FileUploading;
}
It's normal UploadPicture doesn't extend GlobalVariables but it extend AsyncTask.
That it's my "GlobalVariables "
public class AppInfo extends Application {
private static Context context;
private static String user;
public void onCreate(){
super.onCreate();
AppInfo.context = getApplicationContext();
user = null;
}
public static Context getAppContext() {return AppInfo.context;}
public static String getUser() {return user;}
public static void setUser(String user) {AppInfo.user = user;}
}
And I call it everywhere like that:
AppInfo.getUser();
Edit:
GlobalVariables should use static method and variables:
public class GlobalVariables extends Application {
private static Boolean _IsIOIORunning=false;
private static Boolean _FileUploading=false;
public static Boolean get_IsIOIORunning() {
return _IsIOIORunning;
}
public static void set_IsIOIORunning(Boolean _IsIOIORunning) {
GlobalVariables._IsIOIORunning = _IsIOIORunning;
}
public static Boolean get_FileUploading(){
return _FileUploading;
}
public static void set_FileUploading(Boolean _FileUploading){
GlobalVariables._FileUploading = _FileUploading;
}
}
I am trying to use reflection to launch the appropriate method when the user inputs a string command. For instance, if the user inputs "go" in the terminal, the go() method of the Player class will be called by the process() method of the Command class.
However, I cannot get my code working and I get a NoSuchMethodException error that I do not know how to fix. The lines at the source of the problem are half-way through the Command class (complete classes reproduced at the bottom):
try {
Method method = pClass.getMethod(commandWord);
method.invoke(player, this);
}
catch (NoSuchMethodException err1) {
System.out.println("No such method");
}
Could anyone please guide me? I thank you in advance.
LC
Command class:
import java.util.ArrayList;
import java.lang.reflect.*;
public class Command
{
private String commandWord;
private String secondWord;
public Command(String firstWord, String secondWord)
{
commandWord = firstWord;
this.secondWord = secondWord;
}
public boolean process(Player player)
{
ArrayList<String> validCommands = new ArrayList<String>();
String methodName;
int index;
Class pClass = player.getClass();
Method[] methods = pClass.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
if (Modifier.isPublic(methods[i].getModifiers())) {
validCommands.add(methods[i].getName());
}
}
boolean wantToQuit = false;
if(commandWord == null) {
System.out.println("I don't know what you mean...");
return false;
}
if (commandWord.equals("help")) {
System.out.println("You are lost. You are alone. You wander");
System.out.println("around at the university.");
System.out.println();
System.out.println("Your command words are:");
for(String command: validCommands) {
System.out.print(command + " ");
}
System.out.println();
}
else if (commandWord.equals("quit")) {
wantToQuit = quit();
}
//THIS IS WHERE I GET THE ERROR
try {
Method method = pClass.getMethod(commandWord);
method.invoke(player, this);
}
catch (NoSuchMethodException err1) {
System.out.println("No such method");
}
catch (IllegalAccessException err2) {
System.out.println("Illegal access");
}
catch (InvocationTargetException err3) {
System.out.println("Illegal access");
}
return wantToQuit;
}
[...] //some methods omitted
}
Player class:
public class Player
{
private String name;
private Room currentRoom;
private ArrayList<Item> items;
Player (String name, Room startingRoom)
{
this.name = name;
items = new ArrayList<Item>();
this.currentRoom = startingRoom;
printWelcome();
}
public void engage()
{
[...]
}
public void trade(Command command)
{
[...] }
public void goRoom(Command command)
{
[...] }
public void search(Command command)
{
[...] }
public void takeItem(Command command)
{
[...] }
public void dropItem(Command command)
{
[...] }
public void lock(Command command)
{
[...] }
public void unlock(Command command)
{
[...]
}
}
Try this.
pClass.getMethod(commandWord, Command.class)
Seems to me the problem is that you're looking for methods with no
parameters while these methods all seem to have a parameter of type Command.
For more details see here:
Class.getMethod JavaDoc
Actually I programming a IM service (inherited google chat) by using smack API. But when i want to print buddy list and their presences, the compile mode show all presences unavailable, but in the debug mode it shows the real availability!
My code is ...
1- create connection
public boolean openConnection() {
ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration("talk.google.com", 5222, "mail.google.com");
this.connection = new XMPPConnection(connectionConfiguration);
try {
this.connection.connect();
} catch (XMPPException e) {
// TODO: Send Error Information To Programmer's Email Address
}
if(this.connection.isConnected()) {
this.roster = this.connection.getRoster();
this.roster.addRosterListener(new RosterListener() {
public void entriesAdded(Collection<String> addresses) {}
public void entriesDeleted(Collection<String> addresses) {}
public void entriesUpdated(Collection<String> addresses) {}
public void presenceChanged(Presence presence) {}
});
return true;
}
return false;
}
2- login
public boolean login(String jid, String password) {
try {
this.connection.login(jid, password, "smack");
} catch (XMPPException e) {
// TODO: Send Error Information To Programmer's Email Address
}
if(this.connection.isAuthenticated()) return true;
return false;
}
3- buddy list
public void buddiesList() {
Collection<RosterEntry> rosterEntries = this.roster.getEntries();
for(RosterEntry rosterEntry: rosterEntries) {
System.out.println(rosterEntry.username() + " === " + this.roster.getPresence(rosterEntry.getUser()));
}
}
4- implementation
public static void main(String args[]) {
IMService imService = new IMService();
imService.openConnection();
imService.login("google account", "password");
imService.buddiesList();
}
Your RosterListener doesn't do anything. This is where you have to put code to update your roster when things like presence messages are received.
The presence you are retrieving is a snapshot in time of what the state was when it was created. To keep the state current, you have to actually code the RosterListener. This is clearly stated in the Javadoc for the getPresence() method.
Adding a Listener to your roster could be better:
https://www.igniterealtime.org/builds/smack/docs/latest/documentation/extensions/rosterexchange.html
I have an application in Eclipse RCP where I want to fire a function called 'LogOutUser()' if the user leaves his/ her application idle for, say, five minutes.
How do I go about doing this?
I don't know if the RCP framework supports this internally. However, I wrote my own "helper" class, which is a singleton client session manager. Eclipse won't know natively how you connect to your datasource. In my case I am connecting using EJB3 calls and listening to JMS queues and topics.
My class was written to detect when the datasource or "server" went down. It would also reconnect when the server came up. The server inactivity is detected by listening to heartbeat DTO's sent by the server. This feedback is useful to present to the user. I have adapted this class to cater for user interface inactivity.
The class is quite simple. It is a singleton, so it can be called simply at any point in your client-side RCP application. The heartbeat uses an observer and so you will have to add a HeartBeatEventListener to hook into this functionality. You can adapt the class to do the same for the user interface inactivity. However, I have just provided an updateUserInterfaceActivity() method which you must call when there is user activity. Perhaps this can be hooked into a global mouse and a global keyboard event handler.
I have also added a TrayItem to update the user...
Here is the class:
package com.kingsleywebb.clientsessionmanagement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolTip;
import org.eclipse.swt.widgets.TrayItem;
import com.kingsleywebb.clientsessionmanagement.entity.HeartbeatDTO;
public class ClientSessionManagement implements HeartbeatEventListener {
private static final Image IMG_CONNECTED = null; // Set this to a "connected image"
private static final Image IMG_DISCONNECTED = null; // Set this to a "disconnected image"
private static final long CONNECTION_INACTIVITY_TIME_MS = 30000L; // 30 seconds
private static final long USER_INTERFACE_INACTIVITY_TIME_MS = 300000L; // 5 minutes
private static final Log LOG = LogFactory.getLog(ClientSessionManagement.class);
private static ClientSessionManagement theInstance = null;
private static long connectionTimestamp = 0;
private static long userInterfaceActivityTimestamp = 0;
private synchronized static void createInstance() {
if (theInstance == null) {
theInstance = new ClientSessionManagement();
}
}
public static ClientSessionManagement getInstance() {
if (theInstance == null) {
createInstance();
}
return theInstance;
}
private ClientSessionManagement() {
this.connectionListenerList = new ArrayList<ConnectionListener>();
updateConnectionTimestamp();
Cron cron = new Cron();
Thread cronThread = new Thread(cron);
cronThread.start();
}
private boolean connected = true;
private ToolTip toolTipConnected;
private ToolTip toolTipDisconnected;
private TrayItem trayItem = null;
private String appName = null;
private String version = null;
private String whiteLabel = null;
private String userName = null;
private String deskName = null;
private String serverName = null;
private String userMnemonic = null;
private MenuItem miShowPopups;
private MenuItem miSoundBeep;
private List<ConnectionListener> connectionListenerList;
private void updateConnectionTimestamp() {
ClientSessionManagement.connectionTimestamp = System.currentTimeMillis();
}
private synchronized long getLastHeartbeatInMsAgo() {
return System.currentTimeMillis() - ClientSessionManagement.connectionTimestamp;
}
public synchronized void updateHeartbeat() {
updateConnectionTimestamp();
}
public synchronized void checkHeartbeatInterval() {
if (getLastHeartbeatInMsAgo() < CONNECTION_INACTIVITY_TIME_MS) {
showConnected();
}
else {
showDisconnected();
}
}
private void updateUserInterfaceActivityTimestamp() {
ClientSessionManagement.userInterfaceActivityTimestamp = System.currentTimeMillis();
}
private synchronized long getLastUserInterfaceActivityInMsAgo() {
return System.currentTimeMillis() - ClientSessionManagement.userInterfaceActivityTimestamp;
}
public synchronized void updateUserInterfaceActivity() {
updateUserInterfaceActivityTimestamp();
}
public synchronized void checkUserInterfaceActivityInterval() {
if (getLastUserInterfaceActivityInMsAgo() > USER_INTERFACE_INACTIVITY_TIME_MS) {
logoutUser();
}
}
private void logoutUser() {
// Implement logout functionality here
}
private void showConnected() {
if (!connected) {
connected = true;
Display.getDefault().asyncExec(new Runnable() {
public void run() {
// Update icon
if (trayItem != null) {
trayItem.setImage(ClientSessionManagement.IMG_CONNECTED);
trayItem.getToolTip().setVisible(false);
trayItem.setToolTip(toolTipConnected);
trayItem.getToolTip().setVisible(true);
}
// Update hover tooltip
updateHoverTooltip();
}
});
notifyConnectionListeners();
}
}
private void showDisconnected() {
if (connected) {
connected = false;
Display.getDefault().asyncExec(new Runnable() {
public void run() {
// Update icon
if (trayItem != null) {
trayItem.setImage(ClientSessionManagement.IMG_DISCONNECTED);
trayItem.getToolTip().setVisible(false);
trayItem.setToolTip(toolTipDisconnected);
trayItem.getToolTip().setVisible(true);
}
// Update hover tooltip
updateHoverTooltip();
}
});
notifyConnectionListeners();
}
}
private void updateHoverTooltip() {
if (trayItem != null) {
// Application info
String applicationInfo = null;
if (appName != null && version != null && whiteLabel != null) {
// appName* | version | whitelabel
applicationInfo = " Application: " + " " + appName + " " + version + " [" + whiteLabel + "]\r\n";
}
// User info
String userInfo = null;
if (userName != null && deskName != null && serverName != null) {
userInfo = " User: " + " " + userName + " (" + deskName + ") on " + serverName + "\r\n";
}
// Connection info
String connectionInfo = connected ? " Server Connected" : " SERVER DISCONNECTED!!!";
String status = connectionInfo + "\r\n\r\n" + (applicationInfo != null ? applicationInfo : "") +
(userInfo != null ? userInfo : "");
trayItem.setToolTipText(status);
LOG.info(status);
}
}
public void setTrayItem(Shell shell, TrayItem trayItem) {
this.trayItem = trayItem;
/*
* Property files to persist these settings - removed for simplicity
*
* final WorkstationProperties p = WorkstationProperties.getInstance();
* boolean showNotificationPopups = !"No".equalsIgnoreCase(p.getProperty("notifications.showNotificationPopups"));
* boolean soundNotificationBeep = !"No".equalsIgnoreCase(p.getProperty("notifications.soundNotificationBeep"));
*/
boolean showNotificationPopups = true;
boolean soundNotificationBeep = true;
final Menu menu = new Menu (shell, SWT.POP_UP);
miShowPopups = new MenuItem (menu, SWT.CHECK);
miShowPopups.setSelection(showNotificationPopups);
miShowPopups.setText("Show Notification Popups");
miShowPopups.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event event) {
LOG.info("notifications.showNotificationPopups = " + miShowPopups.getSelection());
// Property files to persist these settings - removed for simplicity
//p.setProperty("notifications.showNotificationPopups", miShowPopups.getSelection() ? "Yes" : "No");
}
});
miSoundBeep = new MenuItem (menu, SWT.CHECK);
miSoundBeep.setSelection(soundNotificationBeep);
miSoundBeep.setText("Play Notification Beep");
miSoundBeep.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event event) {
LOG.info("notifications.soundNotificationBeep = " + miSoundBeep.getSelection());
// Property files to persist these settings - removed for simplicity
//p.setProperty("notifications.soundNotificationBeep", miSoundBeep.getSelection() ? "Yes" : "No");
}
});
this.trayItem.addListener (SWT.MenuDetect, new Listener () {
public void handleEvent (Event event) {
menu.setVisible (true);
}
});
toolTipConnected = new ToolTip(shell, SWT.BALLOON);
toolTipConnected.setText((appName != null ? appName : "<Application Name>") + " Status");
toolTipConnected.setMessage("Connected to server.");
toolTipConnected.setLocation(600, 600);
toolTipConnected.setVisible(false);
toolTipDisconnected = new ToolTip(shell, SWT.ICON_WARNING);
toolTipDisconnected.setText((appName != null ? appName : "<Application Name>") + " Status");
toolTipDisconnected.setMessage("DISCONNECTED from server.");
toolTipDisconnected.setLocation(500, 500);
toolTipDisconnected.setVisible(false);
this.trayItem.setToolTip(toolTipConnected);
}
public boolean isShowPopups() {
return miShowPopups.getSelection();
}
public boolean isSoundBeep() {
return miSoundBeep.getSelection();
}
public void setAppName(String appName) {
this.appName = appName;
}
public void setVersion(String version) {
this.version = version;
}
public void setWhiteLabel(String whiteLabel) {
this.whiteLabel = whiteLabel;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setDeskName(String deskName) {
this.deskName = deskName;
}
public void setServerName(String serverName) {
this.serverName = serverName;
updateHoverTooltip();
}
public String getUserMnemonic() {
return userMnemonic;
}
public void setUserMnemonic(String userMnemonic) {
this.userMnemonic = userMnemonic;
}
public void heartbeatArrived(HeartbeatDTO heartbeatDTO) {
updateHeartbeat();
}
public boolean isConnected() {
return connected;
}
public boolean addConnectionListener(ConnectionListener connectionListener) {
return connectionListenerList.add(connectionListener);
}
public boolean removeConnectionListener(ConnectionListener connectionListener) {
return connectionListenerList.remove(connectionListener);
}
public void notifyConnectionListeners() {
for (Iterator<ConnectionListener> i = connectionListenerList.iterator(); i.hasNext();) {
ConnectionListener connectionListener = i.next();
if (connected) {
connectionListener.connected();
}
else {
connectionListener.disconnected();
}
}
}
/**
*
* #author Kingsley Webb
*
* Check heartbeat interval periodically display warning to user accordingly.
*/
class Cron implements Runnable {
public void run() {
// Wait 15s extra before 1st check
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
LOG.error(e);
}
while (true) {
// Check every 5s - increase for better performance, but you get the idea...
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
LOG.error(e);
}
checkHeartbeatInterval();
checkUserInterfaceActivityInterval();
}
}
}
}
Some other supporting classes:
package com.kingsleywebb.clientsessionmanagement;
public interface ConnectionListener {
public void connected();
public void disconnected();
}
package com.kingsleywebb.clientsessionmanagement;
import com.kingsleywebb.clientsessionmanagement.entity.HeartbeatDTO;
public interface HeartbeatEventListener {
public void heartbeatArrived(HeartbeatDTO heartbeatDTO);
}
If you take a look in the bundle org.eclipse.ui.ide.application there is a class org.eclipse.ui.internal.ide.application.IDEIdleHelper which tries to perform gc after a interval of user inactivity. Probably you can reuse the logic that detects the inactivity
I usually use a Display.addFilter(eventType, listener) for the event types that should keep the session alive combined with a Display.timerExec(milliseconds, runnable) that is run periodically and tests for the last interesting event.
I use milliseconds = 5000 so the period is 5 min up to 5 min 5 sec before the user is logged out (or whatever...). Also I listener for the SWT event types (in 3.7) KeyDown, KeyUp, MouseDown, MouseUp, MouseVerticalWheel, MouseHorizontalWheel MouseDoubleClick, Touch, Gesture, Activate, Iconify, Deiconify, Move, and Resize.