import java.sql.SQLException; import java.util.Collection; import com.vaadin.ui.Button; import com.vaadin.ui.CheckBox; import com.vaadin.ui.ProgressIndicator; import com.vaadin.ui.TextField; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.Button.ClickListener; public class Login extends BaseTemplateComponent { private Button ok; private CookieManager cookieManager; ProgressIndicator poller; boolean keepRunning; @Override public void initUI() { setWidth("100%"); final TextField username = new TextField(); final TextField password = new TextField(); final CheckBox saveCookie = new CheckBox(); cookieManager = new CookieManager(true); poller = new ProgressIndicator(); poller.addStyleName("invisible"); poller.setPollingInterval(300); password.setSecret(true); addComponent(username, "username"); addComponent(password, "password"); addComponent(saveCookie, "saveCookie"); addComponent(cookieManager, "cookieMgr"); addComponent(poller, "poller"); ok = new Button("Login"); addComponent(ok, "okbutton"); ok.addListener(new ClickListener() { public void buttonClick(ClickEvent event) { keepRunning = false; if (saveCookie.booleanValue()) { cookieManager.setCookie("saved", "yes"); cookieManager.setCookie("hub_user", username.toString()); cookieManager.setCookie("hub_pass", EncryptDecryptUtil.encrypt(password.toString())); } else { cookieManager.setCookie("saved", "no"); cookieManager.setCookie("hub_user", ""); cookieManager.setCookie("hub_pass", ""); } doLogin(username.toString(), password.toString()); } }); cookieManager.addListener(new UpdateListener() { @Override public void cookiesUpdated(CookieManager clientCookies) { String saved = clientCookies.getCookie("saved"); if ("yes".equals(saved)) { keepRunning = false; String user = clientCookies.getCookie("hub_user"); String pass = clientCookies.getCookie("hub_pass"); pass = EncryptDecryptUtil.decrypt(pass); username.setValue(user); password.setValue(pass); saveCookie.setValue(true); poller.setValue(0.5f); poller.setVisible(true); } } }); keepRunning = true; ReadCookiesThread wt = new ReadCookiesThread(); wt.start(); } private JiraSession session; private User user; private void doLogin(String jiraId, String pass) { try { session = new JiraSession(JiraSession.JIRA_URL, jiraId, pass, false); user = getOrCreateUser(jiraId); if (user == null) { TagGrandCentralApp.showError("Login Error", "User not found"); } else { keepRunning = true; ok.setEnabled(false); poller.setVisible(true); SaveCookiesThread th = new SaveCookiesThread(); th.start(); } } catch (Exception e) { TagGrandCentralApp.showError("Login Error", e); } } public static User getOrCreateUser(String jiraName) throws SQLException { Collection allUsers = CacheManager.getUsers().getUnsorted(); User found = null; for (DBObject userObj : allUsers) { User u = (User) userObj; if (u.getJiraUserId().equalsIgnoreCase(jiraName)) { found = u; } } if (found == null) { found = new User(); found.setJiraUserId(jiraName); AllDaos.USER_DAO.store(found); } return found; } class ReadCookiesThread extends Thread { public void run() { float current = 0.0f; while (keepRunning) { // Do some "work" try { sleep(250); // Sleep for 500 milliseconds } catch (InterruptedException e) { } current += 0.10; //username.setValue(current); poller.setValue(current); cookieManager.setCookie("__dummy__", "__dummy__"); cookieManager.loadFromClient(); if (current > 1) { keepRunning = false; poller.setVisible(false); } } } } class SaveCookiesThread extends Thread { public void run() { float start = 0.5f; while (keepRunning) { start = start + 0.2f; poller.setValue(start); try { sleep(250); // Sleep for 500 milliseconds } catch (InterruptedException e) { } if (start > 1) { keepRunning = false; poller.setVisible(false); } } ((TagGrandCentralApp)(poller.getApplication())).onLogin(session, user); } } }