1 18 19 package sync4j.transport.http.server; 20 21 import java.io.Serializable ; 22 import java.util.Map ; 23 import java.util.Hashtable ; 24 import java.rmi.RemoteException ; 25 26 import javax.servlet.http.*; 27 import javax.naming.*; 28 import javax.ejb.CreateException ; 29 import javax.ejb.RemoveException ; 30 import javax.ejb.EJBException ; 31 import javax.ejb.Handle ; 32 import javax.security.auth.login.LoginContext ; 33 import javax.security.auth.login.LoginException ; 34 35 import sync4j.server.syncbean.SyncRemote; 36 import sync4j.server.syncbean.SyncHomeRemote; 37 38 import sync4j.framework.transport.http.SyncHolder; 39 import sync4j.framework.server.SyncResponse; 40 41 import sync4j.framework.core.Sync4jException; 42 import sync4j.framework.server.error.ServerException; 43 import sync4j.framework.server.error.ServerFailureException; 44 import sync4j.framework.server.error.NotImplementedException; 45 import sync4j.framework.protocol.ProtocolException; 46 47 57 public class RemoteEJBSyncHolder 58 implements SyncHolder, Serializable { 59 60 62 public static final String SYNCEJB_HOME_JNDI_NAME = "java:comp/env/ejb/RemoteSyncBean"; 63 64 public static final String CONTEXT_FACTORY 65 = "org.jnp.interfaces.NamingContextFactory"; 66 public static final String CONTEXT_URL_PREFIXES 67 = "org.jboss.naming:org.jnp.interfaces"; 68 69 71 private transient SyncRemote syncBean = null ; 72 private transient boolean newHolder = false; 73 private Handle syncHandle ; 74 private String sessionId ; 75 private long creationTimestamp ; 76 77 private String jndiAddress; 78 79 81 84 protected RemoteEJBSyncHolder() {} 85 86 97 public RemoteEJBSyncHolder(boolean newHolderFlag) { 98 newHolder = newHolderFlag; 99 if (newHolder) { 100 creationTimestamp = System.currentTimeMillis(); 101 } 102 } 103 104 106 117 public SyncResponse processXMLMessage(final byte[] requestData, 118 final Map parameters , 119 final Map headers ) 120 throws ServerException { 121 try { 122 return getSyncServerBeanInstance() 123 .processXMLMessage(requestData, parameters, headers); 124 } catch (Exception e) { 125 throw new ServerException(e); 126 } 127 } 128 129 140 public SyncResponse processWBXMLMessage(final byte[] requestData, 141 final Map parameters , 142 final Map headers ) 143 throws ServerException { 144 try { 145 return getSyncServerBeanInstance() 146 .processWBXMLMessage(requestData, parameters, headers); 147 } catch (Exception e) { 148 throw new ServerException(e); 149 } 150 } 151 152 public void setSessionId(String sessionId) { 153 this.sessionId = sessionId; 154 } 155 156 public String getSessionId() { 157 return sessionId; 158 } 159 160 public void setJndiAddress(String jndiAddress){ 161 this.jndiAddress = jndiAddress; 162 } 163 164 public String getJndiAddress() { 165 return jndiAddress; 166 } 167 168 175 public void close() throws Exception { 176 try { 177 if (syncBean != null) { 178 syncBean.remove(); 179 } 180 } catch (Exception e) { 181 throw new Exception ("Error in closing the SyncHolder", e); 182 } 183 } 184 185 190 public long getCreationTimestamp() { 191 return creationTimestamp; 192 } 193 194 199 public boolean isNew() { 200 return newHolder; 201 } 202 203 205 217 private SyncRemote getSyncServerBeanInstance() 218 throws ServerFailureException { 219 if (syncBean != null) { 220 return syncBean; 221 } 222 223 if (syncHandle != null) { 224 try { 225 return syncBean = (SyncRemote)syncHandle.getEJBObject(); 226 } catch (RemoteException e) { 227 throw new ServerFailureException(e); 228 } 229 } 230 231 if ((sessionId == null) || (sessionId.length() == 0)) { 236 throw new ServerFailureException("No session id is associated to this handler"); 237 } 238 239 SyncHomeRemote home; 240 InitialContext ctx; 241 242 try { 243 Hashtable env = new Hashtable (); 244 245 env.put(Context.INITIAL_CONTEXT_FACTORY, CONTEXT_FACTORY ); 246 env.put(Context.URL_PKG_PREFIXES, CONTEXT_URL_PREFIXES); 247 env.put(Context.PROVIDER_URL, jndiAddress ); 248 249 ctx = new InitialContext(env); 250 home = (SyncHomeRemote)ctx.lookup(SYNCEJB_HOME_JNDI_NAME);; 251 syncBean = home.create(sessionId); 252 syncHandle = syncBean.getHandle(); 253 } catch (Exception e) { 254 throw new ServerFailureException(e); 255 } 256 257 return syncBean; 258 } 259 260 } 261 | Popular Tags |