iterator = variableBurst.values().iterator();
while (iterator.hasNext()) {
String id = iterator.next().getConnectorId();
if (!getConnectorMap().hasConnector(id)
&& !getConnectorMap().isDragAndDropPaintable(id)) {
// variable owner does not exist anymore
iterator.remove();
VConsole.log("Removed variable from removed component: " + id);
}
}
}
/**
* Checks if deferred commands are (potentially) still being executed as a
* result of an update from the server. Returns true if a deferred command
* might still be executing, false otherwise. This will not work correctly
* if a deferred command is added in another deferred command.
*
* Used by the native "client.isActive" function.
*
*
* @return true if deferred commands are (potentially) being executed, false
* otherwise
*/
private boolean isExecutingDeferredCommands() {
Scheduler s = Scheduler.get();
if (s instanceof VSchedulerImpl) {
return ((VSchedulerImpl) s).hasWorkQueued();
} else {
return false;
}
}
/**
* Returns the loading indicator used by this ApplicationConnection
*
* @return The loading indicator for this ApplicationConnection
*/
public VLoadingIndicator getLoadingIndicator() {
return loadingIndicator;
}
/**
* Determines whether or not the loading indicator is showing.
*
* @return true if the loading indicator is visible
* @deprecated As of 7.1. Use {@link #getLoadingIndicator()} and
* {@link VLoadingIndicator#isVisible()}.isVisible() instead.
*/
@Deprecated
public boolean isLoadingIndicatorVisible() {
return getLoadingIndicator().isVisible();
}
private static native ValueMap parseJSONResponse(String jsonText)
/*-{
try {
return JSON.parse(jsonText);
} catch (ignored) {
return eval('(' + jsonText + ')');
}
}-*/;
private void handleReceivedJSONMessage(Date start, String jsonText,
ValueMap json) {
handleUIDLMessage(start, jsonText, json);
}
/**
* Gets the id of the last received response. This id can be used by
* connectors to determine whether new data has been received from the
* server to avoid doing the same calculations multiple times.
*
* No guarantees are made for the structure of the id other than that there
* will be a new unique value every time a new response with data from the
* server is received.
*
* The initial id when no request has yet been processed is -1.
*
* @return and id identifying the response
*/
public int getLastResponseId() {
return lastResponseId;
}
protected void handleUIDLMessage(final Date start, final String jsonText,
final ValueMap json) {
if (!responseHandlingLocks.isEmpty()) {
// Some component is doing something that can't be interrupted
// (e.g. animation that should be smooth). Enqueue the UIDL
// message for later processing.
VConsole.log("Postponing UIDL handling due to lock...");
pendingUIDLMessages.add(new PendingUIDLMessage(start, jsonText,
json));
forceHandleMessage.schedule(MAX_SUSPENDED_TIMEOUT);
return;
}
/*
* Lock response handling to avoid a situation where something pushed
* from the server gets processed while waiting for e.g. lazily loaded
* connectors that are needed for processing the current message.
*/
final Object lock = new Object();
suspendReponseHandling(lock);
VConsole.log("Handling message from server");
eventBus.fireEvent(new ResponseHandlingStartedEvent(this));
// Handle redirect
if (json.containsKey("redirect")) {
String url = json.getValueMap("redirect").getString("url");
VConsole.log("redirecting to " + url);
redirect(url);
return;
}
lastResponseId++;
final MultiStepDuration handleUIDLDuration = new MultiStepDuration();
// Get security key
if (json.containsKey(ApplicationConstants.UIDL_SECURITY_TOKEN_ID)) {
csrfToken = json
.getString(ApplicationConstants.UIDL_SECURITY_TOKEN_ID);
}
VConsole.log(" * Handling resources from server");
if (json.containsKey("resources")) {
ValueMap resources = json.getValueMap("resources");
JsArrayString keyArray = resources.getKeyArray();
int l = keyArray.length();
for (int i = 0; i < l; i++) {
String key = keyArray.get(i);
resourcesMap.put(key, resources.getAsString(key));
}
}
handleUIDLDuration.logDuration(
" * Handling resources from server completed", 10);
VConsole.log(" * Handling type inheritance map from server");
if (json.containsKey("typeInheritanceMap")) {
configuration.addComponentInheritanceInfo(json
.getValueMap("typeInheritanceMap"));
}
handleUIDLDuration.logDuration(
" * Handling type inheritance map from server completed", 10);
VConsole.log("Handling type mappings from server");
if (json.containsKey("typeMappings")) {
configuration.addComponentMappings(
json.getValueMap("typeMappings"), widgetSet);
}
VConsole.log("Handling resource dependencies");
if (json.containsKey("scriptDependencies")) {
loadScriptDependencies(json.getJSStringArray("scriptDependencies"));
}
if (json.containsKey("styleDependencies")) {
loadStyleDependencies(json.getJSStringArray("styleDependencies"));
}
handleUIDLDuration.logDuration(
" * Handling type mappings from server completed", 10);
/*
* Hook for e.g. TestBench to get details about server peformance
*/
if (json.containsKey("timings")) {
serverTimingInfo = json.getValueMap("timings");
}
Command c = new Command() {
@Override
public void execute() {
handleUIDLDuration.logDuration(" * Loading widgets completed",
10);
Profiler.enter("Handling meta information");
ValueMap meta = null;
if (json.containsKey("meta")) {
VConsole.log(" * Handling meta information");
meta = json.getValueMap("meta");
if (meta.containsKey("repaintAll")) {
prepareRepaintAll();
}
if (meta.containsKey("timedRedirect")) {
final ValueMap timedRedirect = meta
.getValueMap("timedRedirect");
if (redirectTimer != null) {
redirectTimer.cancel();
}
redirectTimer = new Timer() {
@Override
public void run() {
redirect(timedRedirect.getString("url"));
}
};
sessionExpirationInterval = timedRedirect
.getInt("interval");
}
}
Profiler.leave("Handling meta information");
if (redirectTimer != null) {
redirectTimer.schedule(1000 * sessionExpirationInterval);
}
double processUidlStart = Duration.currentTimeMillis();
// Ensure that all connectors that we are about to update exist
JsArrayString createdConnectorIds = createConnectorsIfNeeded(json);
// Update states, do not fire events
JsArrayObject pendingStateChangeEvents = updateConnectorState(
json, createdConnectorIds);
/*
* Doing this here so that locales are available also to the
* connectors which get a state change event before the UI.
*/
Profiler.enter("Handling locales");
VConsole.log(" * Handling locales");
// Store locale data
LocaleService
.addLocales(getUIConnector().getState().localeServiceState.localeData);
Profiler.leave("Handling locales");
// Update hierarchy, do not fire events
ConnectorHierarchyUpdateResult connectorHierarchyUpdateResult = updateConnectorHierarchy(json);
// Fire hierarchy change events
sendHierarchyChangeEvents(connectorHierarchyUpdateResult.events);
updateCaptions(pendingStateChangeEvents,
connectorHierarchyUpdateResult.parentChangedIds);
delegateToWidget(pendingStateChangeEvents);
// Fire state change events.
sendStateChangeEvents(pendingStateChangeEvents);
// Update of legacy (UIDL) style connectors
updateVaadin6StyleConnectors(json);
// Handle any RPC invocations done on the server side
handleRpcInvocations(json);
if (json.containsKey("dd")) {
// response contains data for drag and drop service
VDragAndDropManager.get().handleServerResponse(
json.getValueMap("dd"));
}
unregisterRemovedConnectors();
VConsole.log("handleUIDLMessage: "
+ (Duration.currentTimeMillis() - processUidlStart)
+ " ms");
Profiler.enter("Layout processing");
try {
LayoutManager layoutManager = getLayoutManager();
layoutManager.setEverythingNeedsMeasure();
layoutManager.layoutNow();
} catch (final Throwable e) {
VConsole.error(e);
}
Profiler.leave("Layout processing");
if (ApplicationConfiguration.isDebugMode()) {
Profiler.enter("Dumping state changes to the console");
VConsole.log(" * Dumping state changes to the console");
VConsole.dirUIDL(json, ApplicationConnection.this);
Profiler.leave("Dumping state changes to the console");
}
if (meta != null) {
Profiler.enter("Error handling");
if (meta.containsKey("appError")) {
ValueMap error = meta.getValueMap("appError");
showError(null, error.getString("caption"),
error.getString("message"),
error.getString("url"));
setApplicationRunning(false);
}
Profiler.leave("Error handling");
}
// TODO build profiling for widget impl loading time
lastProcessingTime = (int) ((new Date().getTime()) - start
.getTime());
totalProcessingTime += lastProcessingTime;
if (bootstrapTime == 0) {
bootstrapTime = calculateBootstrapTime();
if (Profiler.isEnabled() && bootstrapTime != -1) {
Profiler.logBootstrapTimings();
}
}
VConsole.log(" Processing time was "
+ String.valueOf(lastProcessingTime) + "ms for "
+ jsonText.length() + " characters of JSON");
VConsole.log("Referenced paintables: " + connectorMap.size());
if (meta == null || !meta.containsKey("async")) {
// End the request if the received message was a response,
// not sent asynchronously
endRequest();
}
resumeResponseHandling(lock);
if (Profiler.isEnabled()) {
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
@Override
public void execute() {
Profiler.logTimings();
Profiler.reset();
}
});
}
}
/**
* Properly clean up any old stuff to ensure everything is properly
* reinitialized.
*/
private void prepareRepaintAll() {
String uiConnectorId = uIConnector.getConnectorId();
if (uiConnectorId == null) {
// Nothing to clear yet
return;
}
// Create fake server response that says that the uiConnector
// has no children
JSONObject fakeHierarchy = new JSONObject();
fakeHierarchy.put(uiConnectorId, new JSONArray());
JSONObject fakeJson = new JSONObject();
fakeJson.put("hierarchy", fakeHierarchy);
ValueMap fakeValueMap = fakeJson.getJavaScriptObject().cast();
// Update hierarchy based on the fake response
ConnectorHierarchyUpdateResult connectorHierarchyUpdateResult = updateConnectorHierarchy(fakeValueMap);
// Send hierarchy events based on the fake update
sendHierarchyChangeEvents(connectorHierarchyUpdateResult.events);
// Unregister all the old connectors that have now been removed
unregisterRemovedConnectors();
getLayoutManager().cleanMeasuredSizes();
}
private void updateCaptions(
JsArrayObject pendingStateChangeEvents,
FastStringSet parentChangedIds) {
Profiler.enter("updateCaptions");
/*
* Find all components that might need a caption update based on
* pending state and hierarchy changes
*/
FastStringSet needsCaptionUpdate = FastStringSet.create();
needsCaptionUpdate.addAll(parentChangedIds);
// Find components with potentially changed caption state
int size = pendingStateChangeEvents.size();
for (int i = 0; i < size; i++) {
StateChangeEvent event = pendingStateChangeEvents.get(i);
if (VCaption.mightChange(event)) {
ServerConnector connector = event.getConnector();
needsCaptionUpdate.add(connector.getConnectorId());
}
}
ConnectorMap connectorMap = getConnectorMap();
// Update captions for all suitable candidates
JsArrayString dump = needsCaptionUpdate.dump();
int needsUpdateLength = dump.length();
for (int i = 0; i < needsUpdateLength; i++) {
String childId = dump.get(i);
ServerConnector child = connectorMap.getConnector(childId);
if (child instanceof ComponentConnector
&& ((ComponentConnector) child)
.delegateCaptionHandling()) {
ServerConnector parent = child.getParent();
if (parent instanceof HasComponentsConnector) {
Profiler.enter("HasComponentsConnector.updateCaption");
((HasComponentsConnector) parent)
.updateCaption((ComponentConnector) child);
Profiler.leave("HasComponentsConnector.updateCaption");
}
}
}
Profiler.leave("updateCaptions");
}
private void delegateToWidget(
JsArrayObject pendingStateChangeEvents) {
Profiler.enter("@DelegateToWidget");
VConsole.log(" * Running @DelegateToWidget");
// Keep track of types that have no @DelegateToWidget in their
// state to optimize performance
FastStringSet noOpTypes = FastStringSet.create();
int size = pendingStateChangeEvents.size();
for (int eventIndex = 0; eventIndex < size; eventIndex++) {
StateChangeEvent sce = pendingStateChangeEvents
.get(eventIndex);
ServerConnector connector = sce.getConnector();
if (connector instanceof ComponentConnector) {
String className = connector.getClass().getName();
if (noOpTypes.contains(className)) {
continue;
}
ComponentConnector component = (ComponentConnector) connector;
Type stateType = AbstractConnector
.getStateType(component);
JsArrayString delegateToWidgetProperties = stateType
.getDelegateToWidgetProperties();
if (delegateToWidgetProperties == null) {
noOpTypes.add(className);
continue;
}
int length = delegateToWidgetProperties.length();
for (int i = 0; i < length; i++) {
String propertyName = delegateToWidgetProperties
.get(i);
if (sce.hasPropertyChanged(propertyName)) {
Property property = stateType
.getProperty(propertyName);
String method = property
.getDelegateToWidgetMethodName();
Profiler.enter("doDelegateToWidget");
doDelegateToWidget(component, property, method);
Profiler.leave("doDelegateToWidget");
}
}
}
}
Profiler.leave("@DelegateToWidget");
}
private void doDelegateToWidget(ComponentConnector component,
Property property, String methodName) {
Type type = TypeData.getType(component.getClass());
try {
Type widgetType = type.getMethod("getWidget")
.getReturnType();
Widget widget = component.getWidget();
Object propertyValue = property.getValue(component
.getState());
widgetType.getMethod(methodName).invoke(widget,
propertyValue);
} catch (NoDataException e) {
throw new RuntimeException(
"Missing data needed to invoke @DelegateToWidget for "
+ Util.getSimpleName(component), e);
}
}
/**
* Sends the state change events created while updating the state
* information.
*
* This must be called after hierarchy change listeners have been
* called. At least caption updates for the parent are strange if
* fired from state change listeners and thus calls the parent
* BEFORE the parent is aware of the child (through a
* ConnectorHierarchyChangedEvent)
*
* @param pendingStateChangeEvents
* The events to send
*/
private void sendStateChangeEvents(
JsArrayObject pendingStateChangeEvents) {
Profiler.enter("sendStateChangeEvents");
VConsole.log(" * Sending state change events");
int size = pendingStateChangeEvents.size();
for (int i = 0; i < size; i++) {
StateChangeEvent sce = pendingStateChangeEvents.get(i);
try {
sce.getConnector().fireEvent(sce);
} catch (final Throwable e) {
VConsole.error(e);
}
}
Profiler.leave("sendStateChangeEvents");
}
private void unregisterRemovedConnectors() {
Profiler.enter("unregisterRemovedConnectors");
int unregistered = 0;
JsArrayObject currentConnectors = connectorMap
.getConnectorsAsJsArray();
int size = currentConnectors.size();
for (int i = 0; i < size; i++) {
ServerConnector c = currentConnectors.get(i);
if (c.getParent() != null) {
// only do this check if debug mode is active
if (ApplicationConfiguration.isDebugMode()) {
Profiler.enter("unregisterRemovedConnectors check parent - this is only performed in debug mode");
// this is slow for large layouts, 25-30% of total
// time for some operations even on modern browsers
if (!c.getParent().getChildren().contains(c)) {
VConsole.error("ERROR: Connector is connected to a parent but the parent does not contain the connector");
}
Profiler.leave("unregisterRemovedConnectors check parent - this is only performed in debug mode");
}
} else if (c == getUIConnector()) {
// UIConnector for this connection, leave as-is
} else if (c instanceof WindowConnector
&& getUIConnector().hasSubWindow(
(WindowConnector) c)) {
// Sub window attached to this UIConnector, leave
// as-is
} else {
// The connector has been detached from the
// hierarchy, unregister it and any possible
// children. The UIConnector should never be
// unregistered even though it has no parent.
Profiler.enter("unregisterRemovedConnectors unregisterConnector");
connectorMap.unregisterConnector(c);
Profiler.leave("unregisterRemovedConnectors unregisterConnector");
unregistered++;
}
}
VConsole.log("* Unregistered " + unregistered + " connectors");
Profiler.leave("unregisterRemovedConnectors");
}
private JsArrayString createConnectorsIfNeeded(ValueMap json) {
VConsole.log(" * Creating connectors (if needed)");
JsArrayString createdConnectors = JavaScriptObject
.createArray().cast();
if (!json.containsKey("types")) {
return createdConnectors;
}
Profiler.enter("Creating connectors");
ValueMap types = json.getValueMap("types");
JsArrayString keyArray = types.getKeyArray();
for (int i = 0; i < keyArray.length(); i++) {
try {
String connectorId = keyArray.get(i);
ServerConnector connector = connectorMap
.getConnector(connectorId);
if (connector != null) {
continue;
}
int connectorType = Integer.parseInt(types
.getString(connectorId));
Class extends ServerConnector> connectorClass = configuration
.getConnectorClassByEncodedTag(connectorType);
// Connector does not exist so we must create it
if (connectorClass != uIConnector.getClass()) {
// create, initialize and register the paintable
Profiler.enter("ApplicationConnection.getConnector");
connector = getConnector(connectorId, connectorType);
Profiler.leave("ApplicationConnection.getConnector");
createdConnectors.push(connectorId);
} else {
// First UIConnector update. Before this the
// UIConnector has been created but not
// initialized as the connector id has not been
// known
connectorMap.registerConnector(connectorId,
uIConnector);
uIConnector.doInit(connectorId,
ApplicationConnection.this);
createdConnectors.push(connectorId);
}
} catch (final Throwable e) {
VConsole.error(e);
}
}
Profiler.leave("Creating connectors");
return createdConnectors;
}
private void updateVaadin6StyleConnectors(ValueMap json) {
Profiler.enter("updateVaadin6StyleConnectors");
JsArray changes = json.getJSValueMapArray("changes");
int length = changes.length();
VConsole.log(" * Passing UIDL to Vaadin 6 style connectors");
// update paintables
for (int i = 0; i < length; i++) {
try {
final UIDL change = changes.get(i).cast();
final UIDL uidl = change.getChildUIDL(0);
String connectorId = uidl.getId();
final ComponentConnector legacyConnector = (ComponentConnector) connectorMap
.getConnector(connectorId);
if (legacyConnector instanceof Paintable) {
String key = null;
if (Profiler.isEnabled()) {
key = "updateFromUIDL for "
+ Util.getSimpleName(legacyConnector);
Profiler.enter(key);
}
((Paintable) legacyConnector).updateFromUIDL(uidl,
ApplicationConnection.this);
if (Profiler.isEnabled()) {
Profiler.leave(key);
}
} else if (legacyConnector == null) {
VConsole.error("Received update for "
+ uidl.getTag()
+ ", but there is no such paintable ("
+ connectorId + ") rendered.");
} else {
VConsole.error("Server sent Vaadin 6 style updates for "
+ Util.getConnectorString(legacyConnector)
+ " but this is not a Vaadin 6 Paintable");
}
} catch (final Throwable e) {
VConsole.error(e);
}
}
Profiler.leave("updateVaadin6StyleConnectors");
}
private void sendHierarchyChangeEvents(
JsArrayObject events) {
int eventCount = events.size();
if (eventCount == 0) {
return;
}
Profiler.enter("sendHierarchyChangeEvents");
VConsole.log(" * Sending hierarchy change events");
for (int i = 0; i < eventCount; i++) {
ConnectorHierarchyChangeEvent event = events.get(i);
try {
logHierarchyChange(event);
event.getConnector().fireEvent(event);
} catch (final Throwable e) {
VConsole.error(e);
}
}
Profiler.leave("sendHierarchyChangeEvents");
}
private void logHierarchyChange(ConnectorHierarchyChangeEvent event) {
if (true) {
// Always disabled for now. Can be enabled manually
return;
}
VConsole.log("Hierarchy changed for "
+ Util.getConnectorString(event.getConnector()));
String oldChildren = "* Old children: ";
for (ComponentConnector child : event.getOldChildren()) {
oldChildren += Util.getConnectorString(child) + " ";
}
VConsole.log(oldChildren);
String newChildren = "* New children: ";
HasComponentsConnector parent = (HasComponentsConnector) event
.getConnector();
for (ComponentConnector child : parent.getChildComponents()) {
newChildren += Util.getConnectorString(child) + " ";
}
VConsole.log(newChildren);
}
private JsArrayObject updateConnectorState(
ValueMap json, JsArrayString createdConnectorIds) {
JsArrayObject events = JavaScriptObject
.createArray().cast();
VConsole.log(" * Updating connector states");
if (!json.containsKey("state")) {
return events;
}
Profiler.enter("updateConnectorState");
FastStringSet remainingNewConnectors = FastStringSet.create();
remainingNewConnectors.addAll(createdConnectorIds);
// set states for all paintables mentioned in "state"
ValueMap states = json.getValueMap("state");
JsArrayString keyArray = states.getKeyArray();
for (int i = 0; i < keyArray.length(); i++) {
try {
String connectorId = keyArray.get(i);
ServerConnector connector = connectorMap
.getConnector(connectorId);
if (null != connector) {
Profiler.enter("updateConnectorState inner loop");
if (Profiler.isEnabled()) {
Profiler.enter("Decode connector state "
+ Util.getSimpleName(connector));
}
JSONObject stateJson = new JSONObject(
states.getJavaScriptObject(connectorId));
if (connector instanceof HasJavaScriptConnectorHelper) {
((HasJavaScriptConnectorHelper) connector)
.getJavascriptConnectorHelper()
.setNativeState(
stateJson.getJavaScriptObject());
}
SharedState state = connector.getState();
Profiler.enter("updateConnectorState decodeValue");
JsonDecoder.decodeValue(new Type(state.getClass()
.getName(), null), stateJson, state,
ApplicationConnection.this);
Profiler.leave("updateConnectorState decodeValue");
if (Profiler.isEnabled()) {
Profiler.leave("Decode connector state "
+ Util.getSimpleName(connector));
}
Profiler.enter("updateConnectorState create event");
boolean isNewConnector = remainingNewConnectors
.contains(connectorId);
if (isNewConnector) {
remainingNewConnectors.remove(connectorId);
}
StateChangeEvent event = new StateChangeEvent(
connector, stateJson, isNewConnector);
events.add(event);
Profiler.leave("updateConnectorState create event");
Profiler.leave("updateConnectorState inner loop");
}
} catch (final Throwable e) {
VConsole.error(e);
}
}
Profiler.enter("updateConnectorState newWithoutState");
// Fire events for properties using the default value for newly
// created connectors even if there were no state changes
JsArrayString dump = remainingNewConnectors.dump();
int length = dump.length();
for (int i = 0; i < length; i++) {
String connectorId = dump.get(i);
ServerConnector connector = connectorMap
.getConnector(connectorId);
StateChangeEvent event = new StateChangeEvent(connector,
new JSONObject(), true);
events.add(event);
}
Profiler.leave("updateConnectorState newWithoutState");
Profiler.leave("updateConnectorState");
return events;
}
/**
* Updates the connector hierarchy and returns a list of events that
* should be fired after update of the hierarchy and the state is
* done.
*
* @param json
* The JSON containing the hierarchy information
* @return A collection of events that should be fired when update
* of hierarchy and state is complete and a list of all
* connectors for which the parent has changed
*/
private ConnectorHierarchyUpdateResult updateConnectorHierarchy(
ValueMap json) {
ConnectorHierarchyUpdateResult result = new ConnectorHierarchyUpdateResult();
VConsole.log(" * Updating connector hierarchy");
if (!json.containsKey("hierarchy")) {
return result;
}
Profiler.enter("updateConnectorHierarchy");
FastStringSet maybeDetached = FastStringSet.create();
ValueMap hierarchies = json.getValueMap("hierarchy");
JsArrayString hierarchyKeys = hierarchies.getKeyArray();
for (int i = 0; i < hierarchyKeys.length(); i++) {
try {
Profiler.enter("updateConnectorHierarchy hierarchy entry");
String connectorId = hierarchyKeys.get(i);
ServerConnector parentConnector = connectorMap
.getConnector(connectorId);
JsArrayString childConnectorIds = hierarchies
.getJSStringArray(connectorId);
int childConnectorSize = childConnectorIds.length();
Profiler.enter("updateConnectorHierarchy find new connectors");
List newChildren = new ArrayList();
List newComponents = new ArrayList();
for (int connectorIndex = 0; connectorIndex < childConnectorSize; connectorIndex++) {
String childConnectorId = childConnectorIds
.get(connectorIndex);
ServerConnector childConnector = connectorMap
.getConnector(childConnectorId);
if (childConnector == null) {
VConsole.error("Hierarchy claims that "
+ childConnectorId
+ " is a child for "
+ connectorId
+ " ("
+ parentConnector.getClass().getName()
+ ") but no connector with id "
+ childConnectorId
+ " has been registered. "
+ "More information might be available in the server-side log if assertions are enabled");
continue;
}
newChildren.add(childConnector);
if (childConnector instanceof ComponentConnector) {
newComponents
.add((ComponentConnector) childConnector);
} else if (!(childConnector instanceof AbstractExtensionConnector)) {
throw new IllegalStateException(
Util.getConnectorString(childConnector)
+ " is not a ComponentConnector nor an AbstractExtensionConnector");
}
if (childConnector.getParent() != parentConnector) {
childConnector.setParent(parentConnector);
result.parentChangedIds.add(childConnectorId);
// Not detached even if previously removed from
// parent
maybeDetached.remove(childConnectorId);
}
}
Profiler.leave("updateConnectorHierarchy find new connectors");
// TODO This check should be done on the server side in
// the future so the hierarchy update is only sent when
// something actually has changed
List oldChildren = parentConnector
.getChildren();
boolean actuallyChanged = !Util.collectionsEquals(
oldChildren, newChildren);
if (!actuallyChanged) {
continue;
}
Profiler.enter("updateConnectorHierarchy handle HasComponentsConnector");
if (parentConnector instanceof HasComponentsConnector) {
HasComponentsConnector ccc = (HasComponentsConnector) parentConnector;
List oldComponents = ccc
.getChildComponents();
if (!Util.collectionsEquals(oldComponents,
newComponents)) {
// Fire change event if the hierarchy has
// changed
ConnectorHierarchyChangeEvent event = GWT
.create(ConnectorHierarchyChangeEvent.class);
event.setOldChildren(oldComponents);
event.setConnector(parentConnector);
ccc.setChildComponents(newComponents);
result.events.add(event);
}
} else if (!newComponents.isEmpty()) {
VConsole.error("Hierachy claims "
+ Util.getConnectorString(parentConnector)
+ " has component children even though it isn't a HasComponentsConnector");
}
Profiler.leave("updateConnectorHierarchy handle HasComponentsConnector");
Profiler.enter("updateConnectorHierarchy setChildren");
parentConnector.setChildren(newChildren);
Profiler.leave("updateConnectorHierarchy setChildren");
Profiler.enter("updateConnectorHierarchy find removed children");
/*
* Find children removed from this parent and mark for
* removal unless they are already attached to some
* other parent.
*/
for (ServerConnector oldChild : oldChildren) {
if (oldChild.getParent() != parentConnector) {
// Ignore if moved to some other connector
continue;
}
if (!newChildren.contains(oldChild)) {
/*
* Consider child detached for now, will be
* cleared if it is later on added to some other
* parent.
*/
maybeDetached.add(oldChild.getConnectorId());
}
}
Profiler.leave("updateConnectorHierarchy find removed children");
} catch (final Throwable e) {
VConsole.error(e);
} finally {
Profiler.leave("updateConnectorHierarchy hierarchy entry");
}
}
Profiler.enter("updateConnectorHierarchy detach removed connectors");
/*
* Connector is in maybeDetached at this point if it has been
* removed from its parent but not added to any other parent
*/
JsArrayString maybeDetachedArray = maybeDetached.dump();
for (int i = 0; i < maybeDetachedArray.length(); i++) {
ServerConnector removed = connectorMap
.getConnector(maybeDetachedArray.get(i));
recursivelyDetach(removed, result.events);
}
Profiler.leave("updateConnectorHierarchy detach removed connectors");
Profiler.leave("updateConnectorHierarchy");
return result;
}
private void recursivelyDetach(ServerConnector connector,
JsArrayObject events) {
/*
* Reset state in an attempt to keep it consistent with the
* hierarchy. No children and no parent is the initial situation
* for the hierarchy, so changing the state to its initial value
* is the closest we can get without data from the server.
* #10151
*/
Profiler.enter("ApplicationConnection recursivelyDetach reset state");
try {
Profiler.enter("ApplicationConnection recursivelyDetach reset state - getStateType");
Type stateType = AbstractConnector.getStateType(connector);
Profiler.leave("ApplicationConnection recursivelyDetach reset state - getStateType");
// Empty state instance to get default property values from
Profiler.enter("ApplicationConnection recursivelyDetach reset state - createInstance");
Object defaultState = stateType.createInstance();
Profiler.leave("ApplicationConnection recursivelyDetach reset state - createInstance");
if (connector instanceof AbstractConnector) {
// optimization as the loop setting properties is very
// slow, especially on IE8
replaceState((AbstractConnector) connector,
defaultState);
} else {
SharedState state = connector.getState();
Profiler.enter("ApplicationConnection recursivelyDetach reset state - properties");
JsArrayObject properties = stateType
.getPropertiesAsArray();
int size = properties.size();
for (int i = 0; i < size; i++) {
Property property = properties.get(i);
property.setValue(state,
property.getValue(defaultState));
}
Profiler.leave("ApplicationConnection recursivelyDetach reset state - properties");
}
} catch (NoDataException e) {
throw new RuntimeException("Can't reset state for "
+ Util.getConnectorString(connector), e);
} finally {
Profiler.leave("ApplicationConnection recursivelyDetach reset state");
}
Profiler.enter("ApplicationConnection recursivelyDetach perform detach");
/*
* Recursively detach children to make sure they get
* setParent(null) and hierarchy change events as needed.
*/
for (ServerConnector child : connector.getChildren()) {
/*
* Server doesn't send updated child data for removed
* connectors -> ignore child that still seems to be a child
* of this connector although it has been moved to some part
* of the hierarchy that is not detached.
*/
if (child.getParent() != connector) {
continue;
}
recursivelyDetach(child, events);
}
Profiler.leave("ApplicationConnection recursivelyDetach perform detach");
/*
* Clear child list and parent
*/
Profiler.enter("ApplicationConnection recursivelyDetach clear children and parent");
connector
.setChildren(Collections. emptyList());
connector.setParent(null);
Profiler.leave("ApplicationConnection recursivelyDetach clear children and parent");
/*
* Create an artificial hierarchy event for containers to give
* it a chance to clean up after its children if it has any
*/
Profiler.enter("ApplicationConnection recursivelyDetach create hierarchy event");
if (connector instanceof HasComponentsConnector) {
HasComponentsConnector ccc = (HasComponentsConnector) connector;
List oldChildren = ccc
.getChildComponents();
if (!oldChildren.isEmpty()) {
/*
* HasComponentsConnector has a separate child component
* list that should also be cleared
*/
ccc.setChildComponents(Collections
. emptyList());
// Create event and add it to the list of pending events
ConnectorHierarchyChangeEvent event = GWT
.create(ConnectorHierarchyChangeEvent.class);
event.setConnector(connector);
event.setOldChildren(oldChildren);
events.add(event);
}
}
Profiler.leave("ApplicationConnection recursivelyDetach create hierarchy event");
}
private native void replaceState(AbstractConnector connector,
Object defaultState)
/*-{
connector.@com.vaadin.client.ui.AbstractConnector::state = defaultState;
}-*/;
private void handleRpcInvocations(ValueMap json) {
if (json.containsKey("rpc")) {
Profiler.enter("handleRpcInvocations");
VConsole.log(" * Performing server to client RPC calls");
JSONArray rpcCalls = new JSONArray(
json.getJavaScriptObject("rpc"));
int rpcLength = rpcCalls.size();
for (int i = 0; i < rpcLength; i++) {
try {
JSONArray rpcCall = (JSONArray) rpcCalls.get(i);
rpcManager.parseAndApplyInvocation(rpcCall,
ApplicationConnection.this);
} catch (final Throwable e) {
VConsole.error(e);
}
}
Profiler.leave("handleRpcInvocations");
}
}
};
ApplicationConfiguration.runWhenDependenciesLoaded(c);
}
private void loadStyleDependencies(JsArrayString dependencies) {
// Assuming no reason to interpret in a defined order
ResourceLoadListener resourceLoadListener = new ResourceLoadListener() {
@Override
public void onLoad(ResourceLoadEvent event) {
ApplicationConfiguration.endDependencyLoading();
}
@Override
public void onError(ResourceLoadEvent event) {
VConsole.error(event.getResourceUrl()
+ " could not be loaded, or the load detection failed because the stylesheet is empty.");
// The show must go on
onLoad(event);
}
};
ResourceLoader loader = ResourceLoader.get();
for (int i = 0; i < dependencies.length(); i++) {
String url = translateVaadinUri(dependencies.get(i));
ApplicationConfiguration.startDependencyLoading();
loader.loadStylesheet(url, resourceLoadListener);
}
}
private void loadScriptDependencies(final JsArrayString dependencies) {
if (dependencies.length() == 0) {
return;
}
// Listener that loads the next when one is completed
ResourceLoadListener resourceLoadListener = new ResourceLoadListener() {
@Override
public void onLoad(ResourceLoadEvent event) {
if (dependencies.length() != 0) {
String url = translateVaadinUri(dependencies.shift());
ApplicationConfiguration.startDependencyLoading();
// Load next in chain (hopefully already preloaded)
event.getResourceLoader().loadScript(url, this);
}
// Call start for next before calling end for current
ApplicationConfiguration.endDependencyLoading();
}
@Override
public void onError(ResourceLoadEvent event) {
VConsole.error(event.getResourceUrl() + " could not be loaded.");
// The show must go on
onLoad(event);
}
};
ResourceLoader loader = ResourceLoader.get();
// Start chain by loading first
String url = translateVaadinUri(dependencies.shift());
ApplicationConfiguration.startDependencyLoading();
loader.loadScript(url, resourceLoadListener);
// Preload all remaining
for (int i = 0; i < dependencies.length(); i++) {
String preloadUrl = translateVaadinUri(dependencies.get(i));
loader.preloadResource(preloadUrl, null);
}
}
// Redirect browser, null reloads current page
private static native void redirect(String url)
/*-{
if (url) {
$wnd.location = url;
} else {
$wnd.location.reload(false);
}
}-*/;
private void addVariableToQueue(String connectorId, String variableName,
Object value, boolean immediate) {
boolean lastOnly = !immediate;
// note that type is now deduced from value
addMethodInvocationToQueue(new LegacyChangeVariablesInvocation(
connectorId, variableName, value), lastOnly, lastOnly);
}
/**
* Adds an explicit RPC method invocation to the send queue.
*
* @since 7.0
*
* @param invocation
* RPC method invocation
* @param delayed
* false
to trigger sending within a short time
* window (possibly combining subsequent calls to a single
* request), true
to let the framework delay sending
* of RPC calls and variable changes until the next non-delayed
* change
* @param lastOnly
* true
to remove all previously delayed invocations
* of the same method that were also enqueued with lastonly set
* to true
. false
to add invocation to
* the end of the queue without touching previously enqueued
* invocations.
*/
public void addMethodInvocationToQueue(MethodInvocation invocation,
boolean delayed, boolean lastOnly) {
if (!isApplicationRunning()) {
getLogger()
.warning(
"Trying to invoke method on not yet started or stopped application");
return;
}
String tag;
if (lastOnly) {
tag = invocation.getLastOnlyTag();
assert !tag.matches("\\d+") : "getLastOnlyTag value must have at least one non-digit character";
pendingInvocations.remove(tag);
} else {
tag = Integer.toString(lastInvocationTag++);
}
pendingInvocations.put(tag, invocation);
if (!delayed) {
sendPendingVariableChanges();
}
}
/**
* Removes any pending invocation of the given method from the queue
*
* @param invocation
* The invocation to remove
*/
public void removePendingInvocations(MethodInvocation invocation) {
Iterator iter = pendingInvocations.values()
.iterator();
while (iter.hasNext()) {
MethodInvocation mi = iter.next();
if (mi.equals(invocation)) {
iter.remove();
}
}
}
/**
* This method sends currently queued variable changes to server. It is
* called when immediate variable update must happen.
*
* To ensure correct order for variable changes (due servers multithreading
* or network), we always wait for active request to be handler before
* sending a new one. If there is an active request, we will put varible
* "burst" to queue that will be purged after current request is handled.
*
*/
public void sendPendingVariableChanges() {
if (!deferedSendPending) {
deferedSendPending = true;
Scheduler.get().scheduleFinally(sendPendingCommand);
}
}
private final ScheduledCommand sendPendingCommand = new ScheduledCommand() {
@Override
public void execute() {
deferedSendPending = false;
doSendPendingVariableChanges();
}
};
private boolean deferedSendPending = false;
private void doSendPendingVariableChanges() {
if (isApplicationRunning()) {
if (hasActiveRequest() || (push != null && !push.isActive())) {
// skip empty queues if there are pending bursts to be sent
if (pendingInvocations.size() > 0 || pendingBursts.size() == 0) {
pendingBursts.add(pendingInvocations);
pendingInvocations = new LinkedHashMap();
// Keep tag string short
lastInvocationTag = 0;
}
} else {
buildAndSendVariableBurst(pendingInvocations);
}
} else {
getLogger()
.warning(
"Trying to send variable changes from not yet started or stopped application");
return;
}
}
/**
* Build the variable burst and send it to server.
*
* When sync is forced, we also force sending of all pending variable-bursts
* at the same time. This is ok as we can assume that DOM will never be
* updated after this.
*
* @param pendingInvocations
* List of RPC method invocations to send
*/
private void buildAndSendVariableBurst(
LinkedHashMap pendingInvocations) {
final StringBuffer req = new StringBuffer();
while (!pendingInvocations.isEmpty()) {
if (ApplicationConfiguration.isDebugMode()) {
Util.logVariableBurst(this, pendingInvocations.values());
}
JSONArray reqJson = new JSONArray();
for (MethodInvocation invocation : pendingInvocations.values()) {
JSONArray invocationJson = new JSONArray();
invocationJson.set(0,
new JSONString(invocation.getConnectorId()));
invocationJson.set(1,
new JSONString(invocation.getInterfaceName()));
invocationJson.set(2,
new JSONString(invocation.getMethodName()));
JSONArray paramJson = new JSONArray();
Type[] parameterTypes = null;
if (!isLegacyVariableChange(invocation)
&& !isJavascriptRpc(invocation)) {
try {
Type type = new Type(invocation.getInterfaceName(),
null);
Method method = type.getMethod(invocation
.getMethodName());
parameterTypes = method.getParameterTypes();
} catch (NoDataException e) {
throw new RuntimeException("No type data for "
+ invocation.toString(), e);
}
}
for (int i = 0; i < invocation.getParameters().length; ++i) {
// TODO non-static encoder?
Type type = null;
if (parameterTypes != null) {
type = parameterTypes[i];
}
Object value = invocation.getParameters()[i];
paramJson.set(i, JsonEncoder.encode(value, type, this));
}
invocationJson.set(3, paramJson);
reqJson.set(reqJson.size(), invocationJson);
}
// escape burst separators (if any)
req.append(escapeBurstContents(reqJson.toString()));
pendingInvocations.clear();
// Keep tag string short
lastInvocationTag = 0;
}
// Include the browser detail parameters if they aren't already sent
String extraParams;
if (!getConfiguration().isBrowserDetailsSent()) {
extraParams = getNativeBrowserDetailsParameters(getConfiguration()
.getRootPanelId());
getConfiguration().setBrowserDetailsSent();
} else {
extraParams = "";
}
if (!getConfiguration().isWidgetsetVersionSent()) {
if (!extraParams.isEmpty()) {
extraParams += "&";
}
String widgetsetVersion = Version.getFullVersion();
extraParams += "v-wsver=" + widgetsetVersion;
getConfiguration().setWidgetsetVersionSent();
}
makeUidlRequest(req.toString(), extraParams);
}
private boolean isJavascriptRpc(MethodInvocation invocation) {
return invocation instanceof JavaScriptMethodInvocation;
}
private boolean isLegacyVariableChange(MethodInvocation invocation) {
return ApplicationConstants.UPDATE_VARIABLE_METHOD.equals(invocation
.getInterfaceName())
&& ApplicationConstants.UPDATE_VARIABLE_METHOD
.equals(invocation.getMethodName());
}
/**
* Sends a new value for the given paintables given variable to the server.
*
* The update is actually queued to be sent at a suitable time. If immediate
* is true, the update is sent as soon as possible. If immediate is false,
* the update will be sent along with the next immediate update.
*
*
* @param paintableId
* the id of the paintable that owns the variable
* @param variableName
* the name of the variable
* @param newValue
* the new value to be sent
* @param immediate
* true if the update is to be sent as soon as possible
*/
public void updateVariable(String paintableId, String variableName,
ServerConnector newValue, boolean immediate) {
addVariableToQueue(paintableId, variableName, newValue, immediate);
}
/**
* Sends a new value for the given paintables given variable to the server.
*
* The update is actually queued to be sent at a suitable time. If immediate
* is true, the update is sent as soon as possible. If immediate is false,
* the update will be sent along with the next immediate update.
*
*
* @param paintableId
* the id of the paintable that owns the variable
* @param variableName
* the name of the variable
* @param newValue
* the new value to be sent
* @param immediate
* true if the update is to be sent as soon as possible
*/
public void updateVariable(String paintableId, String variableName,
String newValue, boolean immediate) {
addVariableToQueue(paintableId, variableName, newValue, immediate);
}
/**
* Sends a new value for the given paintables given variable to the server.
*
* The update is actually queued to be sent at a suitable time. If immediate
* is true, the update is sent as soon as possible. If immediate is false,
* the update will be sent along with the next immediate update.
*
*
* @param paintableId
* the id of the paintable that owns the variable
* @param variableName
* the name of the variable
* @param newValue
* the new value to be sent
* @param immediate
* true if the update is to be sent as soon as possible
*/
public void updateVariable(String paintableId, String variableName,
int newValue, boolean immediate) {
addVariableToQueue(paintableId, variableName, newValue, immediate);
}
/**
* Sends a new value for the given paintables given variable to the server.
*
* The update is actually queued to be sent at a suitable time. If immediate
* is true, the update is sent as soon as possible. If immediate is false,
* the update will be sent along with the next immediate update.
*
*
* @param paintableId
* the id of the paintable that owns the variable
* @param variableName
* the name of the variable
* @param newValue
* the new value to be sent
* @param immediate
* true if the update is to be sent as soon as possible
*/
public void updateVariable(String paintableId, String variableName,
long newValue, boolean immediate) {
addVariableToQueue(paintableId, variableName, newValue, immediate);
}
/**
* Sends a new value for the given paintables given variable to the server.
*
* The update is actually queued to be sent at a suitable time. If immediate
* is true, the update is sent as soon as possible. If immediate is false,
* the update will be sent along with the next immediate update.
*
*
* @param paintableId
* the id of the paintable that owns the variable
* @param variableName
* the name of the variable
* @param newValue
* the new value to be sent
* @param immediate
* true if the update is to be sent as soon as possible
*/
public void updateVariable(String paintableId, String variableName,
float newValue, boolean immediate) {
addVariableToQueue(paintableId, variableName, newValue, immediate);
}
/**
* Sends a new value for the given paintables given variable to the server.
*
* The update is actually queued to be sent at a suitable time. If immediate
* is true, the update is sent as soon as possible. If immediate is false,
* the update will be sent along with the next immediate update.
*
*
* @param paintableId
* the id of the paintable that owns the variable
* @param variableName
* the name of the variable
* @param newValue
* the new value to be sent
* @param immediate
* true if the update is to be sent as soon as possible
*/
public void updateVariable(String paintableId, String variableName,
double newValue, boolean immediate) {
addVariableToQueue(paintableId, variableName, newValue, immediate);
}
/**
* Sends a new value for the given paintables given variable to the server.
*
* The update is actually queued to be sent at a suitable time. If immediate
* is true, the update is sent as soon as possible. If immediate is false,
* the update will be sent along with the next immediate update.
*
*
* @param paintableId
* the id of the paintable that owns the variable
* @param variableName
* the name of the variable
* @param newValue
* the new value to be sent
* @param immediate
* true if the update is to be sent as soon as possible
*/
public void updateVariable(String paintableId, String variableName,
boolean newValue, boolean immediate) {
addVariableToQueue(paintableId, variableName, newValue, immediate);
}
/**
* Sends a new value for the given paintables given variable to the server.
*
* The update is actually queued to be sent at a suitable time. If immediate
* is true, the update is sent as soon as possible. If immediate is false,
* the update will be sent along with the next immediate update.
*
*
* @param paintableId
* the id of the paintable that owns the variable
* @param variableName
* the name of the variable
* @param map
* the new values to be sent
* @param immediate
* true if the update is to be sent as soon as possible
*/
public void updateVariable(String paintableId, String variableName,
Map map, boolean immediate) {
addVariableToQueue(paintableId, variableName, map, immediate);
}
/**
* Sends a new value for the given paintables given variable to the server.
*
* The update is actually queued to be sent at a suitable time. If immediate
* is true, the update is sent as soon as possible. If immediate is false,
* the update will be sent along with the next immediate update.
*
* A null array is sent as an empty array.
*
* @param paintableId
* the id of the paintable that owns the variable
* @param variableName
* the name of the variable
* @param values
* the new value to be sent
* @param immediate
* true if the update is to be sent as soon as possible
*/
public void updateVariable(String paintableId, String variableName,
String[] values, boolean immediate) {
addVariableToQueue(paintableId, variableName, values, immediate);
}
/**
* Sends a new value for the given paintables given variable to the server.
*
* The update is actually queued to be sent at a suitable time. If immediate
* is true, the update is sent as soon as possible. If immediate is false,
* the update will be sent along with the next immediate update.
*
* A null array is sent as an empty array.
*
*
* @param paintableId
* the id of the paintable that owns the variable
* @param variableName
* the name of the variable
* @param values
* the new value to be sent
* @param immediate
* true if the update is to be sent as soon as possible
*/
public void updateVariable(String paintableId, String variableName,
Object[] values, boolean immediate) {
addVariableToQueue(paintableId, variableName, values, immediate);
}
/**
* Encode burst separator characters in a String for transport over the
* network. This protects from separator injection attacks.
*
* @param value
* to encode
* @return encoded value
*/
protected String escapeBurstContents(String value) {
final StringBuilder result = new StringBuilder();
for (int i = 0; i < value.length(); ++i) {
char character = value.charAt(i);
switch (character) {
case VAR_ESCAPE_CHARACTER:
// fall-through - escape character is duplicated
case VAR_BURST_SEPARATOR:
result.append(VAR_ESCAPE_CHARACTER);
// encode as letters for easier reading
result.append(((char) (character + 0x30)));
break;
default:
// the char is not a special one - add it to the result as is
result.append(character);
break;
}
}
return result.toString();
}
/**
* Does absolutely nothing. Replaced by {@link LayoutManager}.
*
* @param container
* @deprecated As of 7.0, serves no purpose
*/
@Deprecated
public void runDescendentsLayout(HasWidgets container) {
}
/**
* This will cause re-layouting of all components. Mainly used for
* development. Published to JavaScript.
*/
public void forceLayout() {
Duration duration = new Duration();
layoutManager.forceLayout();
VConsole.log("forceLayout in " + duration.elapsedMillis() + " ms");
}
/**
* Returns false
*
* @param paintable
* @return false, always
* @deprecated As of 7.0, serves no purpose
*/
@Deprecated
private boolean handleComponentRelativeSize(ComponentConnector paintable) {
return false;
}
/**
* Returns false
*
* @param paintable
* @return false, always
* @deprecated As of 7.0, serves no purpose
*/
@Deprecated
public boolean handleComponentRelativeSize(Widget widget) {
return handleComponentRelativeSize(connectorMap.getConnector(widget));
}
@Deprecated
public ComponentConnector getPaintable(UIDL uidl) {
// Non-component connectors shouldn't be painted from legacy connectors
return (ComponentConnector) getConnector(uidl.getId(),
Integer.parseInt(uidl.getTag()));
}
/**
* Get either an existing ComponentConnector or create a new
* ComponentConnector with the given type and id.
*
* If a ComponentConnector with the given id already exists, returns it.
* Otherwise creates and registers a new ComponentConnector of the given
* type.
*
* @param connectorId
* Id of the paintable
* @param connectorType
* Type of the connector, as passed from the server side
*
* @return Either an existing ComponentConnector or a new ComponentConnector
* of the given type
*/
public ServerConnector getConnector(String connectorId, int connectorType) {
if (!connectorMap.hasConnector(connectorId)) {
return createAndRegisterConnector(connectorId, connectorType);
}
return connectorMap.getConnector(connectorId);
}
/**
* Creates a new ServerConnector with the given type and id.
*
* Creates and registers a new ServerConnector of the given type. Should
* never be called with the connector id of an existing connector.
*
* @param connectorId
* Id of the new connector
* @param connectorType
* Type of the connector, as passed from the server side
*
* @return A new ServerConnector of the given type
*/
private ServerConnector createAndRegisterConnector(String connectorId,
int connectorType) {
Profiler.enter("ApplicationConnection.createAndRegisterConnector");
// Create and register a new connector with the given type
ServerConnector p = widgetSet.createConnector(connectorType,
configuration);
connectorMap.registerConnector(connectorId, p);
p.doInit(connectorId, this);
Profiler.leave("ApplicationConnection.createAndRegisterConnector");
return p;
}
/**
* Gets a recource that has been pre-loaded via UIDL, such as custom
* layouts.
*
* @param name
* identifier of the resource to get
* @return the resource
*/
public String getResource(String name) {
return resourcesMap.get(name);
}
/**
* Singleton method to get instance of app's context menu.
*
* @return VContextMenu object
*/
public VContextMenu getContextMenu() {
if (contextMenu == null) {
contextMenu = new VContextMenu();
contextMenu.setOwner(uIConnector.getWidget());
DOM.setElementProperty(contextMenu.getElement(), "id",
"PID_VAADIN_CM");
}
return contextMenu;
}
/**
* Translates custom protocols in UIDL URI's to be recognizable by browser.
* All uri's from UIDL should be routed via this method before giving them
* to browser due URI's in UIDL may contain custom protocols like theme://.
*
* @param uidlUri
* Vaadin URI from uidl
* @return translated URI ready for browser
*/
public String translateVaadinUri(String uidlUri) {
if (uidlUri == null) {
return null;
}
if (uidlUri.startsWith("theme://")) {
final String themeUri = configuration.getThemeUri();
if (themeUri == null) {
VConsole.error("Theme not set: ThemeResource will not be found. ("
+ uidlUri + ")");
}
uidlUri = themeUri + uidlUri.substring(7);
}
if (uidlUri.startsWith(ApplicationConstants.PUBLISHED_PROTOCOL_PREFIX)) {
// getAppUri *should* always end with /
// substring *should* always start with / (published:///foo.bar
// without published://)
uidlUri = ApplicationConstants.APP_PROTOCOL_PREFIX
+ ApplicationConstants.PUBLISHED_FILE_PATH
+ uidlUri
.substring(ApplicationConstants.PUBLISHED_PROTOCOL_PREFIX
.length());
// Let translation of app:// urls take care of the rest
}
if (uidlUri.startsWith(ApplicationConstants.APP_PROTOCOL_PREFIX)) {
String relativeUrl = uidlUri
.substring(ApplicationConstants.APP_PROTOCOL_PREFIX
.length());
ApplicationConfiguration conf = getConfiguration();
String serviceUrl = conf.getServiceUrl();
if (conf.useServiceUrlPathParam()) {
// Should put path in v-resourcePath parameter and append query
// params to base portlet url
String[] parts = relativeUrl.split("\\?", 2);
String path = parts[0];
// If there's a "?" followed by something, append it as a query
// string to the base URL
if (parts.length > 1) {
String appUrlParams = parts[1];
serviceUrl = addGetParameters(serviceUrl, appUrlParams);
}
if (!path.startsWith("/")) {
path = '/' + path;
}
String pathParam = conf.getServiceUrlParameterName() + "="
+ URL.encodeQueryString(path);
serviceUrl = addGetParameters(serviceUrl, pathParam);
uidlUri = serviceUrl;
} else {
uidlUri = serviceUrl + relativeUrl;
}
}
if (uidlUri.startsWith(ApplicationConstants.VAADIN_PROTOCOL_PREFIX)) {
final String vaadinUri = configuration.getVaadinDirUrl();
String relativeUrl = uidlUri
.substring(ApplicationConstants.VAADIN_PROTOCOL_PREFIX
.length());
uidlUri = vaadinUri + relativeUrl;
}
return uidlUri;
}
/**
* Gets the URI for the current theme. Can be used to reference theme
* resources.
*
* @return URI to the current theme
*/
public String getThemeUri() {
return configuration.getThemeUri();
}
/**
* Listens for Notification hide event, and redirects. Used for system
* messages, such as session expired.
*
*/
private class NotificationRedirect implements VNotification.EventListener {
String url;
NotificationRedirect(String url) {
this.url = url;
}
@Override
public void notificationHidden(HideEvent event) {
redirect(url);
}
}
/* Extended title handling */
private final VTooltip tooltip;
private ConnectorMap connectorMap = GWT.create(ConnectorMap.class);
protected String getUidlSecurityKey() {
return getCsrfToken();
}
/**
* Gets the token (aka double submit cookie) that the server uses to protect
* against Cross Site Request Forgery attacks.
*
* @return the CSRF token string
*/
public String getCsrfToken() {
return csrfToken;
}
/**
* Use to notify that the given component's caption has changed; layouts may
* have to be recalculated.
*
* @param component
* the Paintable whose caption has changed
* @deprecated As of 7.0.2, has not had any effect for a long time
*/
@Deprecated
public void captionSizeUpdated(Widget widget) {
// This doesn't do anything, it's just kept here for compatibility
}
/**
* Gets the main view
*
* @return the main view
*/
public UIConnector getUIConnector() {
return uIConnector;
}
/**
* Gets the {@link ApplicationConfiguration} for the current application.
*
* @see ApplicationConfiguration
* @return the configuration for this application
*/
public ApplicationConfiguration getConfiguration() {
return configuration;
}
/**
* Checks if there is a registered server side listener for the event. The
* list of events which has server side listeners is updated automatically
* before the component is updated so the value is correct if called from
* updatedFromUIDL.
*
* @param paintable
* The connector to register event listeners for
* @param eventIdentifier
* The identifier for the event
* @return true if at least one listener has been registered on server side
* for the event identified by eventIdentifier.
* @deprecated As of 7.0. Use
* {@link AbstractComponentState#hasEventListener(String)}
* instead
*/
@Deprecated
public boolean hasEventListeners(ComponentConnector paintable,
String eventIdentifier) {
return paintable.hasEventListener(eventIdentifier);
}
/**
* Adds the get parameters to the uri and returns the new uri that contains
* the parameters.
*
* @param uri
* The uri to which the parameters should be added.
* @param extraParams
* One or more parameters in the format "a=b" or "c=d&e=f". An
* empty string is allowed but will not modify the url.
* @return The modified URI with the get parameters in extraParams added.
*/
public static String addGetParameters(String uri, String extraParams) {
if (extraParams == null || extraParams.length() == 0) {
return uri;
}
// RFC 3986: The query component is indicated by the first question
// mark ("?") character and terminated by a number sign ("#") character
// or by the end of the URI.
String fragment = null;
int hashPosition = uri.indexOf('#');
if (hashPosition != -1) {
// Fragment including "#"
fragment = uri.substring(hashPosition);
// The full uri before the fragment
uri = uri.substring(0, hashPosition);
}
if (uri.contains("?")) {
uri += "&";
} else {
uri += "?";
}
uri += extraParams;
if (fragment != null) {
uri += fragment;
}
return uri;
}
ConnectorMap getConnectorMap() {
return connectorMap;
}
/**
* @deprecated As of 7.0. No longer serves any purpose.
*/
@Deprecated
public void unregisterPaintable(ServerConnector p) {
VConsole.log("unregisterPaintable (unnecessarily) called for "
+ Util.getConnectorString(p));
}
/**
* Get VTooltip instance related to application connection
*
* @return VTooltip instance
*/
public VTooltip getVTooltip() {
return tooltip;
}
/**
* Method provided for backwards compatibility. Duties previously done by
* this method is now handled by the state change event handler in
* AbstractComponentConnector. The only function this method has is to
* return true if the UIDL is a "cached" update.
*
* @param component
* @param uidl
* @param manageCaption
* @deprecated As of 7.0, no longer serves any purpose
* @return
*/
@Deprecated
public boolean updateComponent(Widget component, UIDL uidl,
boolean manageCaption) {
ComponentConnector connector = getConnectorMap()
.getConnector(component);
if (!AbstractComponentConnector.isRealUpdate(uidl)) {
return true;
}
if (!manageCaption) {
VConsole.error(Util.getConnectorString(connector)
+ " called updateComponent with manageCaption=false. The parameter was ignored - override delegateCaption() to return false instead. It is however not recommended to use caption this way at all.");
}
return false;
}
/**
* @deprecated As of 7.0. Use
* {@link AbstractComponentConnector#hasEventListener(String)}
* instead
*/
@Deprecated
public boolean hasEventListeners(Widget widget, String eventIdentifier) {
ComponentConnector connector = getConnectorMap().getConnector(widget);
if (connector == null) {
/*
* No connector will exist in cases where Vaadin widgets have been
* re-used without implementing server<->client communication.
*/
return false;
}
return hasEventListeners(getConnectorMap().getConnector(widget),
eventIdentifier);
}
LayoutManager getLayoutManager() {
return layoutManager;
}
/**
* Schedules a heartbeat request to occur after the configured heartbeat
* interval elapses if the interval is a positive number. Otherwise, does
* nothing.
*
* @see #sendHeartbeat()
* @see ApplicationConfiguration#getHeartbeatInterval()
*/
protected void scheduleHeartbeat() {
final int interval = getConfiguration().getHeartbeatInterval();
if (interval > 0) {
VConsole.log("Scheduling heartbeat in " + interval + " seconds");
new Timer() {
@Override
public void run() {
sendHeartbeat();
}
}.schedule(interval * 1000);
}
}
/**
* Sends a heartbeat request to the server.
*
* Heartbeat requests are used to inform the server that the client-side is
* still alive. If the client page is closed or the connection lost, the
* server will eventually close the inactive UI.
*
* TODO: Improved error handling, like in doUidlRequest().
*
* @see #scheduleHeartbeat()
*/
protected void sendHeartbeat() {
final String uri = addGetParameters(
translateVaadinUri(ApplicationConstants.APP_PROTOCOL_PREFIX
+ ApplicationConstants.HEARTBEAT_PATH + '/'),
UIConstants.UI_ID_PARAMETER + "="
+ getConfiguration().getUIId());
final RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, uri);
final RequestCallback callback = new RequestCallback() {
@Override
public void onResponseReceived(Request request, Response response) {
int status = response.getStatusCode();
if (status == Response.SC_OK) {
// TODO Permit retry in some error situations
VConsole.log("Heartbeat response OK");
scheduleHeartbeat();
} else if (status == Response.SC_GONE) {
showSessionExpiredError(null);
} else {
VConsole.error("Failed sending heartbeat to server. Error code: "
+ status);
}
}
@Override
public void onError(Request request, Throwable exception) {
VConsole.error("Exception sending heartbeat: " + exception);
}
};
rb.setCallback(callback);
try {
VConsole.log("Sending heartbeat request...");
rb.send();
} catch (RequestException re) {
callback.onError(null, re);
}
}
/**
* Timer used to make sure that no misbehaving components can delay response
* handling forever.
*/
Timer forceHandleMessage = new Timer() {
@Override
public void run() {
VConsole.log("WARNING: reponse handling was never resumed, forcibly removing locks...");
responseHandlingLocks.clear();
handlePendingMessages();
}
};
/**
* This method can be used to postpone rendering of a response for a short
* period of time (e.g. to avoid the rendering process during animation).
*
* @param lock
*/
public void suspendReponseHandling(Object lock) {
responseHandlingLocks.add(lock);
}
/**
* Resumes the rendering process once all locks have been removed.
*
* @param lock
*/
public void resumeResponseHandling(Object lock) {
responseHandlingLocks.remove(lock);
if (responseHandlingLocks.isEmpty()) {
// Cancel timer that breaks the lock
forceHandleMessage.cancel();
if (!pendingUIDLMessages.isEmpty()) {
VConsole.log("No more response handling locks, handling pending requests.");
handlePendingMessages();
}
}
}
/**
* Handles all pending UIDL messages queued while response handling was
* suspended.
*/
private void handlePendingMessages() {
if (!pendingUIDLMessages.isEmpty()) {
/*
* Clear the list before processing enqueued messages to support
* reentrancy
*/
List pendingMessages = pendingUIDLMessages;
pendingUIDLMessages = new ArrayList();
for (PendingUIDLMessage pending : pendingMessages) {
handleReceivedJSONMessage(pending.getStart(),
pending.getJsonText(), pending.getJson());
}
}
}
private boolean handleErrorInDelegate(String details, int statusCode) {
if (communicationErrorDelegate == null) {
return false;
}
return communicationErrorDelegate.onError(details, statusCode);
}
/**
* Sets the delegate that is called whenever a communication error occurrs.
*
* @param delegate
* the delegate.
*/
public void setCommunicationErrorDelegate(CommunicationErrorHandler delegate) {
communicationErrorDelegate = delegate;
}
public void setApplicationRunning(boolean running) {
if (applicationRunning && !running) {
eventBus.fireEvent(new ApplicationStoppedEvent());
}
applicationRunning = running;
}
public boolean isApplicationRunning() {
return applicationRunning;
}
public HandlerRegistration addHandler(
GwtEvent.Type type, H handler) {
return eventBus.addHandler(type, handler);
}
/**
* Calls {@link ComponentConnector#flush()} on the active connector. Does
* nothing if there is no active (focused) connector.
*/
public void flushActiveConnector() {
ComponentConnector activeConnector = getActiveConnector();
if (activeConnector == null) {
return;
}
activeConnector.flush();
}
/**
* Gets the active connector for focused element in browser.
*
* @return Connector for focused element or null.
*/
private ComponentConnector getActiveConnector() {
Element focusedElement = Util.getFocusedElement();
if (focusedElement == null) {
return null;
}
return Util.getConnectorForElement(this, getUIConnector().getWidget(),
focusedElement);
}
/**
* Sets the status for the push connection.
*
* @param enabled
* true
to enable the push connection;
* false
to disable the push connection.
*/
public void setPushEnabled(boolean enabled) {
final PushConfigurationState pushState = uIConnector.getState().pushConfiguration;
if (enabled && push == null) {
push = GWT.create(PushConnection.class);
push.init(this, pushState, new CommunicationErrorHandler() {
@Override
public boolean onError(String details, int statusCode) {
showCommunicationError(details, statusCode);
return true;
}
});
} else if (!enabled && push != null && push.isActive()) {
push.disconnect(new Command() {
@Override
public void execute() {
push = null;
/*
* If push has been enabled again while we were waiting for
* the old connection to disconnect, now is the right time
* to open a new connection
*/
if (pushState.mode.isEnabled()) {
setPushEnabled(true);
}
/*
* Send anything that was enqueued while we waited for the
* connection to close
*/
if (pendingInvocations.size() > 0) {
sendPendingVariableChanges();
}
}
});
}
}
public void handlePushMessage(String message) {
handleJSONText(message, 200);
}
/**
* Returns a human readable string representation of the method used to
* communicate with the server.
*
* @since 7.1
* @return A string representation of the current transport type
*/
public String getCommunicationMethodName() {
if (push != null) {
return "Push (" + push.getTransportType() + ")";
} else {
return "XHR";
}
}
private static Logger getLogger() {
return Logger.getLogger(ApplicationConnection.class.getName());
}
}