Seam SVN: r13417 - in modules/security/trunk: examples/idmconsole/src/main/java/org/jboss/seam/security/examples/idmconsole/model and 5 other directories.
by seam-commits@lists.jboss.org
Author: shane.bryzak(a)jboss.com
Date: 2010-07-17 06:27:05 -0400 (Sat, 17 Jul 2010)
New Revision: 13417
Added:
modules/security/trunk/examples/idmconsole/src/main/java/org/jboss/seam/security/examples/idmconsole/model/IdentityRoleName.java
modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/picketlink/JpaIdentityStore.java
modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/picketlink/JpaIdentityStoreConfiguration.java
modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/picketlink/JpaIdentityStoreSessionImpl.java
Removed:
modules/security/trunk/examples/idmconsole/src/main/java/org/jboss/seam/security/examples/idmconsole/model/RelationshipName.java
modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStore.java
modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStoreSessionImpl.java
modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/picketlink/IdentityConfigurationMetaDataProducer.java
Modified:
modules/security/trunk/examples/idmconsole/src/main/java/org/jboss/seam/security/examples/idmconsole/action/EntityManagerProducer.java
modules/security/trunk/examples/idmconsole/src/main/resources/import.sql
modules/security/trunk/examples/idmconsole/src/main/webapp/WEB-INF/classes/seam-beans.xml
modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/HashGenerator.java
modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/picketlink/IdentitySessionProducer.java
Log:
externalised identity store configuration
Modified: modules/security/trunk/examples/idmconsole/src/main/java/org/jboss/seam/security/examples/idmconsole/action/EntityManagerProducer.java
===================================================================
--- modules/security/trunk/examples/idmconsole/src/main/java/org/jboss/seam/security/examples/idmconsole/action/EntityManagerProducer.java 2010-07-16 23:46:40 UTC (rev 13416)
+++ modules/security/trunk/examples/idmconsole/src/main/java/org/jboss/seam/security/examples/idmconsole/action/EntityManagerProducer.java 2010-07-17 10:27:05 UTC (rev 13417)
@@ -11,5 +11,6 @@
@Stateless
public class EntityManagerProducer
{
- @Produces @RequestScoped @PersistenceContext(unitName = "idmconsoleDatabase") EntityManager entityManager;
+ @Produces /*@ManagedPersistenceContext @ConversationScoped*/ @RequestScoped
+ @PersistenceContext(unitName = "idmconsoleDatabase") EntityManager entityManager;
}
Copied: modules/security/trunk/examples/idmconsole/src/main/java/org/jboss/seam/security/examples/idmconsole/model/IdentityRoleName.java (from rev 13404, modules/security/trunk/examples/idmconsole/src/main/java/org/jboss/seam/security/examples/idmconsole/model/RelationshipName.java)
===================================================================
--- modules/security/trunk/examples/idmconsole/src/main/java/org/jboss/seam/security/examples/idmconsole/model/IdentityRoleName.java (rev 0)
+++ modules/security/trunk/examples/idmconsole/src/main/java/org/jboss/seam/security/examples/idmconsole/model/IdentityRoleName.java 2010-07-17 10:27:05 UTC (rev 13417)
@@ -0,0 +1,42 @@
+package org.jboss.seam.security.examples.idmconsole.model;
+
+import java.io.Serializable;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+/**
+ * This is a simple lookup table containing role names
+ *
+ * @author Shane Bryzak
+ */
+@Entity
+public class IdentityRoleName implements Serializable
+{
+ private static final long serialVersionUID = 8775236263787825703L;
+
+ private Long id;
+ private String name;
+
+ @Id @GeneratedValue
+ public Long getId()
+ {
+ return id;
+ }
+
+ public void setId(Long id)
+ {
+ this.id = id;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public void setName(String name)
+ {
+ this.name = name;
+ }
+}
Deleted: modules/security/trunk/examples/idmconsole/src/main/java/org/jboss/seam/security/examples/idmconsole/model/RelationshipName.java
===================================================================
--- modules/security/trunk/examples/idmconsole/src/main/java/org/jboss/seam/security/examples/idmconsole/model/RelationshipName.java 2010-07-16 23:46:40 UTC (rev 13416)
+++ modules/security/trunk/examples/idmconsole/src/main/java/org/jboss/seam/security/examples/idmconsole/model/RelationshipName.java 2010-07-17 10:27:05 UTC (rev 13417)
@@ -1,42 +0,0 @@
-package org.jboss.seam.security.examples.idmconsole.model;
-
-import java.io.Serializable;
-
-import javax.persistence.Entity;
-import javax.persistence.GeneratedValue;
-import javax.persistence.Id;
-
-/**
- * This is a simple lookup table containing relationship (i.e. role) names.
- *
- * @author Shane Bryzak
- */
-@Entity
-public class RelationshipName implements Serializable
-{
- private static final long serialVersionUID = 8775236263787825703L;
-
- private Long id;
- private String name;
-
- @Id @GeneratedValue
- public Long getId()
- {
- return id;
- }
-
- public void setId(Long id)
- {
- this.id = id;
- }
-
- public String getName()
- {
- return name;
- }
-
- public void setName(String name)
- {
- this.name = name;
- }
-}
Modified: modules/security/trunk/examples/idmconsole/src/main/resources/import.sql
===================================================================
--- modules/security/trunk/examples/idmconsole/src/main/resources/import.sql 2010-07-16 23:46:40 UTC (rev 13416)
+++ modules/security/trunk/examples/idmconsole/src/main/resources/import.sql 2010-07-17 10:27:05 UTC (rev 13417)
@@ -1,5 +1,5 @@
-insert into RelationshipName(id, name) values (1, 'admin');
-insert into RelationshipName(id, name) values (2, 'manager');
+insert into IdentityRoleName(id, name) values (1, 'admin');
+insert into IdentityRoleName(id, name) values (2, 'manager');
insert into IdentityObjectType(id, name) values (1, 'USER');
insert into IdentityObjectType(id, name) values (2, 'GROUP');
Modified: modules/security/trunk/examples/idmconsole/src/main/webapp/WEB-INF/classes/seam-beans.xml
===================================================================
--- modules/security/trunk/examples/idmconsole/src/main/webapp/WEB-INF/classes/seam-beans.xml 2010-07-16 23:46:40 UTC (rev 13416)
+++ modules/security/trunk/examples/idmconsole/src/main/webapp/WEB-INF/classes/seam-beans.xml 2010-07-17 10:27:05 UTC (rev 13417)
@@ -4,6 +4,7 @@
xmlns:s="urn:java:ee"
xmlns:drools="urn:java:org.jboss.seam.drools:org.jboss.seam.drools.config"
xmlns:security="urn:java:org.jboss.seam.security.permission"
+ xmlns:plidm="urn:java:org.jboss.seam.security.management.picketlink"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
@@ -39,4 +40,13 @@
</security:securityRules>
</security:RuleBasedPermissionResolver>
+ <plidm:JpaIdentityStoreConfiguration>
+ <s:overrides/>
+ <plidm:id>jpa</plidm:id>
+ <plidm:identityClass>org.jboss.seam.security.examples.idmconsole.model.IdentityObject</plidm:identityClass>
+ <plidm:credentialClass>org.jboss.seam.security.examples.idmconsole.model.IdentityObjectCredential</plidm:credentialClass>
+ <plidm:relationshipClass>org.jboss.seam.security.examples.idmconsole.model.IdentityObjectRelationship</plidm:relationshipClass>
+ <plidm:roleNameClass>org.jboss.seam.security.examples.idmconsole.model.IdentityRoleName</plidm:roleNameClass>
+ </plidm:JpaIdentityStoreConfiguration>
+
</beans>
Modified: modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/HashGenerator.java
===================================================================
--- modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/HashGenerator.java 2010-07-16 23:46:40 UTC (rev 13416)
+++ modules/security/trunk/examples/seamspace/src/main/java/org/jboss/seam/security/examples/seamspace/action/HashGenerator.java 2010-07-17 10:27:05 UTC (rev 13417)
@@ -4,8 +4,8 @@
import javax.inject.Inject;
import org.jboss.seam.security.crypto.BinTools;
-import org.jboss.seam.security.management.JpaIdentityStore;
import org.jboss.seam.security.management.PasswordHash;
+import org.jboss.seam.security.management.picketlink.JpaIdentityStore;
@Model
public class HashGenerator
Deleted: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStore.java
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStore.java 2010-07-16 23:46:40 UTC (rev 13416)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStore.java 2010-07-17 10:27:05 UTC (rev 13417)
@@ -1,1798 +0,0 @@
-package org.jboss.seam.security.management;
-
-import java.io.Serializable;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import javax.persistence.Entity;
-import javax.persistence.EntityManager;
-import javax.persistence.Id;
-import javax.persistence.NoResultException;
-import javax.persistence.criteria.CriteriaBuilder;
-import javax.persistence.criteria.CriteriaQuery;
-import javax.persistence.criteria.Predicate;
-import javax.persistence.criteria.Root;
-
-import org.jboss.seam.security.annotations.management.IdentityProperty;
-import org.jboss.seam.security.annotations.management.PropertyType;
-import org.jboss.weld.extensions.util.properties.Property;
-import org.jboss.weld.extensions.util.properties.query.AnnotatedPropertyCriteria;
-import org.jboss.weld.extensions.util.properties.query.NamedPropertyCriteria;
-import org.jboss.weld.extensions.util.properties.query.PropertyCriteria;
-import org.jboss.weld.extensions.util.properties.query.PropertyQueries;
-import org.jboss.weld.extensions.util.properties.query.TypedPropertyCriteria;
-import org.picketlink.idm.common.exception.IdentityException;
-import org.picketlink.idm.impl.store.FeaturesMetaDataImpl;
-import org.picketlink.idm.spi.configuration.IdentityStoreConfigurationContext;
-import org.picketlink.idm.spi.configuration.metadata.IdentityObjectAttributeMetaData;
-import org.picketlink.idm.spi.exception.OperationNotSupportedException;
-import org.picketlink.idm.spi.model.IdentityObject;
-import org.picketlink.idm.spi.model.IdentityObjectAttribute;
-import org.picketlink.idm.spi.model.IdentityObjectCredential;
-import org.picketlink.idm.spi.model.IdentityObjectRelationship;
-import org.picketlink.idm.spi.model.IdentityObjectRelationshipType;
-import org.picketlink.idm.spi.model.IdentityObjectType;
-import org.picketlink.idm.spi.search.IdentityObjectSearchCriteria;
-import org.picketlink.idm.spi.store.FeaturesMetaData;
-import org.picketlink.idm.spi.store.IdentityObjectSearchCriteriaType;
-import org.picketlink.idm.spi.store.IdentityStoreInvocationContext;
-import org.picketlink.idm.spi.store.IdentityStoreSession;
-/*import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;*/
-
-/**
- * IdentityStore implementation that allows identity related data to be
- * persisted in a database via JPA
- *
- * @author Shane Bryzak
- */
-public class JpaIdentityStore implements org.picketlink.idm.spi.store.IdentityStore, Serializable
-{
- private static final long serialVersionUID = 7729139146633529501L;
-
- //private Logger log = LoggerFactory.getLogger(JpaIdentityStore.class);
-
- public static final String OPTION_IDENTITY_CLASS_NAME = "identityEntityClassName";
- public static final String OPTION_CREDENTIAL_CLASS_NAME = "credentialEntityClassName";
- public static final String OPTION_RELATIONSHIP_CLASS_NAME = "relationshipEntityClassName";
- public static final String OPTION_RELATIONSHIP_NAME_CLASS_NAME = "relationshipNameEntityClassName";
-
- private static final String DEFAULT_USER_IDENTITY_TYPE = "USER";
- private static final String DEFAULT_ROLE_IDENTITY_TYPE = "ROLE";
- private static final String DEFAULT_GROUP_IDENTITY_TYPE = "GROUP";
-
- private static final String DEFAULT_RELATIONSHIP_TYPE_MEMBERSHIP = "MEMBERSHIP";
- private static final String DEFAULT_RELATIONSHIP_TYPE_ROLE = "ROLE";
-
- // Property keys
-
- private static final String PROPERTY_IDENTITY_ID = "IDENTITY_ID";
- private static final String PROPERTY_IDENTITY_NAME = "IDENTITY_NAME";
- private static final String PROPERTY_IDENTITY_TYPE = "IDENTITY_TYPE";
- private static final String PROPERTY_IDENTITY_TYPE_NAME = "IDENTITY_TYPE_NAME";
- private static final String PROPERTY_CREDENTIAL_VALUE = "CREDENTIAL_VALUE";
- private static final String PROPERTY_CREDENTIAL_TYPE = "CREDENTIAL_TYPE";
- private static final String PROPERTY_CREDENTIAL_TYPE_NAME = "CREDENTIAL_TYPE_NAME";
- private static final String PROPERTY_CREDENTIAL_IDENTITY = "CREDENTIAL_IDENTITY";
- private static final String PROPERTY_RELATIONSHIP_FROM = "RELATIONSHIP_FROM";
- private static final String PROPERTY_RELATIONSHIP_TO = "RELATIONSHIP_TO";
- private static final String PROPERTY_RELATIONSHIP_TYPE = "RELATIONSHIP_TYPE";
- private static final String PROPERTY_RELATIONSHIP_TYPE_NAME = "RELATIONSHIP_TYPE_NAME";
- private static final String PROPERTY_RELATIONSHIP_NAME = "RELATIONSHIP_NAME";
-
- // Distinct from PROPERTY_RELATIONSHIP NAME - this property refers to the name field in the RELATIONSHIP_NAME entity
- private static final String PROPERTY_RELATIONSHIP_NAME_NAME = "RELATIONSHIP_NAME_NAME";
- private static final String PROPERTY_ATTRIBUTE_NAME = "ATTRIBUTE_NAME";
- private static final String PROPERTY_ATTRIBUTE_VALUE = "ATTRIBUTE_VALUE";
- private static final String PROPERTY_ROLE_TYPE_NAME = "ROLE_TYPE_NAME";
-
- private class EntityToSpiConverter
- {
- private static final String IDENTITY_TYPE_CACHE_PREFIX = "identity_type:";
- private static final String RELATIONSHIP_TYPE_CACHE_PREFIX = "relationship_type:";
-
- private Map<Object,Object> cache = new HashMap<Object,Object>();
-
- private Property<?> identityIdProperty = modelProperties.get(PROPERTY_IDENTITY_ID);
- private Property<?> identityNameProperty = modelProperties.get(PROPERTY_IDENTITY_NAME);
- private Property<?> identityTypeProperty = modelProperties.get(PROPERTY_IDENTITY_TYPE);
- private Property<?> identityTypeNameProperty = modelProperties.get(PROPERTY_IDENTITY_TYPE_NAME);
- private Property<?> relationshipTypeNameProperty = modelProperties.get(PROPERTY_RELATIONSHIP_TYPE_NAME);
-
- public IdentityObject convertToIdentityObject(Object entity)
- {
- if (!identityClass.isAssignableFrom(entity.getClass()))
- {
- throw new IllegalArgumentException("Invalid identity entity");
- }
-
- if (cache.containsKey(entity))
- {
- return (IdentityObject) cache.get(entity);
- }
- else
- {
- IdentityObject obj = new IdentityObjectImpl(
- identityIdProperty.getValue(entity).toString(),
- identityNameProperty.getValue(entity).toString(),
- convertToIdentityObjectType(identityTypeProperty.getValue(entity)));
- cache.put(entity, obj);
-
- return obj;
- }
- }
-
- public IdentityObjectType convertToIdentityObjectType(Object value)
- {
- if (value instanceof String)
- {
- String key = IDENTITY_TYPE_CACHE_PREFIX + (String) value;
- if (cache.containsKey(key)) return (IdentityObjectType) cache.get(key);
-
- IdentityObjectType type = new IdentityObjectTypeImpl((String) value);
- cache.put(key, type);
- return type;
- }
- else
- {
- if (cache.containsKey(value)) return (IdentityObjectType) cache.get(value);
- IdentityObjectType type = new IdentityObjectTypeImpl(
- (String) identityTypeNameProperty.getValue(value));
- cache.put(value, type);
- return type;
- }
- }
-
- public IdentityObjectRelationshipType convertToRelationshipType(Object value)
- {
- if (value instanceof String)
- {
- String key = RELATIONSHIP_TYPE_CACHE_PREFIX + (String) value;
- if (cache.containsKey(key)) return (IdentityObjectRelationshipType) cache.get(key);
-
- IdentityObjectRelationshipType type = new IdentityObjectRelationshipTypeImpl((String) value);
- cache.put(key, type);
- return type;
- }
- else
- {
- if (cache.containsKey(value)) return (IdentityObjectRelationshipType) cache.get(value);
- IdentityObjectRelationshipType type = new IdentityObjectRelationshipTypeImpl(
- (String) relationshipTypeNameProperty.getValue(value));
- cache.put(value, type);
- return type;
- }
- }
- }
-
-
- private String id;
-
- // Entity classes
- private Class<?> identityClass;
- private Class<?> credentialClass;
- private Class<?> relationshipClass;
- private Class<?> attributeClass;
- private Class<?> roleTypeClass;
- private Class<?> relationshipNameClass;
-
- private String userIdentityType = DEFAULT_USER_IDENTITY_TYPE;
- private String roleIdentityType = DEFAULT_ROLE_IDENTITY_TYPE;
- private String groupIdentityType = DEFAULT_GROUP_IDENTITY_TYPE;
-
- private String relationshipTypeMembership = DEFAULT_RELATIONSHIP_TYPE_MEMBERSHIP;
- private String relationshipTypeRole = DEFAULT_RELATIONSHIP_TYPE_ROLE;
-
- /**
- * Model properties
- */
- private Map<String,Property<Object>> modelProperties = new HashMap<String,Property<Object>>();
-
- /**
- * Attribute properties
- */
- private Map<String,Property<Object>> attributeProperties = new HashMap<String,Property<Object>>();
-
- private FeaturesMetaData featuresMetaData;
-
- private class PropertyTypeCriteria implements PropertyCriteria
- {
- private PropertyType pt;
-
- public PropertyTypeCriteria(PropertyType pt)
- {
- this.pt = pt;
- }
-
- public boolean fieldMatches(Field f)
- {
- return f.isAnnotationPresent(IdentityProperty.class) &&
- f.getAnnotation(IdentityProperty.class).value().equals(pt);
- }
-
- public boolean methodMatches(Method m)
- {
- return m.isAnnotationPresent(IdentityProperty.class) &&
- m.getAnnotation(IdentityProperty.class).value().equals(pt);
- }
- }
-
- public JpaIdentityStore(String id)
- {
- this.id = id;
- }
-
- public void bootstrap(IdentityStoreConfigurationContext configurationContext)
- throws IdentityException
- {
- String clsName = configurationContext.getStoreConfigurationMetaData()
- .getOptionSingleValue(OPTION_IDENTITY_CLASS_NAME);
-
- try
- {
- identityClass = Class.forName(clsName);
- }
- catch (ClassNotFoundException e)
- {
- throw new IdentityException("Error bootstrapping JpaIdentityStore - invalid identity entity class: " + clsName);
- }
-
- if (identityClass == null)
- {
- throw new IdentityException(
- "Error initializing JpaIdentityStore - identityClass not set");
- }
-
- clsName = configurationContext.getStoreConfigurationMetaData()
- .getOptionSingleValue(OPTION_CREDENTIAL_CLASS_NAME);
-
- if (clsName != null)
- {
- try
- {
- credentialClass = Class.forName(clsName);
- }
- catch (ClassNotFoundException e)
- {
- throw new IdentityException("Error bootstrapping JpaIdentityStore - invalid credential entity class: " + clsName);
- }
- }
-
- clsName = configurationContext.getStoreConfigurationMetaData()
- .getOptionSingleValue(OPTION_RELATIONSHIP_CLASS_NAME);
-
- try
- {
- relationshipClass = Class.forName(clsName);
- }
- catch (ClassNotFoundException e)
- {
- throw new IdentityException("Error bootstrapping JpaIdentityStore - invalid relationship entity class: " + clsName);
- }
-
- boolean namedRelationshipsSupported = false;
-
- clsName = configurationContext.getStoreConfigurationMetaData()
- .getOptionSingleValue(OPTION_RELATIONSHIP_NAME_CLASS_NAME);
-
- if (clsName != null)
- {
- try
- {
- relationshipNameClass = Class.forName(clsName);
- namedRelationshipsSupported = true;
- }
- catch (ClassNotFoundException e)
- {
- throw new IdentityException("Error bootstrapping JpaIdentityStore - invalid relationship name entity class: " + clsName);
- }
- }
-
- configureIdentityId();
- configureIdentityName();
- configureIdentityType();
-
- configureCredentials();
- configureRelationships();
- configureAttributes();
- configureRoleTypeNames();
-
- if (namedRelationshipsSupported)
- {
- configureRelationshipNames();
- }
-
- featuresMetaData = new FeaturesMetaDataImpl(
- configurationContext.getStoreConfigurationMetaData(),
- new HashSet<IdentityObjectSearchCriteriaType>(),
- false,
- namedRelationshipsSupported,
- new HashSet<String>()
- );
- }
-
- protected void configureIdentityId() throws IdentityException
- {
- List<Property<Object>> props = PropertyQueries.createQuery(identityClass)
- .addCriteria(new AnnotatedPropertyCriteria(Id.class))
- .getResultList();
-
- if (props.size() == 1)
- {
- modelProperties.put(PROPERTY_IDENTITY_ID, props.get(0));
- }
- else
- {
- throw new IdentityException("Error initializing JpaIdentityStore - no Identity ID found.");
- }
- }
-
- protected void configureIdentityName() throws IdentityException
- {
- List<Property<Object>> props = PropertyQueries.createQuery(identityClass)
- .addCriteria(new PropertyTypeCriteria(PropertyType.NAME))
- .getResultList();
-
- if (props.size() == 1)
- {
- modelProperties.put(PROPERTY_IDENTITY_NAME, props.get(0));
- }
- else if (props.size() > 1)
- {
- throw new IdentityException(
- "Ambiguous identity name property in identity class " + identityClass.getName());
- }
- else
- {
- Property<Object> p = findNamedProperty(identityClass, "username", "userName", "name");
- if (p != null)
- {
- modelProperties.put(PROPERTY_IDENTITY_NAME, p);
- }
- else
- {
- // Last resort - check whether the entity class exposes a single String property
- // if so, let's assume it's the identity name
- props = PropertyQueries.createQuery(identityClass)
- .addCriteria(new TypedPropertyCriteria(String.class))
- .getResultList();
- if (props.size() == 1)
- {
- modelProperties.put(PROPERTY_IDENTITY_NAME, props.get(0));
- }
- }
- }
-
- if (!modelProperties.containsKey(PROPERTY_IDENTITY_NAME))
- {
- throw new IdentityException("Error initializing JpaIdentityStore - no valid identity name property found.");
- }
- }
-
- protected void configureIdentityType() throws IdentityException
- {
- List<Property<Object>> props = PropertyQueries.createQuery(identityClass)
- .addCriteria(new PropertyTypeCriteria(PropertyType.TYPE))
- .getResultList();
-
- if (props.size() == 1)
- {
- modelProperties.put(PROPERTY_IDENTITY_TYPE, props.get(0));
- }
- else if (props.size() > 1)
- {
- throw new IdentityException(
- "Ambiguous identity type property in identity class " + identityClass.getName());
- }
- else
- {
- Property<Object> p = findNamedProperty(identityClass, "identityObjectType",
- "identityType", "identityObjectTypeName", "identityTypeName",
- "typeName", "discriminator", "accountType", "userType", "type");
- if (p != null)
- {
- modelProperties.put(PROPERTY_IDENTITY_TYPE, props.get(0));
- }
- else
- {
- // Last resort - let's check all properties, and try to find one
- // with an entity type that has "type" in its name
- props = PropertyQueries.createQuery(identityClass).getResultList();
- search: for (Property<Object> typeProp : props)
- {
- if (typeProp.getJavaClass().isAnnotationPresent(Entity.class) &&
- (typeProp.getJavaClass().getSimpleName().contains("type") ||
- typeProp.getJavaClass().getSimpleName().contains("Type")))
- {
- // we have a potential match, let's check if this entity has a name property
- Property<Object> nameProp = findNamedProperty(typeProp.getJavaClass(),
- "identityObjectTypeName", "identityTypeName", "typeName", "name");
- if (nameProp != null)
- {
- modelProperties.put(PROPERTY_IDENTITY_TYPE, typeProp);
- modelProperties.put(PROPERTY_IDENTITY_TYPE_NAME, nameProp);
- break search;
- }
- }
- }
- }
- }
-
- Property<?> typeProp = modelProperties.get(PROPERTY_IDENTITY_TYPE);
-
- if (typeProp == null)
- {
- throw new IdentityException("Error initializing JpaIdentityStore - no valid identity type property found.");
- }
-
- if (!String.class.equals(typeProp.getJavaClass()) &&
- !modelProperties.containsKey(PROPERTY_IDENTITY_TYPE_NAME))
- {
- // We're not dealing with a simple type name - validate the lookup type
- Property<Object> nameProp = findNamedProperty(typeProp.getJavaClass(),
- "identityObjectTypeName", "identityTypeName", "typeName", "name");
- if (nameProp != null)
- {
- modelProperties.put(PROPERTY_IDENTITY_TYPE_NAME, nameProp);
- }
- else
- {
- throw new IdentityException("Error initializing JpaIdentityStore - no valid identity type name property found.");
- }
- }
- }
-
- protected Property<Object> findNamedProperty(Class<?> targetClass, String... allowedNames)
- {
- List<Property<Object>> props = PropertyQueries.createQuery(targetClass)
- .addCriteria(new TypedPropertyCriteria(String.class))
- .addCriteria(new PropertyTypeCriteria(PropertyType.NAME))
- .getResultList();
-
- if (props.size() == 1)
- {
- return props.get(0);
- }
- else
- {
- props = PropertyQueries.createQuery(targetClass)
- .addCriteria(new TypedPropertyCriteria(String.class))
- .addCriteria(new NamedPropertyCriteria(allowedNames))
- .getResultList();
-
- for (String name : allowedNames)
- {
- for (Property<Object> prop : props)
- {
- if (name.equals(prop.getName())) return prop;
- }
- }
- }
-
- return null;
- }
-
- protected void configureCredentials() throws IdentityException
- {
- // If a credential entity has been explicitly configured, scan it
- if (credentialClass != null)
- {
- List<Property<Object>> props = PropertyQueries.createQuery(credentialClass)
- .addCriteria(new PropertyTypeCriteria(PropertyType.VALUE))
- .getResultList();
-
- if (props.size() == 1)
- {
- modelProperties.put(PROPERTY_CREDENTIAL_VALUE, props.get(0));
- }
- else if (props.size() > 1)
- {
- throw new IdentityException(
- "Ambiguous credential value property in credential class " +
- credentialClass.getName());
- }
- else
- {
- // Try scanning for a credential property also
- props = PropertyQueries.createQuery(credentialClass)
- .addCriteria(new PropertyTypeCriteria(PropertyType.CREDENTIAL))
- .getResultList();
- if (props.size() == 1)
- {
- modelProperties.put(PROPERTY_CREDENTIAL_VALUE, props.get(0));
- }
- else if (props.size() > 1)
- {
- throw new IdentityException(
- "Ambiguous credential value property in credential class " +
- credentialClass.getName());
- }
- else
- {
- Property<Object> p = findNamedProperty(credentialClass, "credentialValue",
- "password", "passwordHash", "credential", "value");
- if (p != null) modelProperties.put(PROPERTY_CREDENTIAL_VALUE, p);
- }
- }
-
- // Scan for the credential identity property
- props = PropertyQueries.createQuery(credentialClass)
- .addCriteria(new TypedPropertyCriteria(identityClass))
- .getResultList();
- if (props.size() == 1)
- {
- modelProperties.put(PROPERTY_CREDENTIAL_IDENTITY, props.get(0));
- }
- else if (props.size() > 1)
- {
- throw new IdentityException(
- "Ambiguous identity property in credential class " +
- credentialClass.getName());
- }
- else
- {
- // Scan for a named identity property
- props = PropertyQueries.createQuery(credentialClass)
- .addCriteria(new NamedPropertyCriteria("identity", "identityObject"))
- .getResultList();
- if (!props.isEmpty())
- {
- modelProperties.put(PROPERTY_CREDENTIAL_IDENTITY, props.get(0));
- }
- else
- {
- throw new IdentityException("Error initializing JpaIdentityStore - no credential identity property found.");
- }
- }
- }
- else
- {
- // The credentials may be stored in the identity class
- List<Property<Object>> props = PropertyQueries.createQuery(identityClass)
- .addCriteria(new PropertyTypeCriteria(PropertyType.CREDENTIAL))
- .getResultList();
-
- if (props.size() == 1)
- {
- modelProperties.put(PROPERTY_CREDENTIAL_VALUE, props.get(0));
- }
- else if (props.size() > 1)
- {
- throw new IdentityException(
- "Ambiguous credential property in identity class " +
- identityClass.getName());
- }
- else
- {
- Property<Object> p = findNamedProperty(identityClass, "credentialValue",
- "password", "passwordHash", "credential", "value");
- if (p != null) modelProperties.put(PROPERTY_CREDENTIAL_VALUE, p);
- }
- }
-
- if (!modelProperties.containsKey(PROPERTY_CREDENTIAL_VALUE))
- {
- throw new IdentityException("Error initializing JpaIdentityStore - no credential value property found.");
- }
-
- // Scan for a credential type property
- List<Property<Object>> props = PropertyQueries.createQuery(credentialClass)
- .addCriteria(new PropertyTypeCriteria(PropertyType.TYPE))
- .getResultList();
-
- if (props.size() == 1)
- {
- modelProperties.put(PROPERTY_CREDENTIAL_TYPE, props.get(0));
- }
- else if (props.size() > 1)
- {
- throw new IdentityException(
- "Ambiguous credential type property in credential class " +
- credentialClass.getName());
- }
- else
- {
- props = PropertyQueries.createQuery(credentialClass)
- .addCriteria(new PropertyTypeCriteria(PropertyType.CREDENTIAL_TYPE))
- .getResultList();
-
- if (props.size() == 1)
- {
- modelProperties.put(PROPERTY_CREDENTIAL_TYPE, props.get(0));
- }
- else if (props.size() > 1)
- {
- throw new IdentityException(
- "Ambiguous credential type property in credential class " +
- credentialClass.getName());
- }
- else
- {
- Property<Object> p = findNamedProperty(credentialClass, "credentialType",
- "identityObjectCredentialType", "type");
- if (p != null) modelProperties.put(PROPERTY_CREDENTIAL_TYPE, p);
- }
- }
-
- Property<?> typeProp = modelProperties.get(PROPERTY_CREDENTIAL_TYPE);
-
- // If the credential type property isn't a String, then validate the lookup type
- if (!String.class.equals(typeProp.getJavaClass()))
- {
- Property<Object> nameProp = findNamedProperty(typeProp.getJavaClass(),
- "credentialObjectTypeName", "credentialTypeName", "typeName", "name");
- if (nameProp != null)
- {
- modelProperties.put(PROPERTY_CREDENTIAL_TYPE_NAME, nameProp);
- }
- else
- {
- throw new IdentityException("Error initializing JpaIdentityStore - no valid credential type name property found.");
- }
- }
- }
-
- protected void configureRelationships() throws IdentityException
- {
- if (relationshipClass == null)
- {
- throw new IdentityException("Error initializing JpaIdentityStore - relationshipClass not set.");
- }
-
- List<Property<Object>> props = PropertyQueries.createQuery(relationshipClass)
- .addCriteria(new TypedPropertyCriteria(identityClass))
- .addCriteria(new PropertyTypeCriteria(PropertyType.RELATIONSHIP_FROM))
- .getResultList();
-
- if (props.size() == 1)
- {
- modelProperties.put(PROPERTY_RELATIONSHIP_FROM, props.get(0));
- }
- else if (props.size() > 1)
- {
- throw new IdentityException(
- "Ambiguous relationshipFrom property in relationship class " +
- relationshipClass.getName());
- }
- else
- {
- Property<Object> p = findNamedProperty(relationshipClass, "relationshipFrom",
- "fromIdentityObject", "fromIdentity");
- if (p != null)
- {
- modelProperties.put(PROPERTY_RELATIONSHIP_FROM, p);
- }
- else
- {
- // Last resort - search for a property with a type of identityClass
- // and a "from" in its name
- props = PropertyQueries.createQuery(relationshipClass)
- .addCriteria(new TypedPropertyCriteria(identityClass))
- .getResultList();
-
- for (Property<Object> prop : props)
- {
- if (prop.getName().contains("from"))
- {
- modelProperties.put(PROPERTY_RELATIONSHIP_FROM, prop);
- break;
- }
- }
- }
- }
-
-
- props = PropertyQueries.createQuery(relationshipClass)
- .addCriteria(new TypedPropertyCriteria(identityClass))
- .addCriteria(new PropertyTypeCriteria(PropertyType.RELATIONSHIP_TO))
- .getResultList();
-
- if (props.size() == 1)
- {
- modelProperties.put(PROPERTY_RELATIONSHIP_TO, props.get(0));
- }
- else if (props.size() > 1)
- {
- throw new IdentityException(
- "Ambiguous relationshipTo property in relationship class " +
- relationshipClass.getName());
- }
- else
- {
- Property<Object> p = findNamedProperty(relationshipClass, "relationshipTo",
- "toIdentityObject", "toIdentity");
- if (p != null)
- {
- modelProperties.put(PROPERTY_RELATIONSHIP_TO, p);
- }
- else
- {
- // Last resort - search for a property with a type of identityClass
- // and a "to" in its name
- props = PropertyQueries.createQuery(relationshipClass)
- .addCriteria(new TypedPropertyCriteria(identityClass))
- .getResultList();
-
- for (Property<Object> prop : props)
- {
- if (prop.getName().contains("to"))
- {
- modelProperties.put(PROPERTY_RELATIONSHIP_TO, prop);
- break;
- }
- }
- }
- }
-
- props = PropertyQueries.createQuery(relationshipClass)
- .addCriteria(new PropertyTypeCriteria(PropertyType.TYPE))
- .getResultList();
- if (props.size() == 1)
- {
- modelProperties.put(PROPERTY_RELATIONSHIP_TYPE, props.get(0));
- }
- else if (props.size() > 1)
- {
- throw new IdentityException(
- "Ambiguous relationshipType property in relationship class " +
- relationshipClass.getName());
- }
- else
- {
- Property<Object> p = findNamedProperty(relationshipClass,
- "identityRelationshipType", "relationshipType", "type");
- if (p != null)
- {
- modelProperties.put(PROPERTY_RELATIONSHIP_TYPE, p);
- }
- else
- {
- props = PropertyQueries.createQuery(relationshipClass)
- .getResultList();
- for (Property<Object> prop : props)
- {
- if (prop.getName().contains("type"))
- {
- modelProperties.put(PROPERTY_RELATIONSHIP_TYPE, prop);
- break;
- }
- }
- }
- }
-
- props = PropertyQueries.createQuery(relationshipClass)
- .addCriteria(new PropertyTypeCriteria(PropertyType.NAME))
- .addCriteria(new TypedPropertyCriteria(String.class))
- .getResultList();
-
- if (props.size() == 1)
- {
- modelProperties.put(PROPERTY_RELATIONSHIP_NAME, props.get(0));
- }
- else if (props.size() > 1)
- {
- throw new IdentityException(
- "Ambiguous relationship name property in relationship class " +
- relationshipClass.getName());
- }
- else
- {
- Property<Object> p = findNamedProperty(relationshipClass,
- "relationshipName", "name");
- if (p != null)
- {
- modelProperties.put(PROPERTY_RELATIONSHIP_NAME, p);
- }
- }
-
- if (!modelProperties.containsKey(PROPERTY_RELATIONSHIP_FROM))
- {
- throw new IdentityException(
- "Error initializing JpaIdentityStore - no valid relationship from property found.");
- }
-
- if (!modelProperties.containsKey(PROPERTY_RELATIONSHIP_TO))
- {
- throw new IdentityException(
- "Error initializing JpaIdentityStore - no valid relationship to property found.");
- }
-
- if (!modelProperties.containsKey(PROPERTY_RELATIONSHIP_TYPE))
- {
- throw new IdentityException(
- "Error initializing JpaIdentityStore - no valid relationship type property found.");
- }
-
- if (!modelProperties.containsKey(PROPERTY_RELATIONSHIP_NAME))
- {
- throw new IdentityException(
- "Error initializing JpaIdentityStore - no valid relationship name property found.");
- }
-
- Class<?> typeClass = modelProperties.get(PROPERTY_RELATIONSHIP_TYPE).getJavaClass();
- if (!String.class.equals(typeClass))
- {
- props = PropertyQueries.createQuery(typeClass)
- .addCriteria(new PropertyTypeCriteria(PropertyType.NAME))
- .addCriteria(new TypedPropertyCriteria(String.class))
- .getResultList();
-
- if (props.size() == 1)
- {
- modelProperties.put(PROPERTY_RELATIONSHIP_TYPE_NAME, props.get(0));
- }
- else if (props.size() > 1)
- {
- throw new IdentityException(
- "Ambiguous relationship type name property in class " +
- typeClass.getName());
- }
- else
- {
- Property<Object> p = findNamedProperty(typeClass, "relationshipTypeName",
- "typeName", "name");
- if (p != null)
- {
- modelProperties.put(PROPERTY_RELATIONSHIP_TYPE_NAME, p);
- }
- }
-
- if (!modelProperties.containsKey(PROPERTY_RELATIONSHIP_TYPE_NAME))
- {
- throw new IdentityException(
- "Error initializing JpaIdentityStore - no valid relationship type name property found");
- }
- }
- }
-
- protected void configureAttributes() throws IdentityException
- {
- // If an attribute class has been configured, scan it for attributes
- if (attributeClass != null)
- {
- List<Property<Object>> props = PropertyQueries.createQuery(attributeClass)
- .addCriteria(new PropertyTypeCriteria(PropertyType.NAME))
- .addCriteria(new TypedPropertyCriteria(String.class))
- .getResultList();
-
- if (props.size() == 1)
- {
- modelProperties.put(PROPERTY_ATTRIBUTE_NAME, props.get(0));
- }
- else if (props.size() > 1)
- {
- throw new IdentityException(
- "Ambiguous attribute name property in class " +
- attributeClass.getName());
- }
- else
- {
- Property<Object> prop = findNamedProperty(attributeClass,
- "attributeName", "name");
- if (prop != null) modelProperties.put(PROPERTY_ATTRIBUTE_NAME, prop);
- }
-
- props = PropertyQueries.createQuery(attributeClass)
- .addCriteria(new PropertyTypeCriteria(PropertyType.VALUE))
- .getResultList();
-
- if (props.size() == 1)
- {
- modelProperties.put(PROPERTY_ATTRIBUTE_VALUE, props.get(0));
- }
- else if (props.size() > 1)
- {
- throw new IdentityException(
- "Ambiguous attribute value property in class " +
- attributeClass.getName());
- }
- else
- {
- Property<Object> prop = findNamedProperty(attributeClass,
- "attributeValue", "value");
- if (prop != null) modelProperties.put(PROPERTY_ATTRIBUTE_VALUE, prop);
- }
- }
-
- // Scan for additional attributes in the identity class also
- List<Property<Object>> props = PropertyQueries.createQuery(identityClass)
- .addCriteria(new PropertyTypeCriteria(PropertyType.ATTRIBUTE))
- .getResultList();
-
- for (Property<Object> p : props)
- {
- attributeProperties.put(
- p.getAnnotatedElement().getAnnotation(IdentityProperty.class).attributeName(),
- p);
- }
-
- // scan any entity classes referenced by the identity class also
- props = PropertyQueries.createQuery(identityClass)
- .getResultList();
-
- for (Property<Object> p : props)
- {
- if (!p.isReadOnly() && p.getJavaClass().isAnnotationPresent(Entity.class))
- {
- List<Property<Object>> pp = PropertyQueries.createQuery(p.getJavaClass())
- .addCriteria(new PropertyTypeCriteria(PropertyType.ATTRIBUTE))
- .getResultList();
-
- for (Property<Object> attributeProperty : pp)
- {
- attributeProperties.put(
- attributeProperty.getAnnotatedElement().getAnnotation(IdentityProperty.class).attributeName(),
- attributeProperty);
- }
- }
- }
- }
-
- protected void configureRoleTypeNames()
- {
- if (roleTypeClass != null)
- {
- List<Property<Object>> props = PropertyQueries.createQuery(roleTypeClass)
- .addCriteria(new PropertyTypeCriteria(PropertyType.NAME))
- .getResultList();
-
- if (props.size() == 1)
- {
- modelProperties.put(PROPERTY_ROLE_TYPE_NAME, props.get(0));
- }
- }
- }
-
- protected void configureRelationshipNames()
- {
- Property<Object> relationshipNameProp = findNamedProperty(relationshipNameClass, "name");
- if (relationshipNameProp != null)
- {
- modelProperties.put(PROPERTY_RELATIONSHIP_NAME_NAME, relationshipNameProp);
- }
- }
-
- public String getUserIdentityType()
- {
- return userIdentityType;
- }
-
- public void setUserIdentityType(String userIdentityType)
- {
- this.userIdentityType = userIdentityType;
- }
-
- public String getRoleIdentityType()
- {
- return roleIdentityType;
- }
-
- public void setRoleIdentityType(String roleIdentityType)
- {
- this.roleIdentityType = roleIdentityType;
- }
-
- public String getGroupIdentityType()
- {
- return groupIdentityType;
- }
-
- public void setGroupIdentityType(String groupIdentityType)
- {
- this.groupIdentityType = groupIdentityType;
- }
-
- public String getRelationshipTypeMembership()
- {
- return relationshipTypeMembership;
- }
-
- public void setRelationshipTypeMembership(String relationshipTypeMembership)
- {
- this.relationshipTypeMembership = relationshipTypeMembership;
- }
-
- public String getRelationshipTypeRole()
- {
- return relationshipTypeRole;
- }
-
- public void setRelationshipTypeRole(String relationshipTypeRole)
- {
- this.relationshipTypeRole = relationshipTypeRole;
- }
-
- public IdentityStoreSession createIdentityStoreSession(
- Map<String, Object> sessionOptions) throws IdentityException
- {
- EntityManager em = (EntityManager) sessionOptions.get("ENTITY_MANAGER");
-
- return new JpaIdentityStoreSessionImpl(em);
- }
-
- public IdentityObject createIdentityObject(
- IdentityStoreInvocationContext invocationCtx, String name,
- IdentityObjectType identityObjectType) throws IdentityException
- {
- return createIdentityObject(invocationCtx, name, identityObjectType, null);
- }
-
- protected Object lookupIdentityType(String identityType, EntityManager em)
- {
- try
- {
- Property<Object> typeNameProp = modelProperties.get(PROPERTY_IDENTITY_TYPE_NAME);
-
- // If there is no identity type table, just return the name
- if (typeNameProp == null) return identityType;
-
- Object val = em.createQuery(
- "select t from " + typeNameProp.getDeclaringClass().getName() +
- " t where t." + typeNameProp.getName() +
- " = :identityType")
- .setParameter("identityType", identityType)
- .getSingleResult();
- return val;
- }
- catch (NoResultException ex)
- {
- return null;
- }
- }
-
- public IdentityObject createIdentityObject(
- IdentityStoreInvocationContext ctx, String name,
- IdentityObjectType identityObjectType, Map<String, String[]> attributes)
- throws IdentityException
- {
- try
- {
- Object identityInstance = identityClass.newInstance();
- modelProperties.get(PROPERTY_IDENTITY_NAME).setValue(identityInstance, name);
-
- Property<Object> typeProp = modelProperties.get(PROPERTY_IDENTITY_TYPE);
-
- if (String.class.equals(typeProp.getJavaClass()))
- {
- typeProp.setValue(identityInstance, identityObjectType.getName());
- }
- else
- {
- typeProp.setValue(identityInstance, lookupIdentityType(identityObjectType.getName(),
- getEntityManager(ctx)));
- }
-
- //beanManager.fireEvent(new PrePersistUserEvent(identityInstance));
-
- getEntityManager(ctx).persist(identityInstance);
-
- //beanManager.fireEvent(new UserCreatedEvent(identityInstance));
-
- // TODO persist attributes
-
- Object id = modelProperties.get(PROPERTY_IDENTITY_ID).getValue(identityInstance);
- IdentityObject obj = new IdentityObjectImpl(
- (id != null ? id.toString() : null),
- name, identityObjectType);
-
- return obj;
- }
- catch (Exception ex)
- {
- throw new IdentityException("Error creating identity object", ex);
- }
- }
-
- public IdentityObjectRelationship createRelationship(
- IdentityStoreInvocationContext invocationCtx,
- IdentityObject fromIdentity, IdentityObject toIdentity,
- IdentityObjectRelationshipType relationshipType,
- String relationshipName, boolean createNames) throws IdentityException
- {
- try
- {
- EntityManager em = getEntityManager(invocationCtx);
-
- Object relationship = relationshipClass.newInstance();
-
- modelProperties.get(PROPERTY_RELATIONSHIP_FROM).setValue(relationship,
- lookupIdentity(fromIdentity, em));
- modelProperties.get(PROPERTY_RELATIONSHIP_TO).setValue(relationship,
- lookupIdentity(toIdentity, em));
-
- Property<Object> type = modelProperties.get(PROPERTY_RELATIONSHIP_TYPE);
- if (String.class.equals(modelProperties.get(PROPERTY_RELATIONSHIP_TYPE).getJavaClass()))
- {
- type.setValue(relationship, relationshipType.getName());
- }
- else
- {
- type.setValue(relationship, lookupRelationshipType(relationshipType, em));
- }
-
- modelProperties.get(PROPERTY_RELATIONSHIP_NAME).setValue(relationship,
- relationshipName);
-
- em.persist(relationship);
-
- return new IdentityObjectRelationshipImpl(fromIdentity, toIdentity,
- relationshipName, relationshipType);
- }
- catch (Exception ex)
- {
- throw new IdentityException("Exception creating relationship", ex);
- }
- }
-
- protected Object lookupIdentity(IdentityObject obj, EntityManager em)
- {
- Property<?> identityNameProp = modelProperties.get(PROPERTY_IDENTITY_NAME);
- Property<?> identityTypeProp = modelProperties.get(PROPERTY_IDENTITY_TYPE);
-
- CriteriaBuilder builder = em.getCriteriaBuilder();
- CriteriaQuery<?> criteria = builder.createQuery(identityClass);
- Root<?> root = criteria.from(identityClass);
-
- List<Predicate> predicates = new ArrayList<Predicate>();
- predicates.add(builder.equal(root.get(identityNameProp.getName()), obj.getName()));
- predicates.add(builder.equal(root.get(identityTypeProp.getName()), lookupIdentityType(obj.getIdentityType().getName(), em)));
-
- // TODO add criteria for identity type
-
- criteria.where(predicates.toArray(new Predicate[0]));
-
- return em.createQuery(criteria).getSingleResult();
- }
-
- protected Object lookupCredentialTypeEntity(String name, EntityManager em)
- {
- Property<?> credentialTypeNameProp = modelProperties.get(PROPERTY_CREDENTIAL_TYPE_NAME);
-
- CriteriaBuilder builder = em.getCriteriaBuilder();
- CriteriaQuery<?> criteria = builder.createQuery(credentialTypeNameProp.getDeclaringClass());
- Root<?> root = criteria.from(credentialTypeNameProp.getDeclaringClass());
-
- List<Predicate> predicates = new ArrayList<Predicate>();
- predicates.add(builder.equal(root.get(credentialTypeNameProp.getName()), name));
- criteria.where(predicates.toArray(new Predicate[0]));
-
- return em.createQuery(criteria).getSingleResult();
- }
-
- protected Object lookupRelationshipType(IdentityObjectRelationshipType relationshipType, EntityManager em)
- {
- Property<?> relationshipTypeNameProp = modelProperties.get(PROPERTY_RELATIONSHIP_TYPE_NAME);
-
- if (relationshipTypeNameProp != null)
- {
- CriteriaBuilder builder = em.getCriteriaBuilder();
- CriteriaQuery<?> criteria = builder.createQuery(relationshipTypeNameProp.getDeclaringClass());
- Root<?> root = criteria.from(relationshipTypeNameProp.getDeclaringClass());
-
- List<Predicate> predicates = new ArrayList<Predicate>();
- predicates.add(builder.equal(root.get(relationshipTypeNameProp.getName()), relationshipType.getName()));
- criteria.where(predicates.toArray(new Predicate[0]));
-
- return em.createQuery(criteria).getSingleResult();
- }
- else
- {
- return relationshipType.getName();
- }
- }
-
- public String createRelationshipName(IdentityStoreInvocationContext ctx,
- String name) throws IdentityException, OperationNotSupportedException
- {
- // TODO Auto-generated method stub
- return null;
- }
-
- public EntityManager getEntityManager(IdentityStoreInvocationContext invocationContext)
- {
- return ((JpaIdentityStoreSessionImpl) invocationContext.getIdentityStoreSession()).getEntityManager();
- }
-
- public IdentityObject findIdentityObject(IdentityStoreInvocationContext invocationContext, String id)
- throws IdentityException
- {
- try
- {
- Object identity = getEntityManager(invocationContext).createQuery("select i from " +
- identityClass.getName() + " i where i." +
- modelProperties.get(PROPERTY_IDENTITY_ID).getName() +
- " = :id")
- .setParameter("id", id)
- .getSingleResult();
-
- IdentityObjectType type = modelProperties.containsKey(PROPERTY_IDENTITY_TYPE_NAME) ?
- new IdentityObjectTypeImpl(
- modelProperties.get(PROPERTY_IDENTITY_TYPE_NAME).getValue(
- modelProperties.get(PROPERTY_IDENTITY_TYPE).getValue(identity)).toString()) :
- new IdentityObjectTypeImpl(modelProperties.get(PROPERTY_IDENTITY_TYPE).getValue(identity).toString());
-
-
- return new IdentityObjectImpl(
- modelProperties.get(PROPERTY_IDENTITY_ID).getValue(identity).toString(),
- modelProperties.get(PROPERTY_IDENTITY_NAME).getValue(identity).toString(),
- type);
- }
- catch (NoResultException ex)
- {
- return null;
- }
- }
-
- public IdentityObject findIdentityObject(
- IdentityStoreInvocationContext invocationContext, String name,
- IdentityObjectType identityObjectType) throws IdentityException
- {
- try
- {
- Object identityType = modelProperties.containsKey(PROPERTY_IDENTITY_TYPE_NAME) ?
- lookupIdentityType(identityObjectType.getName(), getEntityManager(invocationContext)) :
- identityObjectType.getName();
-
- Object identity = getEntityManager(invocationContext).createQuery("select i from " +
- identityClass.getName() + " i where i." +
- modelProperties.get(PROPERTY_IDENTITY_NAME).getName() +
- " = :name and i." + modelProperties.get(PROPERTY_IDENTITY_TYPE).getName() +
- " = :type")
- .setParameter("name", name)
- .setParameter("type", identityType)
- .getSingleResult();
-
- return new IdentityObjectImpl(
- modelProperties.get(PROPERTY_IDENTITY_ID).getValue(identity).toString(),
- modelProperties.get(PROPERTY_IDENTITY_NAME).getValue(identity).toString(),
- identityObjectType);
- }
- catch (NoResultException ex)
- {
- return null;
- }
- }
-
- public Collection<IdentityObject> findIdentityObject(
- IdentityStoreInvocationContext ctx,
- IdentityObjectType identityType, IdentityObjectSearchCriteria searchCriteria)
- throws IdentityException
- {
- List<IdentityObject> objs = new ArrayList<IdentityObject>();
-
- EntityManager em = getEntityManager(ctx);
-
- CriteriaBuilder builder = em.getCriteriaBuilder();
- CriteriaQuery<?> criteria = builder.createQuery(identityClass);
-
- Root<?> root = criteria.from(identityClass);
-
- Property<?> identityNameProp = modelProperties.get(PROPERTY_IDENTITY_NAME);
- Property<?> identityTypeProp = modelProperties.get(PROPERTY_IDENTITY_TYPE);
-
- List<Predicate> predicates = new ArrayList<Predicate>();
-
- if (identityType != null)
- {
- predicates.add(builder.equal(root.get(identityTypeProp.getName()),
- lookupIdentityType(identityType.getName(), em)));
- }
-
- criteria.where(predicates.toArray(new Predicate[0]));
-
- List<?> results = em.createQuery(criteria).getResultList();
-
- Property<?> typeProp = modelProperties.get(PROPERTY_IDENTITY_TYPE);
- Property<?> typeNameProp = modelProperties.get(PROPERTY_IDENTITY_TYPE_NAME);
-
- for (Object result : results)
- {
- String name = (String) identityNameProp.getValue(result);
- String typeName;
-
- if (typeNameProp != null)
- {
- typeName = (String) typeNameProp.getValue(typeProp.getValue(result));
- }
- else
- {
- typeName = (String) typeProp.getValue(result);
- }
-
- IdentityObjectType type = new IdentityObjectTypeImpl(typeName);
- objs.add(new IdentityObjectImpl(name, name, type));
- }
-
- return objs;
- }
-
- public Collection<IdentityObject> findIdentityObject(
- IdentityStoreInvocationContext invocationCxt, IdentityObject identity,
- IdentityObjectRelationshipType relationshipType, boolean parent,
- IdentityObjectSearchCriteria criteria) throws IdentityException
- {
- List<IdentityObject> objs = new ArrayList<IdentityObject>();
-
- System.out.println("*** Invoked unimplemented method findIdentityObject()");
-
- // TODO Auto-generated method stub
- return objs;
- }
-
- public String getId()
- {
- return id;
- }
-
- public int getIdentityObjectsCount(
- IdentityStoreInvocationContext invocationCtx,
- IdentityObjectType identityType) throws IdentityException
- {
- System.out.println("*** Invoked unimplemented method getIdentityObjectsCount()");
- // TODO Auto-generated method stub
- return 0;
- }
-
- public Map<String, String> getRelationshipNameProperties(
- IdentityStoreInvocationContext ctx, String name)
- throws IdentityException, OperationNotSupportedException
- {
- System.out.println("*** Invoked unimplemented method getRelationshipNameProperties()");
- // TODO Auto-generated method stub
- return null;
- }
-
- public Set<String> getRelationshipNames(IdentityStoreInvocationContext ctx,
- IdentityObjectSearchCriteria criteria) throws IdentityException,
- OperationNotSupportedException
- {
- System.out.println("*** Invoked unimplemented method getRelationshipNames()");
- // TODO Auto-generated method stub
- return null;
- }
-
- public Set<String> getRelationshipNames(IdentityStoreInvocationContext ctx,
- IdentityObject identity, IdentityObjectSearchCriteria searchCriteria)
- throws IdentityException, OperationNotSupportedException
- {
- Set<String> names = new HashSet<String>();
-
- if (!featuresMetaData.isNamedRelationshipsSupported()) return names;
-
- EntityManager em = getEntityManager(ctx);
-
- CriteriaBuilder builder = em.getCriteriaBuilder();
- CriteriaQuery<?> criteria = builder.createQuery(relationshipClass);
- Root<?> root = criteria.from(relationshipClass);
-
- Property<?> identityToProperty = modelProperties.get(PROPERTY_RELATIONSHIP_TO);
- Property<?> relationshipNameProperty = modelProperties.get(PROPERTY_RELATIONSHIP_NAME);
-
- List<Predicate> predicates = new ArrayList<Predicate>();
- predicates.add(builder.equal(root.get(identityToProperty.getName()),
- lookupIdentity(identity, em)));
-
- criteria.where(predicates.toArray(new Predicate[0]));
-
- List<?> results = em.createQuery(criteria).getResultList();
- for (Object result : results)
- {
- names.add((String) relationshipNameProperty.getValue(result));
- }
-
- return names;
- }
-
- public Map<String, String> getRelationshipProperties(
- IdentityStoreInvocationContext ctx,
- IdentityObjectRelationship relationship) throws IdentityException,
- OperationNotSupportedException
- {
- System.out.println("*** Invoked unimplemented method getRelationshipProperties()");
- // TODO Auto-generated method stub
- return null;
- }
-
- public FeaturesMetaData getSupportedFeatures()
- {
- return featuresMetaData;
- }
-
- public void removeIdentityObject(
- IdentityStoreInvocationContext ctx, IdentityObject identity)
- throws IdentityException
- {
- Property<?> nameProperty = modelProperties.get(PROPERTY_IDENTITY_NAME);
- Property<?> typeProperty = modelProperties.get(PROPERTY_IDENTITY_TYPE);
-
- EntityManager em = getEntityManager(ctx);
-
- CriteriaBuilder builder = em.getCriteriaBuilder();
- CriteriaQuery<?> criteria = builder.createQuery(identityClass);
- Root<?> root = criteria.from(identityClass);
-
- List<Predicate> predicates = new ArrayList<Predicate>();
- predicates.add(builder.equal(root.get(nameProperty.getName()),
- identity.getName()));
- predicates.add(builder.equal(root.get(typeProperty.getName()),
- lookupIdentityType(identity.getIdentityType().getName(), em)));
-
- criteria.where(predicates.toArray(new Predicate[0]));
-
- Object instance = em.createQuery(criteria).getSingleResult();
-
- em.remove(instance);
- }
-
- public void removeRelationship(IdentityStoreInvocationContext ctx,
- IdentityObject fromIdentity, IdentityObject toIdentity,
- IdentityObjectRelationshipType relationshipType,
- String relationshipName) throws IdentityException
- {
- Property<?> fromProperty = modelProperties.get(PROPERTY_RELATIONSHIP_FROM);
- Property<?> toProperty = modelProperties.get(PROPERTY_RELATIONSHIP_TO);
- Property<?> relationshipTypeProp = modelProperties.get(PROPERTY_RELATIONSHIP_TYPE);
-
- EntityManager em = getEntityManager(ctx);
-
- CriteriaBuilder builder = em.getCriteriaBuilder();
- CriteriaQuery<?> criteria = builder.createQuery(identityClass);
- Root<?> root = criteria.from(identityClass);
-
- List<Predicate> predicates = new ArrayList<Predicate>();
- predicates.add(builder.equal(root.get(fromProperty.getName()),
- lookupIdentity(fromIdentity, em)));
- predicates.add(builder.equal(root.get(toProperty.getName()),
- lookupIdentity(toIdentity, em)));
- predicates.add(builder.equal(root.get(relationshipTypeProp.getName()),
- lookupRelationshipType(relationshipType, em)));
-
- criteria.where(predicates.toArray(new Predicate[0]));
-
- Object relationship = em.createQuery(criteria).getSingleResult();
- em.remove(relationship);
- }
-
- public String removeRelationshipName(IdentityStoreInvocationContext ctx,
- String name) throws IdentityException, OperationNotSupportedException
- {
- System.out.println("*** Invoked unimplemented method removeRelationshipName()");
- // TODO Auto-generated method stub
- return null;
- }
-
- public void removeRelationshipNameProperties(
- IdentityStoreInvocationContext ctx, String name, Set<String> properties)
- throws IdentityException, OperationNotSupportedException
- {
- // TODO Auto-generated method stub
- System.out.println("*** Invoked unimplemented method removeRelationshipNameProperties()");
- }
-
- public void removeRelationshipProperties(IdentityStoreInvocationContext ctx,
- IdentityObjectRelationship relationship, Set<String> properties)
- throws IdentityException, OperationNotSupportedException
- {
- // TODO Auto-generated method stub
- System.out.println("*** Invoked unimplemented method removeRelationshipProperties()");
- }
-
- public void removeRelationships(
- IdentityStoreInvocationContext invocationCtx,
- IdentityObject identity1, IdentityObject identity2, boolean named)
- throws IdentityException
- {
- // TODO Auto-generated method stub
- System.out.println("*** Invoked unimplemented method removeRelationships()");
- }
-
- public Set<IdentityObjectRelationship> resolveRelationships(
- IdentityStoreInvocationContext ctx,
- IdentityObject fromIdentity, IdentityObject toIdentity,
- IdentityObjectRelationshipType relationshipType)
- throws IdentityException
- {
- Set<IdentityObjectRelationship> relationships = new HashSet<IdentityObjectRelationship>();
-
- EntityManager em = getEntityManager(ctx);
-
- CriteriaBuilder builder = em.getCriteriaBuilder();
- CriteriaQuery<?> criteria = builder.createQuery(relationshipClass);
- Root<?> root = criteria.from(relationshipClass);
-
- Property<?> relationshipFromProp = modelProperties.get(PROPERTY_RELATIONSHIP_FROM);
- Property<?> relationshipToProp = modelProperties.get(PROPERTY_RELATIONSHIP_TO);
- Property<?> relationshipTypeProp = modelProperties.get(PROPERTY_RELATIONSHIP_TYPE);
- Property<?> relationshipNameProp = modelProperties.get(PROPERTY_RELATIONSHIP_NAME);
-
- List<Predicate> predicates = new ArrayList<Predicate>();
-
- if (fromIdentity != null)
- {
- predicates.add(builder.equal(root.get(relationshipFromProp.getName()),
- lookupIdentity(fromIdentity, em)));
- }
-
- if (toIdentity != null)
- {
- predicates.add(builder.equal(root.get(relationshipToProp.getName()),
- lookupIdentity(toIdentity, em)));
- }
-
- if (relationshipType != null)
- {
- predicates.add(builder.equal(root.get(relationshipTypeProp.getName()),
- lookupRelationshipType(relationshipType, em)));
- }
-
- criteria.where(predicates.toArray(new Predicate[0]));
-
- List<?> results = em.createQuery(criteria).getResultList();
-
- EntityToSpiConverter converter = new EntityToSpiConverter();
-
- for (Object result : results)
- {
- IdentityObjectRelationship relationship = new IdentityObjectRelationshipImpl(
- converter.convertToIdentityObject(relationshipFromProp.getValue(result)),
- converter.convertToIdentityObject(relationshipToProp.getValue(result)),
- (String) relationshipNameProp.getValue(result),
- converter.convertToRelationshipType(relationshipTypeProp.getValue(result))
- );
-
- relationships.add(relationship);
- }
-
- return relationships;
- }
-
- public Set<IdentityObjectRelationship> resolveRelationships(
- IdentityStoreInvocationContext ctx, IdentityObject identity,
- IdentityObjectRelationshipType relationshipType, boolean parent,
- boolean named, String name) throws IdentityException
- {
- Set<IdentityObjectRelationship> relationships = new HashSet<IdentityObjectRelationship>();
-
- EntityManager em = getEntityManager(ctx);
-
- CriteriaBuilder builder = em.getCriteriaBuilder();
- CriteriaQuery<?> criteria = builder.createQuery(relationshipClass);
- Root<?> root = criteria.from(relationshipClass);
-
- Property<?> relationshipFromProp = modelProperties.get(PROPERTY_RELATIONSHIP_FROM);
- Property<?> relationshipToProp = modelProperties.get(PROPERTY_RELATIONSHIP_TO);
- Property<?> relationshipTypeProp = modelProperties.get(PROPERTY_RELATIONSHIP_TYPE);
- Property<?> relationshipNameProp = modelProperties.get(PROPERTY_RELATIONSHIP_NAME);
-
- List<Predicate> predicates = new ArrayList<Predicate>();
-
- if (parent)
- {
- predicates.add(builder.equal(root.get(relationshipFromProp.getName()),
- lookupIdentity(identity, em)));
- }
- else
- {
- predicates.add(builder.equal(root.get(relationshipToProp.getName()),
- lookupIdentity(identity, em)));
- }
-
- if (relationshipType != null)
- {
- predicates.add(builder.equal(root.get(relationshipTypeProp.getName()),
- lookupRelationshipType(relationshipType, em)));
- }
-
- if (named)
- {
- if (name != null)
- {
- predicates.add(builder.equal(root.get(relationshipNameProp.getName()),
- name));
- }
- else
- {
- predicates.add(builder.isNotNull(root.get(relationshipNameProp.getName())));
- }
- }
-
- criteria.where(predicates.toArray(new Predicate[0]));
-
- List<?> results = em.createQuery(criteria).getResultList();
-
- EntityToSpiConverter converter = new EntityToSpiConverter();
-
- for (Object result : results)
- {
- IdentityObjectRelationship relationship = new IdentityObjectRelationshipImpl(
- converter.convertToIdentityObject(relationshipFromProp.getValue(result)),
- converter.convertToIdentityObject(relationshipToProp.getValue(result)),
- (String) relationshipNameProp.getValue(result),
- converter.convertToRelationshipType(relationshipTypeProp.getValue(result))
- );
-
- relationships.add(relationship);
- }
-
- return relationships;
- }
-
- public void setRelationshipNameProperties(
- IdentityStoreInvocationContext ctx, String name,
- Map<String, String> properties) throws IdentityException,
- OperationNotSupportedException
- {
- // TODO Auto-generated method stub
- System.out.println("*** Invoked unimplemented method setRelationshipNameProperties()");
-
- }
-
- public void setRelationshipProperties(IdentityStoreInvocationContext ctx,
- IdentityObjectRelationship relationship, Map<String, String> properties)
- throws IdentityException, OperationNotSupportedException
- {
- // TODO Auto-generated method stub
- System.out.println("*** Invoked unimplemented method setRelationshipProperties()");
- }
-
- public void updateCredential(IdentityStoreInvocationContext ctx,
- IdentityObject identityObject, IdentityObjectCredential credential)
- throws IdentityException
- {
- // TODO Auto-generated method stub
- System.out.println("*** Invoked unimplemented method updateCredential()");
- }
-
- public boolean validateCredential(IdentityStoreInvocationContext ctx,
- IdentityObject identityObject, IdentityObjectCredential credential)
- throws IdentityException
- {
- EntityManager em = getEntityManager(ctx);
-
- Property<?> credentialValue = modelProperties.get(PROPERTY_CREDENTIAL_VALUE);
-
- // Either credentials are stored in their own class...
- if (credentialClass != null)
- {
- Property<?> credentialIdentity = modelProperties.get(PROPERTY_CREDENTIAL_IDENTITY);
- Property<?> credentialType = modelProperties.get(PROPERTY_CREDENTIAL_TYPE);
-
- CriteriaBuilder builder = em.getCriteriaBuilder();
- CriteriaQuery<?> criteria = builder.createQuery(credentialClass);
- Root<?> root = criteria.from(credentialClass);
-
- List<Predicate> predicates = new ArrayList<Predicate>();
- predicates.add(builder.equal(root.get(credentialIdentity.getName()),
- lookupIdentity(identityObject, em)));
-
- if (credentialType != null)
- {
- if (String.class.equals(credentialType.getJavaClass()))
- {
- predicates.add(builder.equal(root.get(credentialType.getName()),
- credential.getType().getName()));
- }
- else
- {
- predicates.add(builder.equal(root.get(credentialType.getName()),
- lookupCredentialTypeEntity(credential.getType().getName(), em)));
- }
- }
-
- criteria.where(predicates.toArray(new Predicate[0]));
-
- List<?> results = em.createQuery(criteria).getResultList();
-
- if (results.isEmpty()) return false;
-
- // TODO this only supports plain text passwords
-
- for (Object result : results)
- {
- Object val = credentialValue.getValue(result);
- if (val.equals(credential.getValue())) return true;
- }
- }
- // or they're stored in the identity class
- else
- {
- Property<?> identityNameProp = modelProperties.get(PROPERTY_IDENTITY_NAME);
-
- CriteriaBuilder builder = em.getCriteriaBuilder();
- CriteriaQuery<?> criteria = builder.createQuery(credentialValue.getDeclaringClass());
-
- Root<?> root = criteria.from(credentialValue.getDeclaringClass());
-
- List<Predicate> predicates = new ArrayList<Predicate>();
- predicates.add(builder.equal(root.get(identityNameProp.getName()),
- identityObject.getName()));
-
- criteria.where(predicates.toArray(new Predicate[0]));
-
- Object result = em.createQuery(criteria).getSingleResult();
-
- Object val = credentialValue.getValue(result);
- if (val.equals(credential.getValue())) return true;
- }
-
- return false;
- }
-
- public void addAttributes(IdentityStoreInvocationContext invocationCtx,
- IdentityObject identity, IdentityObjectAttribute[] attributes)
- throws IdentityException
- {
- // TODO Auto-generated method stub
-
- }
-
- public IdentityObject findIdentityObjectByUniqueAttribute(
- IdentityStoreInvocationContext invocationCtx,
- IdentityObjectType identityObjectType,
- IdentityObjectAttribute attribute) throws IdentityException
- {
- // TODO Auto-generated method stub
- return null;
- }
-
- public IdentityObjectAttribute getAttribute(
- IdentityStoreInvocationContext invocationContext,
- IdentityObject identity, String name) throws IdentityException
- {
- // TODO Auto-generated method stub
- return null;
- }
-
- public Map<String, IdentityObjectAttribute> getAttributes(
- IdentityStoreInvocationContext invocationContext,
- IdentityObject identity) throws IdentityException
- {
- // TODO Auto-generated method stub
- return null;
- }
-
- public Map<String, IdentityObjectAttributeMetaData> getAttributesMetaData(
- IdentityStoreInvocationContext invocationContext,
- IdentityObjectType identityType)
- {
- // TODO Auto-generated method stub
- return null;
- }
-
- public Set<String> getSupportedAttributeNames(
- IdentityStoreInvocationContext invocationContext,
- IdentityObjectType identityType) throws IdentityException
- {
- // TODO Auto-generated method stub
- return null;
- }
-
- public void removeAttributes(IdentityStoreInvocationContext invocationCtx,
- IdentityObject identity, String[] attributeNames)
- throws IdentityException
- {
- // TODO Auto-generated method stub
-
- }
-
- public void updateAttributes(IdentityStoreInvocationContext invocationCtx,
- IdentityObject identity, IdentityObjectAttribute[] attributes)
- throws IdentityException
- {
- // TODO Auto-generated method stub
-
- }
-
- public IdentityStoreSession createIdentityStoreSession()
- throws IdentityException
- {
- return createIdentityStoreSession(null);
- }
-}
Deleted: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStoreSessionImpl.java
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStoreSessionImpl.java 2010-07-16 23:46:40 UTC (rev 13416)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStoreSessionImpl.java 2010-07-17 10:27:05 UTC (rev 13417)
@@ -1,78 +0,0 @@
-package org.jboss.seam.security.management;
-
-import javax.persistence.EntityManager;
-
-import org.picketlink.idm.common.exception.IdentityException;
-import org.picketlink.idm.spi.store.IdentityStoreSession;
-
-/**
- * JPA-specific implementation of IdentityStoreSession, based on an EntityManager.
- *
- * @author Shane Bryzak
- *
- */
-public class JpaIdentityStoreSessionImpl implements IdentityStoreSession
-{
- private EntityManager em;
-
- public JpaIdentityStoreSessionImpl(EntityManager em)
- {
- this.em = em;
- }
-
- public EntityManager getEntityManager()
- {
- return em;
- }
-
- public void clear() throws IdentityException
- {
- em.clear();
- }
-
- public void close() throws IdentityException
- {
- em.close();
- }
-
- public void commitTransaction()
- {
- em.getTransaction().commit();
- }
-
- public Object getSessionContext() throws IdentityException
- {
- return em;
- }
-
- public boolean isOpen()
- {
- return em.isOpen();
- }
-
- public boolean isTransactionActive()
- {
- return em.getTransaction().isActive();
- }
-
- public boolean isTransactionSupported()
- {
- return true;
- }
-
- public void rollbackTransaction()
- {
- em.getTransaction().rollback();
- }
-
- public void save() throws IdentityException
- {
- em.flush();
- }
-
- public void startTransaction()
- {
- em.getTransaction().begin();
- }
-
-}
Deleted: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/picketlink/IdentityConfigurationMetaDataProducer.java
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/picketlink/IdentityConfigurationMetaDataProducer.java 2010-07-16 23:46:40 UTC (rev 13416)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/picketlink/IdentityConfigurationMetaDataProducer.java 2010-07-17 10:27:05 UTC (rev 13417)
@@ -1,26 +0,0 @@
-package org.jboss.seam.security.management.picketlink;
-
-import javax.enterprise.context.ApplicationScoped;
-import javax.enterprise.inject.Produces;
-
-import org.picketlink.idm.impl.configuration.metadata.IdentityConfigurationMetaDataImpl;
-import org.picketlink.idm.spi.configuration.metadata.IdentityConfigurationMetaData;
-
-/**
- * Produces the configuration metadata for PicketLink IDM
- *
- * @author Shane Bryzak
- */
-@ApplicationScoped
-public class IdentityConfigurationMetaDataProducer
-{
- @Produces @ApplicationScoped IdentityConfigurationMetaData createConfig()
- {
- IdentityConfigurationMetaDataImpl config = new IdentityConfigurationMetaDataImpl();
-
- //config.setIdentityStores(identityStores)
-
- // TODO needs actual configuration, realms, identity stores, etc
- return config;
- }
-}
Modified: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/picketlink/IdentitySessionProducer.java
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/picketlink/IdentitySessionProducer.java 2010-07-16 23:46:40 UTC (rev 13416)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/picketlink/IdentitySessionProducer.java 2010-07-17 10:27:05 UTC (rev 13417)
@@ -4,15 +4,17 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.Produces;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
import javax.persistence.EntityManager;
-import org.jboss.seam.security.management.JpaIdentityStore;
import org.picketlink.idm.api.IdentitySession;
import org.picketlink.idm.api.IdentitySessionFactory;
import org.picketlink.idm.api.event.EventListener;
@@ -24,7 +26,6 @@
import org.picketlink.idm.impl.configuration.metadata.IdentityStoreConfigurationMetaDataImpl;
import org.picketlink.idm.impl.configuration.metadata.IdentityStoreMappingMetaDataImpl;
import org.picketlink.idm.impl.configuration.metadata.RealmConfigurationMetaDataImpl;
-import org.picketlink.idm.spi.configuration.metadata.IdentityConfigurationMetaData;
import org.picketlink.idm.spi.configuration.metadata.IdentityRepositoryConfigurationMetaData;
import org.picketlink.idm.spi.configuration.metadata.IdentityStoreConfigurationMetaData;
import org.picketlink.idm.spi.configuration.metadata.IdentityStoreMappingMetaData;
@@ -40,42 +41,45 @@
{
private IdentitySessionFactory factory;
- private String defaultRealm = "default";
+ private String defaultRealm = "default";
+ private String defaultAttributeStoreId;
+ private String defaultIdentityStoreId;
+
+ @Inject BeanManager manager;
- @Inject IdentityConfigurationMetaData config;
-
@Inject
public void init() throws IdentityConfigurationException, IdentityException
{
IdentityConfigurationMetaDataImpl metadata = new IdentityConfigurationMetaDataImpl();
// Create the identity store configuration
- List<IdentityStoreConfigurationMetaData> stores = new ArrayList<IdentityStoreConfigurationMetaData>();
- IdentityStoreConfigurationMetaDataImpl store = new IdentityStoreConfigurationMetaDataImpl();
- store.setId("jpa");
- store.setClassName("org.jboss.seam.security.management.JpaIdentityStore");
+ List<IdentityStoreConfigurationMetaData> stores = new ArrayList<IdentityStoreConfigurationMetaData>();
- // temporary hack to get the example working
- Map<String,List<String>> options = new HashMap<String,List<String>>();
- options.put(JpaIdentityStore.OPTION_IDENTITY_CLASS_NAME,
- createOptionList("org.jboss.seam.security.examples.idmconsole.model.IdentityObject"));
+ String defaultStoreId = null;
- options.put(JpaIdentityStore.OPTION_CREDENTIAL_CLASS_NAME,
- createOptionList("org.jboss.seam.security.examples.idmconsole.model.IdentityObjectCredential"));
+ Set<Bean<?>> storeBeans = manager.getBeans(IdentityStoreConfiguration.class);
+ for (Bean<?> storeBean : storeBeans)
+ {
+ IdentityStoreConfiguration config = (IdentityStoreConfiguration) manager
+ .getReference(storeBean, IdentityStoreConfiguration.class,
+ manager.createCreationalContext(storeBean));
+
+ IdentityStoreConfigurationMetaDataImpl store = new IdentityStoreConfigurationMetaDataImpl();
+ config.configure(store);
- options.put(JpaIdentityStore.OPTION_RELATIONSHIP_CLASS_NAME,
- createOptionList("org.jboss.seam.security.examples.idmconsole.model.IdentityObjectRelationship"));
+ if (defaultStoreId == null && store.getId() != null)
+ {
+ defaultStoreId = store.getId();
+ }
+
+ stores.add(store);
+ }
- options.put(JpaIdentityStore.OPTION_RELATIONSHIP_NAME_CLASS_NAME,
- createOptionList("org.jboss.seam.security.examples.idmconsole.model.RelationshipName"));
-
- store.setOptions(options);
- stores.add(store);
metadata.setIdentityStores(stores);
// Create the default realm
RealmConfigurationMetaDataImpl realm = new RealmConfigurationMetaDataImpl();
- realm.setId("default");
+ realm.setId(getDefaultRealm());
realm.setIdentityMapping("USER");
//realm.setGroupTypeMappings(groupTypeMappings)
realm.setOptions(new HashMap<String,List<String>>());
@@ -84,19 +88,22 @@
metadata.setRealms(realms);
List<IdentityRepositoryConfigurationMetaData> repositories = new ArrayList<IdentityRepositoryConfigurationMetaData>();
+
IdentityRepositoryConfigurationMetaDataImpl repository = new IdentityRepositoryConfigurationMetaDataImpl();
repository.setClassName("org.picketlink.idm.impl.repository.WrapperIdentityStoreRepository");
- repository.setDefaultAttributeStoreId("jpa");
- repository.setDefaultIdentityStoreId("jpa");
+ repository.setDefaultAttributeStoreId(defaultAttributeStoreId != null ? defaultAttributeStoreId : defaultStoreId);
+ repository.setDefaultIdentityStoreId(defaultIdentityStoreId != null ? defaultIdentityStoreId : defaultStoreId);
List<IdentityStoreMappingMetaData> mappings = new ArrayList<IdentityStoreMappingMetaData>();
+
IdentityStoreMappingMetaDataImpl mapping = new IdentityStoreMappingMetaDataImpl();
List<String> identityObjectTypes = new ArrayList<String>();
identityObjectTypes.add("USER");
identityObjectTypes.add("GROUP");
mapping.setIdentityObjectTypeMappings(identityObjectTypes);
- mapping.setIdentityStoreId("jpa");
+ mapping.setIdentityStoreId(defaultIdentityStoreId != null ? defaultIdentityStoreId : defaultStoreId);
mappings.add(mapping);
+
repository.setIdentityStoreToIdentityObjectTypeMappings(mappings);
repositories.add(repository);
@@ -107,14 +114,7 @@
factory = config.buildIdentitySessionFactory();
}
-
- private List<String> createOptionList(String... values)
- {
- List<String> vals = new ArrayList<String>();
- for (String v : values) vals.add(v);
- return vals;
- }
-
+
@Inject Instance<EntityManager> entityManagerInstance;
@Produces @RequestScoped IdentitySession createIdentitySession()
@@ -126,8 +126,8 @@
IdentitySession session = factory.createIdentitySession(getDefaultRealm(), sessionOptions);
session.registerListener(this);
return session;
- }
-
+ }
+
public String getDefaultRealm()
{
return defaultRealm;
@@ -137,4 +137,24 @@
{
this.defaultRealm = defaultRealm;
}
+
+ public String getDefaultAttributeStoreId()
+ {
+ return defaultAttributeStoreId;
+ }
+
+ public void setDefaultAttributeStoreId(String defaultAttributeStoreId)
+ {
+ this.defaultAttributeStoreId = defaultAttributeStoreId;
+ }
+
+ public String getDefaultIdentityStoreId()
+ {
+ return defaultIdentityStoreId;
+ }
+
+ public void setDefaultIdentityStoreId(String defaultIdentityStoreId)
+ {
+ this.defaultIdentityStoreId = defaultIdentityStoreId;
+ }
}
Copied: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/picketlink/JpaIdentityStore.java (from rev 13411, modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStore.java)
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/picketlink/JpaIdentityStore.java (rev 0)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/picketlink/JpaIdentityStore.java 2010-07-17 10:27:05 UTC (rev 13417)
@@ -0,0 +1,1802 @@
+package org.jboss.seam.security.management.picketlink;
+
+import java.io.Serializable;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.persistence.Entity;
+import javax.persistence.EntityManager;
+import javax.persistence.Id;
+import javax.persistence.NoResultException;
+import javax.persistence.criteria.CriteriaBuilder;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Predicate;
+import javax.persistence.criteria.Root;
+
+import org.jboss.seam.security.annotations.management.IdentityProperty;
+import org.jboss.seam.security.annotations.management.PropertyType;
+import org.jboss.seam.security.management.IdentityObjectImpl;
+import org.jboss.seam.security.management.IdentityObjectRelationshipImpl;
+import org.jboss.seam.security.management.IdentityObjectRelationshipTypeImpl;
+import org.jboss.seam.security.management.IdentityObjectTypeImpl;
+import org.jboss.weld.extensions.util.properties.Property;
+import org.jboss.weld.extensions.util.properties.query.AnnotatedPropertyCriteria;
+import org.jboss.weld.extensions.util.properties.query.NamedPropertyCriteria;
+import org.jboss.weld.extensions.util.properties.query.PropertyCriteria;
+import org.jboss.weld.extensions.util.properties.query.PropertyQueries;
+import org.jboss.weld.extensions.util.properties.query.TypedPropertyCriteria;
+import org.picketlink.idm.common.exception.IdentityException;
+import org.picketlink.idm.impl.store.FeaturesMetaDataImpl;
+import org.picketlink.idm.spi.configuration.IdentityStoreConfigurationContext;
+import org.picketlink.idm.spi.configuration.metadata.IdentityObjectAttributeMetaData;
+import org.picketlink.idm.spi.exception.OperationNotSupportedException;
+import org.picketlink.idm.spi.model.IdentityObject;
+import org.picketlink.idm.spi.model.IdentityObjectAttribute;
+import org.picketlink.idm.spi.model.IdentityObjectCredential;
+import org.picketlink.idm.spi.model.IdentityObjectRelationship;
+import org.picketlink.idm.spi.model.IdentityObjectRelationshipType;
+import org.picketlink.idm.spi.model.IdentityObjectType;
+import org.picketlink.idm.spi.search.IdentityObjectSearchCriteria;
+import org.picketlink.idm.spi.store.FeaturesMetaData;
+import org.picketlink.idm.spi.store.IdentityObjectSearchCriteriaType;
+import org.picketlink.idm.spi.store.IdentityStoreInvocationContext;
+import org.picketlink.idm.spi.store.IdentityStoreSession;
+/*import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;*/
+
+/**
+ * IdentityStore implementation that allows identity related data to be
+ * persisted in a database via JPA
+ *
+ * @author Shane Bryzak
+ */
+public class JpaIdentityStore implements org.picketlink.idm.spi.store.IdentityStore, Serializable
+{
+ private static final long serialVersionUID = 7729139146633529501L;
+
+ //private Logger log = LoggerFactory.getLogger(JpaIdentityStore.class);
+
+ public static final String OPTION_IDENTITY_CLASS_NAME = "identityEntityClassName";
+ public static final String OPTION_CREDENTIAL_CLASS_NAME = "credentialEntityClassName";
+ public static final String OPTION_RELATIONSHIP_CLASS_NAME = "relationshipEntityClassName";
+ public static final String OPTION_ROLE_NAME_CLASS_NAME = "roleNameEntityClassName";
+
+ private static final String DEFAULT_USER_IDENTITY_TYPE = "USER";
+ private static final String DEFAULT_ROLE_IDENTITY_TYPE = "ROLE";
+ private static final String DEFAULT_GROUP_IDENTITY_TYPE = "GROUP";
+
+ private static final String DEFAULT_RELATIONSHIP_TYPE_MEMBERSHIP = "MEMBERSHIP";
+ private static final String DEFAULT_RELATIONSHIP_TYPE_ROLE = "ROLE";
+
+ // Property keys
+
+ private static final String PROPERTY_IDENTITY_ID = "IDENTITY_ID";
+ private static final String PROPERTY_IDENTITY_NAME = "IDENTITY_NAME";
+ private static final String PROPERTY_IDENTITY_TYPE = "IDENTITY_TYPE";
+ private static final String PROPERTY_IDENTITY_TYPE_NAME = "IDENTITY_TYPE_NAME";
+ private static final String PROPERTY_CREDENTIAL_VALUE = "CREDENTIAL_VALUE";
+ private static final String PROPERTY_CREDENTIAL_TYPE = "CREDENTIAL_TYPE";
+ private static final String PROPERTY_CREDENTIAL_TYPE_NAME = "CREDENTIAL_TYPE_NAME";
+ private static final String PROPERTY_CREDENTIAL_IDENTITY = "CREDENTIAL_IDENTITY";
+ private static final String PROPERTY_RELATIONSHIP_FROM = "RELATIONSHIP_FROM";
+ private static final String PROPERTY_RELATIONSHIP_TO = "RELATIONSHIP_TO";
+ private static final String PROPERTY_RELATIONSHIP_TYPE = "RELATIONSHIP_TYPE";
+ private static final String PROPERTY_RELATIONSHIP_TYPE_NAME = "RELATIONSHIP_TYPE_NAME";
+ private static final String PROPERTY_RELATIONSHIP_NAME = "RELATIONSHIP_NAME";
+
+ // Distinct from PROPERTY_RELATIONSHIP NAME - this property refers to the name field in the RELATIONSHIP_NAME entity
+ private static final String PROPERTY_RELATIONSHIP_NAME_NAME = "RELATIONSHIP_NAME_NAME";
+ private static final String PROPERTY_ATTRIBUTE_NAME = "ATTRIBUTE_NAME";
+ private static final String PROPERTY_ATTRIBUTE_VALUE = "ATTRIBUTE_VALUE";
+ private static final String PROPERTY_ROLE_TYPE_NAME = "ROLE_TYPE_NAME";
+
+ private class EntityToSpiConverter
+ {
+ private static final String IDENTITY_TYPE_CACHE_PREFIX = "identity_type:";
+ private static final String RELATIONSHIP_TYPE_CACHE_PREFIX = "relationship_type:";
+
+ private Map<Object,Object> cache = new HashMap<Object,Object>();
+
+ private Property<?> identityIdProperty = modelProperties.get(PROPERTY_IDENTITY_ID);
+ private Property<?> identityNameProperty = modelProperties.get(PROPERTY_IDENTITY_NAME);
+ private Property<?> identityTypeProperty = modelProperties.get(PROPERTY_IDENTITY_TYPE);
+ private Property<?> identityTypeNameProperty = modelProperties.get(PROPERTY_IDENTITY_TYPE_NAME);
+ private Property<?> relationshipTypeNameProperty = modelProperties.get(PROPERTY_RELATIONSHIP_TYPE_NAME);
+
+ public IdentityObject convertToIdentityObject(Object entity)
+ {
+ if (!identityClass.isAssignableFrom(entity.getClass()))
+ {
+ throw new IllegalArgumentException("Invalid identity entity");
+ }
+
+ if (cache.containsKey(entity))
+ {
+ return (IdentityObject) cache.get(entity);
+ }
+ else
+ {
+ IdentityObject obj = new IdentityObjectImpl(
+ identityIdProperty.getValue(entity).toString(),
+ identityNameProperty.getValue(entity).toString(),
+ convertToIdentityObjectType(identityTypeProperty.getValue(entity)));
+ cache.put(entity, obj);
+
+ return obj;
+ }
+ }
+
+ public IdentityObjectType convertToIdentityObjectType(Object value)
+ {
+ if (value instanceof String)
+ {
+ String key = IDENTITY_TYPE_CACHE_PREFIX + (String) value;
+ if (cache.containsKey(key)) return (IdentityObjectType) cache.get(key);
+
+ IdentityObjectType type = new IdentityObjectTypeImpl((String) value);
+ cache.put(key, type);
+ return type;
+ }
+ else
+ {
+ if (cache.containsKey(value)) return (IdentityObjectType) cache.get(value);
+ IdentityObjectType type = new IdentityObjectTypeImpl(
+ (String) identityTypeNameProperty.getValue(value));
+ cache.put(value, type);
+ return type;
+ }
+ }
+
+ public IdentityObjectRelationshipType convertToRelationshipType(Object value)
+ {
+ if (value instanceof String)
+ {
+ String key = RELATIONSHIP_TYPE_CACHE_PREFIX + (String) value;
+ if (cache.containsKey(key)) return (IdentityObjectRelationshipType) cache.get(key);
+
+ IdentityObjectRelationshipType type = new IdentityObjectRelationshipTypeImpl((String) value);
+ cache.put(key, type);
+ return type;
+ }
+ else
+ {
+ if (cache.containsKey(value)) return (IdentityObjectRelationshipType) cache.get(value);
+ IdentityObjectRelationshipType type = new IdentityObjectRelationshipTypeImpl(
+ (String) relationshipTypeNameProperty.getValue(value));
+ cache.put(value, type);
+ return type;
+ }
+ }
+ }
+
+
+ private String id;
+
+ // Entity classes
+ private Class<?> identityClass;
+ private Class<?> credentialClass;
+ private Class<?> relationshipClass;
+ private Class<?> attributeClass;
+ private Class<?> roleTypeClass;
+ private Class<?> relationshipNameClass;
+
+ private String userIdentityType = DEFAULT_USER_IDENTITY_TYPE;
+ private String roleIdentityType = DEFAULT_ROLE_IDENTITY_TYPE;
+ private String groupIdentityType = DEFAULT_GROUP_IDENTITY_TYPE;
+
+ private String relationshipTypeMembership = DEFAULT_RELATIONSHIP_TYPE_MEMBERSHIP;
+ private String relationshipTypeRole = DEFAULT_RELATIONSHIP_TYPE_ROLE;
+
+ /**
+ * Model properties
+ */
+ private Map<String,Property<Object>> modelProperties = new HashMap<String,Property<Object>>();
+
+ /**
+ * Attribute properties
+ */
+ private Map<String,Property<Object>> attributeProperties = new HashMap<String,Property<Object>>();
+
+ private FeaturesMetaData featuresMetaData;
+
+ private class PropertyTypeCriteria implements PropertyCriteria
+ {
+ private PropertyType pt;
+
+ public PropertyTypeCriteria(PropertyType pt)
+ {
+ this.pt = pt;
+ }
+
+ public boolean fieldMatches(Field f)
+ {
+ return f.isAnnotationPresent(IdentityProperty.class) &&
+ f.getAnnotation(IdentityProperty.class).value().equals(pt);
+ }
+
+ public boolean methodMatches(Method m)
+ {
+ return m.isAnnotationPresent(IdentityProperty.class) &&
+ m.getAnnotation(IdentityProperty.class).value().equals(pt);
+ }
+ }
+
+ public JpaIdentityStore(String id)
+ {
+ this.id = id;
+ }
+
+ public void bootstrap(IdentityStoreConfigurationContext configurationContext)
+ throws IdentityException
+ {
+ String clsName = configurationContext.getStoreConfigurationMetaData()
+ .getOptionSingleValue(OPTION_IDENTITY_CLASS_NAME);
+
+ try
+ {
+ identityClass = Class.forName(clsName);
+ }
+ catch (ClassNotFoundException e)
+ {
+ throw new IdentityException("Error bootstrapping JpaIdentityStore - invalid identity entity class: " + clsName);
+ }
+
+ if (identityClass == null)
+ {
+ throw new IdentityException(
+ "Error initializing JpaIdentityStore - identityClass not set");
+ }
+
+ clsName = configurationContext.getStoreConfigurationMetaData()
+ .getOptionSingleValue(OPTION_CREDENTIAL_CLASS_NAME);
+
+ if (clsName != null)
+ {
+ try
+ {
+ credentialClass = Class.forName(clsName);
+ }
+ catch (ClassNotFoundException e)
+ {
+ throw new IdentityException("Error bootstrapping JpaIdentityStore - invalid credential entity class: " + clsName);
+ }
+ }
+
+ clsName = configurationContext.getStoreConfigurationMetaData()
+ .getOptionSingleValue(OPTION_RELATIONSHIP_CLASS_NAME);
+
+ try
+ {
+ relationshipClass = Class.forName(clsName);
+ }
+ catch (ClassNotFoundException e)
+ {
+ throw new IdentityException("Error bootstrapping JpaIdentityStore - invalid relationship entity class: " + clsName);
+ }
+
+ boolean namedRelationshipsSupported = false;
+
+ clsName = configurationContext.getStoreConfigurationMetaData()
+ .getOptionSingleValue(OPTION_ROLE_NAME_CLASS_NAME);
+
+ if (clsName != null)
+ {
+ try
+ {
+ relationshipNameClass = Class.forName(clsName);
+ namedRelationshipsSupported = true;
+ }
+ catch (ClassNotFoundException e)
+ {
+ throw new IdentityException("Error bootstrapping JpaIdentityStore - invalid relationship name entity class: " + clsName);
+ }
+ }
+
+ configureIdentityId();
+ configureIdentityName();
+ configureIdentityType();
+
+ configureCredentials();
+ configureRelationships();
+ configureAttributes();
+ configureRoleTypeNames();
+
+ if (namedRelationshipsSupported)
+ {
+ configureRelationshipNames();
+ }
+
+ featuresMetaData = new FeaturesMetaDataImpl(
+ configurationContext.getStoreConfigurationMetaData(),
+ new HashSet<IdentityObjectSearchCriteriaType>(),
+ false,
+ namedRelationshipsSupported,
+ new HashSet<String>()
+ );
+ }
+
+ protected void configureIdentityId() throws IdentityException
+ {
+ List<Property<Object>> props = PropertyQueries.createQuery(identityClass)
+ .addCriteria(new AnnotatedPropertyCriteria(Id.class))
+ .getResultList();
+
+ if (props.size() == 1)
+ {
+ modelProperties.put(PROPERTY_IDENTITY_ID, props.get(0));
+ }
+ else
+ {
+ throw new IdentityException("Error initializing JpaIdentityStore - no Identity ID found.");
+ }
+ }
+
+ protected void configureIdentityName() throws IdentityException
+ {
+ List<Property<Object>> props = PropertyQueries.createQuery(identityClass)
+ .addCriteria(new PropertyTypeCriteria(PropertyType.NAME))
+ .getResultList();
+
+ if (props.size() == 1)
+ {
+ modelProperties.put(PROPERTY_IDENTITY_NAME, props.get(0));
+ }
+ else if (props.size() > 1)
+ {
+ throw new IdentityException(
+ "Ambiguous identity name property in identity class " + identityClass.getName());
+ }
+ else
+ {
+ Property<Object> p = findNamedProperty(identityClass, "username", "userName", "name");
+ if (p != null)
+ {
+ modelProperties.put(PROPERTY_IDENTITY_NAME, p);
+ }
+ else
+ {
+ // Last resort - check whether the entity class exposes a single String property
+ // if so, let's assume it's the identity name
+ props = PropertyQueries.createQuery(identityClass)
+ .addCriteria(new TypedPropertyCriteria(String.class))
+ .getResultList();
+ if (props.size() == 1)
+ {
+ modelProperties.put(PROPERTY_IDENTITY_NAME, props.get(0));
+ }
+ }
+ }
+
+ if (!modelProperties.containsKey(PROPERTY_IDENTITY_NAME))
+ {
+ throw new IdentityException("Error initializing JpaIdentityStore - no valid identity name property found.");
+ }
+ }
+
+ protected void configureIdentityType() throws IdentityException
+ {
+ List<Property<Object>> props = PropertyQueries.createQuery(identityClass)
+ .addCriteria(new PropertyTypeCriteria(PropertyType.TYPE))
+ .getResultList();
+
+ if (props.size() == 1)
+ {
+ modelProperties.put(PROPERTY_IDENTITY_TYPE, props.get(0));
+ }
+ else if (props.size() > 1)
+ {
+ throw new IdentityException(
+ "Ambiguous identity type property in identity class " + identityClass.getName());
+ }
+ else
+ {
+ Property<Object> p = findNamedProperty(identityClass, "identityObjectType",
+ "identityType", "identityObjectTypeName", "identityTypeName",
+ "typeName", "discriminator", "accountType", "userType", "type");
+ if (p != null)
+ {
+ modelProperties.put(PROPERTY_IDENTITY_TYPE, props.get(0));
+ }
+ else
+ {
+ // Last resort - let's check all properties, and try to find one
+ // with an entity type that has "type" in its name
+ props = PropertyQueries.createQuery(identityClass).getResultList();
+ search: for (Property<Object> typeProp : props)
+ {
+ if (typeProp.getJavaClass().isAnnotationPresent(Entity.class) &&
+ (typeProp.getJavaClass().getSimpleName().contains("type") ||
+ typeProp.getJavaClass().getSimpleName().contains("Type")))
+ {
+ // we have a potential match, let's check if this entity has a name property
+ Property<Object> nameProp = findNamedProperty(typeProp.getJavaClass(),
+ "identityObjectTypeName", "identityTypeName", "typeName", "name");
+ if (nameProp != null)
+ {
+ modelProperties.put(PROPERTY_IDENTITY_TYPE, typeProp);
+ modelProperties.put(PROPERTY_IDENTITY_TYPE_NAME, nameProp);
+ break search;
+ }
+ }
+ }
+ }
+ }
+
+ Property<?> typeProp = modelProperties.get(PROPERTY_IDENTITY_TYPE);
+
+ if (typeProp == null)
+ {
+ throw new IdentityException("Error initializing JpaIdentityStore - no valid identity type property found.");
+ }
+
+ if (!String.class.equals(typeProp.getJavaClass()) &&
+ !modelProperties.containsKey(PROPERTY_IDENTITY_TYPE_NAME))
+ {
+ // We're not dealing with a simple type name - validate the lookup type
+ Property<Object> nameProp = findNamedProperty(typeProp.getJavaClass(),
+ "identityObjectTypeName", "identityTypeName", "typeName", "name");
+ if (nameProp != null)
+ {
+ modelProperties.put(PROPERTY_IDENTITY_TYPE_NAME, nameProp);
+ }
+ else
+ {
+ throw new IdentityException("Error initializing JpaIdentityStore - no valid identity type name property found.");
+ }
+ }
+ }
+
+ protected Property<Object> findNamedProperty(Class<?> targetClass, String... allowedNames)
+ {
+ List<Property<Object>> props = PropertyQueries.createQuery(targetClass)
+ .addCriteria(new TypedPropertyCriteria(String.class))
+ .addCriteria(new PropertyTypeCriteria(PropertyType.NAME))
+ .getResultList();
+
+ if (props.size() == 1)
+ {
+ return props.get(0);
+ }
+ else
+ {
+ props = PropertyQueries.createQuery(targetClass)
+ .addCriteria(new TypedPropertyCriteria(String.class))
+ .addCriteria(new NamedPropertyCriteria(allowedNames))
+ .getResultList();
+
+ for (String name : allowedNames)
+ {
+ for (Property<Object> prop : props)
+ {
+ if (name.equals(prop.getName())) return prop;
+ }
+ }
+ }
+
+ return null;
+ }
+
+ protected void configureCredentials() throws IdentityException
+ {
+ // If a credential entity has been explicitly configured, scan it
+ if (credentialClass != null)
+ {
+ List<Property<Object>> props = PropertyQueries.createQuery(credentialClass)
+ .addCriteria(new PropertyTypeCriteria(PropertyType.VALUE))
+ .getResultList();
+
+ if (props.size() == 1)
+ {
+ modelProperties.put(PROPERTY_CREDENTIAL_VALUE, props.get(0));
+ }
+ else if (props.size() > 1)
+ {
+ throw new IdentityException(
+ "Ambiguous credential value property in credential class " +
+ credentialClass.getName());
+ }
+ else
+ {
+ // Try scanning for a credential property also
+ props = PropertyQueries.createQuery(credentialClass)
+ .addCriteria(new PropertyTypeCriteria(PropertyType.CREDENTIAL))
+ .getResultList();
+ if (props.size() == 1)
+ {
+ modelProperties.put(PROPERTY_CREDENTIAL_VALUE, props.get(0));
+ }
+ else if (props.size() > 1)
+ {
+ throw new IdentityException(
+ "Ambiguous credential value property in credential class " +
+ credentialClass.getName());
+ }
+ else
+ {
+ Property<Object> p = findNamedProperty(credentialClass, "credentialValue",
+ "password", "passwordHash", "credential", "value");
+ if (p != null) modelProperties.put(PROPERTY_CREDENTIAL_VALUE, p);
+ }
+ }
+
+ // Scan for the credential identity property
+ props = PropertyQueries.createQuery(credentialClass)
+ .addCriteria(new TypedPropertyCriteria(identityClass))
+ .getResultList();
+ if (props.size() == 1)
+ {
+ modelProperties.put(PROPERTY_CREDENTIAL_IDENTITY, props.get(0));
+ }
+ else if (props.size() > 1)
+ {
+ throw new IdentityException(
+ "Ambiguous identity property in credential class " +
+ credentialClass.getName());
+ }
+ else
+ {
+ // Scan for a named identity property
+ props = PropertyQueries.createQuery(credentialClass)
+ .addCriteria(new NamedPropertyCriteria("identity", "identityObject"))
+ .getResultList();
+ if (!props.isEmpty())
+ {
+ modelProperties.put(PROPERTY_CREDENTIAL_IDENTITY, props.get(0));
+ }
+ else
+ {
+ throw new IdentityException("Error initializing JpaIdentityStore - no credential identity property found.");
+ }
+ }
+ }
+ else
+ {
+ // The credentials may be stored in the identity class
+ List<Property<Object>> props = PropertyQueries.createQuery(identityClass)
+ .addCriteria(new PropertyTypeCriteria(PropertyType.CREDENTIAL))
+ .getResultList();
+
+ if (props.size() == 1)
+ {
+ modelProperties.put(PROPERTY_CREDENTIAL_VALUE, props.get(0));
+ }
+ else if (props.size() > 1)
+ {
+ throw new IdentityException(
+ "Ambiguous credential property in identity class " +
+ identityClass.getName());
+ }
+ else
+ {
+ Property<Object> p = findNamedProperty(identityClass, "credentialValue",
+ "password", "passwordHash", "credential", "value");
+ if (p != null) modelProperties.put(PROPERTY_CREDENTIAL_VALUE, p);
+ }
+ }
+
+ if (!modelProperties.containsKey(PROPERTY_CREDENTIAL_VALUE))
+ {
+ throw new IdentityException("Error initializing JpaIdentityStore - no credential value property found.");
+ }
+
+ // Scan for a credential type property
+ List<Property<Object>> props = PropertyQueries.createQuery(credentialClass)
+ .addCriteria(new PropertyTypeCriteria(PropertyType.TYPE))
+ .getResultList();
+
+ if (props.size() == 1)
+ {
+ modelProperties.put(PROPERTY_CREDENTIAL_TYPE, props.get(0));
+ }
+ else if (props.size() > 1)
+ {
+ throw new IdentityException(
+ "Ambiguous credential type property in credential class " +
+ credentialClass.getName());
+ }
+ else
+ {
+ props = PropertyQueries.createQuery(credentialClass)
+ .addCriteria(new PropertyTypeCriteria(PropertyType.CREDENTIAL_TYPE))
+ .getResultList();
+
+ if (props.size() == 1)
+ {
+ modelProperties.put(PROPERTY_CREDENTIAL_TYPE, props.get(0));
+ }
+ else if (props.size() > 1)
+ {
+ throw new IdentityException(
+ "Ambiguous credential type property in credential class " +
+ credentialClass.getName());
+ }
+ else
+ {
+ Property<Object> p = findNamedProperty(credentialClass, "credentialType",
+ "identityObjectCredentialType", "type");
+ if (p != null) modelProperties.put(PROPERTY_CREDENTIAL_TYPE, p);
+ }
+ }
+
+ Property<?> typeProp = modelProperties.get(PROPERTY_CREDENTIAL_TYPE);
+
+ // If the credential type property isn't a String, then validate the lookup type
+ if (!String.class.equals(typeProp.getJavaClass()))
+ {
+ Property<Object> nameProp = findNamedProperty(typeProp.getJavaClass(),
+ "credentialObjectTypeName", "credentialTypeName", "typeName", "name");
+ if (nameProp != null)
+ {
+ modelProperties.put(PROPERTY_CREDENTIAL_TYPE_NAME, nameProp);
+ }
+ else
+ {
+ throw new IdentityException("Error initializing JpaIdentityStore - no valid credential type name property found.");
+ }
+ }
+ }
+
+ protected void configureRelationships() throws IdentityException
+ {
+ if (relationshipClass == null)
+ {
+ throw new IdentityException("Error initializing JpaIdentityStore - relationshipClass not set.");
+ }
+
+ List<Property<Object>> props = PropertyQueries.createQuery(relationshipClass)
+ .addCriteria(new TypedPropertyCriteria(identityClass))
+ .addCriteria(new PropertyTypeCriteria(PropertyType.RELATIONSHIP_FROM))
+ .getResultList();
+
+ if (props.size() == 1)
+ {
+ modelProperties.put(PROPERTY_RELATIONSHIP_FROM, props.get(0));
+ }
+ else if (props.size() > 1)
+ {
+ throw new IdentityException(
+ "Ambiguous relationshipFrom property in relationship class " +
+ relationshipClass.getName());
+ }
+ else
+ {
+ Property<Object> p = findNamedProperty(relationshipClass, "relationshipFrom",
+ "fromIdentityObject", "fromIdentity");
+ if (p != null)
+ {
+ modelProperties.put(PROPERTY_RELATIONSHIP_FROM, p);
+ }
+ else
+ {
+ // Last resort - search for a property with a type of identityClass
+ // and a "from" in its name
+ props = PropertyQueries.createQuery(relationshipClass)
+ .addCriteria(new TypedPropertyCriteria(identityClass))
+ .getResultList();
+
+ for (Property<Object> prop : props)
+ {
+ if (prop.getName().contains("from"))
+ {
+ modelProperties.put(PROPERTY_RELATIONSHIP_FROM, prop);
+ break;
+ }
+ }
+ }
+ }
+
+
+ props = PropertyQueries.createQuery(relationshipClass)
+ .addCriteria(new TypedPropertyCriteria(identityClass))
+ .addCriteria(new PropertyTypeCriteria(PropertyType.RELATIONSHIP_TO))
+ .getResultList();
+
+ if (props.size() == 1)
+ {
+ modelProperties.put(PROPERTY_RELATIONSHIP_TO, props.get(0));
+ }
+ else if (props.size() > 1)
+ {
+ throw new IdentityException(
+ "Ambiguous relationshipTo property in relationship class " +
+ relationshipClass.getName());
+ }
+ else
+ {
+ Property<Object> p = findNamedProperty(relationshipClass, "relationshipTo",
+ "toIdentityObject", "toIdentity");
+ if (p != null)
+ {
+ modelProperties.put(PROPERTY_RELATIONSHIP_TO, p);
+ }
+ else
+ {
+ // Last resort - search for a property with a type of identityClass
+ // and a "to" in its name
+ props = PropertyQueries.createQuery(relationshipClass)
+ .addCriteria(new TypedPropertyCriteria(identityClass))
+ .getResultList();
+
+ for (Property<Object> prop : props)
+ {
+ if (prop.getName().contains("to"))
+ {
+ modelProperties.put(PROPERTY_RELATIONSHIP_TO, prop);
+ break;
+ }
+ }
+ }
+ }
+
+ props = PropertyQueries.createQuery(relationshipClass)
+ .addCriteria(new PropertyTypeCriteria(PropertyType.TYPE))
+ .getResultList();
+ if (props.size() == 1)
+ {
+ modelProperties.put(PROPERTY_RELATIONSHIP_TYPE, props.get(0));
+ }
+ else if (props.size() > 1)
+ {
+ throw new IdentityException(
+ "Ambiguous relationshipType property in relationship class " +
+ relationshipClass.getName());
+ }
+ else
+ {
+ Property<Object> p = findNamedProperty(relationshipClass,
+ "identityRelationshipType", "relationshipType", "type");
+ if (p != null)
+ {
+ modelProperties.put(PROPERTY_RELATIONSHIP_TYPE, p);
+ }
+ else
+ {
+ props = PropertyQueries.createQuery(relationshipClass)
+ .getResultList();
+ for (Property<Object> prop : props)
+ {
+ if (prop.getName().contains("type"))
+ {
+ modelProperties.put(PROPERTY_RELATIONSHIP_TYPE, prop);
+ break;
+ }
+ }
+ }
+ }
+
+ props = PropertyQueries.createQuery(relationshipClass)
+ .addCriteria(new PropertyTypeCriteria(PropertyType.NAME))
+ .addCriteria(new TypedPropertyCriteria(String.class))
+ .getResultList();
+
+ if (props.size() == 1)
+ {
+ modelProperties.put(PROPERTY_RELATIONSHIP_NAME, props.get(0));
+ }
+ else if (props.size() > 1)
+ {
+ throw new IdentityException(
+ "Ambiguous relationship name property in relationship class " +
+ relationshipClass.getName());
+ }
+ else
+ {
+ Property<Object> p = findNamedProperty(relationshipClass,
+ "relationshipName", "name");
+ if (p != null)
+ {
+ modelProperties.put(PROPERTY_RELATIONSHIP_NAME, p);
+ }
+ }
+
+ if (!modelProperties.containsKey(PROPERTY_RELATIONSHIP_FROM))
+ {
+ throw new IdentityException(
+ "Error initializing JpaIdentityStore - no valid relationship from property found.");
+ }
+
+ if (!modelProperties.containsKey(PROPERTY_RELATIONSHIP_TO))
+ {
+ throw new IdentityException(
+ "Error initializing JpaIdentityStore - no valid relationship to property found.");
+ }
+
+ if (!modelProperties.containsKey(PROPERTY_RELATIONSHIP_TYPE))
+ {
+ throw new IdentityException(
+ "Error initializing JpaIdentityStore - no valid relationship type property found.");
+ }
+
+ if (!modelProperties.containsKey(PROPERTY_RELATIONSHIP_NAME))
+ {
+ throw new IdentityException(
+ "Error initializing JpaIdentityStore - no valid relationship name property found.");
+ }
+
+ Class<?> typeClass = modelProperties.get(PROPERTY_RELATIONSHIP_TYPE).getJavaClass();
+ if (!String.class.equals(typeClass))
+ {
+ props = PropertyQueries.createQuery(typeClass)
+ .addCriteria(new PropertyTypeCriteria(PropertyType.NAME))
+ .addCriteria(new TypedPropertyCriteria(String.class))
+ .getResultList();
+
+ if (props.size() == 1)
+ {
+ modelProperties.put(PROPERTY_RELATIONSHIP_TYPE_NAME, props.get(0));
+ }
+ else if (props.size() > 1)
+ {
+ throw new IdentityException(
+ "Ambiguous relationship type name property in class " +
+ typeClass.getName());
+ }
+ else
+ {
+ Property<Object> p = findNamedProperty(typeClass, "relationshipTypeName",
+ "typeName", "name");
+ if (p != null)
+ {
+ modelProperties.put(PROPERTY_RELATIONSHIP_TYPE_NAME, p);
+ }
+ }
+
+ if (!modelProperties.containsKey(PROPERTY_RELATIONSHIP_TYPE_NAME))
+ {
+ throw new IdentityException(
+ "Error initializing JpaIdentityStore - no valid relationship type name property found");
+ }
+ }
+ }
+
+ protected void configureAttributes() throws IdentityException
+ {
+ // If an attribute class has been configured, scan it for attributes
+ if (attributeClass != null)
+ {
+ List<Property<Object>> props = PropertyQueries.createQuery(attributeClass)
+ .addCriteria(new PropertyTypeCriteria(PropertyType.NAME))
+ .addCriteria(new TypedPropertyCriteria(String.class))
+ .getResultList();
+
+ if (props.size() == 1)
+ {
+ modelProperties.put(PROPERTY_ATTRIBUTE_NAME, props.get(0));
+ }
+ else if (props.size() > 1)
+ {
+ throw new IdentityException(
+ "Ambiguous attribute name property in class " +
+ attributeClass.getName());
+ }
+ else
+ {
+ Property<Object> prop = findNamedProperty(attributeClass,
+ "attributeName", "name");
+ if (prop != null) modelProperties.put(PROPERTY_ATTRIBUTE_NAME, prop);
+ }
+
+ props = PropertyQueries.createQuery(attributeClass)
+ .addCriteria(new PropertyTypeCriteria(PropertyType.VALUE))
+ .getResultList();
+
+ if (props.size() == 1)
+ {
+ modelProperties.put(PROPERTY_ATTRIBUTE_VALUE, props.get(0));
+ }
+ else if (props.size() > 1)
+ {
+ throw new IdentityException(
+ "Ambiguous attribute value property in class " +
+ attributeClass.getName());
+ }
+ else
+ {
+ Property<Object> prop = findNamedProperty(attributeClass,
+ "attributeValue", "value");
+ if (prop != null) modelProperties.put(PROPERTY_ATTRIBUTE_VALUE, prop);
+ }
+ }
+
+ // Scan for additional attributes in the identity class also
+ List<Property<Object>> props = PropertyQueries.createQuery(identityClass)
+ .addCriteria(new PropertyTypeCriteria(PropertyType.ATTRIBUTE))
+ .getResultList();
+
+ for (Property<Object> p : props)
+ {
+ attributeProperties.put(
+ p.getAnnotatedElement().getAnnotation(IdentityProperty.class).attributeName(),
+ p);
+ }
+
+ // scan any entity classes referenced by the identity class also
+ props = PropertyQueries.createQuery(identityClass)
+ .getResultList();
+
+ for (Property<Object> p : props)
+ {
+ if (!p.isReadOnly() && p.getJavaClass().isAnnotationPresent(Entity.class))
+ {
+ List<Property<Object>> pp = PropertyQueries.createQuery(p.getJavaClass())
+ .addCriteria(new PropertyTypeCriteria(PropertyType.ATTRIBUTE))
+ .getResultList();
+
+ for (Property<Object> attributeProperty : pp)
+ {
+ attributeProperties.put(
+ attributeProperty.getAnnotatedElement().getAnnotation(IdentityProperty.class).attributeName(),
+ attributeProperty);
+ }
+ }
+ }
+ }
+
+ protected void configureRoleTypeNames()
+ {
+ if (roleTypeClass != null)
+ {
+ List<Property<Object>> props = PropertyQueries.createQuery(roleTypeClass)
+ .addCriteria(new PropertyTypeCriteria(PropertyType.NAME))
+ .getResultList();
+
+ if (props.size() == 1)
+ {
+ modelProperties.put(PROPERTY_ROLE_TYPE_NAME, props.get(0));
+ }
+ }
+ }
+
+ protected void configureRelationshipNames()
+ {
+ Property<Object> relationshipNameProp = findNamedProperty(relationshipNameClass, "name");
+ if (relationshipNameProp != null)
+ {
+ modelProperties.put(PROPERTY_RELATIONSHIP_NAME_NAME, relationshipNameProp);
+ }
+ }
+
+ public String getUserIdentityType()
+ {
+ return userIdentityType;
+ }
+
+ public void setUserIdentityType(String userIdentityType)
+ {
+ this.userIdentityType = userIdentityType;
+ }
+
+ public String getRoleIdentityType()
+ {
+ return roleIdentityType;
+ }
+
+ public void setRoleIdentityType(String roleIdentityType)
+ {
+ this.roleIdentityType = roleIdentityType;
+ }
+
+ public String getGroupIdentityType()
+ {
+ return groupIdentityType;
+ }
+
+ public void setGroupIdentityType(String groupIdentityType)
+ {
+ this.groupIdentityType = groupIdentityType;
+ }
+
+ public String getRelationshipTypeMembership()
+ {
+ return relationshipTypeMembership;
+ }
+
+ public void setRelationshipTypeMembership(String relationshipTypeMembership)
+ {
+ this.relationshipTypeMembership = relationshipTypeMembership;
+ }
+
+ public String getRelationshipTypeRole()
+ {
+ return relationshipTypeRole;
+ }
+
+ public void setRelationshipTypeRole(String relationshipTypeRole)
+ {
+ this.relationshipTypeRole = relationshipTypeRole;
+ }
+
+ public IdentityStoreSession createIdentityStoreSession(
+ Map<String, Object> sessionOptions) throws IdentityException
+ {
+ EntityManager em = (EntityManager) sessionOptions.get("ENTITY_MANAGER");
+
+ return new JpaIdentityStoreSessionImpl(em);
+ }
+
+ public IdentityObject createIdentityObject(
+ IdentityStoreInvocationContext invocationCtx, String name,
+ IdentityObjectType identityObjectType) throws IdentityException
+ {
+ return createIdentityObject(invocationCtx, name, identityObjectType, null);
+ }
+
+ protected Object lookupIdentityType(String identityType, EntityManager em)
+ {
+ try
+ {
+ Property<Object> typeNameProp = modelProperties.get(PROPERTY_IDENTITY_TYPE_NAME);
+
+ // If there is no identity type table, just return the name
+ if (typeNameProp == null) return identityType;
+
+ Object val = em.createQuery(
+ "select t from " + typeNameProp.getDeclaringClass().getName() +
+ " t where t." + typeNameProp.getName() +
+ " = :identityType")
+ .setParameter("identityType", identityType)
+ .getSingleResult();
+ return val;
+ }
+ catch (NoResultException ex)
+ {
+ return null;
+ }
+ }
+
+ public IdentityObject createIdentityObject(
+ IdentityStoreInvocationContext ctx, String name,
+ IdentityObjectType identityObjectType, Map<String, String[]> attributes)
+ throws IdentityException
+ {
+ try
+ {
+ Object identityInstance = identityClass.newInstance();
+ modelProperties.get(PROPERTY_IDENTITY_NAME).setValue(identityInstance, name);
+
+ Property<Object> typeProp = modelProperties.get(PROPERTY_IDENTITY_TYPE);
+
+ if (String.class.equals(typeProp.getJavaClass()))
+ {
+ typeProp.setValue(identityInstance, identityObjectType.getName());
+ }
+ else
+ {
+ typeProp.setValue(identityInstance, lookupIdentityType(identityObjectType.getName(),
+ getEntityManager(ctx)));
+ }
+
+ //beanManager.fireEvent(new PrePersistUserEvent(identityInstance));
+
+ getEntityManager(ctx).persist(identityInstance);
+
+ //beanManager.fireEvent(new UserCreatedEvent(identityInstance));
+
+ // TODO persist attributes
+
+ Object id = modelProperties.get(PROPERTY_IDENTITY_ID).getValue(identityInstance);
+ IdentityObject obj = new IdentityObjectImpl(
+ (id != null ? id.toString() : null),
+ name, identityObjectType);
+
+ return obj;
+ }
+ catch (Exception ex)
+ {
+ throw new IdentityException("Error creating identity object", ex);
+ }
+ }
+
+ public IdentityObjectRelationship createRelationship(
+ IdentityStoreInvocationContext invocationCtx,
+ IdentityObject fromIdentity, IdentityObject toIdentity,
+ IdentityObjectRelationshipType relationshipType,
+ String relationshipName, boolean createNames) throws IdentityException
+ {
+ try
+ {
+ EntityManager em = getEntityManager(invocationCtx);
+
+ Object relationship = relationshipClass.newInstance();
+
+ modelProperties.get(PROPERTY_RELATIONSHIP_FROM).setValue(relationship,
+ lookupIdentity(fromIdentity, em));
+ modelProperties.get(PROPERTY_RELATIONSHIP_TO).setValue(relationship,
+ lookupIdentity(toIdentity, em));
+
+ Property<Object> type = modelProperties.get(PROPERTY_RELATIONSHIP_TYPE);
+ if (String.class.equals(modelProperties.get(PROPERTY_RELATIONSHIP_TYPE).getJavaClass()))
+ {
+ type.setValue(relationship, relationshipType.getName());
+ }
+ else
+ {
+ type.setValue(relationship, lookupRelationshipType(relationshipType, em));
+ }
+
+ modelProperties.get(PROPERTY_RELATIONSHIP_NAME).setValue(relationship,
+ relationshipName);
+
+ em.persist(relationship);
+
+ return new IdentityObjectRelationshipImpl(fromIdentity, toIdentity,
+ relationshipName, relationshipType);
+ }
+ catch (Exception ex)
+ {
+ throw new IdentityException("Exception creating relationship", ex);
+ }
+ }
+
+ protected Object lookupIdentity(IdentityObject obj, EntityManager em)
+ {
+ Property<?> identityNameProp = modelProperties.get(PROPERTY_IDENTITY_NAME);
+ Property<?> identityTypeProp = modelProperties.get(PROPERTY_IDENTITY_TYPE);
+
+ CriteriaBuilder builder = em.getCriteriaBuilder();
+ CriteriaQuery<?> criteria = builder.createQuery(identityClass);
+ Root<?> root = criteria.from(identityClass);
+
+ List<Predicate> predicates = new ArrayList<Predicate>();
+ predicates.add(builder.equal(root.get(identityNameProp.getName()), obj.getName()));
+ predicates.add(builder.equal(root.get(identityTypeProp.getName()), lookupIdentityType(obj.getIdentityType().getName(), em)));
+
+ // TODO add criteria for identity type
+
+ criteria.where(predicates.toArray(new Predicate[0]));
+
+ return em.createQuery(criteria).getSingleResult();
+ }
+
+ protected Object lookupCredentialTypeEntity(String name, EntityManager em)
+ {
+ Property<?> credentialTypeNameProp = modelProperties.get(PROPERTY_CREDENTIAL_TYPE_NAME);
+
+ CriteriaBuilder builder = em.getCriteriaBuilder();
+ CriteriaQuery<?> criteria = builder.createQuery(credentialTypeNameProp.getDeclaringClass());
+ Root<?> root = criteria.from(credentialTypeNameProp.getDeclaringClass());
+
+ List<Predicate> predicates = new ArrayList<Predicate>();
+ predicates.add(builder.equal(root.get(credentialTypeNameProp.getName()), name));
+ criteria.where(predicates.toArray(new Predicate[0]));
+
+ return em.createQuery(criteria).getSingleResult();
+ }
+
+ protected Object lookupRelationshipType(IdentityObjectRelationshipType relationshipType, EntityManager em)
+ {
+ Property<?> relationshipTypeNameProp = modelProperties.get(PROPERTY_RELATIONSHIP_TYPE_NAME);
+
+ if (relationshipTypeNameProp != null)
+ {
+ CriteriaBuilder builder = em.getCriteriaBuilder();
+ CriteriaQuery<?> criteria = builder.createQuery(relationshipTypeNameProp.getDeclaringClass());
+ Root<?> root = criteria.from(relationshipTypeNameProp.getDeclaringClass());
+
+ List<Predicate> predicates = new ArrayList<Predicate>();
+ predicates.add(builder.equal(root.get(relationshipTypeNameProp.getName()), relationshipType.getName()));
+ criteria.where(predicates.toArray(new Predicate[0]));
+
+ return em.createQuery(criteria).getSingleResult();
+ }
+ else
+ {
+ return relationshipType.getName();
+ }
+ }
+
+ public String createRelationshipName(IdentityStoreInvocationContext ctx,
+ String name) throws IdentityException, OperationNotSupportedException
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public EntityManager getEntityManager(IdentityStoreInvocationContext invocationContext)
+ {
+ return ((JpaIdentityStoreSessionImpl) invocationContext.getIdentityStoreSession()).getEntityManager();
+ }
+
+ public IdentityObject findIdentityObject(IdentityStoreInvocationContext invocationContext, String id)
+ throws IdentityException
+ {
+ try
+ {
+ Object identity = getEntityManager(invocationContext).createQuery("select i from " +
+ identityClass.getName() + " i where i." +
+ modelProperties.get(PROPERTY_IDENTITY_ID).getName() +
+ " = :id")
+ .setParameter("id", id)
+ .getSingleResult();
+
+ IdentityObjectType type = modelProperties.containsKey(PROPERTY_IDENTITY_TYPE_NAME) ?
+ new IdentityObjectTypeImpl(
+ modelProperties.get(PROPERTY_IDENTITY_TYPE_NAME).getValue(
+ modelProperties.get(PROPERTY_IDENTITY_TYPE).getValue(identity)).toString()) :
+ new IdentityObjectTypeImpl(modelProperties.get(PROPERTY_IDENTITY_TYPE).getValue(identity).toString());
+
+
+ return new IdentityObjectImpl(
+ modelProperties.get(PROPERTY_IDENTITY_ID).getValue(identity).toString(),
+ modelProperties.get(PROPERTY_IDENTITY_NAME).getValue(identity).toString(),
+ type);
+ }
+ catch (NoResultException ex)
+ {
+ return null;
+ }
+ }
+
+ public IdentityObject findIdentityObject(
+ IdentityStoreInvocationContext invocationContext, String name,
+ IdentityObjectType identityObjectType) throws IdentityException
+ {
+ try
+ {
+ Object identityType = modelProperties.containsKey(PROPERTY_IDENTITY_TYPE_NAME) ?
+ lookupIdentityType(identityObjectType.getName(), getEntityManager(invocationContext)) :
+ identityObjectType.getName();
+
+ Object identity = getEntityManager(invocationContext).createQuery("select i from " +
+ identityClass.getName() + " i where i." +
+ modelProperties.get(PROPERTY_IDENTITY_NAME).getName() +
+ " = :name and i." + modelProperties.get(PROPERTY_IDENTITY_TYPE).getName() +
+ " = :type")
+ .setParameter("name", name)
+ .setParameter("type", identityType)
+ .getSingleResult();
+
+ return new IdentityObjectImpl(
+ modelProperties.get(PROPERTY_IDENTITY_ID).getValue(identity).toString(),
+ modelProperties.get(PROPERTY_IDENTITY_NAME).getValue(identity).toString(),
+ identityObjectType);
+ }
+ catch (NoResultException ex)
+ {
+ return null;
+ }
+ }
+
+ public Collection<IdentityObject> findIdentityObject(
+ IdentityStoreInvocationContext ctx,
+ IdentityObjectType identityType, IdentityObjectSearchCriteria searchCriteria)
+ throws IdentityException
+ {
+ List<IdentityObject> objs = new ArrayList<IdentityObject>();
+
+ EntityManager em = getEntityManager(ctx);
+
+ CriteriaBuilder builder = em.getCriteriaBuilder();
+ CriteriaQuery<?> criteria = builder.createQuery(identityClass);
+
+ Root<?> root = criteria.from(identityClass);
+
+ Property<?> identityNameProp = modelProperties.get(PROPERTY_IDENTITY_NAME);
+ Property<?> identityTypeProp = modelProperties.get(PROPERTY_IDENTITY_TYPE);
+
+ List<Predicate> predicates = new ArrayList<Predicate>();
+
+ if (identityType != null)
+ {
+ predicates.add(builder.equal(root.get(identityTypeProp.getName()),
+ lookupIdentityType(identityType.getName(), em)));
+ }
+
+ criteria.where(predicates.toArray(new Predicate[0]));
+
+ List<?> results = em.createQuery(criteria).getResultList();
+
+ Property<?> typeProp = modelProperties.get(PROPERTY_IDENTITY_TYPE);
+ Property<?> typeNameProp = modelProperties.get(PROPERTY_IDENTITY_TYPE_NAME);
+
+ for (Object result : results)
+ {
+ String name = (String) identityNameProp.getValue(result);
+ String typeName;
+
+ if (typeNameProp != null)
+ {
+ typeName = (String) typeNameProp.getValue(typeProp.getValue(result));
+ }
+ else
+ {
+ typeName = (String) typeProp.getValue(result);
+ }
+
+ IdentityObjectType type = new IdentityObjectTypeImpl(typeName);
+ objs.add(new IdentityObjectImpl(name, name, type));
+ }
+
+ return objs;
+ }
+
+ public Collection<IdentityObject> findIdentityObject(
+ IdentityStoreInvocationContext invocationCxt, IdentityObject identity,
+ IdentityObjectRelationshipType relationshipType, boolean parent,
+ IdentityObjectSearchCriteria criteria) throws IdentityException
+ {
+ List<IdentityObject> objs = new ArrayList<IdentityObject>();
+
+ System.out.println("*** Invoked unimplemented method findIdentityObject()");
+
+ // TODO Auto-generated method stub
+ return objs;
+ }
+
+ public String getId()
+ {
+ return id;
+ }
+
+ public int getIdentityObjectsCount(
+ IdentityStoreInvocationContext invocationCtx,
+ IdentityObjectType identityType) throws IdentityException
+ {
+ System.out.println("*** Invoked unimplemented method getIdentityObjectsCount()");
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ public Map<String, String> getRelationshipNameProperties(
+ IdentityStoreInvocationContext ctx, String name)
+ throws IdentityException, OperationNotSupportedException
+ {
+ System.out.println("*** Invoked unimplemented method getRelationshipNameProperties()");
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Set<String> getRelationshipNames(IdentityStoreInvocationContext ctx,
+ IdentityObjectSearchCriteria criteria) throws IdentityException,
+ OperationNotSupportedException
+ {
+ System.out.println("*** Invoked unimplemented method getRelationshipNames()");
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Set<String> getRelationshipNames(IdentityStoreInvocationContext ctx,
+ IdentityObject identity, IdentityObjectSearchCriteria searchCriteria)
+ throws IdentityException, OperationNotSupportedException
+ {
+ Set<String> names = new HashSet<String>();
+
+ if (!featuresMetaData.isNamedRelationshipsSupported()) return names;
+
+ EntityManager em = getEntityManager(ctx);
+
+ CriteriaBuilder builder = em.getCriteriaBuilder();
+ CriteriaQuery<?> criteria = builder.createQuery(relationshipClass);
+ Root<?> root = criteria.from(relationshipClass);
+
+ Property<?> identityToProperty = modelProperties.get(PROPERTY_RELATIONSHIP_TO);
+ Property<?> relationshipNameProperty = modelProperties.get(PROPERTY_RELATIONSHIP_NAME);
+
+ List<Predicate> predicates = new ArrayList<Predicate>();
+ predicates.add(builder.equal(root.get(identityToProperty.getName()),
+ lookupIdentity(identity, em)));
+
+ criteria.where(predicates.toArray(new Predicate[0]));
+
+ List<?> results = em.createQuery(criteria).getResultList();
+ for (Object result : results)
+ {
+ names.add((String) relationshipNameProperty.getValue(result));
+ }
+
+ return names;
+ }
+
+ public Map<String, String> getRelationshipProperties(
+ IdentityStoreInvocationContext ctx,
+ IdentityObjectRelationship relationship) throws IdentityException,
+ OperationNotSupportedException
+ {
+ System.out.println("*** Invoked unimplemented method getRelationshipProperties()");
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public FeaturesMetaData getSupportedFeatures()
+ {
+ return featuresMetaData;
+ }
+
+ public void removeIdentityObject(
+ IdentityStoreInvocationContext ctx, IdentityObject identity)
+ throws IdentityException
+ {
+ Property<?> nameProperty = modelProperties.get(PROPERTY_IDENTITY_NAME);
+ Property<?> typeProperty = modelProperties.get(PROPERTY_IDENTITY_TYPE);
+
+ EntityManager em = getEntityManager(ctx);
+
+ CriteriaBuilder builder = em.getCriteriaBuilder();
+ CriteriaQuery<?> criteria = builder.createQuery(identityClass);
+ Root<?> root = criteria.from(identityClass);
+
+ List<Predicate> predicates = new ArrayList<Predicate>();
+ predicates.add(builder.equal(root.get(nameProperty.getName()),
+ identity.getName()));
+ predicates.add(builder.equal(root.get(typeProperty.getName()),
+ lookupIdentityType(identity.getIdentityType().getName(), em)));
+
+ criteria.where(predicates.toArray(new Predicate[0]));
+
+ Object instance = em.createQuery(criteria).getSingleResult();
+
+ em.remove(instance);
+ }
+
+ public void removeRelationship(IdentityStoreInvocationContext ctx,
+ IdentityObject fromIdentity, IdentityObject toIdentity,
+ IdentityObjectRelationshipType relationshipType,
+ String relationshipName) throws IdentityException
+ {
+ Property<?> fromProperty = modelProperties.get(PROPERTY_RELATIONSHIP_FROM);
+ Property<?> toProperty = modelProperties.get(PROPERTY_RELATIONSHIP_TO);
+ Property<?> relationshipTypeProp = modelProperties.get(PROPERTY_RELATIONSHIP_TYPE);
+
+ EntityManager em = getEntityManager(ctx);
+
+ CriteriaBuilder builder = em.getCriteriaBuilder();
+ CriteriaQuery<?> criteria = builder.createQuery(identityClass);
+ Root<?> root = criteria.from(identityClass);
+
+ List<Predicate> predicates = new ArrayList<Predicate>();
+ predicates.add(builder.equal(root.get(fromProperty.getName()),
+ lookupIdentity(fromIdentity, em)));
+ predicates.add(builder.equal(root.get(toProperty.getName()),
+ lookupIdentity(toIdentity, em)));
+ predicates.add(builder.equal(root.get(relationshipTypeProp.getName()),
+ lookupRelationshipType(relationshipType, em)));
+
+ criteria.where(predicates.toArray(new Predicate[0]));
+
+ Object relationship = em.createQuery(criteria).getSingleResult();
+ em.remove(relationship);
+ }
+
+ public String removeRelationshipName(IdentityStoreInvocationContext ctx,
+ String name) throws IdentityException, OperationNotSupportedException
+ {
+ System.out.println("*** Invoked unimplemented method removeRelationshipName()");
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public void removeRelationshipNameProperties(
+ IdentityStoreInvocationContext ctx, String name, Set<String> properties)
+ throws IdentityException, OperationNotSupportedException
+ {
+ // TODO Auto-generated method stub
+ System.out.println("*** Invoked unimplemented method removeRelationshipNameProperties()");
+ }
+
+ public void removeRelationshipProperties(IdentityStoreInvocationContext ctx,
+ IdentityObjectRelationship relationship, Set<String> properties)
+ throws IdentityException, OperationNotSupportedException
+ {
+ // TODO Auto-generated method stub
+ System.out.println("*** Invoked unimplemented method removeRelationshipProperties()");
+ }
+
+ public void removeRelationships(
+ IdentityStoreInvocationContext invocationCtx,
+ IdentityObject identity1, IdentityObject identity2, boolean named)
+ throws IdentityException
+ {
+ // TODO Auto-generated method stub
+ System.out.println("*** Invoked unimplemented method removeRelationships()");
+ }
+
+ public Set<IdentityObjectRelationship> resolveRelationships(
+ IdentityStoreInvocationContext ctx,
+ IdentityObject fromIdentity, IdentityObject toIdentity,
+ IdentityObjectRelationshipType relationshipType)
+ throws IdentityException
+ {
+ Set<IdentityObjectRelationship> relationships = new HashSet<IdentityObjectRelationship>();
+
+ EntityManager em = getEntityManager(ctx);
+
+ CriteriaBuilder builder = em.getCriteriaBuilder();
+ CriteriaQuery<?> criteria = builder.createQuery(relationshipClass);
+ Root<?> root = criteria.from(relationshipClass);
+
+ Property<?> relationshipFromProp = modelProperties.get(PROPERTY_RELATIONSHIP_FROM);
+ Property<?> relationshipToProp = modelProperties.get(PROPERTY_RELATIONSHIP_TO);
+ Property<?> relationshipTypeProp = modelProperties.get(PROPERTY_RELATIONSHIP_TYPE);
+ Property<?> relationshipNameProp = modelProperties.get(PROPERTY_RELATIONSHIP_NAME);
+
+ List<Predicate> predicates = new ArrayList<Predicate>();
+
+ if (fromIdentity != null)
+ {
+ predicates.add(builder.equal(root.get(relationshipFromProp.getName()),
+ lookupIdentity(fromIdentity, em)));
+ }
+
+ if (toIdentity != null)
+ {
+ predicates.add(builder.equal(root.get(relationshipToProp.getName()),
+ lookupIdentity(toIdentity, em)));
+ }
+
+ if (relationshipType != null)
+ {
+ predicates.add(builder.equal(root.get(relationshipTypeProp.getName()),
+ lookupRelationshipType(relationshipType, em)));
+ }
+
+ criteria.where(predicates.toArray(new Predicate[0]));
+
+ List<?> results = em.createQuery(criteria).getResultList();
+
+ EntityToSpiConverter converter = new EntityToSpiConverter();
+
+ for (Object result : results)
+ {
+ IdentityObjectRelationship relationship = new IdentityObjectRelationshipImpl(
+ converter.convertToIdentityObject(relationshipFromProp.getValue(result)),
+ converter.convertToIdentityObject(relationshipToProp.getValue(result)),
+ (String) relationshipNameProp.getValue(result),
+ converter.convertToRelationshipType(relationshipTypeProp.getValue(result))
+ );
+
+ relationships.add(relationship);
+ }
+
+ return relationships;
+ }
+
+ public Set<IdentityObjectRelationship> resolveRelationships(
+ IdentityStoreInvocationContext ctx, IdentityObject identity,
+ IdentityObjectRelationshipType relationshipType, boolean parent,
+ boolean named, String name) throws IdentityException
+ {
+ Set<IdentityObjectRelationship> relationships = new HashSet<IdentityObjectRelationship>();
+
+ EntityManager em = getEntityManager(ctx);
+
+ CriteriaBuilder builder = em.getCriteriaBuilder();
+ CriteriaQuery<?> criteria = builder.createQuery(relationshipClass);
+ Root<?> root = criteria.from(relationshipClass);
+
+ Property<?> relationshipFromProp = modelProperties.get(PROPERTY_RELATIONSHIP_FROM);
+ Property<?> relationshipToProp = modelProperties.get(PROPERTY_RELATIONSHIP_TO);
+ Property<?> relationshipTypeProp = modelProperties.get(PROPERTY_RELATIONSHIP_TYPE);
+ Property<?> relationshipNameProp = modelProperties.get(PROPERTY_RELATIONSHIP_NAME);
+
+ List<Predicate> predicates = new ArrayList<Predicate>();
+
+ if (parent)
+ {
+ predicates.add(builder.equal(root.get(relationshipFromProp.getName()),
+ lookupIdentity(identity, em)));
+ }
+ else
+ {
+ predicates.add(builder.equal(root.get(relationshipToProp.getName()),
+ lookupIdentity(identity, em)));
+ }
+
+ if (relationshipType != null)
+ {
+ predicates.add(builder.equal(root.get(relationshipTypeProp.getName()),
+ lookupRelationshipType(relationshipType, em)));
+ }
+
+ if (named)
+ {
+ if (name != null)
+ {
+ predicates.add(builder.equal(root.get(relationshipNameProp.getName()),
+ name));
+ }
+ else
+ {
+ predicates.add(builder.isNotNull(root.get(relationshipNameProp.getName())));
+ }
+ }
+
+ criteria.where(predicates.toArray(new Predicate[0]));
+
+ List<?> results = em.createQuery(criteria).getResultList();
+
+ EntityToSpiConverter converter = new EntityToSpiConverter();
+
+ for (Object result : results)
+ {
+ IdentityObjectRelationship relationship = new IdentityObjectRelationshipImpl(
+ converter.convertToIdentityObject(relationshipFromProp.getValue(result)),
+ converter.convertToIdentityObject(relationshipToProp.getValue(result)),
+ (String) relationshipNameProp.getValue(result),
+ converter.convertToRelationshipType(relationshipTypeProp.getValue(result))
+ );
+
+ relationships.add(relationship);
+ }
+
+ return relationships;
+ }
+
+ public void setRelationshipNameProperties(
+ IdentityStoreInvocationContext ctx, String name,
+ Map<String, String> properties) throws IdentityException,
+ OperationNotSupportedException
+ {
+ // TODO Auto-generated method stub
+ System.out.println("*** Invoked unimplemented method setRelationshipNameProperties()");
+
+ }
+
+ public void setRelationshipProperties(IdentityStoreInvocationContext ctx,
+ IdentityObjectRelationship relationship, Map<String, String> properties)
+ throws IdentityException, OperationNotSupportedException
+ {
+ // TODO Auto-generated method stub
+ System.out.println("*** Invoked unimplemented method setRelationshipProperties()");
+ }
+
+ public void updateCredential(IdentityStoreInvocationContext ctx,
+ IdentityObject identityObject, IdentityObjectCredential credential)
+ throws IdentityException
+ {
+ // TODO Auto-generated method stub
+ System.out.println("*** Invoked unimplemented method updateCredential()");
+ }
+
+ public boolean validateCredential(IdentityStoreInvocationContext ctx,
+ IdentityObject identityObject, IdentityObjectCredential credential)
+ throws IdentityException
+ {
+ EntityManager em = getEntityManager(ctx);
+
+ Property<?> credentialValue = modelProperties.get(PROPERTY_CREDENTIAL_VALUE);
+
+ // Either credentials are stored in their own class...
+ if (credentialClass != null)
+ {
+ Property<?> credentialIdentity = modelProperties.get(PROPERTY_CREDENTIAL_IDENTITY);
+ Property<?> credentialType = modelProperties.get(PROPERTY_CREDENTIAL_TYPE);
+
+ CriteriaBuilder builder = em.getCriteriaBuilder();
+ CriteriaQuery<?> criteria = builder.createQuery(credentialClass);
+ Root<?> root = criteria.from(credentialClass);
+
+ List<Predicate> predicates = new ArrayList<Predicate>();
+ predicates.add(builder.equal(root.get(credentialIdentity.getName()),
+ lookupIdentity(identityObject, em)));
+
+ if (credentialType != null)
+ {
+ if (String.class.equals(credentialType.getJavaClass()))
+ {
+ predicates.add(builder.equal(root.get(credentialType.getName()),
+ credential.getType().getName()));
+ }
+ else
+ {
+ predicates.add(builder.equal(root.get(credentialType.getName()),
+ lookupCredentialTypeEntity(credential.getType().getName(), em)));
+ }
+ }
+
+ criteria.where(predicates.toArray(new Predicate[0]));
+
+ List<?> results = em.createQuery(criteria).getResultList();
+
+ if (results.isEmpty()) return false;
+
+ // TODO this only supports plain text passwords
+
+ for (Object result : results)
+ {
+ Object val = credentialValue.getValue(result);
+ if (val.equals(credential.getValue())) return true;
+ }
+ }
+ // or they're stored in the identity class
+ else
+ {
+ Property<?> identityNameProp = modelProperties.get(PROPERTY_IDENTITY_NAME);
+
+ CriteriaBuilder builder = em.getCriteriaBuilder();
+ CriteriaQuery<?> criteria = builder.createQuery(credentialValue.getDeclaringClass());
+
+ Root<?> root = criteria.from(credentialValue.getDeclaringClass());
+
+ List<Predicate> predicates = new ArrayList<Predicate>();
+ predicates.add(builder.equal(root.get(identityNameProp.getName()),
+ identityObject.getName()));
+
+ criteria.where(predicates.toArray(new Predicate[0]));
+
+ Object result = em.createQuery(criteria).getSingleResult();
+
+ Object val = credentialValue.getValue(result);
+ if (val.equals(credential.getValue())) return true;
+ }
+
+ return false;
+ }
+
+ public void addAttributes(IdentityStoreInvocationContext invocationCtx,
+ IdentityObject identity, IdentityObjectAttribute[] attributes)
+ throws IdentityException
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+ public IdentityObject findIdentityObjectByUniqueAttribute(
+ IdentityStoreInvocationContext invocationCtx,
+ IdentityObjectType identityObjectType,
+ IdentityObjectAttribute attribute) throws IdentityException
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public IdentityObjectAttribute getAttribute(
+ IdentityStoreInvocationContext invocationContext,
+ IdentityObject identity, String name) throws IdentityException
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Map<String, IdentityObjectAttribute> getAttributes(
+ IdentityStoreInvocationContext invocationContext,
+ IdentityObject identity) throws IdentityException
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Map<String, IdentityObjectAttributeMetaData> getAttributesMetaData(
+ IdentityStoreInvocationContext invocationContext,
+ IdentityObjectType identityType)
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Set<String> getSupportedAttributeNames(
+ IdentityStoreInvocationContext invocationContext,
+ IdentityObjectType identityType) throws IdentityException
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public void removeAttributes(IdentityStoreInvocationContext invocationCtx,
+ IdentityObject identity, String[] attributeNames)
+ throws IdentityException
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void updateAttributes(IdentityStoreInvocationContext invocationCtx,
+ IdentityObject identity, IdentityObjectAttribute[] attributes)
+ throws IdentityException
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+ public IdentityStoreSession createIdentityStoreSession()
+ throws IdentityException
+ {
+ return createIdentityStoreSession(null);
+ }
+}
Added: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/picketlink/JpaIdentityStoreConfiguration.java
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/picketlink/JpaIdentityStoreConfiguration.java (rev 0)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/picketlink/JpaIdentityStoreConfiguration.java 2010-07-17 10:27:05 UTC (rev 13417)
@@ -0,0 +1,121 @@
+package org.jboss.seam.security.management.picketlink;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.picketlink.idm.impl.configuration.metadata.IdentityStoreConfigurationMetaDataImpl;
+
+/**
+ * A convenience class for setting JpaIdentityStore configuration options.
+ *
+ * @author Shane Bryzak
+ */
+public class JpaIdentityStoreConfiguration extends IdentityStoreConfiguration
+{
+ private Class<?> identityClass;
+ private Class<?> credentialClass;
+ private Class<?> relationshipClass;
+ private Class<?> roleNameClass;
+ private Class<?> attributeClass;
+
+ /**
+ * If the identityStoreClass hasn't been set, then return JpaIdentityStore
+ * by default.
+ */
+ @Override
+ public Class<?> getIdentityStoreClass()
+ {
+ return (super.getIdentityStoreClass() == null) ?
+ JpaIdentityStore.class : super.getIdentityStoreClass();
+ }
+
+ public Class<?> getIdentityClass()
+ {
+ return identityClass;
+ }
+
+ public void setIdentityClass(Class<?> identityClass)
+ {
+ this.identityClass = identityClass;
+ }
+
+ public Class<?> getCredentialClass()
+ {
+ return credentialClass;
+ }
+
+ public void setCredentialClass(Class<?> credentialClass)
+ {
+ this.credentialClass = credentialClass;
+ }
+
+ public Class<?> getRelationshipClass()
+ {
+ return relationshipClass;
+ }
+
+ public void setRelationshipClass(Class<?> relationshipClass)
+ {
+ this.relationshipClass = relationshipClass;
+ }
+
+ public Class<?> getRoleNameClass()
+ {
+ return roleNameClass;
+ }
+
+ public void setRoleNameClass(Class<?> roleNameClass)
+ {
+ this.roleNameClass = roleNameClass;
+ }
+
+ public Class<?> getAttributeClass()
+ {
+ return attributeClass;
+ }
+
+ public void setAttributeClass(Class<?> attributeClass)
+ {
+ this.attributeClass = attributeClass;
+ }
+
+ public void doConfigure(IdentityStoreConfigurationMetaDataImpl store)
+ {
+ Map<String,List<String>> options = new HashMap<String,List<String>>();
+
+ if (identityClass != null)
+ {
+ options.put(JpaIdentityStore.OPTION_IDENTITY_CLASS_NAME,
+ createOptionList(identityClass.getName()));
+ }
+
+ if (credentialClass != null)
+ {
+ options.put(JpaIdentityStore.OPTION_CREDENTIAL_CLASS_NAME,
+ createOptionList(credentialClass.getName()));
+ }
+
+ if (relationshipClass != null)
+ {
+ options.put(JpaIdentityStore.OPTION_RELATIONSHIP_CLASS_NAME,
+ createOptionList(relationshipClass.getName()));
+ }
+
+ if (roleNameClass != null)
+ {
+ options.put(JpaIdentityStore.OPTION_ROLE_NAME_CLASS_NAME,
+ createOptionList(roleNameClass.getName()));
+ }
+
+ store.setOptions(options);
+ }
+
+ private List<String> createOptionList(String... values)
+ {
+ List<String> vals = new ArrayList<String>();
+ for (String v : values) vals.add(v);
+ return vals;
+ }
+}
Copied: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/picketlink/JpaIdentityStoreSessionImpl.java (from rev 13293, modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStoreSessionImpl.java)
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/picketlink/JpaIdentityStoreSessionImpl.java (rev 0)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/picketlink/JpaIdentityStoreSessionImpl.java 2010-07-17 10:27:05 UTC (rev 13417)
@@ -0,0 +1,78 @@
+package org.jboss.seam.security.management.picketlink;
+
+import javax.persistence.EntityManager;
+
+import org.picketlink.idm.common.exception.IdentityException;
+import org.picketlink.idm.spi.store.IdentityStoreSession;
+
+/**
+ * JPA-specific implementation of IdentityStoreSession, based on an EntityManager.
+ *
+ * @author Shane Bryzak
+ *
+ */
+public class JpaIdentityStoreSessionImpl implements IdentityStoreSession
+{
+ private EntityManager em;
+
+ public JpaIdentityStoreSessionImpl(EntityManager em)
+ {
+ this.em = em;
+ }
+
+ public EntityManager getEntityManager()
+ {
+ return em;
+ }
+
+ public void clear() throws IdentityException
+ {
+ em.clear();
+ }
+
+ public void close() throws IdentityException
+ {
+ em.close();
+ }
+
+ public void commitTransaction()
+ {
+ em.getTransaction().commit();
+ }
+
+ public Object getSessionContext() throws IdentityException
+ {
+ return em;
+ }
+
+ public boolean isOpen()
+ {
+ return em.isOpen();
+ }
+
+ public boolean isTransactionActive()
+ {
+ return em.getTransaction().isActive();
+ }
+
+ public boolean isTransactionSupported()
+ {
+ return true;
+ }
+
+ public void rollbackTransaction()
+ {
+ em.getTransaction().rollback();
+ }
+
+ public void save() throws IdentityException
+ {
+ em.flush();
+ }
+
+ public void startTransaction()
+ {
+ em.getTransaction().begin();
+ }
+
+}
14 years, 5 months
Seam SVN: r13416 - modules/persistence/trunk/src/main/java/org/jboss/seam/transaction.
by seam-commits@lists.jboss.org
Author: swd847
Date: 2010-07-16 19:46:40 -0400 (Fri, 16 Jul 2010)
New Revision: 13416
Added:
modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/TransactionQualifier.java
Modified:
modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/Transaction.java
modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/TransactionExtension.java
Log:
make transactions use @ManagedProducer
Modified: modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/Transaction.java
===================================================================
--- modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/Transaction.java 2010-07-16 22:31:47 UTC (rev 13415)
+++ modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/Transaction.java 2010-07-16 23:46:40 UTC (rev 13416)
@@ -6,12 +6,15 @@
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
+import org.jboss.weld.extensions.managedproducer.ManagedProducer;
+
/**
* Supports injection of a Seam UserTransaction object that wraps the current
* JTA transaction or EJB container managed transaction.
*
* @author Mike Youngstrom
* @author Gavin King
+ * @author Stuart Douglas
*
*/
@ApplicationScoped
@@ -21,6 +24,8 @@
@Inject
Synchronizations synchronizations;
+ @ManagedProducer
+ @TransactionQualifier
public UserTransaction getTransaction() throws NamingException
{
try
Modified: modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/TransactionExtension.java
===================================================================
--- modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/TransactionExtension.java 2010-07-16 22:31:47 UTC (rev 13415)
+++ modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/TransactionExtension.java 2010-07-16 23:46:40 UTC (rev 13416)
@@ -14,7 +14,6 @@
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.InjectionTarget;
import javax.enterprise.inject.spi.ProcessBean;
-import javax.naming.NamingException;
import org.jboss.weld.extensions.annotated.AnnotatedTypeBuilder;
import org.jboss.weld.extensions.bean.BeanBuilder;
@@ -75,17 +74,7 @@
BeanBuilder<UserTransaction> builder = new BeanBuilder<UserTransaction>(utbuilder.create(), manager);
builder.defineBeanFromAnnotatedType();
- Set<Bean<?>> beans = manager.getBeans(Transaction.class);
- if (beans.isEmpty())
- {
- log.error("No bean with type " + Transaction.class.getName() + " registered, SEAM TRANSACTIONS ARE DISABLED");
- }
- else if (beans.size() > 1)
- {
- log.error("More than 1 bean with type " + Transaction.class.getName() + " registered, SEAM TRANSACTIONS ARE DISABLED");
- }
- Bean<?> bean = beans.iterator().next();
- builder.setBeanLifecycle(new TransactionLifecycle(manager, (Bean) bean));
+ builder.setBeanLifecycle(new TransactionLifecycle(manager));
builder.setInjectionTarget(new NoOpInjectionTarget());
event.addBean(builder.create());
}
@@ -96,26 +85,22 @@
private final BeanManager manager;
- private final Bean<Transaction> transactionBean;
+ private Bean<?> transactionBean;
- public TransactionLifecycle(BeanManager manager, Bean<Transaction> transactionBean)
+ public TransactionLifecycle(BeanManager manager)
{
this.manager = manager;
- this.transactionBean = transactionBean;
-
}
public UserTransaction create(BeanImpl<UserTransaction> bean, CreationalContext<UserTransaction> ctx)
{
- Transaction t = (Transaction) manager.getReference(transactionBean, Transaction.class, ctx);
- try
+ if (transactionBean == null)
{
- return t.getTransaction();
+ // this does not need to be thread safe, it does not matter if this
+ // is initialised twice
+ setupBeanDefinition();
}
- catch (NamingException e)
- {
- throw new RuntimeException(e);
- }
+ return (UserTransaction) manager.getReference(transactionBean, UserTransaction.class, ctx);
}
public void destroy(BeanImpl<UserTransaction> bean, UserTransaction arg0, CreationalContext<UserTransaction> arg1)
@@ -123,6 +108,23 @@
arg1.release();
}
+ /**
+ * we need to init the bean definition lazily, as there
+ */
+ private void setupBeanDefinition()
+ {
+ Set<Bean<?>> beans = manager.getBeans(UserTransaction.class, new TransactionQualifier.TransactionQualifierLiteral());
+ if (beans.isEmpty())
+ {
+ log.error("No bean with type " + UserTransaction.class.getName() + " and qualifier " + TransactionQualifier.class.getName() + " registered, SEAM TRANSACTIONS ARE DISABLED");
+ }
+ else if (beans.size() > 1)
+ {
+ log.error("More than 1 bean with type " + UserTransaction.class.getName() + " and qualifier " + TransactionQualifier.class.getName() + " registered, SEAM TRANSACTIONS ARE DISABLED");
+ }
+ transactionBean = beans.iterator().next();
+ }
+
}
private static class NoOpInjectionTarget implements InjectionTarget<UserTransaction>
@@ -135,7 +137,7 @@
public Set<InjectionPoint> getInjectionPoints()
{
- return Collections.EMPTY_SET;
+ return Collections.emptySet();
}
public void dispose(UserTransaction instance)
Added: modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/TransactionQualifier.java
===================================================================
--- modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/TransactionQualifier.java (rev 0)
+++ modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/TransactionQualifier.java 2010-07-16 23:46:40 UTC (rev 13416)
@@ -0,0 +1,23 @@
+package org.jboss.seam.transaction;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+import javax.enterprise.util.AnnotationLiteral;
+import javax.inject.Qualifier;
+
+/**
+ * Internal qualifier that is used to stop some beans from being exposed to the
+ * user
+ *
+ * @author Stuart Douglas
+ *
+ */
+(a)Retention(RetentionPolicy.RUNTIME)
+@Qualifier
+@interface TransactionQualifier
+{
+ public static class TransactionQualifierLiteral extends AnnotationLiteral<TransactionQualifier> implements TransactionQualifier
+ {
+ }
+}
14 years, 5 months
Seam SVN: r13415 - in modules/persistence/trunk/src: main/resources/META-INF/services and 1 other directories.
by seam-commits@lists.jboss.org
Author: swd847
Date: 2010-07-16 18:31:47 -0400 (Fri, 16 Jul 2010)
New Revision: 13415
Added:
modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/EntityTransaction.java
Modified:
modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/Transaction.java
modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/TransactionExtension.java
modules/persistence/trunk/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/UserTransactionTest.java
Log:
make default UserTransaction registered via portable extension
Added: modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/EntityTransaction.java
===================================================================
--- modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/EntityTransaction.java (rev 0)
+++ modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/EntityTransaction.java 2010-07-16 22:31:47 UTC (rev 13415)
@@ -0,0 +1,160 @@
+package org.jboss.seam.transaction;
+
+import javax.enterprise.context.RequestScoped;
+import javax.inject.Inject;
+import javax.persistence.EntityManager;
+import javax.transaction.HeuristicMixedException;
+import javax.transaction.HeuristicRollbackException;
+import javax.transaction.NotSupportedException;
+import javax.transaction.RollbackException;
+import javax.transaction.Status;
+import javax.transaction.Synchronization;
+import javax.transaction.SystemException;
+
+import org.jboss.weld.extensions.core.Veto;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Support for the JPA EntityTransaction API.
+ *
+ * Adapts JPA transaction management to a Seam UserTransaction
+ * interface.For use in non-JTA-capable environments.
+ *
+ * @author Gavin King
+ *
+ */
+@RequestScoped
+@Veto
+public class EntityTransaction extends AbstractUserTransaction
+{
+ private static final Logger log = LoggerFactory.getLogger(EntityTransaction.class);
+
+ @Inject
+ private EntityManager entityManager;
+
+
+ @Inject
+ public EntityTransaction(Synchronizations sync)
+ {
+ super(sync);
+ }
+
+ private javax.persistence.EntityTransaction getDelegate()
+ {
+ return entityManager.getTransaction();
+ }
+
+ public void begin() throws NotSupportedException, SystemException
+ {
+ log.debug("beginning JPA resource-local transaction");
+ //TODO: translate exceptions that occur into the correct JTA exception
+ try
+ {
+ getDelegate().begin();
+ getSynchronizations().afterTransactionBegin();
+ }
+ catch (RuntimeException re)
+ {
+ throw re;
+ }
+ }
+
+ public void commit() throws RollbackException, HeuristicMixedException,
+ HeuristicRollbackException, SecurityException, IllegalStateException, SystemException
+ {
+ log.debug("committing JPA resource-local transaction");
+ javax.persistence.EntityTransaction delegate = getDelegate();
+ boolean success = false;
+ try
+ {
+ if ( delegate.getRollbackOnly() )
+ {
+ delegate.rollback();
+ throw new RollbackException();
+ }
+ else
+ {
+ getSynchronizations().beforeTransactionCommit();
+ delegate.commit();
+ success = true;
+ }
+ }
+ finally
+ {
+ getSynchronizations().afterTransactionCommit(success);
+ }
+ }
+
+ public void rollback() throws IllegalStateException, SecurityException, SystemException
+ {
+ log.debug("rolling back JPA resource-local transaction");
+ //TODO: translate exceptions that occur into the correct JTA exception
+ javax.persistence.EntityTransaction delegate = getDelegate();
+ try
+ {
+ delegate.rollback();
+ }
+ finally
+ {
+ getSynchronizations().afterTransactionRollback();
+ }
+ }
+
+ public void setRollbackOnly() throws IllegalStateException, SystemException
+ {
+ log.debug("marking JPA resource-local transaction for rollback");
+ getDelegate().setRollbackOnly();
+ }
+
+ public int getStatus() throws SystemException
+ {
+ if (getDelegate().getRollbackOnly())
+ {
+ return Status.STATUS_MARKED_ROLLBACK;
+ }
+ else if (getDelegate().isActive())
+ {
+ return Status.STATUS_ACTIVE;
+ }
+ else
+ {
+ return Status.STATUS_NO_TRANSACTION;
+ }
+ }
+
+ public void setTransactionTimeout(int timeout) throws SystemException
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void registerSynchronization(Synchronization sync)
+ {
+ if ( log.isDebugEnabled() )
+ {
+ log.debug("registering synchronization: " + sync);
+ }
+ //try to register the synchronization directly with the
+ //persistence provider, but if this fails, just hold
+ //on to it myself
+ // if ( !PersistenceProvider.instance().registerSynchronization(sync,
+ // currentEntityManager) )
+ // {
+ // getSynchronizations().registerSynchronization(sync);
+ // }
+ }
+
+ @Override
+ public boolean isConversationContextRequired()
+ {
+ return true;
+ }
+
+ @Override
+ public void enlist(EntityManager entityManager)
+ {
+ //no-op
+ }
+
+}
Modified: modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/Transaction.java
===================================================================
--- modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/Transaction.java 2010-07-16 21:51:51 UTC (rev 13414)
+++ modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/Transaction.java 2010-07-16 22:31:47 UTC (rev 13415)
@@ -1,7 +1,6 @@
package org.jboss.seam.transaction;
import javax.enterprise.context.ApplicationScoped;
-import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.naming.InitialContext;
import javax.naming.NameNotFoundException;
@@ -22,7 +21,6 @@
@Inject
Synchronizations synchronizations;
- @Produces
public UserTransaction getTransaction() throws NamingException
{
try
Modified: modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/TransactionExtension.java
===================================================================
--- modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/TransactionExtension.java 2010-07-16 21:51:51 UTC (rev 13414)
+++ modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/TransactionExtension.java 2010-07-16 22:31:47 UTC (rev 13415)
@@ -1,12 +1,162 @@
package org.jboss.seam.transaction;
+import java.lang.annotation.Annotation;
+import java.util.Collections;
+import java.util.Set;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.Default;
+import javax.enterprise.inject.spi.AfterBeanDiscovery;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.Extension;
+import javax.enterprise.inject.spi.InjectionPoint;
+import javax.enterprise.inject.spi.InjectionTarget;
+import javax.enterprise.inject.spi.ProcessBean;
+import javax.naming.NamingException;
+
+import org.jboss.weld.extensions.annotated.AnnotatedTypeBuilder;
+import org.jboss.weld.extensions.bean.BeanBuilder;
+import org.jboss.weld.extensions.bean.BeanImpl;
+import org.jboss.weld.extensions.bean.BeanLifecycle;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
/**
- * Extension than provides a {@link UserTransaction}
+ * Extension than provides a {@link UserTransaction} if no other UserTransaction
+ * has been registered.
*
- * @author stuart
+ * This allows the user to register a transaction via seam XML and have it
+ * automatically replace the default UserTransaction implementation
*
+ * This is not done with alternatives, because that would require specifying the
+ * transaction manager on a per module basis, and some of the UserTransaction
+ * implementations need to be configured via seam xml anyway, so they would have
+ * to be configured twice
+ *
+ * @author Stuart Douglas
+ *
*/
-public class TransactionExtension
+public class TransactionExtension implements Extension
{
+ private boolean transactionRegistered = false;
+ private static final Logger log = LoggerFactory.getLogger(TransactionExtension.class);
+
+ public void processBean(@Observes ProcessBean<?> event)
+ {
+ Bean<?> bean = event.getBean();
+ if (bean.getTypes().contains(UserTransaction.class))
+ {
+ if (bean.getQualifiers().isEmpty()) // not sure about this
+ {
+ transactionRegistered = true;
+ }
+ else
+ {
+ for (Annotation a : event.getBean().getQualifiers())
+ {
+ if (a.annotationType() == Default.class)
+ {
+ transactionRegistered = true;
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ public void afterBeanDiscovery(@Observes AfterBeanDiscovery event, BeanManager manager)
+ {
+ if (!transactionRegistered)
+ {
+ AnnotatedTypeBuilder<UserTransaction> utbuilder = AnnotatedTypeBuilder.newInstance(UserTransaction.class);
+ BeanBuilder<UserTransaction> builder = new BeanBuilder<UserTransaction>(utbuilder.create(), manager);
+ builder.defineBeanFromAnnotatedType();
+
+ Set<Bean<?>> beans = manager.getBeans(Transaction.class);
+ if (beans.isEmpty())
+ {
+ log.error("No bean with type " + Transaction.class.getName() + " registered, SEAM TRANSACTIONS ARE DISABLED");
+ }
+ else if (beans.size() > 1)
+ {
+ log.error("More than 1 bean with type " + Transaction.class.getName() + " registered, SEAM TRANSACTIONS ARE DISABLED");
+ }
+ Bean<?> bean = beans.iterator().next();
+ builder.setBeanLifecycle(new TransactionLifecycle(manager, (Bean) bean));
+ builder.setInjectionTarget(new NoOpInjectionTarget());
+ event.addBean(builder.create());
+ }
+ }
+
+ private static class TransactionLifecycle implements BeanLifecycle<UserTransaction>
+ {
+
+ private final BeanManager manager;
+
+ private final Bean<Transaction> transactionBean;
+
+ public TransactionLifecycle(BeanManager manager, Bean<Transaction> transactionBean)
+ {
+ this.manager = manager;
+ this.transactionBean = transactionBean;
+
+ }
+
+ public UserTransaction create(BeanImpl<UserTransaction> bean, CreationalContext<UserTransaction> ctx)
+ {
+ Transaction t = (Transaction) manager.getReference(transactionBean, Transaction.class, ctx);
+ try
+ {
+ return t.getTransaction();
+ }
+ catch (NamingException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public void destroy(BeanImpl<UserTransaction> bean, UserTransaction arg0, CreationalContext<UserTransaction> arg1)
+ {
+ arg1.release();
+ }
+
+ }
+
+ private static class NoOpInjectionTarget implements InjectionTarget<UserTransaction>
+ {
+
+ public UserTransaction produce(CreationalContext<UserTransaction> ctx)
+ {
+ return null;
+ }
+
+ public Set<InjectionPoint> getInjectionPoints()
+ {
+ return Collections.EMPTY_SET;
+ }
+
+ public void dispose(UserTransaction instance)
+ {
+
+ }
+
+ public void preDestroy(UserTransaction instance)
+ {
+
+ }
+
+ public void postConstruct(UserTransaction instance)
+ {
+
+ }
+
+ public void inject(UserTransaction instance, CreationalContext<UserTransaction> ctx)
+ {
+
+ }
+
+ }
}
Modified: modules/persistence/trunk/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
===================================================================
--- modules/persistence/trunk/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension 2010-07-16 21:51:51 UTC (rev 13414)
+++ modules/persistence/trunk/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension 2010-07-16 22:31:47 UTC (rev 13415)
@@ -1 +1,2 @@
org.jboss.seam.persistence.PersistenceContextExtension
+org.jboss.seam.transaction.TransactionExtension
\ No newline at end of file
Modified: modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java
===================================================================
--- modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java 2010-07-16 21:51:51 UTC (rev 13414)
+++ modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java 2010-07-16 22:31:47 UTC (rev 13415)
@@ -42,7 +42,7 @@
war.addClasses(TransactionInterceptorTest.class, TransactionManagedBean.class, Hotel.class, EntityManagerProvider.class, DontRollBackException.class, TransactionObserver.class);
war.addWebResource("META-INF/persistence.xml", "classes/META-INF/persistence.xml");
war.addWebResource(new ByteArrayAsset(("<beans><interceptors><class>" + TransactionInterceptor.class.getName() + "</class></interceptors></beans>").getBytes()), "beans.xml");
-
+ war.addWebResource("META-INF/services/javax.enterprise.inject.spi.Extension", "classes/META-INF/services/javax.enterprise.inject.spi.Extension");
return war;
}
Modified: modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/UserTransactionTest.java
===================================================================
--- modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/UserTransactionTest.java 2010-07-16 21:51:51 UTC (rev 13414)
+++ modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/UserTransactionTest.java 2010-07-16 22:31:47 UTC (rev 13415)
@@ -39,7 +39,7 @@
war.addPackage(Transaction.class.getPackage()).addClasses(UserTransactionTest.class, Hotel.class);
war.addWebResource("META-INF/persistence.xml", "classes/META-INF/persistence.xml");
war.addWebResource(new ByteArrayAsset(new byte[0]), "beans.xml");
-
+ war.addWebResource("META-INF/services/javax.enterprise.inject.spi.Extension", "classes/META-INF/services/javax.enterprise.inject.spi.Extension");
return war;
}
14 years, 5 months
Seam SVN: r13414 - branches/community/Seam_2_2/src/main/org/jboss/seam/web.
by seam-commits@lists.jboss.org
Author: shane.bryzak(a)jboss.com
Date: 2010-07-16 17:51:51 -0400 (Fri, 16 Jul 2010)
New Revision: 13414
Modified:
branches/community/Seam_2_2/src/main/org/jboss/seam/web/AuthenticationFilter.java
Log:
wrap ContextualHttpServletRequest around all filter processing
Modified: branches/community/Seam_2_2/src/main/org/jboss/seam/web/AuthenticationFilter.java
===================================================================
--- branches/community/Seam_2_2/src/main/org/jboss/seam/web/AuthenticationFilter.java 2010-07-16 16:24:51 UTC (rev 13413)
+++ branches/community/Seam_2_2/src/main/org/jboss/seam/web/AuthenticationFilter.java 2010-07-16 21:51:51 UTC (rev 13414)
@@ -103,7 +103,7 @@
this.nonceValiditySeconds = value;
}
- public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
+ public void doFilter(ServletRequest request, ServletResponse response, final FilterChain chain)
throws IOException, ServletException
{
if (!(request instanceof HttpServletRequest))
@@ -111,33 +111,39 @@
throw new ServletException("This filter can only process HttpServletRequest requests");
}
- HttpServletRequest httpRequest = (HttpServletRequest) request;
- HttpServletResponse httpResponse = (HttpServletResponse) response;
+ final HttpServletRequest httpRequest = (HttpServletRequest) request;
+ final HttpServletResponse httpResponse = (HttpServletResponse) response;
// Force session creation
httpRequest.getSession();
- if (AUTH_TYPE_BASIC.equals(authType))
- processBasicAuth(httpRequest, httpResponse, chain);
- else if (AUTH_TYPE_DIGEST.equals(authType))
- processDigestAuth(httpRequest, httpResponse, chain);
- else
- throw new ServletException("Invalid authentication type");
+ new ContextualHttpServletRequest(httpRequest)
+ {
+ @Override
+ public void process() throws ServletException, IOException, LoginException
+ {
+ if (AUTH_TYPE_BASIC.equals(authType))
+ processBasicAuth(httpRequest, httpResponse, chain);
+ else if (AUTH_TYPE_DIGEST.equals(authType))
+ processDigestAuth(httpRequest, httpResponse, chain);
+ else
+ throw new ServletException("Invalid authentication type");
+ }
+ }.run();
}
private void processBasicAuth(HttpServletRequest request,
HttpServletResponse response, FilterChain chain)
throws IOException, ServletException
{
- Context ctx = new SessionContext( new ServletRequestSessionMap(request) );
- Identity identity = (Identity) ctx.get(Identity.class);
+ Identity identity = Identity.instance();
if (identity == null)
{
throw new ServletException("Identity not found - please ensure that the Identity component is created on startup.");
}
- Credentials credentials = (Credentials) ctx.get(Credentials.class);
+ Credentials credentials = identity.getCredentials();
boolean requireAuth = false;
@@ -202,15 +208,14 @@
HttpServletResponse response, FilterChain chain)
throws IOException, ServletException
{
- Context ctx = new SessionContext( new ServletRequestSessionMap(request) );
- Identity identity = (Identity) ctx.get(Identity.class);
+ Identity identity = Identity.instance();
if (identity == null)
{
throw new ServletException("Identity not found - please ensure that the Identity component is created on startup.");
}
- Credentials credentials = (Credentials) ctx.get(Credentials.class);
+ Credentials credentials = identity.getCredentials();
boolean requireAuth = false;
boolean nonceExpired = false;
@@ -227,7 +232,6 @@
String[] vals = split(entry, "=");
headerMap.put(vals[0].trim(), vals[1].replace("\"", "").trim());
}
-
DigestRequest digestRequest = new DigestRequest();
digestRequest.setHttpMethod(request.getMethod());
@@ -302,18 +306,11 @@
}
private void authenticate(HttpServletRequest request, final String username)
- throws ServletException, IOException
+ throws ServletException, IOException, LoginException
{
- new ContextualHttpServletRequest(request)
- {
- @Override
- public void process() throws ServletException, IOException, LoginException
- {
- Identity identity = Identity.instance();
- identity.getCredentials().setUsername(username);
- identity.authenticate();
- }
- }.run();
+ Identity identity = Identity.instance();
+ identity.getCredentials().setUsername(username);
+ identity.authenticate();
}
private String[] split(String toSplit, String delimiter)
14 years, 5 months
Seam SVN: r13412 - in modules/security/trunk: examples/idmconsole/src/main/java/org/jboss/seam/security/examples/idmconsole/action and 5 other directories.
by seam-commits@lists.jboss.org
Author: shane.bryzak(a)jboss.com
Date: 2010-07-16 03:34:35 -0400 (Fri, 16 Jul 2010)
New Revision: 13412
Modified:
modules/security/trunk/api/src/main/java/org/jboss/seam/security/management/IdentityManager.java
modules/security/trunk/examples/idmconsole/src/main/java/org/jboss/seam/security/examples/idmconsole/action/EntityManagerProducer.java
modules/security/trunk/examples/idmconsole/src/main/resources/import.sql
modules/security/trunk/examples/idmconsole/src/main/webapp/manageusers.xhtml
modules/security/trunk/impl/src/main/java/org/jboss/seam/security/RememberMe.java
modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityManagerImpl.java
modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/action/UserAction.java
modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/action/UserSearch.java
Log:
transactions kind of working now, but something weird is still going on
Modified: modules/security/trunk/api/src/main/java/org/jboss/seam/security/management/IdentityManager.java
===================================================================
--- modules/security/trunk/api/src/main/java/org/jboss/seam/security/management/IdentityManager.java 2010-07-16 06:29:33 UTC (rev 13411)
+++ modules/security/trunk/api/src/main/java/org/jboss/seam/security/management/IdentityManager.java 2010-07-16 07:34:35 UTC (rev 13412)
@@ -216,17 +216,9 @@
* @param name The user for which to return a list of roles
* @return List containing the names of the granted roles
*/
- Collection<Role> getGrantedRoles(String name);
+ Collection<Role> getUserRoles(String name);
/**
- * Returns a list of roles that are either explicitly or indirectly granted to the specified user.
- *
- * @param name The user for which to return the list of roles
- * @return List containing the names of the implied roles
- */
- List<Role> getImpliedRoles(String name);
-
- /**
* Returns a list of all members that have been granted the specified role
*
* @param roleType The role type of the role
Modified: modules/security/trunk/examples/idmconsole/src/main/java/org/jboss/seam/security/examples/idmconsole/action/EntityManagerProducer.java
===================================================================
--- modules/security/trunk/examples/idmconsole/src/main/java/org/jboss/seam/security/examples/idmconsole/action/EntityManagerProducer.java 2010-07-16 06:29:33 UTC (rev 13411)
+++ modules/security/trunk/examples/idmconsole/src/main/java/org/jboss/seam/security/examples/idmconsole/action/EntityManagerProducer.java 2010-07-16 07:34:35 UTC (rev 13412)
@@ -1,6 +1,7 @@
package org.jboss.seam.security.examples.idmconsole.action;
import javax.enterprise.context.ConversationScoped;
+import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@@ -10,5 +11,5 @@
@Stateless
public class EntityManagerProducer
{
- @Produces @ConversationScoped @PersistenceContext(unitName = "idmconsoleDatabase") EntityManager entityManager;
+ @Produces @RequestScoped @PersistenceContext(unitName = "idmconsoleDatabase") EntityManager entityManager;
}
Modified: modules/security/trunk/examples/idmconsole/src/main/resources/import.sql
===================================================================
--- modules/security/trunk/examples/idmconsole/src/main/resources/import.sql 2010-07-16 06:29:33 UTC (rev 13411)
+++ modules/security/trunk/examples/idmconsole/src/main/resources/import.sql 2010-07-16 07:34:35 UTC (rev 13412)
@@ -7,6 +7,7 @@
insert into IdentityObject (id, name, identity_object_type_id) values (1, 'shane', 1);
insert into IdentityObject (id, name, identity_object_type_id) values (2, 'demo', 1);
insert into IdentityObject (id, name, identity_object_type_id) values (3, 'Head Office', 2);
+insert into IdentityObject (id, name, identity_object_type_id) values (4, 'foo', 1);
insert into IdentityObjectCredentialType (id, name) values (1, 'PASSWORD');
Modified: modules/security/trunk/examples/idmconsole/src/main/webapp/manageusers.xhtml
===================================================================
--- modules/security/trunk/examples/idmconsole/src/main/webapp/manageusers.xhtml 2010-07-16 06:29:33 UTC (rev 13411)
+++ modules/security/trunk/examples/idmconsole/src/main/webapp/manageusers.xhtml 2010-07-16 07:34:35 UTC (rev 13412)
@@ -10,6 +10,12 @@
</ui:define>
<ui:define name="content">
+ <script type="text/javascript">
+ function confirmDelete() {
+ return confirm("Are you sure you wish to delete this user? This action cannot be undone.");
+ }
+ </script>
+
<h3>Manage Users</h3>
<h:form>
@@ -47,9 +53,9 @@
<h:commandLink id="edit" value="Edit" action="#{userAction.editUser(user.username)}"/>
<span> | </span>
</ui:fragment>
- <h:link id="delete" value="Delete" action="#{userAction.deleteUser(user.username)}"
- rendered="#{identity.hasPermission('seam.identity', 'delete')}"
- onclick="return confirmDelete()"/>
+ <ui:fragment rendered="#{identity.hasPermission('seam.identity', 'delete')}">
+ <h:commandLink id="delete" value="Delete" action="#{userAction.deleteUser(user.username)}" onclick="return confirmDelete()"/>
+ </ui:fragment>
</h:column>
</h:dataTable>
</h:form>
Modified: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/RememberMe.java
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/RememberMe.java 2010-07-16 06:29:33 UTC (rev 13411)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/RememberMe.java 2010-07-16 07:34:35 UTC (rev 13412)
@@ -153,7 +153,7 @@
{
userEnabled.value = true;
- for (Role role : identityManager.getImpliedRoles(username))
+ for (Role role : identityManager.getUserRoles(username))
{
roles.add(role);
}
Modified: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityManagerImpl.java
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityManagerImpl.java 2010-07-16 06:29:33 UTC (rev 13411)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityManagerImpl.java 2010-07-16 07:34:35 UTC (rev 13412)
@@ -71,7 +71,7 @@
}
}
- public boolean deleteUser(String name)
+ public @Transactional boolean deleteUser(String name)
{
identity.checkPermission(RESOURCE_IDENTITY, PERMISSION_DELETE);
@@ -263,7 +263,7 @@
* @param name The user for which to return a list of roles
* @return List containing the names of the granted roles
*/
- public Collection<Role> getGrantedRoles(String username)
+ public Collection<Role> getUserRoles(String username)
{
identity.checkPermission(RESOURCE_RELATIONSHIP, PERMISSION_READ);
try
@@ -289,18 +289,6 @@
}
}
- /**
- * Returns a list of roles that are either explicitly or indirectly granted to the specified user.
- *
- * @param name The user for which to return the list of roles
- * @return List containing the names of the implied roles
- */
- public List<Role> getImpliedRoles(String username)
- {
- //return roleIdentityStore.listImpliedRoles(username);
- return null;
- }
-
public Collection<User> listRoleMembers(String roleType, String groupName, String groupType)
{
identity.checkPermission(RESOURCE_RELATIONSHIP, PERMISSION_READ);
Modified: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/action/UserAction.java
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/action/UserAction.java 2010-07-16 06:29:33 UTC (rev 13411)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/action/UserAction.java 2010-07-16 07:34:35 UTC (rev 13412)
@@ -49,7 +49,7 @@
{
conversation.begin();
this.username = username;
- roles = identityManager.getGrantedRoles(username);
+ roles = identityManager.getUserRoles(username);
enabled = identityManager.isUserEnabled(username);
newUserFlag = false;
}
@@ -119,7 +119,7 @@
}
}
- Collection<Role> grantedRoles = identityManager.getGrantedRoles(username);
+ Collection<Role> grantedRoles = identityManager.getUserRoles(username);
if (grantedRoles != null)
{
Modified: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/action/UserSearch.java
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/action/UserSearch.java 2010-07-16 06:29:33 UTC (rev 13411)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/action/UserSearch.java 2010-07-16 07:34:35 UTC (rev 13412)
@@ -5,17 +5,14 @@
import java.util.Collection;
import java.util.List;
-import javax.enterprise.context.SessionScoped;
+import javax.enterprise.inject.Model;
import javax.inject.Inject;
-import javax.inject.Named;
import org.jboss.seam.security.management.IdentityManager;
import org.picketlink.idm.api.Role;
import org.picketlink.idm.api.User;
-@Named
-@SessionScoped
-public class UserSearch implements Serializable
+public @Model class UserSearch implements Serializable
{
private static final long serialVersionUID = 8592034786339372510L;
@@ -39,10 +36,8 @@
public String getUserRoles(String username)
{
- Collection<Role> roles = identityManager.getGrantedRoles(username);
-
- //if (roles == null) return "";
-
+ Collection<Role> roles = identityManager.getUserRoles(username);
+
StringBuilder sb = new StringBuilder();
for (Role role : roles)
14 years, 5 months
Seam SVN: r13411 - modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management.
by seam-commits@lists.jboss.org
Author: shane.bryzak(a)jboss.com
Date: 2010-07-16 02:29:33 -0400 (Fri, 16 Jul 2010)
New Revision: 13411
Modified:
modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityManagerImpl.java
modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStore.java
Log:
take identityType into account for queries
Modified: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityManagerImpl.java
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityManagerImpl.java 2010-07-16 03:36:40 UTC (rev 13410)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityManagerImpl.java 2010-07-16 06:29:33 UTC (rev 13411)
@@ -16,6 +16,7 @@
import org.jboss.seam.transaction.Transactional;
import org.picketlink.idm.api.Credential;
import org.picketlink.idm.api.Group;
+import org.picketlink.idm.api.IdentitySearchCriteria;
import org.picketlink.idm.api.IdentitySession;
import org.picketlink.idm.api.Role;
import org.picketlink.idm.api.RoleType;
@@ -230,17 +231,14 @@
public Collection<User> findUsers(String filter)
{
- identity.checkPermission(RESOURCE_IDENTITY, PERMISSION_READ);
- UserQueryBuilder builder = identitySession.createUserQueryBuilder();
- UserQuery userQuery = builder.createQuery();
-
+ IdentitySearchCriteria criteria = new IdentitySearchCriteriaImpl();
try
{
- return identitySession.execute(userQuery);
+ return identitySession.getPersistenceManager().findUser(criteria);
}
- catch (QueryException ex)
+ catch (IdentityException e)
{
- throw new RuntimeException("Error querying users", ex);
+ throw new RuntimeException(e);
}
}
Modified: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStore.java
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStore.java 2010-07-16 03:36:40 UTC (rev 13410)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStore.java 2010-07-16 06:29:33 UTC (rev 13411)
@@ -1272,11 +1272,19 @@
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<?> criteria = builder.createQuery(identityClass);
- criteria.from(identityClass);
+ Root<?> root = criteria.from(identityClass);
Property<?> identityNameProp = modelProperties.get(PROPERTY_IDENTITY_NAME);
+ Property<?> identityTypeProp = modelProperties.get(PROPERTY_IDENTITY_TYPE);
List<Predicate> predicates = new ArrayList<Predicate>();
+
+ if (identityType != null)
+ {
+ predicates.add(builder.equal(root.get(identityTypeProp.getName()),
+ lookupIdentityType(identityType.getName(), em)));
+ }
+
criteria.where(predicates.toArray(new Predicate[0]));
List<?> results = em.createQuery(criteria).getResultList();
14 years, 5 months
Seam SVN: r13410 - in modules/security/trunk: impl/src/main/java/org/jboss/seam/security/management and 1 other directory.
by seam-commits@lists.jboss.org
Author: shane.bryzak(a)jboss.com
Date: 2010-07-15 23:36:40 -0400 (Thu, 15 Jul 2010)
New Revision: 13410
Modified:
modules/security/trunk/examples/idmconsole/src/main/resources/import.sql
modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityManagerImpl.java
modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStore.java
Log:
fix relationship directions
Modified: modules/security/trunk/examples/idmconsole/src/main/resources/import.sql
===================================================================
--- modules/security/trunk/examples/idmconsole/src/main/resources/import.sql 2010-07-15 14:46:21 UTC (rev 13409)
+++ modules/security/trunk/examples/idmconsole/src/main/resources/import.sql 2010-07-16 03:36:40 UTC (rev 13410)
@@ -16,4 +16,4 @@
insert into IdentityObjectRelationshipType (id, name) values (1, 'JBOSS_IDENTITY_MEMBERSHIP');
insert into IdentityObjectRelationshipType (id, name) values (2, 'JBOSS_IDENTITY_ROLE');
-insert into IdentityObjectRelationship (id, name, relationship_type_id, from_identity_id, to_identity_id) values (1, 'admin', 2, 1, 3);
+insert into IdentityObjectRelationship (id, name, relationship_type_id, from_identity_id, to_identity_id) values (1, 'admin', 2, 3, 1);
Modified: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityManagerImpl.java
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityManagerImpl.java 2010-07-15 14:46:21 UTC (rev 13409)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityManagerImpl.java 2010-07-16 03:36:40 UTC (rev 13410)
@@ -35,7 +35,7 @@
*
* @author Shane Bryzak
*/
-@RequestScoped @Transactional
+@RequestScoped
public class IdentityManagerImpl implements IdentityManager, Serializable
{
private static final long serialVersionUID = 6864253169970552893L;
@@ -55,7 +55,7 @@
@Inject IdentitySession identitySession;
- public boolean createUser(String name, Credential credential)
+ public @Transactional boolean createUser(String name, Credential credential)
{
identity.checkPermission(RESOURCE_IDENTITY, PERMISSION_CREATE);
try
Modified: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStore.java
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStore.java 2010-07-15 14:46:21 UTC (rev 13409)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStore.java 2010-07-16 03:36:40 UTC (rev 13410)
@@ -1364,11 +1364,11 @@
CriteriaQuery<?> criteria = builder.createQuery(relationshipClass);
Root<?> root = criteria.from(relationshipClass);
- Property<?> identityFromProperty = modelProperties.get(PROPERTY_RELATIONSHIP_FROM);
+ Property<?> identityToProperty = modelProperties.get(PROPERTY_RELATIONSHIP_TO);
Property<?> relationshipNameProperty = modelProperties.get(PROPERTY_RELATIONSHIP_NAME);
List<Predicate> predicates = new ArrayList<Predicate>();
- predicates.add(builder.equal(root.get(identityFromProperty.getName()),
+ predicates.add(builder.equal(root.get(identityToProperty.getName()),
lookupIdentity(identity, em)));
criteria.where(predicates.toArray(new Predicate[0]));
@@ -1567,12 +1567,12 @@
if (parent)
{
- predicates.add(builder.equal(root.get(relationshipToProp.getName()),
+ predicates.add(builder.equal(root.get(relationshipFromProp.getName()),
lookupIdentity(identity, em)));
}
else
{
- predicates.add(builder.equal(root.get(relationshipFromProp.getName()),
+ predicates.add(builder.equal(root.get(relationshipToProp.getName()),
lookupIdentity(identity, em)));
}
14 years, 5 months
Seam SVN: r13409 - in modules/persistence/trunk/src: test/java/org/jboss/seam/transactions/test and 1 other directory.
by seam-commits@lists.jboss.org
Author: swd847
Date: 2010-07-15 10:46:21 -0400 (Thu, 15 Jul 2010)
New Revision: 13409
Added:
modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/AfterTransactionCompletion.java
modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/BeforeTransactionCompletion.java
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionObserver.java
Modified:
modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/EjbSynchronizations.java
modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/SeSynchronizations.java
modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/SynchronizationRegistry.java
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionManagedBean.java
Log:
add transaction events
Added: modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/AfterTransactionCompletion.java
===================================================================
--- modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/AfterTransactionCompletion.java (rev 0)
+++ modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/AfterTransactionCompletion.java 2010-07-15 14:46:21 UTC (rev 13409)
@@ -0,0 +1,16 @@
+package org.jboss.seam.transaction;
+
+public class AfterTransactionCompletion
+{
+ private final boolean success;
+
+ public AfterTransactionCompletion(boolean success)
+ {
+ this.success = success;
+ }
+
+ public boolean success()
+ {
+ return success;
+ }
+}
Added: modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/BeforeTransactionCompletion.java
===================================================================
--- modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/BeforeTransactionCompletion.java (rev 0)
+++ modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/BeforeTransactionCompletion.java 2010-07-15 14:46:21 UTC (rev 13409)
@@ -0,0 +1,6 @@
+package org.jboss.seam.transaction;
+
+public class BeforeTransactionCompletion
+{
+
+}
Modified: modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/EjbSynchronizations.java
===================================================================
--- modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/EjbSynchronizations.java 2010-07-15 13:54:12 UTC (rev 13408)
+++ modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/EjbSynchronizations.java 2010-07-15 14:46:21 UTC (rev 13409)
@@ -10,6 +10,8 @@
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.enterprise.context.RequestScoped;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.inject.Inject;
import javax.transaction.Synchronization;
import org.slf4j.Logger;
@@ -31,6 +33,9 @@
{
private static final Logger log = LoggerFactory.getLogger(EjbSynchronizations.class);
+ @Inject
+ private BeanManager beanManager;
+
// maintain two lists to work around a bug in JBoss EJB3 where a new
// SessionSynchronization
// gets registered each time the bean is called
@@ -40,7 +45,7 @@
public void afterBegin()
{
log.debug("afterBegin");
- synchronizations.addLast(new SynchronizationRegistry());
+ synchronizations.addLast(new SynchronizationRegistry(beanManager));
}
public void beforeCompletion() throws EJBException, RemoteException
Modified: modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/SeSynchronizations.java
===================================================================
--- modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/SeSynchronizations.java 2010-07-15 13:54:12 UTC (rev 13408)
+++ modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/SeSynchronizations.java 2010-07-15 14:46:21 UTC (rev 13409)
@@ -4,6 +4,8 @@
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Alternative;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.inject.Inject;
import javax.transaction.Synchronization;
/**
@@ -22,9 +24,12 @@
{
protected Stack<SynchronizationRegistry> synchronizations = new Stack<SynchronizationRegistry>();
+ @Inject
+ private BeanManager beanManager;
+
public void afterTransactionBegin()
{
- synchronizations.push(new SynchronizationRegistry());
+ synchronizations.push(new SynchronizationRegistry(beanManager));
}
public void afterTransactionCommit(boolean success)
Modified: modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/SynchronizationRegistry.java
===================================================================
--- modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/SynchronizationRegistry.java 2010-07-15 13:54:12 UTC (rev 13408)
+++ modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/SynchronizationRegistry.java 2010-07-15 14:46:21 UTC (rev 13409)
@@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.List;
+import javax.enterprise.inject.spi.BeanManager;
import javax.transaction.Status;
import javax.transaction.Synchronization;
@@ -19,6 +20,14 @@
*/
class SynchronizationRegistry
{
+
+ private final BeanManager beanManager;
+
+ public SynchronizationRegistry(BeanManager beanManager)
+ {
+ this.beanManager = beanManager;
+ }
+
private static final Logger log = LoggerFactory.getLogger(SynchronizationRegistry.class);
private List<Synchronization> synchronizations = new ArrayList<Synchronization>();
@@ -30,11 +39,7 @@
void afterTransactionCompletion(boolean success)
{
- // if ( Events.exists() )
- // {
- // Events.instance().raiseEvent("org.jboss.seam.afterTransactionCompletion",
- // success);
- // }
+ beanManager.fireEvent(new AfterTransactionCompletion(success));
for (Synchronization sync : synchronizations)
{
try
@@ -51,10 +56,7 @@
void beforeTransactionCompletion()
{
- // if ( Events.exists() )
- // {
- // Events.instance().raiseEvent("org.jboss.seam.beforeTransactionCompletion");
- // }
+ beanManager.fireEvent(new BeforeTransactionCompletion());
for (Synchronization sync : synchronizations)
{
try
Modified: modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java
===================================================================
--- modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java 2010-07-15 13:54:12 UTC (rev 13408)
+++ modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java 2010-07-15 14:46:21 UTC (rev 13409)
@@ -39,7 +39,7 @@
WebArchive war = ShrinkWrap.create("test.war", WebArchive.class);
war.addLibraries(MavenArtifactResolver.resolve(ArtifactNames.WELD_EXTENSIONS));
war.addPackage(Transaction.class.getPackage());
- war.addClasses(TransactionInterceptorTest.class, TransactionManagedBean.class, Hotel.class, EntityManagerProvider.class, DontRollBackException.class);
+ war.addClasses(TransactionInterceptorTest.class, TransactionManagedBean.class, Hotel.class, EntityManagerProvider.class, DontRollBackException.class, TransactionObserver.class);
war.addWebResource("META-INF/persistence.xml", "classes/META-INF/persistence.xml");
war.addWebResource(new ByteArrayAsset(("<beans><interceptors><class>" + TransactionInterceptor.class.getName() + "</class></interceptors></beans>").getBytes()), "beans.xml");
@@ -55,12 +55,20 @@
@PersistenceContext
EntityManager em;
+ @Inject
+ TransactionObserver observer;
+
@Test
public void testTransactionInterceptor() throws NotSupportedException, SystemException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException
{
-
+ observer.setEnabled(true);
+ try
+ {
+ observer.reset(true);
bean.addHotel();
assertHotels(1);
+ observer.verify();
+ observer.reset(false);
try
{
bean.failToAddHotel();
@@ -69,7 +77,8 @@
{
}
assertHotels(1);
-
+ observer.verify();
+ observer.reset(true);
try
{
bean.addHotelWithApplicationException();
@@ -78,6 +87,13 @@
{
}
assertHotels(2);
+ observer.verify();
+ }
+ catch (Exception e)
+ {
+ observer.setEnabled(false);
+ throw new RuntimeException(e);
+ }
}
Modified: modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionManagedBean.java
===================================================================
--- modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionManagedBean.java 2010-07-15 13:54:12 UTC (rev 13408)
+++ modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionManagedBean.java 2010-07-15 14:46:21 UTC (rev 13409)
@@ -14,6 +14,7 @@
@Inject
EntityManager entityManager;
+
public void addHotel()
{
entityManager.joinTransaction();
@@ -39,4 +40,5 @@
entityManager.flush();
throw new DontRollBackException();
}
+
}
Added: modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionObserver.java
===================================================================
--- modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionObserver.java (rev 0)
+++ modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionObserver.java 2010-07-15 14:46:21 UTC (rev 13409)
@@ -0,0 +1,63 @@
+package org.jboss.seam.transactions.test;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.event.Observes;
+
+import org.jboss.seam.transaction.AfterTransactionCompletion;
+import org.jboss.seam.transaction.BeforeTransactionCompletion;
+
+@ApplicationScoped
+public class TransactionObserver
+{
+ private boolean expectSuccess, beforeTransaction, afterTransaction;
+ private boolean enabled = false;
+
+ public boolean isEnabled()
+ {
+ return enabled;
+ }
+
+ public void setEnabled(boolean enabled)
+ {
+ this.enabled = enabled;
+ }
+
+ public void reset(boolean expected)
+ {
+ beforeTransaction = false;
+ afterTransaction = false;
+ expectSuccess = expected;
+ }
+
+ public boolean isBeforeTransaction()
+ {
+ return beforeTransaction;
+ }
+
+ public boolean isAfterTransaction()
+ {
+ return afterTransaction;
+ }
+
+ public void observeBeforeTransactionCommit(@Observes BeforeTransactionCompletion event)
+ {
+ beforeTransaction = true;
+ }
+
+ public void observeBeforeTransactionCommit(@Observes AfterTransactionCompletion event)
+ {
+ afterTransaction = true;
+ if (expectSuccess != event.success())
+ {
+ throw new RuntimeException("Expected success to be " + expectSuccess);
+ }
+ }
+
+ public void verify()
+ {
+ if (!((beforeTransaction || !expectSuccess) && afterTransaction))
+ {
+ throw new RuntimeException("Events not recieved before:" + beforeTransaction + " after:" + afterTransaction);
+ }
+ }
+}
14 years, 5 months
Seam SVN: r13408 - in modules/persistence/trunk/src: test/java/org/jboss/seam/transactions/test and 1 other directories.
by seam-commits@lists.jboss.org
Author: swd847
Date: 2010-07-15 09:54:12 -0400 (Thu, 15 Jul 2010)
New Revision: 13408
Added:
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/UserTransactionTest.java
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/util/DontRollBackException.java
Removed:
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/SimpleTest.java
Modified:
modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/Work.java
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java
modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionManagedBean.java
Log:
fix transaction interceptor scemantics
add test for ApplicationException
Modified: modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/Work.java
===================================================================
--- modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/Work.java 2010-07-15 13:14:30 UTC (rev 13407)
+++ modules/persistence/trunk/src/main/java/org/jboss/seam/transaction/Work.java 2010-07-15 13:54:12 UTC (rev 13408)
@@ -56,11 +56,24 @@
}
catch (Exception e)
{
- if (newTransactionRequired && userTransaction.getStatus() != Status.STATUS_NO_TRANSACTION && isRollbackRequired(e, true))
+ if (newTransactionRequired && userTransaction.getStatus() != Status.STATUS_NO_TRANSACTION )
{
- log.debug("rolling back transaction");
- userTransaction.rollback();
+ if(isRollbackRequired(e, true))
+ {
+ log.debug("rolling back transaction");
+ userTransaction.rollback();
+ }
+ else
+ {
+ log.debug("committing transaction after ApplicationException(rollback=false):" + e.getMessage());
+ userTransaction.commit();
+ }
}
+ else if (userTransaction.getStatus() != Status.STATUS_NO_TRANSACTION && isRollbackRequired(e, true))
+ {
+ userTransaction.setRollbackOnly();
+ }
+
throw e;
}
Deleted: modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/SimpleTest.java
===================================================================
--- modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/SimpleTest.java 2010-07-15 13:14:30 UTC (rev 13407)
+++ modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/SimpleTest.java 2010-07-15 13:54:12 UTC (rev 13408)
@@ -1,79 +0,0 @@
-package org.jboss.seam.transactions.test;
-
-import java.util.List;
-
-import javax.inject.Inject;
-import javax.persistence.EntityManager;
-import javax.persistence.EntityManagerFactory;
-import javax.persistence.PersistenceUnit;
-import javax.transaction.HeuristicMixedException;
-import javax.transaction.HeuristicRollbackException;
-import javax.transaction.NotSupportedException;
-import javax.transaction.RollbackException;
-import javax.transaction.SystemException;
-
-import junit.framework.Assert;
-
-import org.jboss.arquillian.api.Deployment;
-import org.jboss.arquillian.junit.Arquillian;
-import org.jboss.seam.transaction.Transaction;
-import org.jboss.seam.transaction.UserTransaction;
-import org.jboss.seam.transactions.test.util.ArtifactNames;
-import org.jboss.seam.transactions.test.util.MavenArtifactResolver;
-import org.jboss.shrinkwrap.api.Archive;
-import org.jboss.shrinkwrap.api.ShrinkWrap;
-import org.jboss.shrinkwrap.api.asset.ByteArrayAsset;
-import org.jboss.shrinkwrap.api.spec.WebArchive;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-(a)RunWith(Arquillian.class)
-public class SimpleTest
-{
- @Deployment
- public static Archive<?> createTestArchive()
- {
-
- WebArchive war = ShrinkWrap.create(WebArchive.class);
- war.addLibraries(MavenArtifactResolver.resolve(ArtifactNames.WELD_EXTENSIONS));
- war.addPackage(Transaction.class.getPackage()).addClasses(SimpleTest.class, Hotel.class);
- war.addWebResource("META-INF/persistence.xml", "classes/META-INF/persistence.xml");
- war.addWebResource(new ByteArrayAsset(new byte[0]), "beans.xml");
-
- return war;
- }
-
- @Inject
- UserTransaction transaction;
-
- @PersistenceUnit
- EntityManagerFactory emf;
-
- @Test
- public void simpleTest() throws NotSupportedException, SystemException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException
- {
- transaction.begin();
- EntityManager em = emf.createEntityManager();
- em.joinTransaction();
- Hotel h = new Hotel("test", "Fake St", "Wollongong", "NSW", "2518", "Australia");
- em.persist(h);
- em.flush();
- transaction.commit();
-
- transaction.begin();
- em = emf.createEntityManager();
- em.joinTransaction();
- h = new Hotel("test2", "Fake St", "Wollongong", "NSW", "2518", "Australia");
- em.persist(h);
- em.flush();
- transaction.rollback();
-
- transaction.begin();
- em = emf.createEntityManager();
- em.joinTransaction();
- List<Hotel> hotels = em.createQuery("select h from Hotel h").getResultList();
- Assert.assertTrue(hotels.size() == 1);
- transaction.rollback();
-
- }
-}
Modified: modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java
===================================================================
--- modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java 2010-07-15 13:14:30 UTC (rev 13407)
+++ modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java 2010-07-15 13:54:12 UTC (rev 13408)
@@ -19,6 +19,7 @@
import org.jboss.seam.transaction.TransactionInterceptor;
import org.jboss.seam.transaction.UserTransaction;
import org.jboss.seam.transactions.test.util.ArtifactNames;
+import org.jboss.seam.transactions.test.util.DontRollBackException;
import org.jboss.seam.transactions.test.util.EntityManagerProvider;
import org.jboss.seam.transactions.test.util.MavenArtifactResolver;
import org.jboss.shrinkwrap.api.Archive;
@@ -38,7 +39,7 @@
WebArchive war = ShrinkWrap.create("test.war", WebArchive.class);
war.addLibraries(MavenArtifactResolver.resolve(ArtifactNames.WELD_EXTENSIONS));
war.addPackage(Transaction.class.getPackage());
- war.addClasses(TransactionInterceptorTest.class, TransactionManagedBean.class, Hotel.class, EntityManagerProvider.class);
+ war.addClasses(TransactionInterceptorTest.class, TransactionManagedBean.class, Hotel.class, EntityManagerProvider.class, DontRollBackException.class);
war.addWebResource("META-INF/persistence.xml", "classes/META-INF/persistence.xml");
war.addWebResource(new ByteArrayAsset(("<beans><interceptors><class>" + TransactionInterceptor.class.getName() + "</class></interceptors></beans>").getBytes()), "beans.xml");
@@ -59,21 +60,33 @@
{
bean.addHotel();
-
+ assertHotels(1);
try
{
bean.failToAddHotel();
}
catch (Exception e)
{
+ }
+ assertHotels(1);
+ try
+ {
+ bean.addHotelWithApplicationException();
}
+ catch (DontRollBackException e)
+ {
+ }
+ assertHotels(2);
+ }
+
+ public void assertHotels(int count) throws NotSupportedException, SystemException
+ {
transaction.begin();
em.joinTransaction();
List<Hotel> hotels = em.createQuery("select h from Hotel h").getResultList();
- Assert.assertTrue("Wrong number of hotels: " + hotels.size(), hotels.size() == 1);
+ Assert.assertTrue("Wrong number of hotels: " + hotels.size(), hotels.size() == count);
transaction.rollback();
-
}
}
Modified: modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionManagedBean.java
===================================================================
--- modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionManagedBean.java 2010-07-15 13:14:30 UTC (rev 13407)
+++ modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/TransactionManagedBean.java 2010-07-15 13:54:12 UTC (rev 13408)
@@ -5,6 +5,7 @@
import org.jboss.seam.transaction.TransactionPropagation;
import org.jboss.seam.transaction.Transactional;
+import org.jboss.seam.transactions.test.util.DontRollBackException;
@Transactional(TransactionPropagation.REQUIRED)
public class TransactionManagedBean
@@ -29,4 +30,13 @@
entityManager.flush();
throw new RuntimeException("Roll back transaction");
}
+
+ public void addHotelWithApplicationException() throws DontRollBackException
+ {
+ entityManager.joinTransaction();
+ Hotel h = new Hotel("test3", "Fake St", "Wollongong", "NSW", "2518", "Australia");
+ entityManager.persist(h);
+ entityManager.flush();
+ throw new DontRollBackException();
+ }
}
Copied: modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/UserTransactionTest.java (from rev 13406, modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/SimpleTest.java)
===================================================================
--- modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/UserTransactionTest.java (rev 0)
+++ modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/UserTransactionTest.java 2010-07-15 13:54:12 UTC (rev 13408)
@@ -0,0 +1,79 @@
+package org.jboss.seam.transactions.test;
+
+import java.util.List;
+
+import javax.inject.Inject;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.PersistenceUnit;
+import javax.transaction.HeuristicMixedException;
+import javax.transaction.HeuristicRollbackException;
+import javax.transaction.NotSupportedException;
+import javax.transaction.RollbackException;
+import javax.transaction.SystemException;
+
+import junit.framework.Assert;
+
+import org.jboss.arquillian.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.seam.transaction.Transaction;
+import org.jboss.seam.transaction.UserTransaction;
+import org.jboss.seam.transactions.test.util.ArtifactNames;
+import org.jboss.seam.transactions.test.util.MavenArtifactResolver;
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.ByteArrayAsset;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+(a)RunWith(Arquillian.class)
+public class UserTransactionTest
+{
+ @Deployment
+ public static Archive<?> createTestArchive()
+ {
+
+ WebArchive war = ShrinkWrap.create("test.war", WebArchive.class);
+ war.addLibraries(MavenArtifactResolver.resolve(ArtifactNames.WELD_EXTENSIONS));
+ war.addPackage(Transaction.class.getPackage()).addClasses(UserTransactionTest.class, Hotel.class);
+ war.addWebResource("META-INF/persistence.xml", "classes/META-INF/persistence.xml");
+ war.addWebResource(new ByteArrayAsset(new byte[0]), "beans.xml");
+
+ return war;
+ }
+
+ @Inject
+ UserTransaction transaction;
+
+ @PersistenceUnit
+ EntityManagerFactory emf;
+
+ @Test
+ public void userTransactionTest() throws NotSupportedException, SystemException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException
+ {
+ transaction.begin();
+ EntityManager em = emf.createEntityManager();
+ em.joinTransaction();
+ Hotel h = new Hotel("test", "Fake St", "Wollongong", "NSW", "2518", "Australia");
+ em.persist(h);
+ em.flush();
+ transaction.commit();
+
+ transaction.begin();
+ em = emf.createEntityManager();
+ em.joinTransaction();
+ h = new Hotel("test2", "Fake St", "Wollongong", "NSW", "2518", "Australia");
+ em.persist(h);
+ em.flush();
+ transaction.rollback();
+
+ transaction.begin();
+ em = emf.createEntityManager();
+ em.joinTransaction();
+ List<Hotel> hotels = em.createQuery("select h from Hotel h").getResultList();
+ Assert.assertTrue(hotels.size() == 1);
+ transaction.rollback();
+ }
+
+}
Added: modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/util/DontRollBackException.java
===================================================================
--- modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/util/DontRollBackException.java (rev 0)
+++ modules/persistence/trunk/src/test/java/org/jboss/seam/transactions/test/util/DontRollBackException.java 2010-07-15 13:54:12 UTC (rev 13408)
@@ -0,0 +1,9 @@
+package org.jboss.seam.transactions.test.util;
+
+import javax.ejb.ApplicationException;
+
+@ApplicationException(rollback = false)
+public class DontRollBackException extends Exception
+{
+
+}
14 years, 5 months