Allow the scope of globally scoped annotations to be managed explicitly instead
of making them shared across a JVM.
This commit is contained in:
@@ -41,7 +41,6 @@ import com.samskivert.util.StringUtil;
|
|||||||
|
|
||||||
import static com.samskivert.jdbc.depot.Log.log;
|
import static com.samskivert.jdbc.depot.Log.log;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles the marshalling and unmarshalling of persistent instances to JDBC
|
* Handles the marshalling and unmarshalling of persistent instances to JDBC
|
||||||
* primitives ({@link PreparedStatement} and {@link ResultSet}).
|
* primitives ({@link PreparedStatement} and {@link ResultSet}).
|
||||||
@@ -57,7 +56,7 @@ public class DepotMarshaller<T>
|
|||||||
/**
|
/**
|
||||||
* Creates a marshaller for the specified persistent object class.
|
* Creates a marshaller for the specified persistent object class.
|
||||||
*/
|
*/
|
||||||
public DepotMarshaller (Class<T> pclass)
|
public DepotMarshaller (Class<T> pclass, PersistenceContext context)
|
||||||
{
|
{
|
||||||
_pclass = pclass;
|
_pclass = pclass;
|
||||||
|
|
||||||
@@ -69,7 +68,7 @@ public class DepotMarshaller<T>
|
|||||||
// table as those are shared across all entities
|
// table as those are shared across all entities
|
||||||
TableGenerator generator = pclass.getAnnotation(TableGenerator.class);
|
TableGenerator generator = pclass.getAnnotation(TableGenerator.class);
|
||||||
if (generator != null) {
|
if (generator != null) {
|
||||||
_generators.put(generator.name(), generator);
|
context.tableGenerators.put(generator.name(), generator);
|
||||||
}
|
}
|
||||||
|
|
||||||
// introspect on the class and create marshallers for persistent fields
|
// introspect on the class and create marshallers for persistent fields
|
||||||
@@ -107,7 +106,7 @@ public class DepotMarshaller<T>
|
|||||||
// check if this field defines a new TableGenerator
|
// check if this field defines a new TableGenerator
|
||||||
generator = field.getAnnotation(TableGenerator.class);
|
generator = field.getAnnotation(TableGenerator.class);
|
||||||
if (generator != null) {
|
if (generator != null) {
|
||||||
_generators.put(generator.name(), generator);
|
context.tableGenerators.put(generator.name(), generator);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -134,7 +133,7 @@ public class DepotMarshaller<T>
|
|||||||
|
|
||||||
case TABLE:
|
case TABLE:
|
||||||
String name = gv.generator();
|
String name = gv.generator();
|
||||||
generator = _generators.get(name);
|
generator = context.tableGenerators.get(name);
|
||||||
if (generator == null) {
|
if (generator == null) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"Unknown generator [generator=" + name + "]");
|
"Unknown generator [generator=" + name + "]");
|
||||||
@@ -226,7 +225,8 @@ public class DepotMarshaller<T>
|
|||||||
conn, getTableName(), _columnDefinitions, _postamble);
|
conn, getTableName(), _columnDefinitions, _postamble);
|
||||||
// TODO: insert current version into version table
|
// TODO: insert current version into version table
|
||||||
}
|
}
|
||||||
// if there is a key generator, initialize that too
|
|
||||||
|
// if we have a key generator, initialize that too
|
||||||
if (_keyGenerator != null) {
|
if (_keyGenerator != null) {
|
||||||
_keyGenerator.init(conn);
|
_keyGenerator.init(conn);
|
||||||
}
|
}
|
||||||
@@ -516,10 +516,6 @@ public class DepotMarshaller<T>
|
|||||||
/** Used when creating and migrating our table schema. */
|
/** Used when creating and migrating our table schema. */
|
||||||
protected String _postamble;
|
protected String _postamble;
|
||||||
|
|
||||||
/** A map of name to {@link TableGenerator}, scoped over all classes. */
|
|
||||||
protected static HashMap<String, TableGenerator> _generators =
|
|
||||||
new HashMap<String, TableGenerator>();
|
|
||||||
|
|
||||||
/** The name of the table we use to track schema versions. */
|
/** The name of the table we use to track schema versions. */
|
||||||
protected static final String SCHEMA_VERSION_TABLE = "DepotSchemaVersion";
|
protected static final String SCHEMA_VERSION_TABLE = "DepotSchemaVersion";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,9 +38,24 @@ import com.samskivert.jdbc.DuplicateKeyException;
|
|||||||
*/
|
*/
|
||||||
public class DepotRepository
|
public class DepotRepository
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Creates a repository with the supplied connection provider and its own
|
||||||
|
* private persistence context.
|
||||||
|
*/
|
||||||
protected DepotRepository (ConnectionProvider conprov)
|
protected DepotRepository (ConnectionProvider conprov)
|
||||||
|
{
|
||||||
|
this(conprov, new PersistenceContext());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a repository with the supplied connection provider and
|
||||||
|
* persistence context.
|
||||||
|
*/
|
||||||
|
protected DepotRepository (
|
||||||
|
ConnectionProvider conprov, PersistenceContext context)
|
||||||
{
|
{
|
||||||
_conprov = conprov;
|
_conprov = conprov;
|
||||||
|
_context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -411,7 +426,8 @@ public class DepotRepository
|
|||||||
@SuppressWarnings("unchecked")DepotMarshaller<T> marshaller =
|
@SuppressWarnings("unchecked")DepotMarshaller<T> marshaller =
|
||||||
(DepotMarshaller<T>)_marshallers.get(type);
|
(DepotMarshaller<T>)_marshallers.get(type);
|
||||||
if (marshaller == null) {
|
if (marshaller == null) {
|
||||||
_marshallers.put(type, marshaller = new DepotMarshaller<T>(type));
|
_marshallers.put(
|
||||||
|
type, marshaller = new DepotMarshaller<T>(type, _context));
|
||||||
// initialize the marshaller which may create or migrate the table
|
// initialize the marshaller which may create or migrate the table
|
||||||
// for its underlying persistent object
|
// for its underlying persistent object
|
||||||
final DepotMarshaller<T> fm = marshaller;
|
final DepotMarshaller<T> fm = marshaller;
|
||||||
@@ -570,7 +586,7 @@ public class DepotRepository
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected ConnectionProvider _conprov;
|
protected ConnectionProvider _conprov;
|
||||||
|
protected PersistenceContext _context;
|
||||||
protected HashMap<Class<?>, DepotMarshaller<?>> _marshallers =
|
protected HashMap<Class<?>, DepotMarshaller<?>> _marshallers =
|
||||||
new HashMap<Class<?>, DepotMarshaller<?>>();
|
new HashMap<Class<?>, DepotMarshaller<?>>();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
//
|
||||||
|
// samskivert library - useful routines for java programs
|
||||||
|
// Copyright (C) 2001-2006 Michael Bayne
|
||||||
|
//
|
||||||
|
// This library is free software; you can redistribute it and/or modify it
|
||||||
|
// under the terms of the GNU Lesser General Public License as published
|
||||||
|
// by the Free Software Foundation; either version 2.1 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
// Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public
|
||||||
|
// License along with this library; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
package com.samskivert.jdbc.depot;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import javax.persistence.TableGenerator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines a scope in which global annotations are shared.
|
||||||
|
*/
|
||||||
|
public class PersistenceContext
|
||||||
|
{
|
||||||
|
/** Map {@link TableGenerator} instances by name. */
|
||||||
|
public static HashMap<String, TableGenerator> tableGenerators =
|
||||||
|
new HashMap<String, TableGenerator>();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user