1 package org.enhydra.shark; 2 3 import java.io.Serializable ; 4 5 import org.enhydra.shark.api.client.wfmodel.*; 6 7 import org.enhydra.shark.api.RootException; 8 import org.enhydra.shark.api.SharkTransaction; 9 import org.enhydra.shark.api.client.wfbase.BaseException; 10 import org.enhydra.shark.api.client.wfservice.ConnectFailed; 11 import org.enhydra.shark.api.client.wfservice.NotConnected; 12 import org.enhydra.shark.api.client.wfservice.SharkConnection; 13 import org.enhydra.shark.api.internal.security.SecurityManager; 14 import org.enhydra.shark.api.internal.working.CallbackUtilities; 15 import org.enhydra.shark.api.internal.working.WfProcessInternal; 16 import org.enhydra.shark.api.internal.working.WfProcessMgrInternal; 17 import org.enhydra.shark.api.internal.working.WfRequesterInternal; 18 import org.enhydra.shark.api.internal.working.WfResourceInternal; 19 20 26 public class SharkConnectionImpl implements SharkConnection, Serializable { 27 28 private String userId; 29 private boolean connected=false; 30 31 private String connectionKey; 32 33 private CallbackUtilities cus; 34 35 protected SharkConnectionImpl () { 36 this.cus=SharkEngineManager.getInstance().getCallbackUtilities(); 37 } 38 39 public void connect (String userId,String password,String engineName,String scope) throws BaseException, ConnectFailed { 40 SharkTransaction t = null; 41 try { 42 t = SharkUtilities.createTransaction(); 43 connect(t, userId, password, engineName, scope); 44 SharkUtilities.commitTransaction(t); 45 } catch (RootException e) { 46 SharkUtilities.rollbackTransaction(t,e); 47 if (e instanceof ConnectFailed) 48 throw (ConnectFailed)e; 49 else if (e instanceof BaseException) 50 throw (BaseException)e; 51 else 52 throw new BaseException(e); 53 } finally { 54 SharkUtilities.releaseTransaction(t); 55 } 56 } 57 58 public void connect (SharkTransaction t,String userId,String password,String engineName,String scope) throws BaseException, ConnectFailed { 59 this.userId=userId; 60 SecurityManager sm=SharkEngineManager.getInstance().getSecurityManager(); 61 if (sm!=null) { 62 try { 63 sm.check_sharkconnection_connect(t,userId); 64 } catch (Exception ex) { 65 throw new BaseException(ex); 66 } 67 } 68 try { 69 if (!SharkUtilities.validateUser(userId,password)) { 70 cus.info("SharkConnectionImpl -> Login failed: Invalid username or password "+userId); 71 throw new ConnectFailed("Connection failed, invalid username or password"); 72 } 73 74 if (SharkUtilities.getResource(t, userId)==null) { 75 WfResourceInternal resInternal=SharkEngineManager 76 .getInstance() 77 .getObjectFactory() 78 .createResource(t,userId); 79 } 80 connectionKey=SharkUtilities.connect(userId); 81 connected=true; 82 cus.info("SharkConnectionImpl -> User "+userId+" is logged on"); 83 } catch (Exception ex) { 84 if (ex instanceof ConnectFailed) { 85 throw (ConnectFailed)ex; 86 } 87 cus.info("SharkConnectionImpl -> Unexpected error while user "+userId+" loggs on"); 88 ex.printStackTrace(); 89 throw new BaseException(ex); 90 } 91 } 92 93 public void disconnect () throws BaseException, NotConnected { 94 SharkTransaction t = null; 95 try { 96 t = SharkUtilities.createTransaction(); 97 disconnect(t); 98 } catch (RootException e) { 100 SharkUtilities.emptyCaches(t); 102 if (e instanceof NotConnected) 103 throw (NotConnected)e; 104 else if (e instanceof BaseException) 105 throw (BaseException)e; 106 else 107 throw new BaseException(e); 108 } finally { 109 SharkUtilities.releaseTransaction(t); 110 } 111 } 112 113 public void disconnect (SharkTransaction t) throws BaseException, NotConnected { 114 if (!connected) { 115 throw new NotConnected("The connection is not established..."); 116 } 117 try { 118 SharkUtilities.disconnect(connectionKey); 119 connected=false; 120 cus.info("SharkConnectionImpl -> User "+userId+" with connection key "+connectionKey+" is logged off"); 121 } catch (Exception ex) { 122 if (ex instanceof NotConnected) { 123 throw (NotConnected)ex; 124 } 125 cus.info("SharkConnectionImpl -> Unexpected error while user "+userId+" loggs off"); 126 throw new BaseException(ex); 127 } 128 } 129 130 public WfResource getResourceObject () throws BaseException, NotConnected { 131 WfResource ret = null; 132 SharkTransaction t = null; 133 try { 134 t = SharkUtilities.createTransaction(); 135 ret = getResourceObject(t); 136 } catch (RootException e) { 138 SharkUtilities.emptyCaches(t); 140 if (e instanceof NotConnected) 141 throw (NotConnected)e; 142 else if (e instanceof BaseException) 143 throw (BaseException)e; 144 else 145 throw new BaseException(e); 146 } finally { 147 SharkUtilities.releaseTransaction(t); 148 } 149 return ret; 150 } 151 152 public WfResource getResourceObject (SharkTransaction t) throws BaseException, NotConnected { 153 if (!connected) { 154 throw new NotConnected("The connection is not established..."); 155 } 156 SecurityManager sm=SharkEngineManager.getInstance().getSecurityManager(); 157 if (sm!=null) { 158 try { 159 sm.check_sharkconnection_getResourceObject(t,userId); 160 } catch (Exception ex) { 161 throw new BaseException(ex); 162 } 163 } 164 try { 165 WfResource ret=SharkEngineManager.getInstance().getObjectFactory().createResourceWrapper(userId,userId); 166 return ret; 167 } catch (Exception ex) { 168 cus.info("SharkConnectionImpl -> Unexpected error while user "+userId+" tries to get its resource object"); 169 throw new BaseException(ex); 170 } 171 } 172 173 public WfProcess createProcess (String pkgId,String pDefId) throws BaseException, NotConnected, NotEnabled { 174 WfProcess ret = null; 175 SharkTransaction t = null; 176 try { 177 t = SharkUtilities.createTransaction(); 178 ret = createProcess(t, pkgId, pDefId); 179 SharkUtilities.commitTransaction(t); 180 } catch (RootException e) { 181 SharkUtilities.rollbackTransaction(t,e); 182 if (e instanceof NotConnected) 183 throw (NotConnected)e; 184 else if (e instanceof NotEnabled) 185 throw (NotEnabled)e; 186 else if (e instanceof BaseException) 187 throw (BaseException)e; 188 else 189 throw new BaseException(e); 190 } finally { 191 SharkUtilities.releaseTransaction(t); 192 } 193 return ret; 194 } 195 196 public WfProcess createProcess (SharkTransaction t,String pkgId,String pDefId) throws BaseException, NotConnected, NotEnabled { 197 if (!connected) { 198 throw new NotConnected("The connection is not established..."); 199 } 200 201 String curVer=SharkUtilities.getCurrentPkgVersion(pkgId,true); 202 203 WfRequesterInternal req=SharkEngineManager.getInstance().getObjectFactory().createDefaultRequester(userId,null); 205 WfProcessMgrInternal mgr=SharkUtilities.getProcessMgr(t, SharkUtilities.createProcessMgrKey(pkgId,curVer,pDefId)); 206 if (mgr==null) throw new BaseException("Can't create process for given definition - can't find manager"); 207 SecurityManager sm=SharkEngineManager.getInstance().getSecurityManager(); 208 if (sm!=null) { 209 try { 210 sm.check_sharkconnection_createProcess(t,mgr.name(t),userId,pkgId,curVer,pDefId); 211 } catch (Exception ex) { 212 throw new BaseException(ex); 213 } 214 } 215 216 try { 217 WfProcessInternal procInternal=mgr.create_process(t,req); 218 WfProcess proc=SharkEngineManager.getInstance().getObjectFactory().createProcessWrapper(userId,procInternal.manager_name(t),procInternal.key(t)); 219 return proc; 220 } catch (InvalidRequester ir) { 221 throw new BaseException(ir); 222 } catch (RequesterRequired rr) { 223 throw new BaseException(rr); 224 } 227 } 228 229 public WfProcess createProcess (WfRequester r, String pkgId,String pDefId) 230 throws BaseException, NotConnected , NotEnabled, InvalidRequester, RequesterRequired { 231 WfProcess ret = null; 232 SharkTransaction t=null; 233 try { 234 t = SharkUtilities.createTransaction(); 235 ret = createProcess(t, r, pkgId, pDefId); 236 SharkUtilities.commitTransaction(t); 237 } catch (RootException e) { 238 SharkUtilities.rollbackTransaction(t,e); 239 if (e instanceof NotConnected) 240 throw (NotConnected)e; 241 else if (e instanceof NotEnabled) 242 throw (NotEnabled)e; 243 else if (e instanceof InvalidRequester) 244 throw (InvalidRequester)e; 245 else if (e instanceof RequesterRequired) 246 throw (RequesterRequired)e; 247 else if (e instanceof BaseException) 248 throw (BaseException)e; 249 else 250 throw new BaseException(e); 251 } finally { 252 SharkUtilities.releaseTransaction(t); 253 } 254 return ret; 255 } 256 257 262 public WfProcess createProcess (SharkTransaction t, WfRequester r, String pkgId,String pDefId) 263 throws BaseException, NotConnected, NotEnabled, InvalidRequester, RequesterRequired { 264 if (!connected) { 265 throw new NotConnected("The connection is not established..."); 266 } 267 if (r==null) throw new BaseException("Trying to create process with external requester which is null!"); 268 269 String curVer=SharkUtilities.getCurrentPkgVersion(pkgId,true); 270 271 WfRequesterInternal req=SharkEngineManager.getInstance().getObjectFactory().createDefaultRequester(userId,r); 272 WfProcessMgrInternal mgr=SharkUtilities.getProcessMgr(t, SharkUtilities.createProcessMgrKey(pkgId,curVer,pDefId)); 273 274 if (mgr==null) throw new BaseException("Can't create process for given definition - can't find manager"); 275 SecurityManager sm=SharkEngineManager.getInstance().getSecurityManager(); 276 if (sm!=null) { 277 try { 278 sm.check_sharkconnection_createProcess(t,mgr.name(t),userId,pkgId,curVer,pDefId); 279 } catch (Exception ex) { 280 throw new BaseException(ex); 281 } 282 } 283 284 WfProcessInternal procInternal=mgr.create_process(t,req); 285 WfProcess proc=SharkEngineManager.getInstance().getObjectFactory().createProcessWrapper(userId,procInternal.manager_name(t),procInternal.key(t)); 286 return proc; 287 } 288 289 public void finalize() throws Throwable { 291 if (connected) { 292 connected=false; 293 SharkUtilities.disconnect(connectionKey); 294 cus.info("SharkConnectionImpl -> User "+userId+" with connection key "+connectionKey+" is automatically disconnected"); 295 } 296 } 297 298 } 299 | Popular Tags |