String fragmentKey = getFragmentPropertyListFragmentKey(baseFragmentElementImpl);
Subject subject = JSSubject.getSubject(AccessController.getContext());
Principal userPrincipal = ((subject != null) ? SubjectHelper.getBestPrincipal(subject, User.class) : null);
String fragmentListKey = getFragmentPropertyListKey(fragmentKey, userPrincipal);
Map threadLocalCache = (Map)fragmentPropertyListsCache.get();
FragmentPropertyList list = ((threadLocalCache != null) ? (FragmentPropertyList)threadLocalCache.get(fragmentListKey) : null);
// get and cache persistent list
if (list == null)
{
// get cached fragment property list components or
// query from database if not cached and cache
DatabasePageManagerCachedFragmentPropertyList globalFragmentPropertyList = DatabasePageManagerCache.fragmentPropertyListCacheLookup(fragmentKey);
if (globalFragmentPropertyList == null)
{
globalFragmentPropertyList = new DatabasePageManagerCachedFragmentPropertyList(baseFragmentElementImpl.getBaseFragmentsElement().getPath());
Criteria filter = new Criteria();
filter.addEqualTo("fragment", new Integer(baseFragmentElementImpl.getIdentity()));
filter.addIsNull("scope");
QueryByCriteria query = QueryFactory.newQuery(FragmentPropertyImpl.class, filter);
Collection fragmentProperties = getPersistenceBrokerTemplate().getCollectionByQuery(query);
globalFragmentPropertyList.addAll(fragmentProperties);
DatabasePageManagerCache.fragmentPropertyListCacheAdd(fragmentKey, globalFragmentPropertyList, false);
}
Map principalFragmentPropertyLists = null;
DatabasePageManagerCachedFragmentPropertyList userFragmentPropertyList = null;
if (subject != null)
{
if (GROUP_AND_ROLE_PROPERTY_SCOPES_ENABLED)
{
Set principals = subject.getPrincipals();
Iterator principalsIter = principals.iterator();
while (principalsIter.hasNext())
{
Principal principal = (Principal)principalsIter.next();
String principalScope = null;
if (principal instanceof User)
{
principalScope = USER_PROPERTY_SCOPE;
}
else if (principal instanceof Group)
{
principalScope = GROUP_PROPERTY_SCOPE;
}
else if (principal instanceof Role)
{
principalScope = ROLE_PROPERTY_SCOPE;
}
if (principalScope != null)
{
String principalKey = getFragmentPropertyListPrincipalKey(principalScope, principal.getName());
DatabasePageManagerCachedFragmentPropertyList principalFragmentPropertyList = DatabasePageManagerCache.principalFragmentPropertyListCacheLookup(principalKey);
if (principalFragmentPropertyList == null)
{
principalFragmentPropertyList = new DatabasePageManagerCachedFragmentPropertyList(principalScope, principalKey);
Criteria filter = new Criteria();
filter.addEqualTo("scope", principalScope);
filter.addEqualTo("scopeValue", principal.getName());
QueryByCriteria query = QueryFactory.newQuery(FragmentPropertyImpl.class, filter);
Collection fragmentProperties = getPersistenceBrokerTemplate().getCollectionByQuery(query);
principalFragmentPropertyList.addAll(fragmentProperties);
DatabasePageManagerCache.principalFragmentPropertyListCacheAdd(principalKey, principalFragmentPropertyList, false);
}
if (principalFragmentPropertyList != null)
{
if (principalFragmentPropertyLists == null)
{
principalFragmentPropertyLists = new HashMap();
}
principalFragmentPropertyLists.put(principalKey, principalFragmentPropertyList);
}
}
}
}
else if (userPrincipal != null)
{
String principalKey = getFragmentPropertyListPrincipalKey(USER_PROPERTY_SCOPE, userPrincipal.getName());
userFragmentPropertyList = DatabasePageManagerCache.principalFragmentPropertyListCacheLookup(principalKey);
if (userFragmentPropertyList == null)
{
userFragmentPropertyList = new DatabasePageManagerCachedFragmentPropertyList(USER_PROPERTY_SCOPE, principalKey);
Criteria filter = new Criteria();
filter.addEqualTo("scope", USER_PROPERTY_SCOPE);
filter.addEqualTo("scopeValue", userPrincipal.getName());
QueryByCriteria query = QueryFactory.newQuery(FragmentPropertyImpl.class, filter);
Collection fragmentProperties = getPersistenceBrokerTemplate().getCollectionByQuery(query);
userFragmentPropertyList.addAll(fragmentProperties);
DatabasePageManagerCache.principalFragmentPropertyListCacheAdd(principalKey, userFragmentPropertyList, false);
}
}
}
// assemble fragment property list instance, (use transient
// list or create new fragment property list)
list = new FragmentPropertyList(baseFragmentElementImpl);
list.getProperties().addAll(globalFragmentPropertyList);
if (subject != null)
{
if (GROUP_AND_ROLE_PROPERTY_SCOPES_ENABLED)
{
if (principalFragmentPropertyLists != null)
{
Set principals = subject.getPrincipals();
Iterator principalsIter = principals.iterator();
while (principalsIter.hasNext())
{
Principal principal = (Principal)principalsIter.next();
String principalScope = null;
if (principal instanceof User)
{
principalScope = USER_PROPERTY_SCOPE;
}
else if (principal instanceof Group)
{
principalScope = GROUP_PROPERTY_SCOPE;
}
else if (principal instanceof Role)
{
principalScope = ROLE_PROPERTY_SCOPE;
}
if (principalScope != null)
{
String principalKey = getFragmentPropertyListPrincipalKey(principalScope, principal.getName());
DatabasePageManagerCachedFragmentPropertyList principalFragmentPropertyList = (DatabasePageManagerCachedFragmentPropertyList)principalFragmentPropertyLists.get(principalKey);
List principalFragmentProperties = filterPrincipalFragmentPropertyList(principalFragmentPropertyList, baseFragmentElementImpl);
if (principalFragmentProperties != null)
{
list.getProperties().addAll(principalFragmentProperties);
}
}
}
}
}
else if (userFragmentPropertyList != null)
{
List userFragmentProperties = filterPrincipalFragmentPropertyList(userFragmentPropertyList, baseFragmentElementImpl);
if (userFragmentProperties != null)
{
list.getProperties().addAll(userFragmentProperties);
}
}
}
// merge results into transient list if specified
if ((list != null) && (transientList != null))
{
synchronized (transientList)
{
// merge into persistent list; this is unsafe due to the
// lack of transactional isolation in shared objects, but
// this should only happen before new objects are committed
// and here we are assuming that only the current user has
// access to the new objects
Iterator sourceIter = transientList.iterator();
while (sourceIter.hasNext())
{
FragmentProperty sourceProperty = (FragmentProperty)sourceIter.next();
FragmentProperty targetProperty = list.getMatchingProperty(sourceProperty);
if (targetProperty != null)
{
targetProperty.setValue(sourceProperty.getValue());
}
else
{
list.add(sourceProperty);
}
}
// clear transient list
transientList.clearProperties();