]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.api.cms/src/org/argeo/api/cms/transaction/WorkingCopyXaResource.java
1 package org
.argeo
.api
.cms
.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
) {
51 WC wc
= workingCopies
.get(xid
);
54 workingCopies
.remove(xid
);
60 public int prepare(Xid xid
) throws XAException
{
63 if (wc
.noModifications())
66 processor
.prepare(wc
);
67 } catch (Exception e
) {
69 throw new XAException(XAException
.XAER_RMERR
);
75 public void commit(Xid xid
, boolean onePhase
) throws XAException
{
79 if (wc
.noModifications())
82 processor
.prepare(wc
);
84 } catch (Exception e
) {
86 throw new XAException(XAException
.XAER_RMERR
);
93 public void rollback(Xid xid
) throws XAException
{
96 processor
.rollback(wc(xid
));
97 } catch (Exception e
) {
99 throw new XAException(XAException
.XAER_RMERR
);
106 public void forget(Xid xid
) throws XAException
{
107 throw new UnsupportedOperationException();
111 public boolean isSameRM(XAResource xares
) throws XAException
{
112 return xares
== this;
116 public Xid
[] recover(int flag
) throws XAException
{
121 public int getTransactionTimeout() throws XAException
{
122 return transactionTimeout
;
126 public boolean setTransactionTimeout(int seconds
) throws XAException
{
127 transactionTimeout
= seconds
;
131 private void checkXid(Xid xid
) throws XAException
{
133 throw new XAException(XAException
.XAER_OUTSIDE
);
134 if (!xid
.equals(xid
))
135 throw new XAException(XAException
.XAER_NOTA
);