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.Iterator;
import java.util.List;
import java.util.Locale;
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))
pidPrefix = appPid.substring(0, lastDotIndex);
}
} else {
- // TODO doe it make sense to accept that?
+ // TODO does it make sense to accept that?
appPid = "<unknown>";
}
-
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 = 60 * 60 * 1000;// 1h
+ // long janitorPeriod = 60 * 1000;// min
+ janitorTimer.schedule(new TimerTask() {
+
+ @Override
+ public void run() {
+ try {
+ // copy Map in order to avoid concurrent modification exception
+ Iterator<Map.Entry<String, WeakReference<SwtAppUi>>> uiRefs = new HashMap<>(managedUis).entrySet()
+ .iterator();
+ refs: while (uiRefs.hasNext()) {
+ Map.Entry<String, WeakReference<SwtAppUi>> entry = uiRefs.next();
+ String uiUuid = entry.getKey();
+ WeakReference<SwtAppUi> uiRef = entry.getValue();
+ SwtAppUi ui = uiRef.get();
+ if (ui == null) {
+ if (log.isTraceEnabled())
+ log.warn("Unreferenced UI " + uiUuid + " in " + appPid + ", removing it");
+ managedUis.remove(uiUuid);
+ continue refs;
+ }
+ if (!ui.isDisposed() && !ui.getDisplay().isDisposed()) {
+ if (ui.isTimedOut()) {
+ if (log.isTraceEnabled())
+ log.trace("Killing timed-out UI " + uiUuid + " in " + appPid);
+ UiContext.killDisplay(ui.getDisplay());
+ }
+ } else {
+ if (log.isTraceEnabled())
+ log.warn("Disposed UI " + uiUuid + " still referenced in " + appPid + ", removing it");
+ managedUis.remove(uiUuid);
+ }
+ }
+ if (log.isTraceEnabled())
+ log.trace(managedUis.size() + " UIs being managed by app " + appPid);
+ } catch (Exception e) {
+ log.error("Could not clean up timed-out UIs", e);
+ }
+ }
+ }, janitorPeriod, janitorPeriod);
+
+ if (log.isDebugEnabled())
+ log.info("Argeo Suite App " + appPid + " started");
}
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(6 * 60 * 60 * 1000);// 6 hours
+ // argeoSuiteUi.setUiTimeout(60 * 1000);// 1 min
String uid = cmsView.getUid();
- managedUis.put(uid, argeoSuiteUi);
argeoSuiteUi.addDisposeListener(new CleanUpUi(uid));
-// argeoSuiteUi.addDisposeListener((e) -> {
-// managedUis.remove(uid);
-// if (log.isDebugEnabled())
-// log.debug("Suite UI " + uid + " has been disposed.");
-// });
-// Display.getCurrent().disposeExec(() -> {
-// if (managedUis.containsKey(uid)) {
-// managedUis.remove(uid);
-// if (log.isDebugEnabled())
-// log.debug("Suite UI " + uid + " has been disposed from Display#disposeExec().");
-// }
-// });
+ managedUis.put(uid, new WeakReference<>(argeoSuiteUi));
return argeoSuiteUi;
}
try {
Content context = null;
SwtAppUi ui = (SwtAppUi) cmsUi;
+ ui.updateLastAccess();
String uiName = Objects.toString(ui.getParent().getData(UI_NAME_PROPERTY), null);
if (uiName == null)
.get(Content.ROOT_PATH + CmsConstants.SYS_WORKSPACE + publicBasePath);
ui.setUserDir(userDir);
} else {
- Content userDir = appUserState.getOrCreateSessionDir(contentSession, cmsSession);
+ Content userDir = appUserState.getOrCreateSessionDir(cmsSession);
ui.setUserDir(userDir);
// Node userDirNode = jcrContentProvider.doInAdminSession((adminSession) -> {
// Node node = SuiteUtils.getOrCreateCmsSessionNode(adminSession, cmsSession);
// }
}
-// private static String listTypes(Node context) {
-// try {
-// StringBuilder sb = new StringBuilder();
-// sb.append(context.getPrimaryNodeType().getName());
-// for (NodeType superType : context.getPrimaryNodeType().getDeclaredSupertypes()) {
-// sb.append(' ');
-// sb.append(superType.getName());
-// }
-//
-// for (NodeType nodeType : context.getMixinNodeTypes()) {
-// sb.append(' ');
-// sb.append(nodeType.getName());
-// if (nodeType.getName().equals(EntityType.local.get()))
-// sb.append('/').append(context.getProperty(EntityNames.ENTITY_TYPE).getString());
-// for (NodeType superType : nodeType.getDeclaredSupertypes()) {
-// sb.append(' ');
-// sb.append(superType.getName());
-// }
-// }
-// return sb.toString();
-// } catch (RepositoryException e) {
-// throw new JcrException(e);
-// }
-// }
-
@Override
public void setState(CmsUi cmsUi, String state) {
AppUi ui = (AppUi) cmsUi;
if (state == null)
return;
if (!state.startsWith("/")) {
-// if (cmsUi instanceof SwtAppUi) {
-// SwtAppUi ui = (SwtAppUi) cmsUi;
if (LOGIN.equals(state)) {
String appTitle = "";
if (ui.getTitle() != null)
properties.put(SuiteUxEvent.LAYER, layerId);
properties.put(SuiteUxEvent.CONTENT_PATH, HOME_STATE);
ui.getCmsView().sendEvent(SuiteUxEvent.switchLayer.topic(), properties);
-// }
return;
}
-// SwtAppUi suiteUi = (SwtAppUi) cmsUi;
if (ui.isLoginScreen()) {
return;
}
SwtAppUi ui = getRelatedUi(event);
if (ui == null)
return;
+ ui.updateLastAccess();
ui.getCmsView().runAs(() -> {
try {
String appTitle = "";
ui.getCmsView().stateChanged(nodeToState(node), stateTitle(appTitle, CmsUxUtils.getTitle(node)));
} else if (isTopic(topic, SuiteUxEvent.switchLayer)) {
String layerId = get(event, SuiteUxEvent.LAYER);
- if (layerId != null) {
+ if (layerId != null && !"".equals(layerId.trim())) {
SwtAppLayer suiteLayer = findLayer(layerId);
if (suiteLayer == null)
throw new IllegalArgumentException("No layer '" + layerId + "' available.");
}
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;
}