- BasicService
This commit is contained in:
Konstantin Krivopustov 2008-11-14 09:02:36 +00:00
parent 9f748b0abd
commit 9a3de7d7de
19 changed files with 541 additions and 18 deletions

View File

@ -328,6 +328,7 @@
<root url="jar://$PROJECT_DIR$/../lib/test/jboss-ejb3-embeddable-ALPHA-9.jar!/" />
<root url="jar://$PROJECT_DIR$/../lib/test/jboss-ejb3-embeddable-thirdparty-ALPHA-9.jar!/" />
<root url="jar://$PROJECT_DIR$/../lib/test/junit-4.5.jar!/" />
<root url="jar://$PROJECT_DIR$/../lib/test/hibernate3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>

View File

@ -21,6 +21,8 @@ public interface EntityManagerAdapter
<T extends BaseEntity> T find(Class<T> clazz, Object key);
QueryAdapter createQuery(String qlStr);
void flush();
void close();

View File

@ -32,6 +32,10 @@ public abstract class Locator
return (T) getInstance().__lookupLocal(name);
}
public static <T> T lookupRemote(String name) {
return (T) getInstance().__lookupRemote(name);
}
public static TransactionAdapter createTransaction() {
return getInstance().__createTransaction();
}
@ -40,5 +44,7 @@ public abstract class Locator
protected abstract Object __lookupLocal(String name);
protected abstract Object __lookupRemote(String name);
protected abstract TransactionAdapter __createTransaction();
}

View File

@ -11,6 +11,12 @@ package com.haulmont.cuba.core;
import com.haulmont.cuba.core.impl.ManagedPersistenceProvider;
import javax.persistence.Entity;
import java.lang.annotation.Annotation;
import org.jboss.remoting.samples.chat.exceptions.InvalidArgumentException;
import org.apache.commons.lang.StringUtils;
public abstract class PersistenceProvider
{
public static final int LOGIN_FIELD_LEN = 20;
@ -32,6 +38,17 @@ public abstract class PersistenceProvider
return getInstance().__getEntityManager();
}
public static String getEntityName(Class entityClass) {
Annotation annotation = entityClass.getAnnotation(Entity.class);
if (annotation == null)
throw new IllegalArgumentException("Class " + entityClass + " is not an entity");
String name = ((Entity) annotation).name();
if (!StringUtils.isEmpty(name))
return name;
else
return entityClass.getSimpleName();
}
protected abstract EntityManagerFactoryAdapter __getEntityManagerFactory();
protected abstract EntityManagerAdapter __getEntityManager();

View File

@ -0,0 +1,34 @@
/*
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
* Haulmont Technology proprietary and confidential.
* Use is subject to license terms.
* Author: Konstantin Krivopustov
* Created: 13.11.2008 18:02:40
*
* $Id$
*/
package com.haulmont.cuba.core;
import javax.persistence.TemporalType;
import java.util.List;
import java.util.Date;
public interface QueryAdapter
{
List getResultList();
Object getSingleResult();
QueryAdapter setMaxResults(int maxResult);
QueryAdapter setFirstResult(int startPosition);
QueryAdapter setParameter(String name, Object value);
QueryAdapter setParameter(String name, Date value, TemporalType temporalType);
QueryAdapter setParameter(int position, Object value);
QueryAdapter setParameter(int position, Date value, TemporalType temporalType);
}

View File

@ -0,0 +1,54 @@
/*
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
* Haulmont Technology proprietary and confidential.
* Use is subject to license terms.
* Author: Konstantin Krivopustov
* Created: 13.11.2008 11:26:51
*
* $Id$
*/
package com.haulmont.cuba.core.ejb;
import com.haulmont.cuba.core.entity.BaseEntity;
import com.haulmont.cuba.core.intf.BasicInvocationContext;
import com.haulmont.cuba.core.intf.BasicService;
import com.haulmont.cuba.core.intf.BasicServiceRemote;
import com.haulmont.cuba.core.Locator;
import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
import java.util.List;
@Stateless(name = BasicService.JNDI_NAME)
@Interceptors({ServiceInterceptor.class})
public class BasicServiceBean implements BasicService, BasicServiceRemote
{
private BasicWorker getBasicWorker() {
return Locator.lookupLocal(BasicWorker.JNDI_NAME);
}
public <T extends BaseEntity> T create(T entity) {
return getBasicWorker().create(entity);
}
public <T extends BaseEntity> T update(T entity) {
return getBasicWorker().update(entity);
}
public void delete(BasicInvocationContext ctx) {
getBasicWorker().delete(ctx);
}
public <T extends BaseEntity> T get(BasicInvocationContext ctx) {
return (T) getBasicWorker().get(ctx);
}
public <T extends BaseEntity> T load(BasicInvocationContext ctx) {
return (T) getBasicWorker().load(ctx);
}
public <T extends BaseEntity> List<T> loadList(BasicInvocationContext ctx) {
return getBasicWorker().loadList(ctx);
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
* Haulmont Technology proprietary and confidential.
* Use is subject to license terms.
* Author: Konstantin Krivopustov
* Created: 13.11.2008 15:28:22
*
* $Id$
*/
package com.haulmont.cuba.core.ejb;
import com.haulmont.cuba.core.entity.BaseEntity;
import com.haulmont.cuba.core.intf.BasicInvocationContext;
import javax.ejb.Local;
import java.util.List;
@Local
public interface BasicWorker
{
String JNDI_NAME = "cuba/core/BasicWorker";
<T extends BaseEntity> T create(T entity);
<T extends BaseEntity> T update(T entity);
void delete(BasicInvocationContext ctx);
<T extends BaseEntity> T get(BasicInvocationContext ctx);
<T extends BaseEntity> T load(BasicInvocationContext ctx);
<T extends BaseEntity> List<T> loadList(BasicInvocationContext ctx);
}

View File

@ -0,0 +1,66 @@
/*
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
* Haulmont Technology proprietary and confidential.
* Use is subject to license terms.
* Author: Konstantin Krivopustov
* Created: 13.11.2008 15:34:56
*
* $Id$
*/
package com.haulmont.cuba.core.ejb;
import com.haulmont.cuba.core.entity.BaseEntity;
import com.haulmont.cuba.core.intf.BasicInvocationContext;
import com.haulmont.cuba.core.EntityManagerAdapter;
import com.haulmont.cuba.core.PersistenceProvider;
import com.haulmont.cuba.core.QueryAdapter;
import javax.ejb.Stateless;
import java.util.List;
@Stateless(name = BasicWorker.JNDI_NAME)
public class BasicWorkerBean implements BasicWorker
{
public <T extends BaseEntity> T create(T entity) {
EntityManagerAdapter em = PersistenceProvider.getEntityManager();
em.persist(entity);
return entity;
}
public <T extends BaseEntity> T update(T entity) {
EntityManagerAdapter em = PersistenceProvider.getEntityManager();
T result = em.merge(entity);
return result;
}
public void delete(BasicInvocationContext ctx) {
EntityManagerAdapter em = PersistenceProvider.getEntityManager();
BaseEntity entity = em.find(ctx.getEntityClass(), ctx.getId());
em.remove(entity);
}
public <T extends BaseEntity> T get(BasicInvocationContext ctx) {
EntityManagerAdapter em = PersistenceProvider.getEntityManager();
BaseEntity result = em.find(ctx.getEntityClass(), ctx.getId());
return (T) result;
}
public <T extends BaseEntity> T load(BasicInvocationContext ctx) {
EntityManagerAdapter em = PersistenceProvider.getEntityManager();
String queryString =
"select e from " +
PersistenceProvider.getEntityName(ctx.getEntityClass()) + " e where e.id = ?1";
QueryAdapter query = em.createQuery(queryString);
query.setParameter(1, ctx.getId());
Object result = query.getSingleResult();
return (T) result;
}
public <T extends BaseEntity> List<T> loadList(BasicInvocationContext ctx) {
EntityManagerAdapter em = PersistenceProvider.getEntityManager();
QueryAdapter query = em.createQuery(ctx.getQueryString());
List resultList = query.getResultList();
return resultList;
}
}

View File

@ -0,0 +1,31 @@
/*
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
* Haulmont Technology proprietary and confidential.
* Use is subject to license terms.
* Author: Konstantin Krivopustov
* Created: 13.11.2008 14:13:23
*
* $Id$
*/
package com.haulmont.cuba.core.ejb;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
public class ServiceInterceptor
{
@AroundInvoke
private Object aroundInvoke(InvocationContext ctx) throws Exception {
try {
return ctx.proceed();
} catch (Exception e) {
Log log = LogFactory.getLog(ctx.getTarget().getClass());
log.error("ServiceInterceptor caught exception: ", e);
throw e;
}
}
}

View File

@ -14,7 +14,7 @@ import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.EntityListeners;
@Entity
@Entity(name = "core$Server")
@Table(name = "SYS_SERVER")
public class Server extends StandardEntity
{

View File

@ -10,13 +10,18 @@
package com.haulmont.cuba.core.impl;
import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.haulmont.cuba.core.EntityManagerAdapter;
import com.haulmont.cuba.core.SecurityProvider;
import com.haulmont.cuba.core.QueryAdapter;
import com.haulmont.cuba.core.entity.BaseEntity;
import com.haulmont.cuba.core.entity.DeleteDeferred;
public class EntityManagerAdapterImpl implements EntityManagerAdapter
{
private Log log = LogFactory.getLog(EntityManagerAdapterImpl.class);
private OpenJPAEntityManager jpaEm;
EntityManagerAdapterImpl(OpenJPAEntityManager jpaEntityManager) {
@ -45,6 +50,11 @@ public class EntityManagerAdapterImpl implements EntityManagerAdapter
return jpaEm.find(clazz, key);
}
public QueryAdapter createQuery(String qlStr) {
log.debug("Creating JPQL query: " + qlStr);
return new QueryAdapterImpl(jpaEm.createQuery(qlStr));
}
public void flush() {
jpaEm.flush();
}

View File

@ -42,6 +42,15 @@ public class LocatorImpl extends Locator
}
}
protected Object __lookupRemote(String name) {
Context ctx = __getJndiContextImpl();
try {
return ctx.lookup(name + "/remote");
} catch (NamingException e) {
throw new RuntimeException(e);
}
}
protected TransactionAdapter __createTransaction() {
Context ctx = __getJndiContextImpl();
TransactionManager tm;

View File

@ -108,7 +108,7 @@ public class ManagedPersistenceProvider extends PersistenceProvider
}
else {
log.trace("Creating new non-transactional EntityManager");
em = getEntityManagerFactory().createEntityManager();
em = __getEntityManagerFactory().createEntityManager();
}
return em;
} catch (NamingException e) {

View File

@ -0,0 +1,66 @@
/*
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
* Haulmont Technology proprietary and confidential.
* Use is subject to license terms.
* Author: Konstantin Krivopustov
* Created: 13.11.2008 18:15:55
*
* $Id$
*/
package com.haulmont.cuba.core.impl;
import com.haulmont.cuba.core.QueryAdapter;
import javax.persistence.TemporalType;
import java.util.List;
import java.util.Date;
import org.apache.openjpa.persistence.OpenJPAQuery;
public class QueryAdapterImpl implements QueryAdapter
{
private OpenJPAQuery query;
public QueryAdapterImpl(OpenJPAQuery query) {
this.query = query;
}
public List getResultList() {
return query.getResultList();
}
public Object getSingleResult() {
return query.getSingleResult();
}
public QueryAdapter setMaxResults(int maxResult) {
query.setMaxResults(maxResult);
return this;
}
public QueryAdapter setFirstResult(int startPosition) {
query.setFirstResult(startPosition);
return this;
}
public QueryAdapter setParameter(String name, Object value) {
query.setParameter(name, value);
return this;
}
public QueryAdapter setParameter(String name, Date value, TemporalType temporalType) {
query.setParameter(name, value, temporalType);
return this;
}
public QueryAdapter setParameter(int position, Object value) {
query.setParameter(position, value);
return this;
}
public QueryAdapter setParameter(int position, Date value, TemporalType temporalType) {
query.setParameter(position, value, temporalType);
return this;
}
}

View File

@ -0,0 +1,51 @@
/*
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
* Haulmont Technology proprietary and confidential.
* Use is subject to license terms.
* Author: Konstantin Krivopustov
* Created: 13.11.2008 11:13:41
*
* $Id$
*/
package com.haulmont.cuba.core.intf;
import com.haulmont.cuba.core.entity.BaseEntity;
import java.io.Serializable;
public class BasicInvocationContext implements Serializable
{
private Class<? extends BaseEntity> entityClass;
private Object id;
private String queryString;
public Class<? extends BaseEntity> getEntityClass() {
return entityClass;
}
public BasicInvocationContext setEntityClass(Class<? extends BaseEntity> entityClass) {
this.entityClass = entityClass;
return this;
}
public Object getId() {
return id;
}
public BasicInvocationContext setId(Object id) {
this.id = id;
return this;
}
public String getQueryString() {
return queryString;
}
public BasicInvocationContext setQueryString(String queryString) {
this.queryString = queryString;
return this;
}
}

View File

@ -0,0 +1,19 @@
/*
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
* Haulmont Technology proprietary and confidential.
* Use is subject to license terms.
* Author: Konstantin Krivopustov
* Created: 13.11.2008 11:35:32
*
* $Id$
*/
package com.haulmont.cuba.core.intf;
import javax.ejb.Local;
@Local
public interface BasicService extends BasicServiceRemote
{
String JNDI_NAME = "cuba/core/BasicService";
}

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
* Haulmont Technology proprietary and confidential.
* Use is subject to license terms.
* Author: Konstantin Krivopustov
* Created: 13.11.2008 11:13:20
*
* $Id$
*/
package com.haulmont.cuba.core.intf;
import com.haulmont.cuba.core.entity.BaseEntity;
import javax.ejb.Remote;
import java.util.List;
@Remote
public interface BasicServiceRemote
{
<T extends BaseEntity> T create(T entity);
<T extends BaseEntity> T update(T entity);
void delete(BasicInvocationContext ctx);
<T extends BaseEntity> T get(BasicInvocationContext ctx);
<T extends BaseEntity> T load(BasicInvocationContext ctx);
<T extends BaseEntity> List<T> loadList(BasicInvocationContext ctx);
}

View File

@ -0,0 +1,85 @@
/*
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
* Haulmont Technology proprietary and confidential.
* Use is subject to license terms.
* Author: Konstantin Krivopustov
* Created: 13.11.2008 14:25:43
*
* $Id$
*/
package com.haulmont.cuba.core;
import com.haulmont.cuba.core.intf.BasicService;
import com.haulmont.cuba.core.intf.BasicInvocationContext;
import com.haulmont.cuba.core.intf.BasicServiceRemote;
import com.haulmont.cuba.core.entity.Server;
import java.util.UUID;
import java.util.List;
public class BasicServiceTest extends CubaTestCase
{
public void test() {
BasicService bs = Locator.lookupLocal(BasicService.JNDI_NAME);
Server server = new Server();
UUID id = server.getId();
server.setName("localhost");
server.setAddress("127.0.0.1");
server.setRunning(true);
bs.create(server);
server = bs.get(new BasicInvocationContext().setEntityClass(Server.class).setId(id));
assertEquals("localhost", server.getName());
server.setName("krivopustov");
bs.update(server);
}
public void testRemoteWithException() {
BasicServiceRemote bs = Locator.lookupRemote(BasicService.JNDI_NAME);
Object id = "some key";
try {
bs.get(new BasicInvocationContext().setEntityClass(Server.class).setId(id));
fail();
} catch (Exception e) {
System.out.println("Done");
}
}
public void testLoad() {
BasicService bs = Locator.lookupLocal(BasicService.JNDI_NAME);
Server server = new Server();
UUID id = server.getId();
server.setName("localhost");
server.setAddress("127.0.0.1");
server.setRunning(true);
bs.create(server);
server = bs.load(new BasicInvocationContext().setEntityClass(Server.class).setId(id));
assertEquals("localhost", server.getName());
}
public void testLoadList() {
BasicService bs = Locator.lookupLocal(BasicService.JNDI_NAME);
Server server = new Server();
server.setName("localhost");
server.setAddress("127.0.0.1");
server.setRunning(true);
bs.create(server);
BasicInvocationContext ctx = new BasicInvocationContext()
.setEntityClass(Server.class)
.setQueryString("select s from " + PersistenceProvider.getEntityName(Server.class) + " s");
List<Server> list = bs.loadList(ctx);
assertTrue(list.size() > 0);
}
}

View File

@ -26,10 +26,15 @@
<!--
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
-->
<param name="ConversionPattern" value="%-5p %d{dd-MM HH:mm:ss,SSS} (%F:%M:%L) -%m%n"/>
<!--<param name="ConversionPattern" value="%-5p %d{dd-MM HH:mm:ss,SSS} (%F:%M:%L) -%m%n"/>-->
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
<category name="org.jboss">
<priority value="INFO"/>
</category>
<root>
<appender-ref ref="CONSOLE"/>
</root>