📄 desktopimpl.java
字号:
public Collection getComponents() { return _comps.values(); } public Component getComponentByUuid(String uuid) { final Component comp = (Component)_comps.get(uuid); if (comp == null) throw new ComponentNotFoundException("Component not found: "+uuid); return comp; } public Component getComponentByUuidIfAny(String uuid) { return (Component)_comps.get(uuid); } public void addComponent(Component comp) { //to avoid misuse, check whether new comp belongs to the same device type final LanguageDefinition langdef = comp.getDefinition().getLanguageDefinition(); if (langdef != null && !_devType.equals(langdef.getDeviceType())) throw new UiException("Component, "+comp+", does not belong to the same device type of the desktop, "+_devType); final Object old = _comps.put(comp.getUuid(), comp); if (old != comp && old != null) { _comps.put(((Component)old).getUuid(), old); //recover throw new InternalError("Caller shall prevent it: Register a component twice: "+comp); } } public void removeComponent(Component comp) { _comps.remove(comp.getUuid()); } public Map getAttributes() { return _attrs; } public Object getAttribute(String name) { return _attrs.get(name); } public Object setAttribute(String name, Object value) { return value != null ? _attrs.put(name, value): removeAttribute(name); } public Object removeAttribute(String name) { return _attrs.remove(name); } public WebApp getWebApp() { return _wapp; } public String getRequestPath() { return _path; } public String getCurrentDirectory() { return _dir; } public void setCurrentDirectory(String dir) { if (dir == null) { dir = ""; } else { final int len = dir.length() - 1; if (len >= 0 && dir.charAt(len) != '/') dir += '/'; } _dir = dir; } public boolean isAlive() { return _comps != null; } //-- DesktopCtrl --// public RequestQueue getRequestQueue() { return _rque; } public void setExecution(Execution exec) { _exec = exec; } public int getNextKey() { return _nextKey++; } public String getNextUuid() { if ((_nextUuid & UUID_STEP_MASK) == 0) //run out _nextUuid = ((SessionCtrl)_sess).getNextUuidGroup(UUID_STEP); return ComponentsCtrl.toAutoId(_uuidPrefix, _nextUuid++); } private static final int UUID_STEP = 128, UUID_STEP_MASK = UUID_STEP - 1; public void addPage(Page page) { //We have to synchronize it due to getPage allows concurrent access synchronized (_pages) { final Object old = _pages.put(page.getId(), page); if (old != null) { _pages.put(((Page)old).getId(), old); //recover log.warning( page == old ? "Register a page twice: "+page: "Replicated ID: "+page+"; already used by "+old); } if (D.ON && log.debugable()) log.debug("After added, pages: "+_pages); } } public void removePage(Page page) { synchronized (_pages) { if (_pages.remove(page.getId()) == null) { log.warning("Removing non-exist page: "+page+"\nCurrent pages: "+_pages.values()); return; } if (D.ON && log.debugable()) log.debug("After removed, pages: "+_pages.values()); } removeComponents(page.getRoots()); ((PageCtrl)page).destroy(); } private void removeComponents(Collection comps) { for (Iterator it = comps.iterator(); it.hasNext();) { final Component comp = (Component)it.next(); removeComponents(comp.getChildren()); //recursive removeComponent(comp); } } public void setBookmarkByClient(String name) { _bookmark = name != null ? name: ""; } public void setId(String id) { if (!((ExecutionCtrl)_exec).isRecovering()) throw new IllegalStateException("Callable only in recovring"); if (id == null || id.length() <= 1 || id.charAt(0) != 'g') throw new IllegalArgumentException("Invalid desktop ID. You have to recover to the original value, not creating a new value: "+id); final DesktopCache dc = ((WebAppCtrl)_wapp).getDesktopCache(_sess); dc.removeDesktop(this); _id = id; updateUuidPrefix(); dc.addDesktop(this); } public void recoverDidFail(Throwable ex) { ((WebAppCtrl)_wapp).getDesktopCache(_sess).removeDesktop(this); } public int getResponseSequence(boolean advance) { if (advance && ++_respSeqId == MAX_RESPONSE_SEQUENCE) _respSeqId = 0; return _respSeqId; } public void setResponseSequence(int seqId) { if (seqId >= 1024) throw new IllegalArgumentException("Invalid sequence: "+seqId); _respSeqId = seqId < 0 ? MAX_RESPONSE_SEQUENCE - 1: seqId; } public void destroy() { for (Iterator it = _pages.values().iterator(); it.hasNext();) { final PageCtrl pgc = (PageCtrl)it.next(); try { pgc.destroy(); } catch (Throwable ex) { log.error("Failed to destroy "+pgc, ex); } } if (_spush != null) { _spush.stop(); _spush = null; } //theorectically, the following is not necessary, but, to be safe... _pages.clear(); _comps = _attrs = null; _meds = null; _rque = null; } public Collection getSuspendedThreads() { return ((WebAppCtrl)_wapp).getUiEngine().getSuspendedThreads(this); } public boolean ceaseSuspendedThread(EventProcessingThread evtthd, String cause) { return ((WebAppCtrl)_wapp).getUiEngine() .ceaseSuspendedThread(this, evtthd, cause); } //-- Object --// public String toString() { return "[Desktop "+_id+']'; } public void sessionWillPassivate(Session sess) { for (Iterator it = _pages.values().iterator(); it.hasNext();) ((PageCtrl)it.next()).sessionWillPassivate(this); if (_dev != null) _dev.sessionWillPassivate(this); } public void sessionDidActivate(Session sess) { _sess = sess; _wapp = sess.getWebApp(); if (_dev != null) _dev.sessionDidActivate(this); for (Iterator it = _pages.values().iterator(); it.hasNext();) ((PageCtrl)it.next()).sessionDidActivate(this); } //-- Serializable --// //NOTE: they must be declared as private private synchronized void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { s.defaultWriteObject(); willSerialize(_attrs.values()); Serializables.smartWrite(s, _attrs); } private void willSerialize(Collection c) { if (c != null) for (Iterator it = c.iterator(); it.hasNext();) willSerialize(it.next()); } private void willSerialize(Object o) { if (o instanceof DesktopSerializationListener) ((DesktopSerializationListener)o).willSerialize(this); } private synchronized void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { s.defaultReadObject(); init(); //get back _comps from _pages for (Iterator it = _pages.values().iterator(); it.hasNext();) for (Iterator e = ((Page)it.next()).getRoots().iterator(); e.hasNext();) addAllComponents((Component)e.next()); Serializables.smartRead(s, _attrs); didDeserialize(_attrs.values()); } private void didDeserialize(Collection c) { if (c != null) for (Iterator it = c.iterator(); it.hasNext();) didDeserialize(it.next()); } private void didDeserialize(Object o) { if (o instanceof DesktopSerializationListener) ((DesktopSerializationListener)o).didDeserialize(this); } private void addAllComponents(Component comp) { addComponent(comp); for (Iterator it = comp.getChildren().iterator(); it.hasNext();) addAllComponents((Component)it.next()); } public void addEventInterceptor(EventInterceptor ei) { _eis.addEventInterceptor(ei); } public boolean removeEventInterceptor(EventInterceptor ei) { return _eis.removeEventInterceptor(ei); } public Event beforeSendEvent(Event event) { event = _eis.beforeSendEvent(event); if (event != null) event = _wapp.getConfiguration().beforeSendEvent(event); return event; } public Event beforePostEvent(Event event) { event = _eis.beforePostEvent(event); if (event != null) event = _wapp.getConfiguration().beforePostEvent(event); return event; } public Event beforeProcessEvent(Event event) { event = _eis.beforeProcessEvent(event); if (event != null) event = _wapp.getConfiguration().beforeProcessEvent(event); return event; } public void afterProcessEvent(Event event) { _eis.afterProcessEvent(event); _wapp.getConfiguration().afterProcessEvent(event); } //Server Push// public boolean enableServerPush(boolean enable) { final boolean old = _spush != null; if (old != enable) { if (enable) { final Class cls = getDevice().getServerPushClass(); if (cls == null) throw new UiException("No server push defined. Make sure you are using the professional or enterprise edition, or you configured your own implementation"); try { _spush = (ServerPush)cls.newInstance(); } catch (Throwable ex) { throw UiException.Aide.wrap(ex, "Unable to instantiate "+cls); } _spush.start(this); } else { _spush.stop(); _spush = null; } } return old; } public boolean enableServerPush(ServerPush serverpush) { if (serverpush == null) return enableServerPush(false); final boolean old = _spush != null; if (!old || serverpush != _spush) { if (old) enableServerPush(false); _spush = serverpush; _spush.start(this); } return old; } public boolean isServerPushEnabled() { return _spush != null; } public ServerPush getServerPush() { return _spush; } public void setServerPushDelay(int min, int max, int factor) { if (_spush == null) throw new IllegalStateException("Not started"); _spush.setDelay(min, max, factor); } public void onPiggybackListened(Component comp, boolean listen) { //we don't cache comp to avoid the risk of memory leak (maybe not //a problem) //On the other hand, most pages don't listen onPiggyback at all, //so _piggybackListened is good enough to improve the performance if (listen) _piggybackListened = true; } public void onPiggyback() { if (_piggybackListened) { for (Iterator it = _pages.values().iterator(); it.hasNext();) { final Page p = (Page)it.next(); if (Executions.getCurrent().isAsyncUpdate(p)) { //ignore new created pages for (Iterator e = p.getRoots().iterator(); e.hasNext();) { final Component c = (Component)e.next(); if (Events.isListened(c, Events.ON_PIGGYBACK, false)) //asap+deferrable Events.postEvent(new Event(Events.ON_PIGGYBACK, c)); } } } } if (_spush != null) _spush.onPiggyback(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -