import static org.argeo.api.cms.ux.CmsView.CMS_VIEW_UID_PROPERTY;
+import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
+import java.util.Timer;
+import java.util.TimerTask;
import java.util.TreeMap;
import java.util.TreeSet;
// private CmsUserManager cmsUserManager;
// TODO make more optimal or via CmsSession/CmsView
- private Map<String, SwtAppUi> managedUis = Collections.synchronizedMap(new HashMap<>());
+ private static Timer janitorTimer = new Timer(true);
+ private Map<String, WeakReference<SwtAppUi>> managedUis = new HashMap<>();
// ACR
private ContentRepository contentRepository;
getCmsContext().getCmsEventBus().addEventSubscriber(event.topic(), this);
}
- if (log.isDebugEnabled())
- log.info("Argeo Suite App started");
-
if (properties.containsKey(DEFAULT_UI_NAME_PROPERTY))
defaultUiName = LangUtils.get(properties, DEFAULT_UI_NAME_PROPERTY);
if (properties.containsKey(DEFAULT_THEME_ID_PROPERTY))
// TODO does it make sense to accept that?
appPid = "<unknown>";
}
+ if (log.isDebugEnabled())
+ log.info("Argeo Suite App " + appPid + " started");
Objects.requireNonNull(contentRepository, "Content repository must be provided");
Objects.requireNonNull(appUserState, "App user state must be provided");
-// if (pidPrefix == null)
-// throw new IllegalArgumentException("PID prefix must be set.");
-
-// headerPid = pidPrefix + "header";
-// footerPid = pidPrefix + "footer";
-// leadPanePid = pidPrefix + "leadPane";
-// adminLeadPanePid = pidPrefix + "adminLeadPane";
-// loginScreenPid = pidPrefix + "loginScreen";
+
+ long janitorPeriod = 12 * 60 * 60 * 1000;// 12h
+ janitorTimer.schedule(new TimerTask() {
+
+ @Override
+ public void run() {
+ refs: for (WeakReference<SwtAppUi> uiRef : managedUis.values()) {
+ SwtAppUi ui = uiRef.get();
+ if (ui == null)
+ continue refs;
+ ui.disposeIfTimedout();
+ }
+ if (log.isDebugEnabled())
+ log.debug(managedUis.size() + " UIs being managed by app " + appPid);
+ }
+ }, janitorPeriod, janitorPeriod);
}
public void stop(Map<String, Object> properties) {
- for (SwtAppUi ui : managedUis.values())
+ refs: for (WeakReference<SwtAppUi> uiRef : managedUis.values()) {
+ SwtAppUi ui = uiRef.get();
+ if (ui == null)
+ continue refs;
if (!ui.isDisposed() && !ui.getDisplay().isDisposed()) {
ui.getDisplay().syncExec(() -> ui.dispose());
}
+ }
managedUis.clear();
if (log.isDebugEnabled())
log.info("Argeo Suite App stopped");
if (theme != null)
CmsSwtUtils.registerCmsTheme(uiParent.getShell(), theme);
SwtAppUi argeoSuiteUi = new SwtAppUi(uiParent, SWT.INHERIT_DEFAULT);
+ // TODO make timeout configurable
+ argeoSuiteUi.setUiTimeout(12 * 60 * 60 * 1000);// 12 hours
String uid = cmsView.getUid();
- managedUis.put(uid, argeoSuiteUi);
argeoSuiteUi.addDisposeListener(new CleanUpUi(uid));
+ managedUis.put(uid, new WeakReference<>(argeoSuiteUi));
return argeoSuiteUi;
}
}
private SwtAppUi getRelatedUi(Map<String, Object> eventProperties) {
- return managedUis.get(get(eventProperties, CMS_VIEW_UID_PROPERTY));
+ WeakReference<SwtAppUi> uiRef = managedUis.get(get(eventProperties, CMS_VIEW_UID_PROPERTY));
+ if (uiRef == null)
+ return null;
+ return uiRef.get();
}
public static String get(Map<String, Object> eventProperties, String key) {
}
}
-// public void setCmsUserManager(CmsUserManager cmsUserManager) {
-// this.cmsUserManager = cmsUserManager;
-// }
-
-// protected ContentRepository getContentRepository() {
-// return contentRepository;
-// }
-
public void setContentRepository(ContentRepository contentRepository) {
this.contentRepository = contentRepository;
}
final String uid;
public CleanUpUi(String uid) {
- super();
this.uid = uid;
}