Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / server / webdav / milton / GssLockManager.java @ 739df99e

History | View | Annotate | Download (5.1 kB)

1
/*
2
 * Copyright 2011 Electronic Business Systems Ltd.
3
 *
4
 * This file is part of GSS.
5
 *
6
 * GSS is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * GSS is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
package gr.ebs.gss.server.webdav.milton;
20

    
21

    
22

    
23
import static gr.ebs.gss.server.configuration.GSSConfigurationFactory.getConfiguration;
24
import gr.ebs.gss.client.exceptions.RpcException;
25
import gr.ebs.gss.server.domain.GssLock;
26
import gr.ebs.gss.server.ejb.ExternalAPI;
27

    
28

    
29
import java.util.Date;
30
import java.util.HashMap;
31
import java.util.Map;
32
import java.util.UUID;
33

    
34
import javax.naming.Context;
35
import javax.naming.InitialContext;
36
import javax.naming.NamingException;
37
import javax.rmi.PortableRemoteObject;
38

    
39
import org.slf4j.Logger;
40
import org.slf4j.LoggerFactory;
41

    
42
import com.bradmcevoy.http.LockInfo;
43
import com.bradmcevoy.http.LockResult;
44
import com.bradmcevoy.http.LockTimeout;
45
import com.bradmcevoy.http.LockToken;
46
import com.bradmcevoy.http.LockableResource;
47
import com.bradmcevoy.http.exceptions.NotAuthorizedException;
48
import com.ettrema.http.fs.LockManager;
49

    
50

    
51
/**
52
 * @author kman
53
 *
54
 */
55
public class GssLockManager implements LockManager {
56

    
57
    private static final Logger log = LoggerFactory.getLogger( GssLockManager.class );
58
    /**
59
     * maps current locks by the file associated with the resource
60
     */
61
    
62
    public GssLockManager() {
63

    
64
    }
65

    
66
    public synchronized LockResult lock( LockTimeout timeout, LockInfo lockInfo, LockableResource r ) {
67
        GssResource resource = (GssResource) r;
68
        LockToken currentLock = currentLock( resource );
69
        if( currentLock != null ) {
70
            return LockResult.failed( LockResult.FailureReason.ALREADY_LOCKED );
71
        }
72

    
73
        LockToken newToken = new LockToken( UUID.randomUUID().toString(), lockInfo, timeout );
74
        GssLock newLock = new GssLock( resource.getUniqueId(), newToken);
75
        getService().saveOrUpdateLock(newLock);
76
        return LockResult.success( newToken );
77
    }
78

    
79
    public synchronized LockResult refresh( String tokenId, LockableResource resource ) {
80
        GssLock curLock = getService().getLockByToken(tokenId);
81
        if( curLock == null ) {
82
            log.debug( "can't refresh because no lock");
83
            return LockResult.failed( LockResult.FailureReason.PRECONDITION_FAILED );
84
        } else {
85
            curLock.setFrom( new Date() );
86
            return LockResult.success( curLock.toToken() );
87
        }
88
    }
89

    
90
    public synchronized void unlock( String tokenId, LockableResource r ) throws NotAuthorizedException {
91
            GssResource resource = (GssResource) r;
92
        LockToken lockToken = currentLock( resource );
93
        if( lockToken == null ) {
94
            log.debug( "not locked" );
95
            return;
96
        }
97
        if( lockToken.tokenId.equals( tokenId ) ) {
98
            removeLock( lockToken );
99
        } else {
100
            throw new NotAuthorizedException( resource );
101
        }
102
    }
103

    
104
    private LockToken currentLock( GssResource resource ) {
105
        GssLock curLock = getService().getLockById(resource.getUniqueId());
106
        if( curLock == null ) return null;
107
        LockToken token = curLock.toToken();
108
        if( token.isExpired() ) {
109
            removeLock( token );
110
            return null;
111
        } else {
112
            return token;
113
        }
114
    }
115

    
116
    private void removeLock( LockToken token ) {
117
        log.debug( "removeLock: " + token.tokenId );
118
        GssLock currentLock = getService().getLockByToken(token.tokenId);
119
        if( currentLock != null ) {
120
            getService().removeLock(currentLock);
121
        } else {
122
            log.warn( "couldnt find lock: " + token.tokenId );
123
        }
124
    }
125

    
126
    public LockToken getCurrentToken( LockableResource r ) {
127
            GssResource resource = (GssResource) r;
128
        GssLock lock = getService().getLockById( resource.getUniqueId() );
129
        if( lock == null ) return null;
130
        LockToken token = new LockToken();
131
        token.info = new LockInfo( LockInfo.LockScope.EXCLUSIVE, LockInfo.LockType.WRITE, lock.lockedByUser, LockInfo.LockDepth.ZERO );
132
        token.info.lockedByUser = lock.lockedByUser;
133
        token.timeout = lock.toToken().timeout;
134
        token.tokenId = lock.getTokenId();
135
        return token;
136
    }
137

    
138
    protected ExternalAPI getService() throws RuntimeException {
139
                try {
140
                        final Context ctx = new InitialContext();
141
                        final Object ref = ctx.lookup(getConfiguration().getString("externalApiPath"));
142
                        return (ExternalAPI) PortableRemoteObject.narrow(ref, ExternalAPI.class);
143
                } catch (final NamingException e) {
144
                        log.error("Unable to retrieve the ExternalAPI EJB", e);
145
                        throw new RuntimeException("An error occurred while contacting the naming service");
146
                }
147
        }
148

    
149
}