ozeigermann 2004/05/25 07:28:49
Added: transaction/src/java/org/apache/commons/transaction/memory/jca
MapManagedConnectionFactory.java
MemoryMapResourceManager.java MapXAResource.java
MapConnectionSpec.java MapConnection.java
MapConnectionFactory.java MapManagedConnection.java
MapLocalTransaction.java
Log:
Added initial JCA implementation for Maps
Revision Changes Path
1.1
jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/jca/MapManagedConnectionFactory.java
Index: MapManagedConnectionFactory.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/jca/MapManagedConnectionFactory.java,v
1.1 2004/05/25 14:28:49 ozeigermann Exp $
* $Revision: 1.1 $
* $Date: 2004/05/25 14:28:49 $
*
* ====================================================================
*
* Copyright 1999-2002 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.commons.transaction.memory.jca;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Set;
import javax.resource.ResourceException;
import javax.resource.spi.ConnectionManager;
import javax.resource.spi.ConnectionRequestInfo;
import javax.resource.spi.ManagedConnection;
import javax.resource.spi.ManagedConnectionFactory;
import javax.security.auth.Subject;
/**
*
* @author <a href="mailto:[EMAIL PROTECTED]">Oliver Zeigermann</a>
*
*/
public class MapManagedConnectionFactory implements ManagedConnectionFactory {
private PrintWriter writer;
/**
* @see ManagedConnectionFactory#createConnectionFactory(ConnectionManager)
*/
public Object createConnectionFactory(ConnectionManager cm)
throws ResourceException {
return new MapConnectionFactory(this, cm);
}
/**
* @see ManagedConnectionFactory#createConnectionFactory()
*/
public Object createConnectionFactory() throws ResourceException {
return new MapConnectionFactory(this, null);
}
/**
* @see ManagedConnectionFactory#createManagedConnection(Subject,
ConnectionRequestInfo)
*/
public ManagedConnection createManagedConnection(
Subject subject,
ConnectionRequestInfo cxRequestInfo)
throws ResourceException {
return new MapManagedConnection();
}
/**
* @see ManagedConnectionFactory#matchManagedConnections(Set, Subject,
ConnectionRequestInfo)
*/
public ManagedConnection matchManagedConnections(
Set connectionSet,
Subject subject,
ConnectionRequestInfo cxRequestInfo)
throws ResourceException {
ManagedConnection match = null;
Iterator iterator = connectionSet.iterator();
if (iterator.hasNext()) {
match = (ManagedConnection) iterator.next();
}
return match;
}
/**
* @see ManagedConnectionFactory#setLogWriter(PrintWriter)
*/
public void setLogWriter(PrintWriter writer) throws ResourceException {
this.writer = writer;
}
/**
* @see ManagedConnectionFactory#getLogWriter()
*/
public PrintWriter getLogWriter() throws ResourceException {
return writer;
}
public boolean equals(Object other) {
if (other instanceof MapManagedConnectionFactory) {
return true;
}
return false;
}
public int hashCode() {
return 0;
}
}
1.1
jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/jca/MemoryMapResourceManager.java
Index: MemoryMapResourceManager.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/jca/MemoryMapResourceManager.java,v
1.1 2004/05/25 14:28:49 ozeigermann Exp $
* $Revision: 1.1 $
* $Date: 2004/05/25 14:28:49 $
*
* ====================================================================
*
* Copyright 1999-2002 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.commons.transaction.memory.jca;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.transaction.memory.TransactionalMapWrapper;
/**
*
* @author <a href="mailto:[EMAIL PROTECTED]">Oliver Zeigermann</a>
*
*/
public class MemoryMapResourceManager {
protected static MemoryMapResourceManager instance = new
MemoryMapResourceManager();
public static MemoryMapResourceManager getInstance() {
return instance;
}
protected Map maps = new HashMap();
public synchronized TransactionalMapWrapper lookup(Object id) {
TransactionalMapWrapper map = (TransactionalMapWrapper) maps.get(id);
// XXX simply create it when not there
if (map == null) {
map = new TransactionalMapWrapper(new HashMap());
maps.put(id, map);
}
return map;
}
}
1.1
jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/jca/MapXAResource.java
Index: MapXAResource.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/jca/MapXAResource.java,v
1.1 2004/05/25 14:28:49 ozeigermann Exp $
* $Revision: 1.1 $
* $Date: 2004/05/25 14:28:49 $
*
* ====================================================================
*
* Copyright 1999-2002 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.commons.transaction.memory.jca;
import javax.transaction.Status;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
import org.apache.commons.transaction.memory.TransactionalMapWrapper;
/**
* Wraps an <code>Xid</code> to guarantee methods for equality and hashcode are
* implemented correctly. This is escpecially necessary when the <code>Xid</code> is
used as a key in a <code>HashMap</code>.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Oliver Zeigermann</a>
*
*/
public class MapXAResource implements XAResource, Status {
TransactionalMapWrapper map;
TransactionalMapWrapper suspended = null;
boolean includeBranch = true;
public MapXAResource(TransactionalMapWrapper map) {
this.map = map;
}
public int getTransactionTimeout() throws XAException {
return 0;
}
public boolean setTransactionTimeout(int seconds) throws XAException {
return false;
}
public boolean isSameRM(XAResource xares) throws XAException {
return (xares != null && xares instanceof MapXAResource &&
map.equals(((MapXAResource) xares).map));
}
public Xid[] recover(int flag) throws XAException {
return null;
}
public int prepare(Xid xid) throws XAException {
System.out.println("Prepare "+xid);
if (map == null) {
throw new XAException(XAException.XAER_NOTA);
}
if (xid == null) {
throw new XAException(XAException.XAER_INVAL);
}
if (map.isTransactionMarkedForRollback()) {
throw new XAException(XAException.XA_RBROLLBACK);
}
if (map.isReadOnly()) {
return XA_RDONLY;
}
return XA_OK;
}
public void forget(Xid xid) throws XAException {
System.out.println("Forget "+xid);
map = null;
}
public void rollback(Xid xid) throws XAException {
System.out.println("Rollback "+xid);
if (map == null)
throw new XAException(XAException.XAER_NOTA);
if (xid == null)
throw new XAException(XAException.XAER_INVAL);
try {
map.rollbackTransaction();
} catch (IllegalStateException e) {
throw new XAException(e.toString());
}
}
public void end(Xid xid, int flags) throws XAException {
System.out.println("End "+xid);
if (map == null)
throw new XAException(XAException.XAER_NOTA);
if (xid == null)
throw new XAException(XAException.XAER_INVAL);
if (flags == XAResource.TMSUSPEND) {
suspended = map;
map = null;
}
if (flags == XAResource.TMFAIL) {
map.markTransactionForRollback();
}
}
public void start(Xid xid, int flags) throws XAException {
System.out.println("Start "+xid);
switch (flags) {
case TMNOFLAGS :
case TMJOIN :
try {
map.startTransaction();
} catch (IllegalStateException e) {
throw new XAException(e.toString());
}
break;
case TMRESUME :
if (suspended == null) {
throw new XAException(XAException.XAER_NOTA);
}
map = suspended;
suspended = null;
}
}
public void commit(Xid xid, boolean onePhase) throws XAException {
System.out.println("Commit "+xid);
if (map == null)
throw new XAException(XAException.XAER_NOTA);
if (xid == null)
throw new XAException(XAException.XAER_INVAL);
try {
map.commitTransaction();
} catch (IllegalStateException e) {
throw new XAException(e.toString());
}
}
}
1.1
jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/jca/MapConnectionSpec.java
Index: MapConnectionSpec.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/jca/MapConnectionSpec.java,v
1.1 2004/05/25 14:28:49 ozeigermann Exp $
* $Revision: 1.1 $
* $Date: 2004/05/25 14:28:49 $
*
* ====================================================================
*
* Copyright 1999-2002 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.commons.transaction.memory.jca;
import javax.resource.cci.ConnectionSpec;
import javax.resource.spi.ConnectionRequestInfo;
/**
*
* @author <a href="mailto:[EMAIL PROTECTED]">Oliver Zeigermann</a>
*
*/
public class MapConnectionSpec implements ConnectionSpec, ConnectionRequestInfo {
protected String name;
public MapConnectionSpec(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
1.1
jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/jca/MapConnection.java
Index: MapConnection.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/jca/MapConnection.java,v
1.1 2004/05/25 14:28:49 ozeigermann Exp $
* $Revision: 1.1 $
* $Date: 2004/05/25 14:28:49 $
*
* ====================================================================
*
* Copyright 1999-2002 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.commons.transaction.memory.jca;
import java.util.Map;
import javax.resource.ResourceException;
import javax.resource.cci.Connection;
import javax.resource.cci.ConnectionMetaData;
import javax.resource.cci.Interaction;
import javax.resource.cci.LocalTransaction;
import javax.resource.cci.ResultSetInfo;
import javax.resource.spi.ManagedConnection;
/**
*
* @author <a href="mailto:[EMAIL PROTECTED]">Oliver Zeigermann</a>
*
*/
public class MapConnection implements Connection {
protected MapManagedConnection mc;
public MapConnection(ManagedConnection mc) {
this.mc = (MapManagedConnection) mc;
}
public Map getMap() {
return mc.map;
}
public void close() throws ResourceException {
mc.close();
}
public Interaction createInteraction() throws ResourceException {
return null;
}
public LocalTransaction getLocalTransaction() throws ResourceException {
return (LocalTransaction)mc.getLocalTransaction();
}
public ConnectionMetaData getMetaData() throws ResourceException {
return null;
}
public ResultSetInfo getResultSetInfo() throws ResourceException {
return null;
}
void invalidate() {
mc = null;
}
}
1.1
jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/jca/MapConnectionFactory.java
Index: MapConnectionFactory.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/jca/MapConnectionFactory.java,v
1.1 2004/05/25 14:28:49 ozeigermann Exp $
* $Revision: 1.1 $
* $Date: 2004/05/25 14:28:49 $
*
* ====================================================================
*
* Copyright 1999-2002 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.commons.transaction.memory.jca;
import javax.naming.NamingException;
import javax.naming.Reference;
import javax.resource.NotSupportedException;
import javax.resource.ResourceException;
import javax.resource.cci.Connection;
import javax.resource.cci.ConnectionFactory;
import javax.resource.cci.ConnectionSpec;
import javax.resource.cci.RecordFactory;
import javax.resource.cci.ResourceAdapterMetaData;
import javax.resource.spi.ConnectionManager;
import javax.resource.spi.ManagedConnectionFactory;
/**
*
* @author <a href="mailto:[EMAIL PROTECTED]">Oliver Zeigermann</a>
*
*/
public class MapConnectionFactory implements ConnectionFactory {
Reference reference;
ConnectionManager cm;
ManagedConnectionFactory mcf;
String name;
public MapConnectionFactory(ManagedConnectionFactory mcf, ConnectionManager cm) {
System.out.println("MCF Init with mcf " + mcf + " cm " + cm);
this.mcf = mcf;
this.cm = cm;
}
public Connection getConnection() throws ResourceException {
throw new NotSupportedException("Need name of accessed map. Call
getConnection(ConnectionSpec spec) instead");
}
public Connection getConnection(ConnectionSpec spec) throws ResourceException {
if (!(spec instanceof MapConnectionSpec)) {
throw new NotSupportedException("ConnectionSpec must be instance of
MapConnectionSpec");
}
System.out.println("Getting connection with spec "+spec);
return (Connection) cm.allocateConnection(mcf, (MapConnectionSpec)spec);
}
public RecordFactory getRecordFactory() throws ResourceException {
return null;
}
public ResourceAdapterMetaData getMetaData() throws ResourceException {
return null;
}
public void setReference(Reference reference) {
this.reference = reference;
}
public Reference getReference() throws NamingException {
return reference;
}
public String getName() {
System.out.println("Getting name " + name);
return name;
}
public void setName(String string) {
System.out.println("Setting name " + string);
name = string;
}
}
1.1
jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/jca/MapManagedConnection.java
Index: MapManagedConnection.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/jca/MapManagedConnection.java,v
1.1 2004/05/25 14:28:49 ozeigermann Exp $
* $Revision: 1.1 $
* $Date: 2004/05/25 14:28:49 $
*
* ====================================================================
*
* Copyright 1999-2002 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.commons.transaction.memory.jca;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.util.Map;
import javax.resource.ResourceException;
import javax.resource.spi.ConnectionEvent;
import javax.resource.spi.ConnectionEventListener;
import javax.resource.spi.ConnectionRequestInfo;
import javax.resource.spi.LocalTransaction;
import javax.resource.spi.ManagedConnection;
import javax.resource.spi.ManagedConnectionMetaData;
import javax.security.auth.Subject;
import javax.transaction.xa.XAResource;
import org.apache.commons.transaction.memory.TransactionalMapWrapper;
/**
*
* @author <a href="mailto:[EMAIL PROTECTED]">Oliver Zeigermann</a>
*
*/
public class MapManagedConnection implements ManagedConnection {
MapXAResource xares = null;
MapLocalTransaction tx = null;
String name = null;
TransactionalMapWrapper map = null;
protected MapConnection connection = null;
protected List listeners = new ArrayList();
protected PrintWriter out;
Map getMap() {
return map;
}
public void close() {
ConnectionEvent event = new ConnectionEvent(this,
ConnectionEvent.CONNECTION_CLOSED);
for (Iterator it = listeners.iterator(); it.hasNext();) {
((ConnectionEventListener) it.next()).connectionClosed(event);
}
connection.invalidate();
connection = null;
}
/**
* @see ManagedConnection#getConnection(Subject, ConnectionRequestInfo)
*/
public Object getConnection(Subject subject, ConnectionRequestInfo
cxRequestInfo) throws ResourceException {
name = ((MapConnectionSpec) cxRequestInfo).getName();
map = MemoryMapResourceManager.getInstance().lookup(name);
xares = new MapXAResource(map);
tx = new MapLocalTransaction(map);
if (connection == null) {
connection = new MapConnection(this);
}
return connection;
}
/**
* @see ManagedConnection#destroy()
*/
public void destroy() throws ResourceException {
if (connection != null) {
connection.invalidate();
connection = null;
}
listeners = null;
name = null;
map = null;
xares = null;
tx = null;
}
/**
* @see ManagedConnection#cleanup()
*/
public void cleanup() throws ResourceException {
if (connection != null) {
connection.invalidate();
}
}
/**
* @see ManagedConnection#associateConnection(Object)
*/
public void associateConnection(Object connection) throws ResourceException {
if (!(connection instanceof MapConnection)) {
throw new ResourceException("Connection is not of type MapConnection");
}
this.connection = (MapConnection)connection;
this.connection.mc = this;
}
/**
* @see ManagedConnection#addConnectionEventListener(ConnectionEventListener)
*/
public void addConnectionEventListener(ConnectionEventListener listener) {
listeners.add(listener);
}
/**
* @see ManagedConnection#removeConnectionEventListener(ConnectionEventListener)
*/
public void removeConnectionEventListener(ConnectionEventListener listener) {
listeners.remove(listener);
}
/**
* @see ManagedConnection#getXAResource()
*/
public XAResource getXAResource() throws ResourceException {
return xares;
}
/**
* @see ManagedConnection#getLocalTransaction()
*/
public LocalTransaction getLocalTransaction() throws ResourceException {
return tx;
}
/**
* @see ManagedConnection#getMetaData()
*/
public ManagedConnectionMetaData getMetaData() throws ResourceException {
return null;
}
/**
* @see ManagedConnection#setLogWriter(PrintWriter)
*/
public void setLogWriter(PrintWriter out) throws ResourceException {
this.out = out;
}
/**
* @see ManagedConnection#getLogWriter()
*/
public PrintWriter getLogWriter() throws ResourceException {
return out;
}
}
1.1
jakarta-commons-sandbox/transaction/src/java/org/apache/commons/transaction/memory/jca/MapLocalTransaction.java
Index: MapLocalTransaction.java
===================================================================
/*
* Created on 17.05.2004
*
* To change this generated comment go to
* Window>Preferences>Java>Code Generation>Code Template
*/
package org.apache.commons.transaction.memory.jca;
import javax.resource.ResourceException;
import org.apache.commons.transaction.memory.TransactionalMapWrapper;
/**
*
* @author <a href="mailto:[EMAIL PROTECTED]">Oliver Zeigermann</a>
*
*/
public class MapLocalTransaction implements javax.resource.spi.LocalTransaction,
javax.resource.cci.LocalTransaction {
TransactionalMapWrapper map;
public MapLocalTransaction(TransactionalMapWrapper map) {
this.map = map;
}
public void begin() throws ResourceException {
map.startTransaction();
}
public void commit() throws ResourceException {
map.commitTransaction();
}
public void rollback() throws ResourceException {
map.rollbackTransaction();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]