Search in sources :

Example 1 with SessionListener

use of org.netxms.client.SessionListener in project netxms by netxms.

the class AlarmNotifier method init.

/**
 * Initialize alarm notifier
 */
public static void init(NXCSession session) {
    AlarmNotifier.session = session;
    ps = Activator.getDefault().getPreferenceStore();
    workspaceUrl = Platform.getInstanceLocation().getURL();
    checkSounds();
    lastReminderTime = System.currentTimeMillis();
    try {
        Map<Long, Alarm> alarms = session.getAlarms();
        for (Alarm a : alarms.values()) {
            alarmStates.put(a.getId(), a.getState());
            if (a.getState() == Alarm.STATE_OUTSTANDING)
                outstandingAlarms++;
        }
    } catch (Exception e) {
    }
    listener = new SessionListener() {

        @Override
        public void notificationHandler(SessionNotification n) {
            if ((n.getCode() == SessionNotification.NEW_ALARM) || (n.getCode() == SessionNotification.ALARM_CHANGED)) {
                processNewAlarm((Alarm) n.getObject());
            } else if ((n.getCode() == SessionNotification.ALARM_TERMINATED) || (n.getCode() == SessionNotification.ALARM_DELETED)) {
                Alarm a = (Alarm) n.getObject();
                Integer state = alarmStates.get(a.getId());
                if (state != null) {
                    if (state == Alarm.STATE_OUTSTANDING)
                        outstandingAlarms--;
                    alarmStates.remove(a.getId());
                }
            } else if (n.getCode() == SessionNotification.MULTIPLE_ALARMS_RESOLVED) {
                BulkAlarmStateChangeData d = (BulkAlarmStateChangeData) n.getObject();
                for (Long id : d.getAlarms()) {
                    Integer state = alarmStates.get(id);
                    if (state != null) {
                        if (state == Alarm.STATE_OUTSTANDING) {
                            outstandingAlarms--;
                        }
                    }
                    alarmStates.put(id, Alarm.STATE_RESOLVED);
                }
            } else if (n.getCode() == SessionNotification.MULTIPLE_ALARMS_TERMINATED) {
                BulkAlarmStateChangeData d = (BulkAlarmStateChangeData) n.getObject();
                for (Long id : d.getAlarms()) {
                    Integer state = alarmStates.get(id);
                    if (state != null) {
                        if (state == Alarm.STATE_OUTSTANDING)
                            outstandingAlarms--;
                        alarmStates.remove(id);
                    }
                }
            }
        }
    };
    session.addListener(listener);
    Thread reminderThread = new Thread(new Runnable() {

        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                }
                IPreferenceStore ps = Activator.getDefault().getPreferenceStore();
                long currTime = System.currentTimeMillis();
                if (// $NON-NLS-1$
                ps.getBoolean("OUTSTANDING_ALARMS_REMINDER") && (outstandingAlarms > 0) && (lastReminderTime + 300000 <= currTime)) {
                    Display.getDefault().syncExec(new Runnable() {

                        @Override
                        public void run() {
                            soundQueue.offer(SEVERITY_TEXT[SEVERITY_TEXT.length - 1]);
                            AlarmReminderDialog dlg = new AlarmReminderDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
                            dlg.open();
                        }
                    });
                    lastReminderTime = currTime;
                }
            }
        }
    }, // $NON-NLS-1$
    "AlarmReminderThread");
    reminderThread.setDaemon(true);
    reminderThread.start();
    Thread playerThread = new Thread(new Runnable() {

        @Override
        public void run() {
            while (true) {
                String soundId;
                try {
                    soundId = soundQueue.take();
                } catch (InterruptedException e) {
                    continue;
                }
                Clip sound = null;
                try {
                    String fileName = getSoundAndDownloadIfRequired(soundId);
                    if (fileName != null) {
                        sound = (Clip) AudioSystem.getLine(new Line.Info(Clip.class));
                        sound.open(AudioSystem.getAudioInputStream(new File(workspaceUrl.getPath(), fileName).getAbsoluteFile()));
                        sound.start();
                        while (!sound.isRunning()) Thread.sleep(10);
                        while (sound.isRunning()) {
                            Thread.sleep(10);
                        }
                    }
                } catch (Exception e) {
                    // $NON-NLS-1$
                    Activator.logError("Exception in alarm sound player", e);
                } finally {
                    if ((sound != null) && sound.isOpen())
                        sound.close();
                }
            }
        }
    }, "AlarmSoundPlayer");
    playerThread.setDaemon(true);
    playerThread.start();
}
Also used : BulkAlarmStateChangeData(org.netxms.client.events.BulkAlarmStateChangeData) StatusDisplayInfo(org.netxms.ui.eclipse.console.resources.StatusDisplayInfo) IOException(java.io.IOException) Clip(javax.sound.sampled.Clip) AlarmReminderDialog(org.netxms.ui.eclipse.alarmviewer.dialogs.AlarmReminderDialog) Alarm(org.netxms.client.events.Alarm) SessionListener(org.netxms.client.SessionListener) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) SessionNotification(org.netxms.client.SessionNotification) File(java.io.File)

Example 2 with SessionListener

use of org.netxms.client.SessionListener in project netxms by netxms.

the class ApplicationWorkbenchAdvisor method postStartup.

/* (non-Javadoc)
	 * @see org.eclipse.ui.application.WorkbenchAdvisor#postStartup()
	 */
@Override
public void postStartup() {
    super.postStartup();
    final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
    shell.addShellListener(new ShellAdapter() {

        public void shellIconified(ShellEvent e) {
            if (// $NON-NLS-1$
            Activator.getDefault().getPreferenceStore().getBoolean("HIDE_WHEN_MINIMIZED"))
                shell.setVisible(false);
        }
    });
    final NXCSession session = (NXCSession) ConsoleSharedData.getSession();
    session.addListener(new SessionListener() {

        @Override
        public void notificationHandler(final SessionNotification n) {
            if ((n.getCode() == SessionNotification.CONNECTION_BROKEN) || (n.getCode() == SessionNotification.SERVER_SHUTDOWN) || (n.getCode() == SessionNotification.SESSION_KILLED)) {
                PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        String productName = BrandingManager.getInstance().getProductName();
                        MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), Messages.get().ApplicationWorkbenchAdvisor_CommunicationError, ((n.getCode() == SessionNotification.CONNECTION_BROKEN) ? String.format(Messages.get().ApplicationWorkbenchAdvisor_ConnectionLostMessage, productName) : ((n.getCode() == SessionNotification.SESSION_KILLED) ? Messages.get().ApplicationWorkbenchAdvisor_SessionTerminated : String.format(Messages.get().ApplicationWorkbenchAdvisor_ServerShutdownMessage, productName))) + Messages.get().ApplicationWorkbenchAdvisor_OKToCloseMessage);
                        PlatformUI.getWorkbench().getActiveWorkbenchWindow().close();
                    }
                });
            }
        }
    });
}
Also used : Shell(org.eclipse.swt.widgets.Shell) ShellAdapter(org.eclipse.swt.events.ShellAdapter) NXCSession(org.netxms.client.NXCSession) ShellEvent(org.eclipse.swt.events.ShellEvent) SessionListener(org.netxms.client.SessionListener) SessionNotification(org.netxms.client.SessionNotification)

Example 3 with SessionListener

use of org.netxms.client.SessionListener in project netxms by netxms.

the class SessionStore method registerSession.

/**
 * @param s
 */
public synchronized SessionToken registerSession(final NXCSession session) {
    if (sessionManager == null) {
        sessionManager = new Thread(new Runnable() {

            @Override
            public void run() {
                sessionManagerThread();
            }
        }, "Session Manager");
        sessionManager.setDaemon(true);
        sessionManager.start();
    }
    final SessionToken token = new SessionToken(session);
    sessions.put(token.getGuid(), token);
    session.addListener(new SessionListener() {

        @Override
        public void notificationHandler(SessionNotification n) {
            if ((n.getCode() == SessionNotification.CONNECTION_BROKEN) || (n.getCode() == SessionNotification.SERVER_SHUTDOWN) || (n.getCode() == SessionNotification.SESSION_KILLED)) {
                log.info("Received disconnect notification for session " + token.getGuid());
                session.disconnect();
                unregisterSession(token.getGuid());
            }
        }
    });
    log.info("Session " + token.getGuid() + " registered");
    return token;
}
Also used : SessionListener(org.netxms.client.SessionListener) SessionNotification(org.netxms.client.SessionNotification)

Example 4 with SessionListener

use of org.netxms.client.SessionListener in project netxms by netxms.

the class EventProcessingPolicyEditor method createPartControl.

/*
    * (non-Javadoc)
    * 
    * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
    */
@Override
public void createPartControl(Composite parent) {
    session = (NXCSession) ConsoleSharedData.getSession();
    IDialogSettings settings = Activator.getDefault().getDialogSettings();
    // $NON-NLS-1$
    filterEnabled = settings.getBoolean("EventProcessingPolicyEditor.filterEnabled");
    // Initiate loading of required plugins if they was not loaded yet
    try {
        // $NON-NLS-1$
        Platform.getAdapterManager().loadAdapter(new EventTemplate(0), "org.eclipse.ui.model.IWorkbenchAdapter");
        // $NON-NLS-1$
        Platform.getAdapterManager().loadAdapter(new ServerAction(0), "org.eclipse.ui.model.IWorkbenchAdapter");
        // $NON-NLS-1$
        Platform.getAdapterManager().loadAdapter(session.getTopLevelObjects()[0], "org.eclipse.ui.model.IWorkbenchAdapter");
    } catch (Exception e) {
    }
    // $NON-NLS-1$
    imageStop = Activator.getImageDescriptor("icons/stop.png").createImage();
    // $NON-NLS-1$
    imageAlarm = Activator.getImageDescriptor("icons/alarm.png").createImage();
    // $NON-NLS-1$
    imageSituation = Activator.getImageDescriptor("icons/situation.gif").createImage();
    // $NON-NLS-1$
    imageExecute = Activator.getImageDescriptor("icons/execute.png").createImage();
    // $NON-NLS-1$
    imageTerminate = Activator.getImageDescriptor("icons/terminate.png").createImage();
    imageCollapse = SharedIcons.COLLAPSE.createImage();
    imageExpand = SharedIcons.EXPAND.createImage();
    imageEdit = SharedIcons.EDIT.createImage();
    parent.setLayout(new FormLayout());
    // Create filter area
    filterControl = new FilterText(parent, SWT.NONE);
    filterControl.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent e) {
            onFilterModify();
        }
    });
    filterControl.setCloseAction(new Action() {

        @Override
        public void run() {
            enableFilter(false);
        }
    });
    scroller = new ScrolledComposite(parent, SWT.V_SCROLL);
    dataArea = new Composite(scroller, SWT.NONE);
    GridLayout layout = new GridLayout();
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    layout.verticalSpacing = 0;
    dataArea.setLayout(layout);
    dataArea.setBackground(BACKGROUND_COLOR);
    scroller.setContent(dataArea);
    scroller.setExpandVertical(true);
    scroller.setExpandHorizontal(true);
    scroller.addControlListener(new ControlAdapter() {

        public void controlResized(ControlEvent e) {
            Rectangle r = scroller.getClientArea();
            scroller.setMinSize(dataArea.computeSize(r.width, SWT.DEFAULT));
        }
    });
    // Setup layout
    FormData fd = new FormData();
    fd.left = new FormAttachment(0, 0);
    fd.top = new FormAttachment(filterControl);
    fd.right = new FormAttachment(100, 0);
    fd.bottom = new FormAttachment(100, 0);
    scroller.setLayoutData(fd);
    fd = new FormData();
    fd.left = new FormAttachment(0, 0);
    fd.top = new FormAttachment(0, 0);
    fd.right = new FormAttachment(100, 0);
    filterControl.setLayoutData(fd);
    normalFont = JFaceResources.getDefaultFont();
    boldFont = JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT);
    sessionListener = new SessionListener() {

        @Override
        public void notificationHandler(SessionNotification n) {
            processSessionNotification(n);
        }
    };
    session.addListener(sessionListener);
    selection = new TreeSet<RuleEditor>(new Comparator<RuleEditor>() {

        @Override
        public int compare(RuleEditor arg0, RuleEditor arg1) {
            return arg0.getRuleNumber() - arg1.getRuleNumber();
        }
    });
    createActions();
    contributeToActionBars();
    openEventProcessingPolicy();
    activateContext();
    // Set initial focus to filter input line
    if (filterEnabled)
        filterControl.setFocus();
    else
        // Will hide filter area correctly
        enableFilter(false);
}
Also used : FormLayout(org.eclipse.swt.layout.FormLayout) FormData(org.eclipse.swt.layout.FormData) ServerAction(org.netxms.client.ServerAction) Action(org.eclipse.jface.action.Action) ModifyListener(org.eclipse.swt.events.ModifyListener) Composite(org.eclipse.swt.widgets.Composite) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) ControlAdapter(org.eclipse.swt.events.ControlAdapter) EventTemplate(org.netxms.client.events.EventTemplate) Rectangle(org.eclipse.swt.graphics.Rectangle) Comparator(java.util.Comparator) ModifyEvent(org.eclipse.swt.events.ModifyEvent) GridLayout(org.eclipse.swt.layout.GridLayout) IDialogSettings(org.eclipse.jface.dialogs.IDialogSettings) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) FilterText(org.netxms.ui.eclipse.widgets.FilterText) SessionListener(org.netxms.client.SessionListener) RuleEditor(org.netxms.ui.eclipse.epp.widgets.RuleEditor) ControlEvent(org.eclipse.swt.events.ControlEvent) ServerAction(org.netxms.client.ServerAction) SessionNotification(org.netxms.client.SessionNotification) FormAttachment(org.eclipse.swt.layout.FormAttachment)

Example 5 with SessionListener

use of org.netxms.client.SessionListener in project netxms by netxms.

the class ApplicationWorkbenchAdvisor method doLogin.

/**
 * Show login dialog and perform login
 */
private void doLogin() {
    boolean success = false;
    final Properties properties = new AppPropertiesLoader().load();
    String password = "";
    // $NON-NLS-1$
    boolean autoLogin = (Application.getParameter("auto") != null);
    String ssoTicket = Application.getParameter("ticket");
    if (ssoTicket != null) {
        autoLogin = true;
        // $NON-NLS-1$
        String server = Application.getParameter("server");
        if (server == null)
            // $NON-NLS-1$ //$NON-NLS-2$
            server = properties.getProperty("server", "127.0.0.1");
        success = connectToServer(server, null, ssoTicket);
    } else if (autoLogin) {
        // $NON-NLS-1$
        String server = Application.getParameter("server");
        if (server == null)
            // $NON-NLS-1$ //$NON-NLS-2$
            server = properties.getProperty("server", "127.0.0.1");
        // $NON-NLS-1$
        String login = Application.getParameter("login");
        if (login == null)
            login = "guest";
        // $NON-NLS-1$
        password = Application.getParameter("password");
        if (password == null)
            password = "";
        success = connectToServer(server, login, password);
    }
    if (!autoLogin || !success) {
        Window loginDialog;
        do {
            loginDialog = BrandingManager.getInstance().getLoginForm(Display.getCurrent().getActiveShell(), properties);
            if (loginDialog.open() != Window.OK)
                continue;
            password = ((LoginForm) loginDialog).getPassword();
            success = connectToServer(// $NON-NLS-1$ //$NON-NLS-2$
            properties.getProperty("server", "127.0.0.1"), ((LoginForm) loginDialog).getLogin(), password);
        } while (!success);
    }
    if (success) {
        final NXCSession session = (NXCSession) RWT.getUISession().getAttribute(ConsoleSharedData.ATTRIBUTE_SESSION);
        final Display display = Display.getCurrent();
        session.addListener(new SessionListener() {

            private final Object MONITOR = new Object();

            @Override
            public void notificationHandler(final SessionNotification n) {
                if ((n.getCode() == SessionNotification.CONNECTION_BROKEN) || (n.getCode() == SessionNotification.SERVER_SHUTDOWN) || (n.getCode() == SessionNotification.SESSION_KILLED)) {
                    display.asyncExec(new Runnable() {

                        @Override
                        public void run() {
                            RWT.getUISession().setAttribute("NoPageReload", Boolean.TRUE);
                            PlatformUI.getWorkbench().close();
                            String productName = BrandingManager.getInstance().getProductName();
                            String msg = (n.getCode() == SessionNotification.CONNECTION_BROKEN) ? String.format(Messages.get().ApplicationWorkbenchAdvisor_ConnectionLostMessage, productName) : ((n.getCode() == SessionNotification.SESSION_KILLED) ? "Communication session was terminated by system administrator" : String.format(Messages.get().ApplicationWorkbenchAdvisor_ServerShutdownMessage, productName));
                            JavaScriptExecutor executor = RWT.getClient().getService(JavaScriptExecutor.class);
                            if (executor != null)
                                executor.execute("document.body.innerHTML='" + "<div style=\"position: absolute; top: 0; left: 0; width: 100%; height: 100%; background-image: none; background-color: white;\">" + "<p style=\"margin-top: 30px; margin-left: 30px; color: #800000; font-family: \"Segoe UI\", Verdana, Arial, sans-serif; font-weight: bold; font-size: 2em;\">" + msg + "</p><br/>" + "<button style=\"margin-left: 30px\" onclick=\"location.reload(true)\">RELOAD</button>" + "</div>';");
                            synchronized (MONITOR) {
                                MONITOR.notifyAll();
                            }
                        }
                    });
                    synchronized (MONITOR) {
                        try {
                            MONITOR.wait(5000);
                        } catch (InterruptedException e) {
                        }
                    }
                    ((UISessionImpl) RWT.getUISession(display)).shutdown();
                }
            }
        });
        try {
            RWT.getSettingStore().loadById(session.getUserName() + "@" + session.getServerId());
        } catch (IOException e) {
        }
        // Suggest user to change password if it is expired
        if (session.isPasswordExpired()) {
            final PasswordExpiredDialog dlg = new PasswordExpiredDialog(null, session.getGraceLogins());
            while (true) {
                if (dlg.open() != Window.OK)
                    return;
                final String currentPassword = password;
                IRunnableWithProgress job = new IRunnableWithProgress() {

                    @Override
                    public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                        try {
                            NXCSession session = (NXCSession) RWT.getUISession(display).getAttribute(ConsoleSharedData.ATTRIBUTE_SESSION);
                            session.setUserPassword(session.getUserId(), dlg.getPassword(), currentPassword);
                        } catch (Exception e) {
                            throw new InvocationTargetException(e);
                        } finally {
                            monitor.done();
                        }
                    }
                };
                try {
                    ProgressMonitorDialog pd = new ProgressMonitorDialog(null);
                    pd.run(false, false, job);
                    MessageDialog.openInformation(null, Messages.get().ApplicationWorkbenchWindowAdvisor_Information, Messages.get().ApplicationWorkbenchWindowAdvisor_PasswordChanged);
                    return;
                } catch (InvocationTargetException e) {
                    MessageDialog.openError(null, Messages.get().ApplicationWorkbenchWindowAdvisor_Error, Messages.get().ApplicationWorkbenchWindowAdvisor_CannotChangePswd + e.getCause().getLocalizedMessage());
                } catch (InterruptedException e) {
                    MessageDialog.openError(null, Messages.get().ApplicationWorkbenchWindowAdvisor_Exception, e.toString());
                }
            }
        }
    }
}
Also used : Window(org.eclipse.jface.window.Window) NXCSession(org.netxms.client.NXCSession) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) JavaScriptExecutor(org.eclipse.rap.rwt.client.service.JavaScriptExecutor) IOException(java.io.IOException) Properties(java.util.Properties) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) PasswordExpiredDialog(org.netxms.ui.eclipse.console.dialogs.PasswordExpiredDialog) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) SessionListener(org.netxms.client.SessionListener) LoginForm(org.netxms.ui.eclipse.console.api.LoginForm) SessionNotification(org.netxms.client.SessionNotification) Display(org.eclipse.swt.widgets.Display)

Aggregations

SessionListener (org.netxms.client.SessionListener)18 SessionNotification (org.netxms.client.SessionNotification)18 ArrayContentProvider (org.eclipse.jface.viewers.ArrayContentProvider)6 DisposeEvent (org.eclipse.swt.events.DisposeEvent)6 DisposeListener (org.eclipse.swt.events.DisposeListener)6 SortableTableViewer (org.netxms.ui.eclipse.widgets.SortableTableViewer)6 DoubleClickEvent (org.eclipse.jface.viewers.DoubleClickEvent)5 IDoubleClickListener (org.eclipse.jface.viewers.IDoubleClickListener)5 ISelectionChangedListener (org.eclipse.jface.viewers.ISelectionChangedListener)5 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)5 SelectionChangedEvent (org.eclipse.jface.viewers.SelectionChangedEvent)5 IDialogSettings (org.eclipse.jface.dialogs.IDialogSettings)4 Composite (org.eclipse.swt.widgets.Composite)4 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)3 GridLayout (org.eclipse.swt.layout.GridLayout)3 NXCSession (org.netxms.client.NXCSession)3 AbstractObject (org.netxms.client.objects.AbstractObject)3 IOException (java.io.IOException)2 Action (org.eclipse.jface.action.Action)2 ModifyEvent (org.eclipse.swt.events.ModifyEvent)2