/**********************************************************************
Copyright (c) 2006 Andy Jefferson and others. All rights reserved.
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.
Contributors:
...
**********************************************************************/
package org.jpox.jdo;
import javax.jdo.JDOUserException;
import javax.jdo.datastore.JDOConnection;
import org.jpox.store.JPOXConnection;
import org.jpox.util.Localiser;
/**
* Implementation of a generic JDO connection for non-RDBMS datastores.
* Takes the native connection as input, implementing the JDOConnection interface.
*/
final public class JDOConnectionImpl implements JDOConnection, JPOXConnection
{
/** Localisation utility for output messages */
protected static final Localiser LOCALISER = Localiser.getInstance("org.jpox.jdo.Localisation",
JDOPersistenceManagerFactory.class.getClassLoader());
/** Native connection for this datastore. **/
private final Object nativeConnection;
/** run "onClose" on close call */
private final Runnable onClose;
/** whether this connection is available to the developer */
private boolean isAvailable = true;
/**
* Constructor.
* @param conn the native connection
* @param onClose the Runnable operation that will be called at user call to close()
*/
public JDOConnectionImpl(Object conn, Runnable onClose)
{
this.nativeConnection = conn;
this.onClose = onClose;
}
/**
* Method to close the connection.
*/
public void close()
{
if (!isAvailable)
{
throw new JDOUserException(LOCALISER.msg("046001"));
}
isAvailable = false;
onClose.run();
}
/**
* Accessor for the native connection for this datastore.
* For RDBMS this would be a java.sql.Connection, or for db4o an ObjectContainer etc.
* @return The native connection
*/
public Object getNativeConnection()
{
return nativeConnection;
}
}