Hi,

Extract the locking managing code in a service.
Then use Spring/CDI/Guice/... to manage the service dependencies for you.

Martin Grigorov
Wicket Training and Consulting


On Mon, Feb 3, 2014 at 2:38 PM, ChambreNoire <a...@tentelemed.com> wrote:

> Hey,
>
> I've just been sketching out an entity locking mechanism (below) to prevent
> users from editing entities which another user is already in the process of
> editing. It works fine but the problem is that placing this in my
> WebApplication class means that I need to make MyApplication.get() call
> from
> within my service layer and this seems a tad messy. Is this a Wicket
> cardinal sin or am I just stressing over nothing?
>
> Cheers,
>
> Chambre
>
>     class EntityLock {
>
>         public String entityId;
>         public String entityName;
>
>         public String sessionId;
>
>         public Date locked;
>         public Date lastBumped;
>     }
>
>     @SuppressWarnings("unchecked")
>     private Map<String, EntityLock> getEntityLocksByEntity() {
>
>         HashMap<String, EntityLock> locksByEntity = (HashMap<String,
> EntityLock>) getMetaData(ENTITY_LOCKS_BY_ENTITY);
>
>         if (locksByEntity == null) {
>             locksByEntity = new HashMap<String, EntityLock>();
>
>             setMetaData(ENTITY_LOCKS_BY_ENTITY, locksByEntity);
>         }
>         return locksByEntity;
>     }
>
>     public boolean acquireEntityLock(PersistentEntity entity) {
>
>         EntityLock lock = getEntityLocksByEntity().get(entity.getId());
>
>         if (lock != null) {
>             if ((System.currentTimeMillis() - lock.lastBumped.getTime()) >
> (1 * 60 * 1000)) {
>
>                 releaseEntityLock(entity);
>             } else {
>                 return false;
>             }
>         }
>
>         getEntityLocksByEntity().put(entity.getId(),
> newEntityLock(entity));
>
>         return true;
>     }
>
>     private EntityLock newEntityLock(PersistentEntity entity) {
>
>         EntityLock newLock = new EntityLock();
>
>         newLock.entityId = entity.getId();
>
>         if (entity instanceof HibernateProxy) {
>             newLock.entityName = ((HibernateProxy)
> entity).getHibernateLazyInitializer().getEntityName();
>         } else {
>             newLock.entityName = entity.getClass().getName();
>         }
>
>         newLock.sessionId = getSession().getId();
>
>         Date now = new Date();
>
>         newLock.locked = now;
>         newLock.lastBumped = now;
>
>         return newLock;
>     }
>
>     public void releaseEntityLock(PersistentEntity entity) {
>
>         getEntityLocksByEntity().remove(entity.getId());
>     }
>
>     public boolean isEntityLocked(PersistentEntity entity) {
>
>         return getEntityLocksByEntity().get(entity.getId()) != null;
>     }
>
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Accessing-WebApplication-from-the-service-layer-tp4664145.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> For additional commands, e-mail: users-h...@wicket.apache.org
>
>

Reply via email to