From dafeeec087f4b7aa3d18f4697f747de8b128f0dd Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 22 Sep 2006 18:59:48 +0000 Subject: [PATCH] Allow the scope of globally scoped annotations to be managed explicitly instead of making them shared across a JVM. --- .../jdbc/depot/DepotMarshaller.java | 16 ++++------ .../jdbc/depot/DepotRepository.java | 20 ++++++++++-- .../jdbc/depot/PersistenceContext.java | 32 +++++++++++++++++++ 3 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 src/java/com/samskivert/jdbc/depot/PersistenceContext.java diff --git a/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java b/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java index 9748003..1414fa9 100644 --- a/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java +++ b/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java @@ -41,7 +41,6 @@ import com.samskivert.util.StringUtil; import static com.samskivert.jdbc.depot.Log.log; - /** * Handles the marshalling and unmarshalling of persistent instances to JDBC * primitives ({@link PreparedStatement} and {@link ResultSet}). @@ -57,7 +56,7 @@ public class DepotMarshaller /** * Creates a marshaller for the specified persistent object class. */ - public DepotMarshaller (Class pclass) + public DepotMarshaller (Class pclass, PersistenceContext context) { _pclass = pclass; @@ -69,7 +68,7 @@ public class DepotMarshaller // table as those are shared across all entities TableGenerator generator = pclass.getAnnotation(TableGenerator.class); if (generator != null) { - _generators.put(generator.name(), generator); + context.tableGenerators.put(generator.name(), generator); } // introspect on the class and create marshallers for persistent fields @@ -107,7 +106,7 @@ public class DepotMarshaller // check if this field defines a new TableGenerator generator = field.getAnnotation(TableGenerator.class); if (generator != null) { - _generators.put(generator.name(), generator); + context.tableGenerators.put(generator.name(), generator); } } } @@ -134,7 +133,7 @@ public class DepotMarshaller case TABLE: String name = gv.generator(); - generator = _generators.get(name); + generator = context.tableGenerators.get(name); if (generator == null) { throw new IllegalArgumentException( "Unknown generator [generator=" + name + "]"); @@ -226,7 +225,8 @@ public class DepotMarshaller conn, getTableName(), _columnDefinitions, _postamble); // 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) { _keyGenerator.init(conn); } @@ -516,10 +516,6 @@ public class DepotMarshaller /** Used when creating and migrating our table schema. */ protected String _postamble; - /** A map of name to {@link TableGenerator}, scoped over all classes. */ - protected static HashMap _generators = - new HashMap(); - /** The name of the table we use to track schema versions. */ protected static final String SCHEMA_VERSION_TABLE = "DepotSchemaVersion"; } diff --git a/src/java/com/samskivert/jdbc/depot/DepotRepository.java b/src/java/com/samskivert/jdbc/depot/DepotRepository.java index 7a1f81b..8c977cd 100644 --- a/src/java/com/samskivert/jdbc/depot/DepotRepository.java +++ b/src/java/com/samskivert/jdbc/depot/DepotRepository.java @@ -38,9 +38,24 @@ import com.samskivert.jdbc.DuplicateKeyException; */ public class DepotRepository { + /** + * Creates a repository with the supplied connection provider and its own + * private persistence context. + */ 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; + _context = context; } /** @@ -411,7 +426,8 @@ public class DepotRepository @SuppressWarnings("unchecked")DepotMarshaller marshaller = (DepotMarshaller)_marshallers.get(type); if (marshaller == null) { - _marshallers.put(type, marshaller = new DepotMarshaller(type)); + _marshallers.put( + type, marshaller = new DepotMarshaller(type, _context)); // initialize the marshaller which may create or migrate the table // for its underlying persistent object final DepotMarshaller fm = marshaller; @@ -570,7 +586,7 @@ public class DepotRepository } protected ConnectionProvider _conprov; - + protected PersistenceContext _context; protected HashMap, DepotMarshaller> _marshallers = new HashMap, DepotMarshaller>(); } diff --git a/src/java/com/samskivert/jdbc/depot/PersistenceContext.java b/src/java/com/samskivert/jdbc/depot/PersistenceContext.java new file mode 100644 index 0000000..167261d --- /dev/null +++ b/src/java/com/samskivert/jdbc/depot/PersistenceContext.java @@ -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 tableGenerators = + new HashMap(); +}