use of org.eclipse.scout.rt.platform.util.CompositeObject in project scout.rt by eclipse.
the class ClientSessionProviderWithCache method provide.
/**
* Returns the cached client session for the given <em>sessionId</em>, or the context's {@link Subject} if
* <em>sessionId</em> is not specified. On cache miss, a new session is created via {@link ClientSessionProvider}.
*
* @param sessionId
* unique session ID to identify the cached session. If <code>null</code>, the context's {@link Subject} is
* used for identification. On cache miss, this <em>sessionId</em> is used to create a new session, or a
* random UUID if <code>null</code>.
* @param clientRunContext
* applied during session start, and to get the session's {@link Subject}.
* @return session found in cache, or a new session on cache miss.
* @throws RuntimeException
* if session creation failed.
*/
@Override
public <SESSION extends IClientSession> SESSION provide(final String sessionId, final ClientRunContext clientRunContext) {
// 1. Create session lookup key.
final CompositeObject sessionCacheKey = newSessionCacheKey(sessionId, clientRunContext.getSubject());
if (sessionCacheKey == null) {
LOG.warn("Cannot identify cached client session because the cache key is undefined [sessionId={}, subject={}]", sessionId, clientRunContext.getSubject());
return super.provide(sessionId, clientRunContext);
}
// 2. Lookup session in the cache.
@SuppressWarnings("unchecked") SESSION clientSession = (SESSION) m_cache.get(sessionCacheKey);
if (clientSession != null) {
return clientSession;
}
// 3. Cache miss (optimistic locking because session creation might be a long running operation)
prepareSessionCreatingClientRunContext(clientRunContext);
clientSession = super.provide(sessionId, clientRunContext);
// 4. Cache the new client session, or return present session if created by another thread in the meantime (optimistic locking).
@SuppressWarnings("unchecked") final SESSION cachedClientSession = (SESSION) m_cache.putIfAbsent(sessionCacheKey, clientSession);
if (cachedClientSession != null) {
clientSession = cachedClientSession;
}
return clientSession;
}
use of org.eclipse.scout.rt.platform.util.CompositeObject in project scout.rt by eclipse.
the class FindFieldByFormDataIdVisitor method visitField.
@Override
public boolean visitField(IFormField field, int level, int fieldIndex) {
if (m_searchContextRootForm == null) {
// auto-initialize search context
// we assume the form field tree is traversed pre-order (i.e. first node itself, then its children)
m_searchContextRootForm = field.getForm();
}
if (matchesAllParts(field)) {
int fieldTypeRank = 0;
if (m_searchContextRootForm != null) {
IForm form = field.getForm();
while (form != null && form != m_searchContextRootForm) {
fieldTypeRank += 10;
form = form.getOuterForm();
}
}
if (field instanceof IValueField) {
// fieldTypeRank is fine
} else if (field instanceof ITableField) {
fieldTypeRank += 1;
} else if (!(field instanceof ICompositeField)) {
fieldTypeRank += 2;
} else {
fieldTypeRank += 3;
}
// Compute the enclosing field path rank that is used as additional hint for determining the
// best matching form field for the requested formId. Note: for compatibility reasons, the enclosing
// field path is not enforced.
int enclosingFieldPathRank = getEnclosingFieldPathRank(field);
m_prioMap.put(new CompositeObject(fieldTypeRank, enclosingFieldPathRank), field);
}
return !m_prioMap.containsKey(PERFECT_VALUE_FIELD_MATCH_KEY);
}
use of org.eclipse.scout.rt.platform.util.CompositeObject in project scout.rt by eclipse.
the class BookmarkUtility method resolvePage.
public static IPage<?> resolvePage(List<? extends IPage> pages, String className, String bookmarkIdentifier) {
if (className == null) {
return null;
}
TreeMap<CompositeObject, IPage> sortMap = new TreeMap<CompositeObject, IPage>();
String simpleClassName = className.replaceAll("^.*\\.", "");
int index = 0;
for (IPage<?> p : pages) {
int classNameScore = 0;
int userPreferenceContextScore = 0;
if (p.getClass().getName().equals(className)) {
classNameScore = -2;
} else if (p.getClass().getSimpleName().equalsIgnoreCase(simpleClassName)) {
classNameScore = -1;
}
IBookmarkAdapter bookmarkAdapter = getBookmarkAdapter(p);
if (bookmarkIdentifier == null || bookmarkIdentifier.equalsIgnoreCase(bookmarkAdapter.getIdentifier())) {
userPreferenceContextScore = -1;
}
if (classNameScore != 0 && userPreferenceContextScore != 0) {
sortMap.put(new CompositeObject(classNameScore, userPreferenceContextScore, index), p);
}
index++;
}
if (sortMap.isEmpty()) {
return null;
}
CompositeObject bestMatchingKey = sortMap.firstKey();
IPage<?> bestMatchingPage = sortMap.remove(bestMatchingKey);
if (!sortMap.isEmpty()) {
// check ambiguity
CompositeObject nextKey = sortMap.firstKey();
if (ObjectUtility.equals(bestMatchingKey.getComponent(0), nextKey.getComponent(0)) && ObjectUtility.equals(bestMatchingKey.getComponent(1), nextKey.getComponent(1))) {
LOG.warn("More than one pages found for page class [{}] and bookmark Identifier [{}]", className, bookmarkIdentifier);
}
}
return bestMatchingPage;
}
use of org.eclipse.scout.rt.platform.util.CompositeObject in project scout.rt by eclipse.
the class CompositeObjectTest method testCompareToNotComparable.
@Test
public void testCompareToNotComparable() {
// not of type comparable -> uses toString()
CompositeObject a = new CompositeObject(new A(123456L));
CompositeObject b = new CompositeObject(new B(123456L));
CompositeObject c = new CompositeObject(new C(123456L));
CompositeObject c2 = new CompositeObject(new C(123456L));
assertTrue(a.compareTo(b) == 0);
assertTrue(b.compareTo(a) == 0);
assertTrue(a.compareTo(c) < 0);
assertTrue(c.compareTo(a) > 0);
assertTrue(b.compareTo(c) < 0);
assertTrue(c.compareTo(b) > 0);
assertTrue(c.compareTo(c2) == 0);
assertTrue(c2.compareTo(c) == 0);
}
use of org.eclipse.scout.rt.platform.util.CompositeObject in project scout.rt by eclipse.
the class ConfigurationUtility method sortFilteredClassesByOrderAnnotation.
/**
* Filters the given class array and sorts the remaining elements according to their {@link Order} annotation.
*
* @param classes
* @param filter
* @return
*/
@SuppressWarnings("unchecked")
public static <T> List<Class<? extends T>> sortFilteredClassesByOrderAnnotation(List<? extends Class> classes, Class<T> filter) {
TreeMap<CompositeObject, Class<? extends T>> orderedClassesMap = new TreeMap<CompositeObject, Class<? extends T>>();
int i = 0;
for (Class candidate : classes) {
if (filter.isAssignableFrom(candidate)) {
if (candidate.isAnnotationPresent(Order.class)) {
Order order = (Order) candidate.getAnnotation(Order.class);
orderedClassesMap.put(new CompositeObject(order.value(), i), (Class<T>) candidate);
} else {
if (!candidate.isAnnotationPresent(Replace.class)) {
LOG.error("missing @Order annotation: {}", candidate.getName());
}
orderedClassesMap.put(new CompositeObject(Double.MAX_VALUE, i), (Class<T>) candidate);
}
i++;
}
}
return CollectionUtility.arrayList(orderedClassesMap.values());
}
Aggregations