You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by ChambreNoire <aw...@tentelemed.com> on 2014/02/03 14:38:53 UTC

Accessing WebApplication from the service layer

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-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Accessing WebApplication from the service layer

Posted by ChambreNoire <aw...@tentelemed.com>.
Yup that's exactly what I have done. My problem lies with the lock creation
in newEntityLock(PersistentEntity entity) which needs the session Id...

Chambre

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Accessing-WebApplication-from-the-service-layer-tp4664145p4664152.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Accessing WebApplication from the service layer

Posted by Martin Grigorov <mg...@apache.org>.
class MyApp extends WebApplication {

@Inject EntityLockService lockService;

@Override
public void sessionUnbound(String sessionId) {
   lockService.cleanup(sessionId);
}
}

Martin Grigorov
Wicket Training and Consulting


On Mon, Feb 3, 2014 at 3:09 PM, ChambreNoire <aw...@tentelemed.com> wrote:

> Yes however I also store the id of the session that owns the lock in a map
> so
> that I can easily flush locks when a user session expires. This would mean
> that EntityLockService would need access to the current WebSession which
> also seems messy...
>
> Chambre
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Accessing-WebApplication-from-the-service-layer-tp4664145p4664149.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: Accessing WebApplication from the service layer

Posted by ChambreNoire <aw...@tentelemed.com>.
Yes however I also store the id of the session that owns the lock in a map so
that I can easily flush locks when a user session expires. This would mean
that EntityLockService would need access to the current WebSession which
also seems messy...

Chambre

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Accessing-WebApplication-from-the-service-layer-tp4664145p4664149.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Accessing WebApplication from the service layer

Posted by Bas Gooren <ba...@iswd.nl>.
Well, you can either store it as application metadata or make your 
locking service implementation a singleton (managed by your dependency 
injection framework of choice).

Either way, you abstract away the exact storage location of your locks 
behind the facade of your EntityLockingService.

Met vriendelijke groet,
Kind regards,

Bas Gooren

schreef ChambreNoire op 3-2-2014 14:48:
> Hello,
>
> OK so no storing my Map<String, EntityLock> in the Application MetaData
> then?
>
> Many thanks,
>
> Chambre
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/Accessing-WebApplication-from-the-service-layer-tp4664145p4664147.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>


Re: Accessing WebApplication from the service layer

Posted by ChambreNoire <aw...@tentelemed.com>.
Hello,

OK so no storing my Map<String, EntityLock> in the Application MetaData
then?

Many thanks,

Chambre

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Accessing-WebApplication-from-the-service-layer-tp4664145p4664147.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Accessing WebApplication from the service layer

Posted by Martin Grigorov <mg...@apache.org>.
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 <aw...@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-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>