}
}
public void startApplication(final JSONObject data, final Request request, final Response response) throws Exception {
final Session session = request.getSession();
synchronized (session) {
Long reloadedViewID = null;
boolean isNewHttpSession = false;
Application application = (Application) session.getAttribute(Application.class.getCanonicalName());
if (application == null) {
log.info("Creating a new application ... Session ID #" + session.getId() + " - " + request.getHeader("User-Agent") + " - " + request.getRemoteAddr());
final String applicationID = System.getProperty(SystemProperty.APPLICATION_ID);
final String applicationName = System.getProperty(SystemProperty.APPLICATION_NAME);
application = new Application(applicationID, applicationName, session, options);
session.setUserAgent(request.getHeader("User-Agent"));
session.setAttribute(Application.class.getCanonicalName(), application);
isNewHttpSession = true;
} else {
if (data.has(APPLICATION.VIEW_ID)) reloadedViewID = data.getLong(APPLICATION.VIEW_ID);
log.info("Reloading application " + reloadedViewID + " on session #" + session.getId());
}
final UIContext uiContext = new UIContext(application);
UIContext.setCurrent(uiContext);
if (reloadedViewID != null) {
final UIContext previousUIContext = application.getUIContext(reloadedViewID);
if (previousUIContext != null) previousUIContext.destroy();
}
try {
final Txn txn = Txn.get();
txn.begin(new TxnContextHttp(true, request, response));
try {
final long receivedSeqNum = data.getLong(APPLICATION.SEQ_NUM);
uiContext.updateIncomingSeqNum(receivedSeqNum);
final EntryPoint entryPoint = initializePonySession(uiContext);
final String historyToken = data.getString(HISTORY.TOKEN);
if (historyToken != null && !historyToken.isEmpty()) uiContext.getHistory().newItem(historyToken, false);
final PCookies pCookies = uiContext.getCookies();
final JSONArray cookies = data.getJSONArray(PROPERTY.COOKIES);
for (int i = 0; i < cookies.length(); i++) {
final JSONObject jsoObject = cookies.getJSONObject(i);
final String name = jsoObject.getString(PROPERTY.KEY);
final String value = jsoObject.getString(PROPERTY.VALUE);
pCookies.cacheCookie(name, value);
}
if (isNewHttpSession) {
entryPoint.start(uiContext);
} else {
entryPoint.restart(uiContext);
}
txn.commit();
} catch (final Throwable e) {
log.error("Cannot send instructions to the browser, Session ID #" + session.getId(), e);
txn.rollback();
}
} finally {
UIContext.remove();
}