001 package org.cumulus4j.keymanager.api.internal.local;
002
003 import java.io.IOException;
004
005 import org.cumulus4j.keymanager.AppServer;
006 import org.cumulus4j.keymanager.api.AuthenticationException;
007 import org.cumulus4j.keymanager.api.CryptoSession;
008
009 /**
010 * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
011 */
012 public class LocalCryptoSession implements CryptoSession
013 {
014 private LocalKeyManagerAPI localKeyManagerAPI;
015
016 private AppServer appServer;
017
018 public LocalCryptoSession(LocalKeyManagerAPI localKeyManagerAPI, AppServer appServer)
019 {
020 if (localKeyManagerAPI == null)
021 throw new IllegalArgumentException("localKeyManagerAPI == null");
022
023 this.localKeyManagerAPI = localKeyManagerAPI;
024 this.appServer = appServer;
025 }
026
027 @Override
028 public String getAppServerID() {
029 return appServer.getAppServerID();
030 }
031
032 @Override
033 public String getAppServerBaseURL() {
034 return appServer.getAppServerBaseURL();
035 }
036
037 // @Override
038 // public String getCryptoSessionID() throws AuthenticationException, IOException
039 // {
040 // org.cumulus4j.keymanager.Session rs = realSession;
041 // if (rs == null)
042 // return null;
043 // else
044 // return rs.getCryptoSessionID();
045 //// try {
046 //// return appServer.getSessionManager().openSession(localKeyManagerAPI.getAuthUserName(), localKeyManagerAPI.getAuthPassword()).getCryptoSessionID();
047 //// } catch (org.cumulus4j.keystore.AuthenticationException e) {
048 //// throw new AuthenticationException(e);
049 //// }
050 // }
051
052 private int unlockCounter = 0;
053
054 private org.cumulus4j.keymanager.Session realSession = null;
055
056 @Override
057 public synchronized String acquire() throws AuthenticationException, IOException
058 {
059 ++unlockCounter;
060 if (realSession == null) {
061 try {
062 realSession = appServer.getSessionManager().acquireSession(localKeyManagerAPI.getAuthUserName(), localKeyManagerAPI.getAuthPassword());
063 } catch (org.cumulus4j.keystore.AuthenticationException e) {
064 throw new AuthenticationException(e);
065 }
066 }
067 else {
068 realSession.reacquire();
069 }
070 return realSession.getCryptoSessionID();
071 }
072
073 @Override
074 public synchronized void release() throws AuthenticationException, IOException
075 {
076 int counter = --unlockCounter;
077
078 if (counter < 0)
079 throw new IllegalStateException("lock() called more often than unlock()!!!");
080
081 if (counter > 0)
082 return;
083
084 if (realSession == null)
085 throw new IllegalStateException("unlockCounter inconsistent with realSession! realSession is null!");
086 realSession.release();
087 realSession = null;
088 }
089
090 // @Override
091 // public void close() {
092 // org.cumulus4j.keymanager.Session underlyingSession = appServer.getSessionManager().getSessionForUserName(localKeyManagerAPI.getAuthUserName());
093 // if (underlyingSession != null)
094 // underlyingSession.close();
095 // }
096
097 }