1 package org
.argeo
.osgi
.useradmin
;
3 import java
.util
.HashMap
;
6 import javax
.transaction
.xa
.XAException
;
7 import javax
.transaction
.xa
.XAResource
;
8 import javax
.transaction
.xa
.Xid
;
10 import org
.apache
.commons
.logging
.Log
;
11 import org
.apache
.commons
.logging
.LogFactory
;
13 /** {@link XAResource} for a user directory being edited. */
14 class WcXaResource
implements XAResource
{
15 private final static Log log
= LogFactory
.getLog(WcXaResource
.class);
17 private final AbstractUserDirectory userDirectory
;
19 private Map
<Xid
, UserDirectoryWorkingCopy
> workingCopies
= new HashMap
<Xid
, UserDirectoryWorkingCopy
>();
20 private Xid editingXid
= null;
21 private int transactionTimeout
= 0;
23 public WcXaResource(AbstractUserDirectory userDirectory
) {
24 this.userDirectory
= userDirectory
;
28 public synchronized void start(Xid xid
, int flags
) throws XAException
{
29 if (editingXid
!= null)
30 throw new UserDirectoryException("Already editing " + editingXid
);
31 UserDirectoryWorkingCopy wc
= workingCopies
.put(xid
,
32 new UserDirectoryWorkingCopy());
34 throw new UserDirectoryException(
35 "There is already a working copy for " + xid
);
36 this.editingXid
= xid
;
40 public void end(Xid xid
, int flags
) throws XAException
{
44 private UserDirectoryWorkingCopy
wc(Xid xid
) {
45 return workingCopies
.get(xid
);
48 synchronized UserDirectoryWorkingCopy
wc() {
49 if (editingXid
== null)
51 UserDirectoryWorkingCopy wc
= workingCopies
.get(editingXid
);
53 throw new UserDirectoryException("No working copy found for "
58 private synchronized void cleanUp(Xid xid
) {
60 workingCopies
.remove(xid
);
65 public int prepare(Xid xid
) throws XAException
{
67 UserDirectoryWorkingCopy wc
= wc(xid
);
68 if (wc
.noModifications())
71 userDirectory
.prepare(wc
);
72 } catch (Exception e
) {
73 log
.error("Cannot prepare " + xid
, e
);
74 throw new XAException(XAException
.XAER_RMERR
);
80 public void commit(Xid xid
, boolean onePhase
) throws XAException
{
83 UserDirectoryWorkingCopy wc
= wc(xid
);
84 if (wc
.noModifications())
87 userDirectory
.prepare(wc
);
88 userDirectory
.commit(wc
);
89 } catch (Exception e
) {
90 log
.error("Cannot commit " + xid
, e
);
91 throw new XAException(XAException
.XAER_RMERR
);
98 public void rollback(Xid xid
) throws XAException
{
101 userDirectory
.rollback(wc(xid
));
102 } catch (Exception e
) {
103 log
.error("Cannot rollback " + xid
, e
);
104 throw new XAException(XAException
.XAER_RMERR
);
111 public void forget(Xid xid
) throws XAException
{
112 throw new UnsupportedOperationException();
116 public boolean isSameRM(XAResource xares
) throws XAException
{
117 return xares
== this;
121 public Xid
[] recover(int flag
) throws XAException
{
126 public int getTransactionTimeout() throws XAException
{
127 return transactionTimeout
;
131 public boolean setTransactionTimeout(int seconds
) throws XAException
{
132 transactionTimeout
= seconds
;
136 private void checkXid(Xid xid
) throws XAException
{
138 throw new XAException(XAException
.XAER_OUTSIDE
);
139 if (!xid
.equals(xid
))
140 throw new XAException(XAException
.XAER_NOTA
);