001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Boris Kuznetsov
020: * @version $Revision$
021: */package org.apache.harmony.xnet.provider.jsse;
022:
023: import java.util.Arrays;
024: import java.util.Enumeration;
025: import java.util.Hashtable;
026:
027: import javax.net.ssl.SSLSession;
028: import javax.net.ssl.SSLSessionContext;
029:
030: /**
031: *
032: * SSLSessionContext implementation
033: * @see javax.net.ssl.SSLSessionContext
034: */
035: public class SSLSessionContextImpl implements SSLSessionContext {
036:
037: private int cacheSize = 0;
038:
039: private long timeout = 0;
040:
041: private final Hashtable sessions = new Hashtable();
042:
043: /**
044: *
045: * @see javax.net.ssl.SSLSessionContext.getIds()
046: */
047: public Enumeration getIds() {
048: return new Enumeration() {
049: Enumeration keys = sessions.keys();
050:
051: public boolean hasMoreElements() {
052: return keys.hasMoreElements();
053: }
054:
055: public Object nextElement() {
056: return ((IdKey) keys.nextElement()).id;
057: }
058: };
059: }
060:
061: /**
062: *
063: * @see javax.net.ssl.SSLSessionContext.getSession(byte[] sessionId)
064: */
065: public SSLSession getSession(byte[] sessionId) {
066: return (SSLSession) sessions.get(new IdKey(sessionId));
067: }
068:
069: /**
070: * @see javax.net.ssl.SSLSessionContext.getSessionCacheSize()
071: */
072: public int getSessionCacheSize() {
073: return cacheSize;
074: }
075:
076: /**
077: * @see javax.net.ssl.SSLSessionContext.getSessionTimeout()
078: */
079: public int getSessionTimeout() {
080: return (int) (timeout / 1000);
081: }
082:
083: /**
084: * @see javax.net.ssl.SSLSessionContext.setSessionCacheSize(int size)
085: */
086: public void setSessionCacheSize(int size)
087: throws IllegalArgumentException {
088: if (size < 0) {
089: throw new IllegalArgumentException("size < 0");
090: }
091: cacheSize = size;
092: if (size > 0 && sessions.size() < size) {
093: // remove size-sessions.size() oldest sessions
094: removeOldest(size - sessions.size());
095: }
096:
097: }
098:
099: /**
100: * @see javax.net.ssl.SSLSessionContext.setSessionTimeout(int seconds)
101: */
102: public void setSessionTimeout(int seconds)
103: throws IllegalArgumentException {
104: if (seconds < 0) {
105: throw new IllegalArgumentException("seconds < 0");
106: }
107: timeout = seconds * 1000;
108:
109: // Check timeouts and remome expired sessions
110: SSLSessionImpl ses;
111: for (Enumeration en = sessions.keys(); en.hasMoreElements();) {
112: ses = (SSLSessionImpl) (sessions.get(en.nextElement()));
113: if (!ses.isValid()) {
114: sessions.remove(ses.getId());
115: }
116: }
117: }
118:
119: /**
120: * Adds session to the session cach
121: * @param ses
122: */
123: void putSession(SSLSessionImpl ses) {
124: if (cacheSize > 0 && sessions.size() == cacheSize) {
125: // remove 1 oldest session
126: removeOldest(1);
127: }
128: ses.context = this ;
129: sessions.put(new IdKey(ses.getId()), ses);
130: }
131:
132: // removes invalidated/oldest sessions from the session cache
133: private void removeOldest(int num) {
134: //TODO
135: // ses.context = null;
136: }
137:
138: private class IdKey {
139: private byte[] id;
140:
141: private IdKey(byte[] id) {
142: this .id = id;
143: }
144:
145: public boolean equals(Object o) {
146: if (!(o instanceof IdKey)) {
147: return false;
148: }
149: return Arrays.equals(id, ((IdKey) o).id);
150: }
151:
152: public int hashCode() {
153: // TODO uncomment for 1.5
154: // return Arrays.hashCode(id);
155: int hash = 0;
156: for (int i = 0; i < id.length; i++) {
157: hash += id[i];
158: }
159: return hash;
160: }
161: }
162:
163: }
|