001: /**********************************************************************
002: Copyright (c) 2003 Erik Bengtson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015:
016: Contributors:
017: 2003 Andy Jefferson - coding standards
018: 2004 Andy Jefferson - fixed creation of repository when block failed
019: 2004 Andy Jefferson - removed MetaData requirement
020: 2006 Andy Jefferson - changed to hold the generator rather than creating one each time
021: 2006 Andy Jefferson - rewritten to handle the creation of PoidGenerator nad lookup.
022: ...
023: **********************************************************************/package org.jpox.store.poid;
024:
025: import java.lang.reflect.Constructor;
026: import java.util.HashMap;
027: import java.util.Map;
028: import java.util.Properties;
029:
030: import org.jpox.store.StoreManager;
031: import org.jpox.util.JPOXLogger;
032: import org.jpox.util.Localiser;
033:
034: /**
035: * Manager for the creation of PoidGenerators.
036: * Allows creation of generators and provides lookup by symbolic name.
037: *
038: * @version $Revision $
039: */
040: public class PoidManager {
041: /** Localisation of messages */
042: protected static final Localiser LOCALISER = Localiser
043: .getInstance("org.jpox.store.Localisation");
044:
045: /** Map of PoidGenerator keyed by the symbolic name. */
046: protected Map generatorsByName = new HashMap();
047:
048: /**
049: * Constructor.
050: */
051: public PoidManager() {
052: }
053:
054: /**
055: * Method to clear out the generators managed by this manager.
056: */
057: public void clear() {
058: generatorsByName.clear();
059: }
060:
061: /**
062: * Accessor for the PoidGenerator with the given symbolic name.
063: * @param name Name of the PoidGenerator when created
064: * @return The PoidGenerator with this name
065: */
066: public synchronized PoidGenerator getPoidGenerator(String name) {
067: if (name == null) {
068: return null;
069: }
070: return (PoidGenerator) generatorsByName.get(name);
071: }
072:
073: /**
074: * Method to create a PoidGenerator when the generator is datastore based.
075: * @param name Symbolic name of the generator
076: * @param generatorClass Class for the generator type
077: * @param props Properties to control the generator
078: * @param storeMgr Manager for the store
079: * @param connectionProvider Provider for connections
080: * @return The PoidGenerator
081: */
082: public synchronized PoidGenerator createPoidGenerator(String name,
083: Class generatorClass, Properties props,
084: StoreManager storeMgr,
085: PoidConnectionProvider connectionProvider) {
086: // Create the requested generator
087: PoidGenerator generator;
088: try {
089: if (JPOXLogger.POID.isDebugEnabled()) {
090: JPOXLogger.POID.debug(LOCALISER.msg("040001",
091: generatorClass.getName(), name));
092: }
093: Class[] argTypes = new Class[] { String.class,
094: Properties.class };
095: Object[] args = new Object[] { name, props };
096: Constructor ctor = generatorClass.getConstructor(argTypes);
097: generator = (PoidGenerator) ctor.newInstance(args);
098: } catch (Exception e) {
099: JPOXLogger.POID.error(e);
100: throw new PoidException(LOCALISER.msg("040000",
101: generatorClass.getName(), e), e);
102: }
103:
104: if (generator instanceof AbstractDatastorePoidGenerator
105: && storeMgr != null) {
106: // Set the store manager and connection provider for any datastore-based generators
107: ((AbstractDatastorePoidGenerator) generator)
108: .setStoreManager(storeMgr);
109: ((AbstractDatastorePoidGenerator) generator)
110: .setConnectionProvider(connectionProvider);
111: }
112:
113: // Store the generator
114: generatorsByName.put(name, generator);
115:
116: return generator;
117: }
118: }
|