View | Details | Raw Unified | Return to bug 51919
Collapse All | Expand All

(-)src/protocol/http/org/apache/jmeter/protocol/http/control/CookieManager.java (-27 / +58 lines)
Lines 30-35 Link Here
30
import java.net.URL;
30
import java.net.URL;
31
import java.util.ArrayList;
31
import java.util.ArrayList;
32
import java.util.Date;
32
import java.util.Date;
33
import java.util.concurrent.locks.ReentrantLock;
33
34
34
import org.apache.commons.httpclient.cookie.CookiePolicy;
35
import org.apache.commons.httpclient.cookie.CookiePolicy;
35
import org.apache.commons.httpclient.cookie.CookieSpec;
36
import org.apache.commons.httpclient.cookie.CookieSpec;
Lines 101-106 Link Here
101
102
102
    public static final String DEFAULT_POLICY = CookiePolicy.BROWSER_COMPATIBILITY;
103
    public static final String DEFAULT_POLICY = CookiePolicy.BROWSER_COMPATIBILITY;
103
104
105
    private final ReentrantLock CONTEXT_VARIABLES_LOCK = new ReentrantLock();
106
107
    private final ReentrantLock COOKIE_MANAGER_UPDATE_LOCK = new ReentrantLock();
108
104
    public CookieManager() {
109
    public CookieManager() {
105
        clearCookies(); // Ensure that there is always a collection available
110
        clearCookies(); // Ensure that there is always a collection available
106
    }
111
    }
Lines 153-158 Link Here
153
        writer.println("# JMeter generated Cookie file");// $NON-NLS-1$
158
        writer.println("# JMeter generated Cookie file");// $NON-NLS-1$
154
        PropertyIterator cookies = getCookies().iterator();
159
        PropertyIterator cookies = getCookies().iterator();
155
        long now = System.currentTimeMillis();
160
        long now = System.currentTimeMillis();
161
        // No need for sync as not called during test plan
156
        while (cookies.hasNext()) {
162
        while (cookies.hasNext()) {
157
            Cookie cook = (Cookie) cookies.next().getObjectValue();
163
            Cookie cook = (Cookie) cookies.next().getObjectValue();
158
            final long expiresMillis = cook.getExpiresMillis();
164
            final long expiresMillis = cook.getExpiresMillis();
Lines 183-188 Link Here
183
        // N.B. this must agree with the save() and cookieToString() methods
189
        // N.B. this must agree with the save() and cookieToString() methods
184
        String line;
190
        String line;
185
        try {
191
        try {
192
            // No need for sync as not called during load test
186
            final CollectionProperty cookies = getCookies();
193
            final CollectionProperty cookies = getCookies();
187
            while ((line = reader.readLine()) != null) {
194
            while ((line = reader.readLine()) != null) {
188
                try {
195
                try {
Lines 265-275 Link Here
265
            if (log.isDebugEnabled()) {
272
            if (log.isDebugEnabled()) {
266
                log.debug("Add cookie to store " + c.toString());
273
                log.debug("Add cookie to store " + c.toString());
267
            }
274
            }
268
            getCookies().addItem(c);
275
            try {
276
                COOKIE_MANAGER_UPDATE_LOCK.lock();
277
                getCookies().addItem(c);
278
            } finally {
279
                COOKIE_MANAGER_UPDATE_LOCK.unlock();
280
            }
269
            if (SAVE_COOKIES)  {
281
            if (SAVE_COOKIES)  {
270
                JMeterContext context = getThreadContext();
282
                JMeterContext context = getThreadContext();
271
                if (context.isSamplingStarted()) {
283
                if (context.isSamplingStarted()) {
272
                    context.getVariables().put(COOKIE_NAME_PREFIX+cn, cv);
284
                    try {
285
                        // If we do not sync Map inside Variables might be corrupted, 
286
                        // note that this may happen from other callers (as initially 
287
                        // JMeterVariables are not supposed to be shared
288
                        CONTEXT_VARIABLES_LOCK.lock();
289
                        context.getVariables().put(COOKIE_NAME_PREFIX+cn, cv);                        
290
                    } finally {
291
                        CONTEXT_VARIABLES_LOCK.unlock();
292
                    }
273
                }
293
                }
274
            }
294
            }
275
        }
295
        }
Lines 332-351 Link Here
332
     *
352
     *
333
     */
353
     */
334
    public org.apache.commons.httpclient.Cookie[] getCookiesForUrl(URL url){
354
    public org.apache.commons.httpclient.Cookie[] getCookiesForUrl(URL url){
335
        CollectionProperty jar=getCookies();
355
        org.apache.commons.httpclient.Cookie[] cookies= null;
336
        org.apache.commons.httpclient.Cookie cookies[]=
356
        try {
337
            new org.apache.commons.httpclient.Cookie[jar.size()];
357
            COOKIE_MANAGER_UPDATE_LOCK.lock();
338
        int i=0;
358
            CollectionProperty jar=getCookies();
339
        for (PropertyIterator iter = getCookies().iterator(); iter.hasNext();) {
359
            cookies =
340
            Cookie jmcookie = (Cookie) iter.next().getObjectValue();
360
                new org.apache.commons.httpclient.Cookie[jar.size()];
341
            // Set to running version, to allow function evaluation for the cookie values (bug 28715)
361
            int i=0;
342
            if (ALLOW_VARIABLE_COOKIES) {
362
            for (PropertyIterator iter = getCookies().iterator(); iter.hasNext();) {
343
                jmcookie.setRunningVersion(true);
363
                Cookie jmcookie = (Cookie) iter.next().getObjectValue();
344
            }
364
                // Set to running version, to allow function evaluation for the cookie values (bug 28715)
345
            cookies[i++] = makeCookie(jmcookie);
365
                if (ALLOW_VARIABLE_COOKIES) {
346
            if (ALLOW_VARIABLE_COOKIES) {
366
                    jmcookie.setRunningVersion(true);
347
                jmcookie.setRunningVersion(false);
367
                }
368
                cookies[i++] = makeCookie(jmcookie);
369
                if (ALLOW_VARIABLE_COOKIES) {
370
                    jmcookie.setRunningVersion(false);
371
                }
348
            }
372
            }
373
        } finally {
374
            COOKIE_MANAGER_UPDATE_LOCK.unlock();
349
        }
375
        }
350
        String host = url.getHost();
376
        String host = url.getHost();
351
        String protocol = url.getProtocol();
377
        String protocol = url.getProtocol();
Lines 461-479 Link Here
461
487
462
    private void removeMatchingCookies(Cookie newCookie){
488
    private void removeMatchingCookies(Cookie newCookie){
463
        // Scan for any matching cookies
489
        // Scan for any matching cookies
464
        PropertyIterator iter = getCookies().iterator();
490
        try {
465
        while (iter.hasNext()) {
491
            COOKIE_MANAGER_UPDATE_LOCK.lock();
466
            Cookie cookie = (Cookie) iter.next().getObjectValue();
492
            PropertyIterator iter = getCookies().iterator();
467
            if (cookie == null) {// TODO is this possible?
493
            while (iter.hasNext()) {
468
                continue;
494
                Cookie cookie = (Cookie) iter.next().getObjectValue();
469
            }
495
                if (cookie == null) {// TODO is this possible?
470
            if (match(cookie,newCookie)) {
496
                    continue;
471
                if (log.isDebugEnabled()) {
472
                    log.debug("New Cookie = " + newCookie.toString()
473
                              + " removing matching Cookie " + cookie.toString());
474
                }
497
                }
475
                iter.remove();
498
                if (match(cookie,newCookie)) {
476
            }
499
                    if (log.isDebugEnabled()) {
500
                        log.debug("New Cookie = " + newCookie.toString()
501
                                  + " removing matching Cookie " + cookie.toString());
502
                    }
503
                    iter.remove();
504
                }
505
            }   
506
        } finally {
507
            COOKIE_MANAGER_UPDATE_LOCK.unlock();
477
        }
508
        }
478
    }
509
    }
479
510

Return to bug 51919