]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.util/src/org/argeo/util/transaction/WorkingCopyXaResource.java
ebafd267fb04cca13ab5ba0ecbcfeda336d8a0b7
1 package org
.argeo
.util
.transaction
;
3 import java
.util
.HashMap
;
6 import javax
.transaction
.xa
.XAException
;
7 import javax
.transaction
.xa
.XAResource
;
8 import javax
.transaction
.xa
.Xid
;
10 /** {@link XAResource} for a user directory being edited. */
11 public class WorkingCopyXaResource
<WC
extends WorkingCopy
<?
, ?
, ?
>> implements XAResource
{
12 private final WorkingCopyProcessor
<WC
> processor
;
14 private Map
<Xid
, WC
> workingCopies
= new HashMap
<Xid
, WC
>();
15 private Xid editingXid
= null;
16 private int transactionTimeout
= 0;
18 public WorkingCopyXaResource(WorkingCopyProcessor
<WC
> processor
) {
19 this.processor
= processor
;
23 public synchronized void start(Xid xid
, int flags
) throws XAException
{
24 if (editingXid
!= null)
25 throw new IllegalStateException("Already editing " + editingXid
);
26 WC wc
= workingCopies
.put(xid
, processor
.newWorkingCopy());
28 throw new IllegalStateException("There is already a working copy for " + xid
);
29 this.editingXid
= xid
;
33 public void end(Xid xid
, int flags
) throws XAException
{
37 private WC
wc(Xid xid
) {
38 return workingCopies
.get(xid
);
41 public synchronized WC
wc() {
42 if (editingXid
== null)
44 WC wc
= workingCopies
.get(editingXid
);
46 throw new IllegalStateException("No working copy found for " + editingXid
);
50 private synchronized void cleanUp(Xid xid
) {
52 workingCopies
.remove(xid
);
57 public int prepare(Xid xid
) throws XAException
{
60 if (wc
.noModifications())
63 processor
.prepare(wc
);
64 } catch (Exception e
) {
66 throw new XAException(XAException
.XAER_RMERR
);
72 public void commit(Xid xid
, boolean onePhase
) throws XAException
{
76 if (wc
.noModifications())
79 processor
.prepare(wc
);
81 } catch (Exception e
) {
83 throw new XAException(XAException
.XAER_RMERR
);
90 public void rollback(Xid xid
) throws XAException
{
93 processor
.rollback(wc(xid
));
94 } catch (Exception e
) {
96 throw new XAException(XAException
.XAER_RMERR
);
103 public void forget(Xid xid
) throws XAException
{
104 throw new UnsupportedOperationException();
108 public boolean isSameRM(XAResource xares
) throws XAException
{
109 return xares
== this;
113 public Xid
[] recover(int flag
) throws XAException
{
118 public int getTransactionTimeout() throws XAException
{
119 return transactionTimeout
;
123 public boolean setTransactionTimeout(int seconds
) throws XAException
{
124 transactionTimeout
= seconds
;
128 private void checkXid(Xid xid
) throws XAException
{
130 throw new XAException(XAException
.XAER_OUTSIDE
);
131 if (!xid
.equals(xid
))
132 throw new XAException(XAException
.XAER_NOTA
);