069b33ffc5657c6ce996a337a4fd3664d0a5f5be
[public/netxms.git] / webui / webapp / Core / src / org / netxms / ui / eclipse / console / ApplicationWorkbenchAdvisor.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2010 Victor Kirhenshtein
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 package org.netxms.ui.eclipse.console;
20
21 import java.io.IOException;
22 import java.lang.reflect.InvocationTargetException;
23 import java.util.Properties;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.jface.dialogs.MessageDialog;
26 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
27 import org.eclipse.jface.operation.IRunnableWithProgress;
28 import org.eclipse.jface.window.Window;
29 import org.eclipse.rap.rwt.RWT;
30 import org.eclipse.swt.widgets.Display;
31 import org.eclipse.ui.PlatformUI;
32 import org.eclipse.ui.application.IWorkbenchConfigurer;
33 import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
34 import org.eclipse.ui.application.WorkbenchAdvisor;
35 import org.eclipse.ui.application.WorkbenchWindowAdvisor;
36 import org.eclipse.ui.model.ContributionComparator;
37 import org.eclipse.ui.model.IContributionService;
38 import org.netxms.client.NXCSession;
39 import org.netxms.client.SessionListener;
40 import org.netxms.client.SessionNotification;
41 import org.netxms.ui.eclipse.console.api.LoginForm;
42 import org.netxms.ui.eclipse.console.dialogs.PasswordExpiredDialog;
43 import org.netxms.ui.eclipse.jobs.LoginJob;
44 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
45
46 /**
47 * Workbench advisor for NetXMS console application
48 */
49 public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor
50 {
51 private static final String PERSPECTIVE_ID = "org.netxms.ui.eclipse.console.ManagementPerspective"; //$NON-NLS-1$
52
53 /* (non-Javadoc)
54 * @see org.eclipse.ui.application.WorkbenchAdvisor#createWorkbenchWindowAdvisor(org.eclipse.ui.application.IWorkbenchWindowConfigurer)
55 */
56 public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer)
57 {
58 return new ApplicationWorkbenchWindowAdvisor(configurer);
59 }
60
61 /* (non-Javadoc)
62 * @see org.eclipse.ui.application.WorkbenchAdvisor#getInitialWindowPerspectiveId()
63 */
64 public String getInitialWindowPerspectiveId()
65 {
66 String p = BrandingManager.getInstance().getDefaultPerspective();
67 return (p != null) ? p : PERSPECTIVE_ID;
68 }
69
70 /* (non-Javadoc)
71 * @see org.eclipse.ui.application.WorkbenchAdvisor#initialize(org.eclipse.ui.application.IWorkbenchConfigurer)
72 */
73 @Override
74 public void initialize(IWorkbenchConfigurer configurer)
75 {
76 super.initialize(configurer);
77 configurer.setSaveAndRestore(true);
78 }
79
80 /* (non-Javadoc)
81 * @see org.eclipse.ui.application.WorkbenchAdvisor#openWindows()
82 */
83 @Override
84 public boolean openWindows()
85 {
86 doLogin();
87 return super.openWindows();
88 }
89
90 /* (non-Javadoc)
91 * @see org.eclipse.ui.application.WorkbenchAdvisor#getComparatorFor(java.lang.String)
92 */
93 @Override
94 public ContributionComparator getComparatorFor(String contributionType)
95 {
96 if (contributionType.equals(IContributionService.TYPE_PROPERTY))
97 return new ExtendedContributionComparator();
98 return super.getComparatorFor(contributionType);
99 }
100
101 /**
102 * Connect to NetXMS server
103 *
104 * @param server
105 * @param login
106 * @param password
107 * @return
108 */
109 private boolean connectToServer(String server, String login, String password)
110 {
111 boolean success = false;
112 try
113 {
114 LoginJob job = new LoginJob(Display.getCurrent(), server, login, false, false);
115 job.setPassword(password);
116 ProgressMonitorDialog pd = new ProgressMonitorDialog(null);
117 pd.run(false, false, job);
118 success = true;
119 }
120 catch(InvocationTargetException e)
121 {
122 e.printStackTrace();
123 MessageDialog.openError(null, Messages.get().ApplicationWorkbenchWindowAdvisor_ConnectionError, e.getCause().getLocalizedMessage());
124 }
125 catch(Exception e)
126 {
127 e.printStackTrace();
128 MessageDialog.openError(null, Messages.get().ApplicationWorkbenchWindowAdvisor_Exception, e.toString());
129 }
130 return success;
131 }
132
133 /**
134 * Show login dialog and perform login
135 */
136 private void doLogin()
137 {
138 boolean success = false;
139
140 final Properties properties = new AppPropertiesLoader().load();
141
142 String password = "";
143 boolean autoLogin = (RWT.getRequest().getParameter("auto") != null); //$NON-NLS-1$
144
145 String ssoTicket = RWT.getRequest().getParameter("ticket");
146 if (ssoTicket != null)
147 {
148 autoLogin = true;
149 String server = RWT.getRequest().getParameter("server"); //$NON-NLS-1$
150 if (server == null)
151 server = properties.getProperty("server", "127.0.0.1"); //$NON-NLS-1$ //$NON-NLS-2$
152 success = connectToServer(server, null, ssoTicket);
153 }
154 else if (autoLogin)
155 {
156 String server = RWT.getRequest().getParameter("server"); //$NON-NLS-1$
157 if (server == null)
158 server = properties.getProperty("server", "127.0.0.1"); //$NON-NLS-1$ //$NON-NLS-2$
159 String login = RWT.getRequest().getParameter("login"); //$NON-NLS-1$
160 if (login == null)
161 login = "guest";
162 password = RWT.getRequest().getParameter("password"); //$NON-NLS-1$
163 if (password == null)
164 password = "";
165 success = connectToServer(server, login, password);
166 }
167
168 if (!autoLogin || !success)
169 {
170 Window loginDialog;
171 do
172 {
173 loginDialog = BrandingManager.getInstance().getLoginForm(Display.getCurrent().getActiveShell(), properties);
174 if (loginDialog.open() != Window.OK)
175 continue;
176 password = ((LoginForm)loginDialog).getPassword();
177
178 success = connectToServer(properties.getProperty("server", "127.0.0.1"), //$NON-NLS-1$ //$NON-NLS-2$
179 ((LoginForm)loginDialog).getLogin(),
180 password);
181 } while(!success);
182 }
183
184 if (success)
185 {
186 final NXCSession session = (NXCSession)RWT.getUISession().getAttribute(ConsoleSharedData.ATTRIBUTE_SESSION);
187 final Display display = Display.getCurrent();
188 session.addListener(new SessionListener() {
189 @Override
190 public void notificationHandler(final SessionNotification n)
191 {
192 if ((n.getCode() == SessionNotification.CONNECTION_BROKEN) ||
193 (n.getCode() == SessionNotification.SERVER_SHUTDOWN) ||
194 (n.getCode() == SessionNotification.SESSION_KILLED))
195 {
196 display.asyncExec(new Runnable() {
197 @Override
198 public void run()
199 {
200 String productName = BrandingManager.getInstance().getProductName();
201 MessageDialog.openError(
202 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
203 Messages.get().ApplicationWorkbenchAdvisor_CommunicationError,
204 ((n.getCode() == SessionNotification.CONNECTION_BROKEN) ?
205 String.format(Messages.get().ApplicationWorkbenchAdvisor_ConnectionLostMessage, productName)
206 : ((n.getCode() == SessionNotification.CONNECTION_BROKEN) ?
207 "Communication session was terminated by system administrator"
208 : String.format(Messages.get().ApplicationWorkbenchAdvisor_ServerShutdownMessage, productName)))
209 + Messages.get().ApplicationWorkbenchAdvisor_OKToCloseMessage);
210 PlatformUI.getWorkbench().getActiveWorkbenchWindow().close();
211 }
212 });
213 }
214 }
215 });
216
217 try
218 {
219 RWT.getSettingStore().loadById(session.getUserName() + "@" + session.getServerId());
220 }
221 catch(IOException e)
222 {
223 }
224
225 // Suggest user to change password if it is expired
226 if (session.isPasswordExpired())
227 {
228 final PasswordExpiredDialog dlg = new PasswordExpiredDialog(null);
229 if (dlg.open() == Window.OK)
230 {
231 final String currentPassword = password;
232 IRunnableWithProgress job = new IRunnableWithProgress() {
233 @Override
234 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException
235 {
236 try
237 {
238 NXCSession session = (NXCSession)RWT.getUISession(display).getAttribute(ConsoleSharedData.ATTRIBUTE_SESSION);
239 session.setUserPassword(session.getUserId(), dlg.getPassword(), currentPassword);
240 }
241 catch(Exception e)
242 {
243 throw new InvocationTargetException(e);
244 }
245 finally
246 {
247 monitor.done();
248 }
249 }
250 };
251 try
252 {
253 ProgressMonitorDialog pd = new ProgressMonitorDialog(null);
254 pd.run(false, false, job);
255 MessageDialog.openInformation(null, Messages.get().ApplicationWorkbenchWindowAdvisor_Information, Messages.get().ApplicationWorkbenchWindowAdvisor_PasswordChanged);
256 }
257 catch(InvocationTargetException e)
258 {
259 MessageDialog.openError(null, Messages.get().ApplicationWorkbenchWindowAdvisor_Error, Messages.get().ApplicationWorkbenchWindowAdvisor_CannotChangePswd + e.getCause().getLocalizedMessage());
260 }
261 catch(InterruptedException e)
262 {
263 MessageDialog.openError(null, Messages.get().ApplicationWorkbenchWindowAdvisor_Exception, e.toString());
264 }
265 }
266 }
267 }
268 }
269 }