Fix compilation with JDK8. Minor polishing in mongo model
This commit is contained in:
parent
dec86fc6fe
commit
2371960e33
13 changed files with 63 additions and 66 deletions
|
@ -10,15 +10,15 @@ import org.keycloak.models.mongo.api.MongoStore;
|
|||
*/
|
||||
public interface MongoStoreInvocationContext {
|
||||
|
||||
void addCreatedObject(MongoIdentifiableEntity entity);
|
||||
void addCreatedEntity(MongoIdentifiableEntity entity);
|
||||
|
||||
void addLoadedObject(MongoIdentifiableEntity entity);
|
||||
void addLoadedEntity(MongoIdentifiableEntity entity);
|
||||
|
||||
<T extends MongoIdentifiableEntity> T getLoadedObject(Class<T> type, String id);
|
||||
<T extends MongoIdentifiableEntity> T getLoadedEntity(Class<T> type, String id);
|
||||
|
||||
void addUpdateTask(MongoIdentifiableEntity entityToUpdate, MongoTask task);
|
||||
|
||||
void addRemovedObject(MongoIdentifiableEntity entityToRemove);
|
||||
void addRemovedEntity(MongoIdentifiableEntity entity);
|
||||
|
||||
void beforeDBSearch(Class<? extends MongoIdentifiableEntity> entityType);
|
||||
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package org.keycloak.models.mongo.api.context;
|
||||
|
||||
import org.keycloak.models.mongo.api.MongoStore;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
|
||||
*/
|
||||
|
|
|
@ -2,7 +2,7 @@ package org.keycloak.models.mongo.api.types;
|
|||
|
||||
/**
|
||||
* SPI object to convert object from application type to database type and vice versa. Shouldn't be directly used by application.
|
||||
* Various mappers should be registered in TypeMapper, which is main entry point to be used by application
|
||||
* Various mappers should be registered in MapperRegistry, which is main entry point to be used by application
|
||||
*
|
||||
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
|
||||
*/
|
||||
|
|
|
@ -10,7 +10,7 @@ public class MapperContext<T, S> {
|
|||
// object to convert
|
||||
private final T objectToConvert;
|
||||
|
||||
// expected return type, which could be useful information in some converters, so they are able to dynamically instantiate types
|
||||
// expected return type, which could be useful information in some mappers, so they are able to dynamically instantiate types
|
||||
private final Class<? extends S> expectedReturnType;
|
||||
|
||||
// in case that expected return type is generic type (like "List<String>"), then genericTypes could contain list of expected generic arguments
|
||||
|
|
|
@ -83,7 +83,7 @@ public class MapperRegistry {
|
|||
throw new IllegalArgumentException("Converter " + mapper + " has return type " + mapper.getExpectedReturnType() +
|
||||
" but we need type " + expectedDBObjectType);
|
||||
}
|
||||
return mapper.convertObject(new MapperContext(applicationObject, expectedDBObjectType, null));
|
||||
return mapper.convertObject(new MapperContext<Object, S>(applicationObject, expectedDBObjectType, null));
|
||||
}
|
||||
|
||||
// Try to find converter for given type or all it's supertypes
|
||||
|
|
|
@ -14,14 +14,14 @@ import java.util.Map;
|
|||
*/
|
||||
public class EntityInfo {
|
||||
|
||||
private final Class<? extends MongoEntity> objectClass;
|
||||
private final Class<? extends MongoEntity> entityClass;
|
||||
|
||||
private final String dbCollectionName;
|
||||
|
||||
private final Map<String, Property<Object>> properties;
|
||||
|
||||
public EntityInfo(Class<? extends MongoEntity> objectClass, String dbCollectionName, List<Property<Object>> properties) {
|
||||
this.objectClass = objectClass;
|
||||
public EntityInfo(Class<? extends MongoEntity> entityClass, String dbCollectionName, List<Property<Object>> properties) {
|
||||
this.entityClass = entityClass;
|
||||
this.dbCollectionName = dbCollectionName;
|
||||
|
||||
Map<String, Property<Object>> props= new HashMap<String, Property<Object>>();
|
||||
|
@ -31,8 +31,8 @@ public class EntityInfo {
|
|||
this.properties = Collections.unmodifiableMap(props);
|
||||
}
|
||||
|
||||
public Class<? extends MongoEntity> getObjectClass() {
|
||||
return objectClass;
|
||||
public Class<? extends MongoEntity> getEntityClass() {
|
||||
return entityClass;
|
||||
}
|
||||
|
||||
public String getDbCollectionName() {
|
||||
|
|
|
@ -132,8 +132,8 @@ public class MongoStoreImpl implements MongoStore {
|
|||
entity.setId(dbObject.getString("_id"));
|
||||
}
|
||||
|
||||
// Treat object as if it is read (It is already submited to transaction)
|
||||
context.addLoadedObject(entity);
|
||||
// Treat object as created in this transaction (It is already submited to transaction)
|
||||
context.addCreatedEntity(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -171,7 +171,7 @@ public class MongoStoreImpl implements MongoStore {
|
|||
@Override
|
||||
public <T extends MongoIdentifiableEntity> T loadEntity(Class<T> type, String id, MongoStoreInvocationContext context) {
|
||||
// First look if we already read the object with this oid and type during this transaction. If yes, use it instead of DB lookup
|
||||
T cached = context.getLoadedObject(type, id);
|
||||
T cached = context.getLoadedEntity(type, id);
|
||||
if (cached != null) return cached;
|
||||
|
||||
DBCollection dbCollection = getDBCollectionForType(type);
|
||||
|
@ -185,7 +185,7 @@ public class MongoStoreImpl implements MongoStore {
|
|||
T converted = mapperRegistry.convertDBObjectToApplicationObject(mapperContext);
|
||||
|
||||
// Now add it to loaded objects
|
||||
context.addLoadedObject(converted);
|
||||
context.addLoadedEntity(converted);
|
||||
|
||||
return converted;
|
||||
}
|
||||
|
@ -234,7 +234,7 @@ public class MongoStoreImpl implements MongoStore {
|
|||
dbCollection.remove(dbQuery);
|
||||
logger.info("Entity of type: " + type + ", id: " + id + " removed from MongoDB.");
|
||||
|
||||
context.addRemovedObject(found);
|
||||
context.addRemovedEntity(found);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -251,7 +251,7 @@ public class MongoStoreImpl implements MongoStore {
|
|||
logger.info("Removed " + foundObjects.size() + " entities of type: " + type + ", query: " + query);
|
||||
|
||||
for (MongoIdentifiableEntity found : foundObjects) {
|
||||
context.addRemovedObject(found);;
|
||||
context.addRemovedEntity(found);;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -359,17 +359,17 @@ public class MongoStoreImpl implements MongoStore {
|
|||
mapperRegistry.addDBObjectMapper(mapper);
|
||||
}
|
||||
|
||||
public EntityInfo getEntityInfo(Class<? extends MongoEntity> objectClass) {
|
||||
EntityInfo entityInfo = entityInfoCache.get(objectClass);
|
||||
public EntityInfo getEntityInfo(Class<? extends MongoEntity> entityClass) {
|
||||
EntityInfo entityInfo = entityInfoCache.get(entityClass);
|
||||
if (entityInfo == null) {
|
||||
List<Property<Object>> properties = PropertyQueries.createQuery(objectClass).addCriteria(new AnnotatedPropertyCriteria(MongoField.class)).getResultList();
|
||||
List<Property<Object>> properties = PropertyQueries.createQuery(entityClass).addCriteria(new AnnotatedPropertyCriteria(MongoField.class)).getResultList();
|
||||
|
||||
MongoCollection classAnnotation = objectClass.getAnnotation(MongoCollection.class);
|
||||
MongoCollection classAnnotation = entityClass.getAnnotation(MongoCollection.class);
|
||||
|
||||
String dbCollectionName = classAnnotation==null ? null : classAnnotation.collectionName();
|
||||
entityInfo = new EntityInfo(objectClass, dbCollectionName, properties);
|
||||
entityInfo = new EntityInfo(entityClass, dbCollectionName, properties);
|
||||
|
||||
EntityInfo existing = entityInfoCache.putIfAbsent(objectClass, entityInfo);
|
||||
EntityInfo existing = entityInfoCache.putIfAbsent(entityClass, entityInfo);
|
||||
if (existing != null) {
|
||||
entityInfo = existing;
|
||||
}
|
||||
|
@ -385,16 +385,16 @@ public class MongoStoreImpl implements MongoStore {
|
|||
for (DBObject dbObject : cursor) {
|
||||
// First look if we already have loaded object cached. If yes, we will use cached instance
|
||||
String id = dbObject.get("_id").toString();
|
||||
T object = context.getLoadedObject(type, id);
|
||||
T entity = context.getLoadedEntity(type, id);
|
||||
|
||||
if (object == null) {
|
||||
if (entity == null) {
|
||||
// So convert and use fresh instance from DB
|
||||
MapperContext<Object, T> mapperContext = new MapperContext<Object, T>(dbObject, type, null);
|
||||
object = mapperRegistry.convertDBObjectToApplicationObject(mapperContext);
|
||||
context.addLoadedObject(object);
|
||||
entity = mapperRegistry.convertDBObjectToApplicationObject(mapperContext);
|
||||
context.addLoadedEntity(entity);
|
||||
}
|
||||
|
||||
result.add(object);
|
||||
result.add(entity);
|
||||
}
|
||||
} finally {
|
||||
cursor.close();
|
||||
|
|
|
@ -20,15 +20,15 @@ public class SimpleMongoStoreInvocationContext implements MongoStoreInvocationCo
|
|||
}
|
||||
|
||||
@Override
|
||||
public void addCreatedObject(MongoIdentifiableEntity entity) {
|
||||
public void addCreatedEntity(MongoIdentifiableEntity entity) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addLoadedObject(MongoIdentifiableEntity entity) {
|
||||
public void addLoadedEntity(MongoIdentifiableEntity entity) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends MongoIdentifiableEntity> T getLoadedObject(Class<T> type, String id) {
|
||||
public <T extends MongoIdentifiableEntity> T getLoadedEntity(Class<T> type, String id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -38,8 +38,8 @@ public class SimpleMongoStoreInvocationContext implements MongoStoreInvocationCo
|
|||
}
|
||||
|
||||
@Override
|
||||
public void addRemovedObject(MongoIdentifiableEntity entityToRemove) {
|
||||
entityToRemove.afterRemove(this);
|
||||
public void addRemovedEntity(MongoIdentifiableEntity entity) {
|
||||
entity.afterRemove(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -33,18 +33,18 @@ public class TransactionMongoStoreInvocationContext implements MongoStoreInvocat
|
|||
}
|
||||
|
||||
@Override
|
||||
public void addCreatedObject(MongoIdentifiableEntity entity) {
|
||||
public void addCreatedEntity(MongoIdentifiableEntity entity) {
|
||||
// For now just add it to list of loaded objects
|
||||
addLoadedObject(entity);
|
||||
addLoadedEntity(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addLoadedObject(MongoIdentifiableEntity entity) {
|
||||
public void addLoadedEntity(MongoIdentifiableEntity entity) {
|
||||
loadedObjects.put(entity.getId(), entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends MongoIdentifiableEntity> T getLoadedObject(Class<T> type, String id) {
|
||||
public <T extends MongoIdentifiableEntity> T getLoadedEntity(Class<T> type, String id) {
|
||||
return (T)loadedObjects.get(id);
|
||||
}
|
||||
|
||||
|
@ -76,12 +76,12 @@ public class TransactionMongoStoreInvocationContext implements MongoStoreInvocat
|
|||
}
|
||||
|
||||
@Override
|
||||
public void addRemovedObject(MongoIdentifiableEntity entityToRemove) {
|
||||
public void addRemovedEntity(MongoIdentifiableEntity entity) {
|
||||
// Remove all pending tasks and object from cache
|
||||
pendingUpdateTasks.remove(entityToRemove);
|
||||
loadedObjects.remove(entityToRemove.getId());
|
||||
pendingUpdateTasks.remove(entity);
|
||||
loadedObjects.remove(entity.getId());
|
||||
|
||||
entityToRemove.afterRemove(this);
|
||||
entity.afterRemove(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -26,12 +26,12 @@ public class BasicDBObjectMapper<S extends MongoEntity> implements Mapper<BasicD
|
|||
|
||||
private final MongoStoreImpl mongoStoreImpl;
|
||||
private final MapperRegistry mapperRegistry;
|
||||
private final Class<S> expectedObjectType;
|
||||
private final Class<S> expectedEntityType;
|
||||
|
||||
public BasicDBObjectMapper(MongoStoreImpl mongoStoreImpl, MapperRegistry mapperRegistry, Class<S> expectedObjectType) {
|
||||
public BasicDBObjectMapper(MongoStoreImpl mongoStoreImpl, MapperRegistry mapperRegistry, Class<S> expectedEntityType) {
|
||||
this.mongoStoreImpl = mongoStoreImpl;
|
||||
this.mapperRegistry = mapperRegistry;
|
||||
this.expectedObjectType = expectedObjectType;
|
||||
this.expectedEntityType = expectedEntityType;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -41,11 +41,11 @@ public class BasicDBObjectMapper<S extends MongoEntity> implements Mapper<BasicD
|
|||
return null;
|
||||
}
|
||||
|
||||
EntityInfo entityInfo = mongoStoreImpl.getEntityInfo(expectedObjectType);
|
||||
EntityInfo entityInfo = mongoStoreImpl.getEntityInfo(expectedEntityType);
|
||||
|
||||
S object;
|
||||
S entity;
|
||||
try {
|
||||
object = expectedObjectType.newInstance();
|
||||
entity = expectedEntityType.newInstance();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
@ -56,30 +56,30 @@ public class BasicDBObjectMapper<S extends MongoEntity> implements Mapper<BasicD
|
|||
|
||||
if ("_id".equals(key)) {
|
||||
// Current property is "id"
|
||||
if (object instanceof MongoIdentifiableEntity) {
|
||||
((MongoIdentifiableEntity)object).setId(value.toString());
|
||||
if (entity instanceof MongoIdentifiableEntity) {
|
||||
((MongoIdentifiableEntity)entity).setId(value.toString());
|
||||
}
|
||||
|
||||
} else if ((property = entityInfo.getPropertyByName(key)) != null) {
|
||||
// It's declared property with @DBField annotation
|
||||
setPropertyValue(object, value, property);
|
||||
setPropertyValue(entity, value, property);
|
||||
|
||||
} else {
|
||||
// Show warning if it's unknown
|
||||
logger.warn("Property with key " + key + " not known for type " + expectedObjectType);
|
||||
logger.warn("Property with key " + key + " not known for type " + expectedEntityType);
|
||||
}
|
||||
}
|
||||
|
||||
return object;
|
||||
return entity;
|
||||
}
|
||||
|
||||
private void setPropertyValue(MongoEntity object, Object valueFromDB, Property property) {
|
||||
private void setPropertyValue(MongoEntity entity, Object valueFromDB, Property property) {
|
||||
if (valueFromDB == null) {
|
||||
property.setValue(object, null);
|
||||
property.setValue(entity, null);
|
||||
return;
|
||||
}
|
||||
|
||||
MapperContext<Object, ?> context;
|
||||
MapperContext<Object, Object> context;
|
||||
|
||||
Type type = property.getBaseType();
|
||||
|
||||
|
@ -105,10 +105,10 @@ public class BasicDBObjectMapper<S extends MongoEntity> implements Mapper<BasicD
|
|||
Object appObject = mapperRegistry.convertDBObjectToApplicationObject(context);
|
||||
|
||||
if (Types.boxedClass(property.getJavaClass()).isAssignableFrom(appObject.getClass())) {
|
||||
property.setValue(object, appObject);
|
||||
property.setValue(entity, appObject);
|
||||
} else {
|
||||
throw new IllegalStateException("Converted object " + appObject + " is not of type " + context.getExpectedReturnType() +
|
||||
". So can't be assigned as property " + property.getName() + " of " + object.getClass());
|
||||
". So can't be assigned as property " + property.getName() + " of " + entity.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -119,6 +119,6 @@ public class BasicDBObjectMapper<S extends MongoEntity> implements Mapper<BasicD
|
|||
|
||||
@Override
|
||||
public Class<S> getExpectedReturnType() {
|
||||
return expectedObjectType;
|
||||
return expectedEntityType;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ import org.keycloak.models.mongo.api.types.Mapper;
|
|||
import org.keycloak.models.mongo.api.types.MapperContext;
|
||||
|
||||
/**
|
||||
* For now, we support just convert to Map<String, String>
|
||||
* For now, there is support just for convert to Map<String, String>
|
||||
*
|
||||
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
|
||||
*/
|
||||
|
@ -16,10 +16,10 @@ public class BasicDBObjectToMapMapper implements Mapper<BasicDBObject, Map> {
|
|||
|
||||
@Override
|
||||
public Map convertObject(MapperContext<BasicDBObject, Map> context) {
|
||||
BasicDBObject objectToConvert = context.getObjectToConvert();
|
||||
BasicDBObject dbObjectToConvert = context.getObjectToConvert();
|
||||
|
||||
HashMap<String, String> result = new HashMap<String, String>();
|
||||
for (Map.Entry<String, Object> entry : objectToConvert.entrySet()) {
|
||||
for (Map.Entry<String, Object> entry : dbObjectToConvert.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
String value = (String)entry.getValue();
|
||||
|
||||
|
|
|
@ -8,7 +8,6 @@ import org.keycloak.models.mongo.api.types.MapperContext;
|
|||
*/
|
||||
public class EnumToStringMapper implements Mapper<Enum, String> {
|
||||
|
||||
// It will be saved in form of "org.keycloak.Gender#MALE" so it's possible to parse enumType out of it
|
||||
@Override
|
||||
public String convertObject(MapperContext<Enum, String> context) {
|
||||
Enum objectToConvert = context.getObjectToConvert();
|
||||
|
|
|
@ -32,7 +32,7 @@ public class MongoEntityMapper<T extends MongoEntity> implements Mapper<T, Basic
|
|||
|
||||
EntityInfo entityInfo = mongoStoreImpl.getEntityInfo(applicationObject.getClass());
|
||||
|
||||
// Create instance of BasicDBObject and add all declared properties to it (properties with null value probably should be skipped)
|
||||
// Create instance of BasicDBObject and add all declared properties to it
|
||||
BasicDBObject dbObject = new BasicDBObject();
|
||||
Collection<Property<Object>> props = entityInfo.getProperties();
|
||||
for (Property<Object> property : props) {
|
||||
|
|
Loading…
Reference in a new issue