1 23 package org.apache.slide.store.mem; 24 25 import java.lang.reflect.Constructor ; 26 import java.lang.reflect.InvocationTargetException ; 27 import java.text.MessageFormat ; 28 import java.util.Enumeration ; 29 import java.util.HashMap ; 30 import java.util.Hashtable ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.NoSuchElementException ; 34 35 import javax.transaction.xa.XAException ; 36 import javax.transaction.xa.XAResource ; 37 import javax.transaction.xa.Xid ; 38 39 import org.apache.commons.transaction.memory.ConflictException; 40 import org.apache.commons.transaction.memory.TransactionalMapWrapper; 41 import org.apache.commons.transaction.memory.jca.MapXAResource; 42 import org.apache.commons.transaction.util.LoggerFacade; 43 44 import org.apache.slide.common.AbstractServiceBase; 45 import org.apache.slide.common.NamespaceAccessToken; 46 import org.apache.slide.common.ServiceAccessException; 47 import org.apache.slide.common.ServiceConnectionFailedException; 48 import org.apache.slide.common.ServiceDisconnectionFailedException; 49 import org.apache.slide.common.ServiceInitializationFailedException; 50 import org.apache.slide.common.ServiceParameterErrorException; 51 import org.apache.slide.common.ServiceParameterMissingException; 52 import org.apache.slide.common.ServiceResetFailedException; 53 import org.apache.slide.util.logger.Logger; 54 import org.apache.slide.util.logger.TxLogger; 55 56 72 public abstract class AbstractTransientStore extends AbstractServiceBase { 73 74 static final String MAP_IMPL_PARAMETER = "map-classname"; 75 static final String MAP_IMPL_PARAMETER_DEFAULT = 76 "org.apache.commons.transaction.memory.OptimisticMapWrapper"; 77 78 private Map parameters = null; 79 private boolean isConnected = false; 80 81 84 private TransactionalMapWrapper store = null; 85 86 89 private MapXAResource xaResource = null; 90 91 93 public void start(Xid xid, int flags) 94 throws XAException 95 { 96 debug("start {0} {1}", xid, Integer.toString(flags)); 97 this.xaResource.start(xid, flags); 98 } 99 100 public void commit(Xid xid, boolean onePhase) 101 throws XAException 102 { 103 debug("commit {0} {1}", xid, onePhase ? "true" : "false"); 104 try { 105 this.xaResource.commit(xid, onePhase); 106 } 107 catch (ConflictException e) { 108 this.xaResource.rollback(xid); 109 throw new XAException (XAException.XA_RBOTHER); } 114 } 115 116 public int prepare(Xid xid) 117 throws XAException 118 { 119 debug("prepare {0}", xid); 120 return this.xaResource.prepare(xid); 121 } 122 123 public void end(Xid xid, int flags) 124 throws XAException 125 { 126 debug("end {0} {1}", xid, Integer.toString(flags)); 127 this.xaResource.end(xid, flags); 128 } 129 130 public void rollback(Xid xid) 131 throws XAException 132 { 133 debug("rollback {0}", xid); 134 this.xaResource.rollback(xid); 135 } 136 137 public Xid [] recover(int flag) 138 throws XAException 139 { 140 debug("recover {0}", Integer.toString(flag)); 141 return this.xaResource.recover(flag); 142 } 143 144 public void forget(Xid xid) 145 throws XAException 146 { 147 debug("forget {0}", xid); 148 this.xaResource.forget(xid); 149 } 150 151 public int getTransactionTimeout() 152 throws XAException 153 { 154 return this.xaResource.getTransactionTimeout(); 155 } 156 157 public boolean setTransactionTimeout(int seconds) 158 throws XAException 159 { 160 return this.xaResource.setTransactionTimeout(seconds); 161 } 162 163 public boolean isSameRM(XAResource xares) 164 throws XAException 165 { 166 return this == xares; 167 } 168 169 170 171 172 174 protected void put(Object key, Object value) { 175 this.store.put(key, value); 176 } 177 178 protected Object get(Object key) { 179 return this.store.get(key); 180 } 181 182 protected Object remove(Object key) { 183 return this.store.remove(key); 184 } 185 186 188 public void setParameters(Hashtable parameters) 189 throws ServiceParameterErrorException, 190 ServiceParameterMissingException 191 { 192 this.parameters = new HashMap (parameters); 193 } 194 195 protected String getParameter(String name) { 196 if (this.parameters != null) { 197 return (String )this.parameters.get(name); 198 } else { 199 throw new IllegalStateException ("Parameter not yet set!"); 200 } 201 } 202 public void initialize(NamespaceAccessToken token) 203 throws ServiceInitializationFailedException 204 { 205 super.initialize(token); 206 207 TxLogger txLogger = new TxLogger(getLogger(), LogChannel()); 208 209 String param = (String )this.parameters.get(MAP_IMPL_PARAMETER); 210 if (param == null) { 211 param = MAP_IMPL_PARAMETER_DEFAULT; 212 } 213 try { 214 info("Initializing {0} using {1}.", getClass().getName(), param); 215 Map toBeWrapped = new HashMap (); 216 Class mapClass = Class.forName(param); 217 218 try { 219 Class [] ctorFormalArgs = { Map .class, LoggerFacade.class }; 220 Constructor ctor = mapClass.getConstructor(ctorFormalArgs); 221 Object [] ctorArgs = { toBeWrapped, txLogger }; 222 this.store = (TransactionalMapWrapper)ctor.newInstance(ctorArgs); 223 } 224 catch (NoSuchMethodException e) { 225 try { 227 Class [] ctorFormalArgs = { Map .class }; 228 Constructor ctor = mapClass.getConstructor(ctorFormalArgs); 229 Object [] ctorArgs = { toBeWrapped }; 230 this.store = (TransactionalMapWrapper)ctor.newInstance(ctorArgs); 231 } 232 catch (NoSuchMethodException ee) { 233 error("Initialization error: ", ee); 234 throw new ServiceInitializationFailedException(this, ee); 235 } 236 } 237 } 238 catch (ClassNotFoundException e) { 239 error("Initialization error: ", e); 240 throw new ServiceInitializationFailedException(this, e); 241 } 242 catch (InstantiationException e) { 243 error("Initialization error: ", e); 244 throw new ServiceInitializationFailedException(this, e); 245 } 246 catch (IllegalAccessException e) { 247 error("Initialization error: ", e); 248 throw new ServiceInitializationFailedException(this, e); 249 } 250 catch (InvocationTargetException e) { 251 error("Initialization error: ", e); 252 throw new ServiceInitializationFailedException(this, e); 253 } 254 catch (ClassCastException e) { 255 error("Initialization error: ", e); 256 throw new ServiceInitializationFailedException(this, 257 "class in parameter '" + MAP_IMPL_PARAMETER + "' must " + 258 "be derived from TransactionalMapWrapper"); 259 } 260 261 this.xaResource = new MapXAResource(this.store); 262 } 265 266 public void connect() throws ServiceConnectionFailedException { 267 this.isConnected = true; 268 } 269 public void disconnect() throws ServiceDisconnectionFailedException { 270 this.isConnected = false; 271 } 272 public void reset() throws ServiceResetFailedException { 273 } 274 public boolean isConnected() throws ServiceAccessException { 275 return this.isConnected; 276 } 277 278 279 281 282 protected String LogChannel() { 283 return LOG_CHANNEL; 284 } 285 protected void debug(String msg, Object o) { 286 if (this.getLogger().isEnabled(Logger.DEBUG)) { 287 Object [] args = { o }; 288 this.getLogger().log(MessageFormat.format(msg, args), 289 LogChannel(), Logger.DEBUG); 290 } 291 } 292 protected void debug(String msg, Object o1, Object o2) { 293 if (this.getLogger().isEnabled(Logger.DEBUG)) { 294 Object [] args = { o1, o2 }; 295 this.getLogger().log(MessageFormat.format(msg, args), 296 LogChannel(), Logger.DEBUG); 297 } 298 } 299 private void info(String msg, Object o1, Object o2) { 300 if (this.getLogger().isEnabled(Logger.INFO)) { 301 Object [] args = { o1, o2 }; 302 this.getLogger().log(MessageFormat.format(msg, args), 303 LogChannel(), Logger.INFO); 304 } 305 } 306 private void error(String msg, Throwable t) { 307 if (this.getLogger().isEnabled(Logger.INFO)) { 308 this.getLogger().log(msg, t, 309 LogChannel(), Logger.INFO); 310 } 311 } 312 313 316 protected static Enumeration EMPTY_ENUM = new Enumeration () { 317 public boolean hasMoreElements() { 318 return false; 319 } 320 public Object nextElement() { 321 throw new NoSuchElementException (); 322 } 323 }; 324 325 328 protected static class IteratorEnum implements Enumeration { 329 private Iterator iterator; 330 public IteratorEnum(Iterator iterator){ 331 this.iterator = iterator; 332 } 333 public boolean hasMoreElements() { 334 return this.iterator.hasNext(); 335 } 336 public Object nextElement() { 337 return this.iterator.next(); 338 } 339 } 340 } 341 | Popular Tags |