1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */

17 package org.apache.tomcat.util.http;
18
19 import org.apache.tomcat.util.res.StringManager;
20
21 /**
22  * This class is not thread-safe.
23  */

24 public class ServerCookies {
25
26     private static final StringManager sm = StringManager.getManager(ServerCookies.class);
27
28     private ServerCookie[] serverCookies;
29
30     private int cookieCount = 0;
31     private int limit = 200;
32
33
34     public ServerCookies(int initialSize) {
35         serverCookies = new ServerCookie[initialSize];
36     }
37
38
39     /**
40      * Register a new, initialized cookie. Cookies are recycled, and most of the
41      * time an existing ServerCookie object is returned. The caller can set the
42      * name/value and attributes for the cookie.
43      * @return the new cookie
44      */

45     public ServerCookie addCookie() {
46         if (limit > -1 && cookieCount >= limit) {
47             throw new IllegalArgumentException(
48                     sm.getString("cookies.maxCountFail", Integer.valueOf(limit)));
49         }
50
51         if (cookieCount >= serverCookies.length) {
52             int newSize = limit > -1 ? Math.min(2*cookieCount, limit) : 2*cookieCount;
53             ServerCookie scookiesTmp[] = new ServerCookie[newSize];
54             System.arraycopy(serverCookies, 0, scookiesTmp, 0, cookieCount);
55             serverCookies = scookiesTmp;
56         }
57
58         ServerCookie c = serverCookies[cookieCount];
59         if (c == null) {
60             c = new ServerCookie();
61             serverCookies[cookieCount] = c;
62         }
63         cookieCount++;
64         return c;
65     }
66
67
68     public ServerCookie getCookie(int idx) {
69         return serverCookies[idx];
70     }
71
72
73     public int getCookieCount() {
74         return cookieCount;
75     }
76
77
78     public void setLimit(int limit) {
79         this.limit = limit;
80         if (limit > -1 && serverCookies.length > limit && cookieCount <= limit) {
81             // shrink cookie list array
82             ServerCookie scookiesTmp[] = new ServerCookie[limit];
83             System.arraycopy(serverCookies, 0, scookiesTmp, 0, cookieCount);
84             serverCookies = scookiesTmp;
85         }
86     }
87
88
89     public void recycle() {
90         for (int i = 0; i < cookieCount; i++) {
91             serverCookies[i].recycle();
92         }
93         cookieCount = 0;
94     }
95 }
96