You need maven2 and java 6
+By default eclipselink is used as persistence provider.
+Use mvn -Phibernate,!eclipselink (escape the ! if needed) to build with hibernate as
+persistence provider.
+
Releasing
=========
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
- <version>2.0.0</version>
+ <version>2.0.2</version>
<scope>compile</scope>
</dependency>
@Version
private int version;
+ /**
+ * Name for this instance of the authorization service.
+ */
+ private String name;
+
public AbstractAuthorizationService() {
// Empty.
}
+ public AbstractAuthorizationService(String aName) {
+ name = aName;
+ }
+
public AbstractAuthorizationService(AbstractAuthorizationService aSvc) {
primaryKey = aSvc.primaryKey;
version = aSvc.version;
+ name = aSvc.name;
+ }
+
+ public String getName() {
+ return name;
}
}
@Transient
private UserAccessor userAccessor;
- /**
- * Name for this instance of the authorization service.
- */
- private String name;
-
/**
* Constructs the service.
*
* Name of this instance of the service.
*/
public DefaultAuthorizationService(UserAccessor aAccessor, String aName) {
+ super(aName);
rules = new ArrayList<AuthorizationRule>();
userAccessor = aAccessor;
- name = aName;
}
/**
public DefaultAuthorizationService() {
rules = new ArrayList<AuthorizationRule>();
userAccessor = null;
- name = null;
}
/**
return aResource;
}
- protected String getName() {
- return name;
- }
-
- public void setName(String aName) {
- name = aName;
- }
-
/*
* (non-Javadoc)
*
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
+import javax.persistence.OneToOne;
import javax.persistence.Transient;
import org.apache.log4j.Logger;
*
* @return Returns the operationCondition.
*/
- @ManyToOne(cascade = CascadeType.ALL, targetEntity = AbstractOperationCondition.class)
+ @OneToOne(cascade = CascadeType.ALL, targetEntity = AbstractOperationCondition.class, orphanRemoval = true)
@JoinColumn(name = "OPER_COND_PK")
public OperationCondition getOperationCondition() {
return operationCondition;
*
* @return Returns the pathCondition.
*/
- @ManyToOne(cascade = CascadeType.ALL, targetEntity = AbstractPathCondition.class)
+ @OneToOne(cascade = CascadeType.ALL, targetEntity = AbstractPathCondition.class, orphanRemoval = true)
@JoinColumn(name = "PATH_COND_PK")
public PathCondition getPathCondition() {
return pathCondition;
*
* @return Returns the userCondition.
*/
- @ManyToOne(cascade = CascadeType.ALL, targetEntity = AbstractUserCondition.class)
+ @OneToOne(cascade = CascadeType.ALL, targetEntity = AbstractUserCondition.class, orphanRemoval = true)
@JoinColumn(name = "USER_COND_PK")
public UserCondition getUserCondition() {
return userCondition;
*/
private void initialize() {
if (service == null) {
- refreshByReload();
+ service = refreshByReload();
}
}
- private void refreshByReload() {
+ private AuthorizationService refreshByReload() {
+ AuthorizationService service;
try {
service = entityManager.createNamedQuery(
AbstractAuthorizationService.QUERY_FIND_BY_NAME,
AbstractAuthorizationService.class).setParameter(
- AbstractAuthorizationService.NAME_PARAM, name).getSingleResult();
+ DefaultAuthorizationService.NAME_PARAM, name).getSingleResult();
service.setUserAccessor(userAccessor);
} catch (NonUniqueResultException e) {
throw new IllegalArgumentException(
service = new DefaultAuthorizationService(userAccessor, name);
entityManager.persist(service);
}
+ return service;
}
/*
initialize();
refresh();
service.appendRule(aRule);
- save();
+ save(); // service might still be detached as service is cached.
}
/*
initialize();
refresh();
service.removeRule(aIndex);
- save();
+ save(); // service might still be detached as service is cached.
}
/*
initialize();
refresh();
service.insertRuleAfter(aIndex, aRule);
- save();
+ save(); // service might still be detached as service is cached.
}
/**
long time = System.currentTimeMillis();
if ((time - lastRefreshTime) > refreshInterval) {
- refreshByReload();
+ service = refreshByReload();
lastRefreshTime = time;
}
}
User.NAME_PARAM),
@NamedQuery(name = User.QUERY_FIND_BY_GROUP_NAME, query = "select user from User user join user.groups grp where grp.name = :name"),
@NamedQuery(name = User.QUERY_COUNT_USERS, query = "select count(u) from User u"),
- @NamedQuery(name = User.QUERY_ALL_USERS, query = "select u from User u")})
+ @NamedQuery(name = User.QUERY_ALL_USERS, query = "select u from User u") })
public class User implements Serializable, Comparable {
public static final String QUERY_FIND_BY_NAME = "User.findByName";
*
*/
public User(User aUser) {
- primaryKey = aUser.primaryKey;
+ primaryKey = aUser.primaryKey;
version = aUser.version;
name = aUser.name;
password = aUser.password;
*/
@Override
public boolean equals(Object aUser) {
+ if (aUser == null) {
+ return false;
+ }
if (!(aUser instanceof User)) {
return false;
}
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
- */
+ */
package org.wamblee.usermgt;
import static org.wamblee.usermgt.UserMgtException.Reason.DUPLICATE_GROUP;
import static org.wamblee.usermgt.UserMgtException.Reason.UNKNOWN_GROUP;
import static org.wamblee.usermgt.UserMgtException.Reason.UNKNOWN_USER;
+import java.util.HashSet;
import java.util.Set;
/**
* @see org.wamblee.usermgt.UserAdministration#getUser(java.lang.String)
*/
public User getUser(String aName) {
- return users.find(aName);
+ User user = users.find(aName);
+ if (user == null) {
+ return user;
+ }
+ return new User(user);
}
/*
* @see org.wamblee.usermgt.UserAdministration#getGroup(java.lang.String)
*/
public Group getGroup(String aName) {
- return groups.find(aName);
+ Group group = groups.find(aName);
+ if ( group == null ) {
+ return group;
+ }
+ return new Group(group);
}
/*
* @see org.wamblee.usermgt.UserAdministration#getUsers()
*/
public Set<User> getUsers() {
- return users.list();
+ Set<User> res = new HashSet<User>();
+ for (User user: users.list()) {
+ res.add(new User(user));
+ }
+ return res;
}
/*
* )
*/
public Set<User> getUsers(Group aGroup) {
- return users.list(aGroup);
+ Set<User> res = new HashSet<User>();
+ for (User user: users.list(aGroup)) {
+ res.add(new User(user));
+ }
+ return res;
}
/*
* @see org.wamblee.usermgt.UserAdministration#getGroups()
*/
public Set<Group> getGroups() {
- return groups.list();
+ Set<Group> res = new HashSet<Group>();
+ for (Group group: groups.list()) {
+ res.add(new Group(group));
+ }
+ return res;
}
/*
}
userValidator.validate(aUserName);
-
+
aUser.setName(aUserName);
users.userModified(aUser);
}
}
groupValidator.validate(aGroupName);
-
+
aGroup.setName(aGroupName);
groups.groupModified(aGroup);
}
return false;
}
em.persist(aGroup);
+ em.flush(); // to make sure the version is updated.
return true;
}
}
entityManager.persist(aUser);
+ entityManager.flush(); // to make sure the version is updated.
setPasswordInfo(aUser);
cache.put(aUser.getName(), aUser);
for (User user : list) {
setPasswordInfo(user);
- users.add(new User(user));
+ users.add(user);
}
return users;
query.setParameter(User.NAME_PARAM, aGroup.getName());
List<User> list = query.getResultList();
-
- for (User user : list) {
+ users.addAll(list);
+ for (User user : users) {
setPasswordInfo(user);
- users.add(new User(user));
}
return users;
service.appendRule(rule1);
service.appendRule(rule2);
service.appendRule(rule3);
+ checkRuleCount(3);
}
protected void resetTestRules() {
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <parent>
+ <parent>
+ <groupId>org.wamblee</groupId>
+ <artifactId>wamblee-utils</artifactId>
+ <version>0.2.3-SNAPSHOT</version>
+ </parent>
+
+ <modelVersion>4.0.0</modelVersion>
<groupId>org.wamblee</groupId>
- <artifactId>wamblee-utils</artifactId>
- <version>0.2.3-SNAPSHOT</version>
- </parent>
+ <artifactId>wamblee-security-jpatest</artifactId>
+ <packaging>jar</packaging>
+ <name>/security/jpatest</name>
+ <url>http://wamblee.org</url>
+ <dependencies>
- <modelVersion>4.0.0</modelVersion>
- <groupId>org.wamblee</groupId>
- <artifactId>wamblee-security-jpatest</artifactId>
- <packaging>jar</packaging>
- <name>/security/jpatest</name>
- <url>http://wamblee.org</url>
- <dependencies>
+ <dependency>
+ <groupId>org.wamblee</groupId>
+ <artifactId>wamblee-security-impl</artifactId>
+ <version>0.2.3-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>org.wamblee</groupId>
+ <artifactId>wamblee-security-impl</artifactId>
+ <version>0.2.3-SNAPSHOT</version>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-entitymanager</artifactId>
- <version>3.5.0-Final</version>
- <exclusions>
- <exclusion>
- <groupId>javax.transaction</groupId>
- <artifactId>jta</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>org.wamblee</groupId>
- <artifactId>wamblee-security-impl</artifactId>
- <version>0.2.3-SNAPSHOT</version>
- </dependency>
- <dependency>
- <groupId>org.wamblee</groupId>
- <artifactId>wamblee-security-impl</artifactId>
- <version>0.2.3-SNAPSHOT</version>
- <type>test-jar</type>
- <scope>test</scope>
- </dependency>
-
- <dependency>
- <groupId>org.wamblee</groupId>
- <artifactId>wamblee-test-enterprise</artifactId>
- <version>0.2.3-SNAPSHOT</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.wamblee</groupId>
- <artifactId>wamblee-test-hibernate</artifactId>
- <version>0.2.3-SNAPSHOT</version>
- <scope>test</scope>
- </dependency>
-
- </dependencies>
+ <dependency>
+ <groupId>org.wamblee</groupId>
+ <artifactId>wamblee-test-enterprise</artifactId>
+ <version>0.2.3-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
- <repositories>
- <repository>
- <id>jboss</id>
- <name>JBoss Repo</name>
- <url>http://repository.jboss.org/maven2</url>
- </repository>
- </repositories>
+ </dependencies>
+ <profiles>
+ <profile>
+ <id>eclipselink</id>
+ <activation>
+ <activeByDefault>true</activeByDefault>
+ </activation>
+ <dependencies>
+ <dependency>
+ <groupId>org.wamblee</groupId>
+ <artifactId>wamblee-test-eclipselink</artifactId>
+ <version>0.2.3-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+ <repositories>
+ <repository>
+ <id>jboss</id>
+ <name>JBoss Repo</name>
+ <url>http://repository.jboss.org/maven2</url>
+ </repository>
+ </repositories>
+ </profile>
+ <profile>
+ <id>hibernate</id>
+ <dependencies>
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-entitymanager</artifactId>
+ <version>3.5.0-Final</version>
+ <exclusions>
+ <exclusion>
+ <groupId>javax.transaction</groupId>
+ <artifactId>jta</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
+ <groupId>org.wamblee</groupId>
+ <artifactId>wamblee-test-hibernate</artifactId>
+ <version>0.2.3-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+ </profile>
+ </profiles>
+
</project>
*/
package org.wamblee.security.authorization.jpa;
+import static org.wamblee.security.authorization.AuthorizationResult.*;
+
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;
-import org.wamblee.security.authorization.AbstractAuthorizationService;
import org.wamblee.security.authorization.AllOperation;
-import org.wamblee.security.authorization.AuthorizationResult;
+import org.wamblee.security.authorization.AuthorizationRule;
import org.wamblee.security.authorization.AuthorizationService;
import org.wamblee.security.authorization.AuthorizationServiceTest;
-import org.wamblee.security.authorization.TestAuthorizationRule;
+import org.wamblee.security.authorization.DefaultAuthorizationService;
import org.wamblee.support.persistence.JpaTester;
import org.wamblee.support.persistence.TransactionProxyFactory;
import org.wamblee.support.persistence.JpaBuilder.JpaUnitOfWork;
super.setUp();
}
+ @Override
+ protected void tearDown() throws Exception {
+ jpaTester.stop();
+ super.tearDown();
+ }
+
/*
* (non-Javadoc)
*
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.junit.Ignore;
import org.wamblee.cache.EhCache;
import org.wamblee.io.ClassPathResource;
import org.wamblee.security.encryption.Md5HexMessageDigester;
protected UserAdministration createAdmin() {
return userAdmin;
}
-
+
public void testAllTestsInASeparateTransaction() throws Exception {
Method[] methods = UserAdministrationImplTest.class.getMethods();
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
-
- <parent>
- <groupId>org.wamblee</groupId>
- <artifactId>wamblee-utils</artifactId>
- <version>0.2.3-SNAPSHOT</version>
- </parent>
-
- <modelVersion>4.0.0</modelVersion>
- <groupId>org.wamblee</groupId>
- <artifactId>wamblee-test-eclipselink</artifactId>
- <packaging>jar</packaging>
- <name>/test/eclipselink</name>
- <url>http://wamblee.org</url>
-
- <dependencies>
- <dependency>
- <groupId>org.wamblee</groupId>
- <artifactId>wamblee-test-enterprise</artifactId>
- <version>0.2.3-SNAPSHOT</version>
- </dependency>
-
- <dependency>
- <groupId>org.wamblee</groupId>
- <artifactId>wamblee-test-enterprise</artifactId>
- <version>0.2.3-SNAPSHOT</version>
- <type>test-jar</type>
- </dependency>
-
- <dependency>
- <groupId>org.dbunit</groupId>
- <artifactId>dbunit</artifactId>
- </dependency>
-
- <dependency>
- <groupId>javax.persistence</groupId>
- <artifactId>persistence-api</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.eclipse.persistence</groupId>
- <artifactId>eclipselink</artifactId>
- </dependency>
-
- </dependencies>
-
- <repositories>
- <repository>
- <id>EclipseLink Repo</id>
- <url>http://www.eclipse.org/downloads/download.php?r=1&nf=1&file=/rt/eclipselink/maven.repo</url>
- </repository>
-
- </repositories>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <parent>
+ <groupId>org.wamblee</groupId>
+ <artifactId>wamblee-utils</artifactId>
+ <version>0.2.3-SNAPSHOT</version>
+ </parent>
+
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.wamblee</groupId>
+ <artifactId>wamblee-test-eclipselink</artifactId>
+ <packaging>jar</packaging>
+ <name>/test/eclipselink</name>
+ <url>http://wamblee.org</url>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.wamblee</groupId>
+ <artifactId>wamblee-test-enterprise</artifactId>
+ <version>0.2.3-SNAPSHOT</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.wamblee</groupId>
+ <artifactId>wamblee-test-enterprise</artifactId>
+ <version>0.2.3-SNAPSHOT</version>
+ <type>test-jar</type>
+ </dependency>
+
+ <dependency>
+ <groupId>org.dbunit</groupId>
+ <artifactId>dbunit</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>org.eclipse.persistence</groupId>
+ <artifactId>javax.persistence</artifactId>
+ <version>2.0.0</version>
+ </dependency>
+
+ <dependency>
+ <groupId>javax.persistence</groupId>
+ <artifactId>persistence-api</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>org.eclipse.persistence</groupId>
+ <artifactId>eclipselink</artifactId>
+ </dependency>
+
+ </dependencies>
+
+ <repositories>
+ <repository>
+ <id>EclipseLink Repo</id>
+ <url>http://www.eclipse.org/downloads/download.php?r=1&nf=1&file=/rt/eclipselink/maven.repo</url>
+ </repository>
+
+ </repositories>
</project>
// DDL generation for toplink
aJpaProperties.put("eclipselink.ddl-generation", "create-tables");
+
+ // Use JTA transaction type
+ aJpaProperties.put("javax.persistence.transactionType", "JTA");
}
@Override
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
- */
+ */
package org.wamblee.support.jndi;
import java.util.HashMap;
import javax.naming.InitialContext;
import javax.naming.Name;
+import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
public class StubInitialContext extends InitialContext {
@Override
public Object lookup(String aName) throws NamingException {
- return bindings.get(aName);
+ Object value = bindings.get(aName);
+ if (value == null) {
+ throw new NameNotFoundException(aName);
+ }
+ return value;
}
@Override
public Object lookup(Name aName) throws NamingException {
- return super.lookup(aName.toString());
+ Object value = super.lookup(aName.toString());
+ if (value == null) {
+ throw new NameNotFoundException(aName.toString());
+ }
+ return value;
}
}
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
- */
+ */
package org.wamblee.support.persistence;
import java.util.Map;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
+import javax.persistence.PersistenceException;
import javax.sql.DataSource;
import org.wamblee.support.jndi.StubInitialContextFactory;
throw new RuntimeException("JNDI problem", e);
}
factory = createFactory();
- execute(new JpaUnitOfWork<Void>() {
- public Void execute(EntityManager aEm) {
- // Empty, just to trigger database schema creation.
- return null;
- }
- });
+ try {
+ execute(new JpaUnitOfWork<Void>() {
+ public Void execute(EntityManager aEm) {
+ // Empty, just to trigger database schema creation.
+ return null;
+ }
+ });
+ } catch (PersistenceException e) {
+ factory.close();
+ throw e;
+ }
}
/**
// DDL generation for toplink
aJpaProperties.put("toplink.ddl-generation", "create-tables");
+
+ // Use JTA transaction type
+ aJpaProperties.put("javax.persistence.transactionType", "JTA");
}
@Override