configuration.java
来自「非常接近C/S操作方式的Java Ajax框架-ZK 用ZK框架使你的B/S应」· Java 代码 · 共 1,073 行 · 第 1/3 页
JAVA
1,073 行
/* Configuration.java{{IS_NOTE Purpose: Description: History: Sun Mar 26 16:06:56 2006, Created by tomyeh}}IS_NOTECopyright (C) 2006 Potix Corporation. All Rights Reserved.{{IS_RIGHT This program is distributed under GPL Version 2.0 in the hope that it will be useful, but WITHOUT ANY WARRANTY.}}IS_RIGHT*/package org.zkoss.zk.ui.util;import java.util.Iterator;import java.util.List;import java.util.LinkedList;import java.util.Collections;import java.util.HashMap;import java.util.Map;import java.util.Set;import org.zkoss.lang.Classes;import org.zkoss.lang.PotentialDeadLockException;import org.zkoss.lang.Exceptions;import org.zkoss.util.WaitLock;import org.zkoss.util.logging.Log;import org.zkoss.zk.ui.Component;import org.zkoss.zk.ui.WebApp;import org.zkoss.zk.ui.Session;import org.zkoss.zk.ui.Desktop;import org.zkoss.zk.ui.Execution;import org.zkoss.zk.ui.Executions;import org.zkoss.zk.ui.Richlet;import org.zkoss.zk.ui.UiException;import org.zkoss.zk.ui.event.Event;import org.zkoss.zk.ui.event.EventThreadInit;import org.zkoss.zk.ui.event.EventThreadCleanup;import org.zkoss.zk.ui.event.EventThreadSuspend;import org.zkoss.zk.ui.event.EventThreadResume;import org.zkoss.zk.ui.util.SessionInit;import org.zkoss.zk.ui.util.SessionCleanup;import org.zkoss.zk.ui.sys.UiEngine;import org.zkoss.zk.ui.sys.DesktopCacheProvider;import org.zkoss.zk.ui.sys.LocaleProvider;import org.zkoss.zk.ui.sys.TimeZoneProvider;import org.zkoss.zk.ui.sys.UiFactory;import org.zkoss.zk.ui.impl.RichletConfigImpl;/** * The ZK configuration. * * <p>To retrieve the current configuration, use * {@link org.zkoss.zk.ui.WebApp#getConfiguration}. * * <p>Note: A {@link Configuration} instance can be assigned to at most one * {@link WebApp} instance. * * @author tomyeh */public class Configuration { private static final Log log = Log.lookup(Configuration.class); private final WebApp _wapp; private final List _evtInits = new LinkedList(), _evtCleans = new LinkedList(), _evtSusps = new LinkedList(), _evtResus = new LinkedList(), _appInits = new LinkedList(), _appCleans = new LinkedList(), _sessInits = new LinkedList(), _sessCleans = new LinkedList(), _dtInits = new LinkedList(), _dtCleans = new LinkedList(), _execInits = new LinkedList(), _execCleans = new LinkedList(); private final Map _prefs = Collections.synchronizedMap(new HashMap()), _richlets = new HashMap(); /** List(ErrorPage). */ private final List _errpgs = new LinkedList(); private Monitor _monitor; private String _timeoutUri; private final List _themeUris = new LinkedList(); private transient String[] _roThemeUris = new String[0]; private Class _uiengcls, _dcpcls, _uiftycls, _tzpcls, _lpcls; private Integer _dtTimeout, _dtMax, _sessTimeout, _evtThdMax; private Integer _maxUploadSize = new Integer(5120); private String _charset = "UTF-8"; /** Contructor. */ public Configuration(WebApp wapp) { _wapp = wapp; } /** Adds a listener class. */ public void addListener(Class klass) throws Exception { if (Monitor.class.isAssignableFrom(klass)) { if (_monitor != null) throw new UiException("Monitor listener can be assigned only once"); _monitor = (Monitor)klass.newInstance(); } if (EventThreadInit.class.isAssignableFrom(klass)) { synchronized (_evtInits) { _evtInits.add(klass); } } if (EventThreadCleanup.class.isAssignableFrom(klass)) { synchronized (_evtCleans) { _evtCleans.add(klass); } } if (EventThreadSuspend.class.isAssignableFrom(klass)) { synchronized (_evtSusps) { _evtSusps.add(klass); } } if (EventThreadResume.class.isAssignableFrom(klass)) { synchronized (_evtResus) { _evtResus.add(klass); } } if (WebAppInit.class.isAssignableFrom(klass)) { synchronized (_appInits) { _appInits.add(klass); } } if (WebAppCleanup.class.isAssignableFrom(klass)) { synchronized (_appCleans) { _appCleans.add(klass); } } if (SessionInit.class.isAssignableFrom(klass)) { synchronized (_sessInits) { _sessInits.add(klass); } } if (SessionCleanup.class.isAssignableFrom(klass)) { synchronized (_sessCleans) { _sessCleans.add(klass); } } if (DesktopInit.class.isAssignableFrom(klass)) { synchronized (_dtInits) { _dtInits.add(klass); } } if (DesktopCleanup.class.isAssignableFrom(klass)) { synchronized (_dtCleans) { _dtCleans.add(klass); } } if (ExecutionInit.class.isAssignableFrom(klass)) { synchronized (_execInits) { _execInits.add(klass); } } if (ExecutionCleanup.class.isAssignableFrom(klass)) { synchronized (_execCleans) { _execCleans.add(klass); } } } /** Removes a listener class. */ public void removeListener(Class klass) { synchronized (_evtInits) { _evtInits.remove(klass); } synchronized (_evtCleans) { _evtCleans.remove(klass); } synchronized (_evtSusps) { _evtSusps.remove(klass); } synchronized (_evtResus) { _evtResus.remove(klass); } synchronized (_appInits) { _appInits.remove(klass); } synchronized (_appCleans) { _appCleans.remove(klass); } synchronized (_sessInits) { _sessInits.remove(klass); } synchronized (_sessCleans) { _sessCleans.remove(klass); } synchronized (_dtInits) { _dtInits.remove(klass); } synchronized (_dtCleans) { _dtCleans.remove(klass); } synchronized (_execInits) { _execInits.remove(klass); } synchronized (_execCleans) { _execCleans.remove(klass); } } /** Contructs a list of {@link EventThreadInit} instances and invokes * {@link EventThreadInit#prepare} for * each relevant listener registered by {@link #addListener}. * * <p>It is called by UiEngine before starting an event processing * thread. * * @exception UiException to prevent a thread from being processed * if {@link EventThreadInit#prepare} throws an exception * @return a list of {@link EventThreadInit} instances that are * constructed in this method (and their {@link EventThreadInit#prepare} * are called successfully). */ public List newEventThreadInits(Component comp, Event evt) throws UiException { if (_evtInits.isEmpty()) return Collections.EMPTY_LIST; //it is OK to test LinkedList.isEmpty without synchronized final List inits = new LinkedList(); synchronized (_evtInits) { for (Iterator it = _evtInits.iterator(); it.hasNext();) { final Class klass = (Class)it.next(); try { final EventThreadInit init = (EventThreadInit)klass.newInstance(); init.prepare(comp, evt); inits.add(init); } catch (Throwable ex) { throw UiException.Aide.wrap(ex); //Don't intercept; to prevent the event being processed } } } return inits; } /** Invokes {@link EventThreadInit#init} for each instance returned * by {@link #newEventThreadInits}. * * @param inits a list of {@link EventThreadInit} instances returned from * {@link #newEventThreadInits}, or null if no instance at all. * @param comp the component which the event is targeting * @param evt the event to process * @exception UiException to prevent a thread from being processed * if {@link EventThreadInit#prepare} throws an exception */ public void invokeEventThreadInits(List inits, Component comp, Event evt) throws UiException { if (inits == null || inits.isEmpty()) return; for (Iterator it = inits.iterator(); it.hasNext();) { final EventThreadInit fn = (EventThreadInit)it.next(); try { fn.init(comp, evt); } catch (Throwable ex) { throw UiException.Aide.wrap(ex); //Don't intercept; to prevent the event being processed } } } /** Invokes {@link EventThreadCleanup#cleanup} for each relevant * listener registered by {@link #addListener}. * * <p>An instance of {@link EventThreadCleanup} is constructed first, * and then invoke {@link EventThreadCleanup#cleanup}. * * <p>It never throws an exception but logs and adds it to the errs argument, * if not null. * * @param comp the component which the event is targeting * @param evt the event to process * @param ex the exception being thrown (and not handled) during * the processing of the event, or null it is executed successfully. * @param errs used to hold the exceptions that are thrown by * {@link EventThreadCleanup#cleanup}. * If null, all exceptions are ignored (but logged) * @return a list of {@link EventThreadCleanup} instances that is * constructed in this method (and their {@link EventThreadCleanup#cleanup} * are called successfully -- without throwing any exception). */ public List newEventThreadCleanups(Component comp, Event evt, Throwable ex, List errs) { if (_evtCleans.isEmpty()) return Collections.EMPTY_LIST; //it is OK to test LinkedList.isEmpty without synchronized final List cleanups = new LinkedList(); synchronized (_evtCleans) { for (Iterator it = _evtCleans.iterator(); it.hasNext();) { final Class klass = (Class)it.next(); try { final EventThreadCleanup cleanup = (EventThreadCleanup)klass.newInstance(); cleanup.cleanup(comp, evt, ex); cleanups.add(cleanup); } catch (Throwable t) { if (errs != null) errs.add(t); log.error("Failed to invoke "+klass, t); } } } return cleanups; } /** Invoke {@link EventThreadCleanup#complete} for each instance returned by * {@link #newEventThreadCleanups}. * * <p>It never throws an exception but logs and adds it to the errs argument, * if not null. * * @param cleanups a list of {@link EventThreadCleanup} instances returned from * {@link #newEventThreadCleanups}, or null if no instance at all. * @param errs used to hold the exceptions that are thrown by * {@link EventThreadCleanup#complete}. * If null, all exceptions are ignored (but logged). */ public void invokeEventThreadCompletes(List cleanups, Component comp, Event evt, List errs) { if (cleanups == null || cleanups.isEmpty()) return; for (Iterator it = cleanups.iterator(); it.hasNext();) { final EventThreadCleanup fn = (EventThreadCleanup)it.next(); try { fn.complete(comp, evt); } catch (Throwable ex) { if (errs != null) errs.add(ex); log.error("Failed to invoke "+fn, ex); } } } /** Invokes {@link EventThreadSuspend#beforeSuspend} for each relevant * listener registered by {@link #addListener}. * * <p>An instance of {@link EventThreadSuspend} is constructed first, * and then invoke {@link EventThreadSuspend#beforeSuspend}. * * @param comp the component which the event is targeting * @param evt the event to process * @param obj which object that {@link Executions#wait} * is called with. * @exception UiException to prevent a thread from suspending */ public void invokeEventThreadSuspends(Component comp, Event evt, Object obj) throws UiException { if (_evtSusps.isEmpty()) return; //it is OK to test LinkedList.isEmpty without synchronized synchronized (_evtSusps) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?