Michael Mogley wrote:

OK, I've got two Tomcat webapps. Each with it's own OJB.properties and repository xml files defining two different mappings and two different connection descriptors.

Ojb libraries are in common/lib to allow sharing among webapps.

What you can do is use different "OJB profiles" for each webapp and let the shared (static/global) instance of OJB contain only the OJB-internal repository mappings.


If you let "OJB.properties" and "repository.xml" reside in the shared instance, cut down the repository to a bare minimum:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE descriptor-repository SYSTEM "repository.dtd" [
    <!ENTITY internal        SYSTEM "repository_internal.xml">
]>
<descriptor-repository version="1.0">
    &internal;
</descriptor-repository>


The "OJB profiles" I am talking about are defined by the org.apache.ojb.broker.metadata.MetadataManager class and the methods:
addProfile()
loadProfile()


If you want to be able to use different OJB profiles for different threads you need to activate per-thread-repository handling with:
MetadataManager.getInstance().setEnablePerThreadChanges(true);




What I do is have a static initializer in one of my main persistence classes in one webapp set up an OJB profile like this:

(Java-indenting is pretty poor to make lines short...)

public static String profileKey = "APP_1";

static {
  try {
    MetadataManager mm = MetadataManager.getInstance();
    mm.setEnablePerThreadChanges(true);

    // Merge in JDBC connection-pools for application 1
    // (I do this globally and not per-thread)

    final ConnectionRepository connections;
    connections = mm.readConnectionRepository(
      "repository_database_app1.xml");
    mm.mergeConnectionRepository(connections);

    // Merge in class-descriptors for application 1
    // Made on a copy of the global repository, this is my "profile"
    DescriptorRepository repository = mm.copyOfGlobalRepository();
    final DescriptorRepository descriptors;
    descriptors = mm.readDescriptorRepository(
      "repository_user_app1.xml");
    mm.mergeDescriptorRepository(repository, descriptors, false);

    mm.addProfile(profileKey, repository);
  } catch (Throwable t) {

    // Just an example...
    throw new RuntimeException(t);
  }
}

Example "repository_database_app1.xml":
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE database-repository SYSTEM "repository.dtd">
<database-repository>
 <jdbc-connection-descriptor jcd-alias="APP1_ALIAS1" ...>
  <connection-pool .../>
  <sequence-manager .../>
  ....
 </jdbc-connection-descriptor>
</database-repository>

Example "repository_user_app1.xml":
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE descriptor-repository SYSTEM "repository.dtd">
<descriptor-repository version="1.0">
 <class-descriptor class="com.app1.FirstClass" .../>
 ...
</descriptor-repository>

Both these XML-files would reside in webapps/APP1/WEB-INF/classes or a JAR in webapps/APP1/WEB-INF/lib. (Mine are in a JAR which works fine.)

With per-thread support enabled and the static initializer done, I can then request the OJB-profile at runtime using my profileKey:

public void initialize() throws Exception {
  MetadataManager mm = MetadataManager.getInstance();
  mm.loadProfile(profileKey);
}


Note that the profile key is an arbitrary Object-key for a Map so you don't have to use String like I do; you could e.g. use Class-objects for dynamically looking up an OJB-profile for a given class from a central "profile repository".


HTH,
 Martin


P.S. The above snippets are from my first shot at OJB profiles, so I would appreciate if others who might have used it commented on my pattern. Thanks!


--
Martin Kal�n
Curalia AB              Web:  http://www.curalia.se
Orrspelsv�gen 2B        Mail: [EMAIL PROTECTED]
SE-182 79  Stocksund    Tel:  +46-8-410 064 40


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to