01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jboss.ejb.plugins.cmp.jdbc.bridge;
23:
24: import org.jboss.ejb.EntityEnterpriseContext;
25: import org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager;
26: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCCMPFieldMetaData;
27: import org.jboss.ejb.plugins.keygenerator.KeyGeneratorFactory;
28: import org.jboss.ejb.plugins.keygenerator.KeyGenerator;
29: import org.jboss.deployment.DeploymentException;
30:
31: import javax.naming.InitialContext;
32: import javax.naming.NamingException;
33:
34: /**
35: *
36: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
37: */
38: public class JDBCKeyGenVersionFieldBridge extends
39: JDBCCMP2xVersionFieldBridge {
40: private final KeyGenerator keyGenerator;
41:
42: public JDBCKeyGenVersionFieldBridge(JDBCStoreManager manager,
43: JDBCCMPFieldMetaData metadata, String keygenFactoryName)
44: throws DeploymentException {
45: super (manager, metadata);
46: keyGenerator = initKeyGenerator(keygenFactoryName);
47: }
48:
49: public JDBCKeyGenVersionFieldBridge(JDBCCMP2xFieldBridge cmpField,
50: String keygenFactoryName) throws DeploymentException {
51: super (cmpField);
52: keyGenerator = initKeyGenerator(keygenFactoryName);
53: }
54:
55: private KeyGenerator initKeyGenerator(String keygenFactoryName)
56: throws DeploymentException {
57: try {
58: InitialContext ctx = new InitialContext();
59: KeyGeneratorFactory keygenFactory = (KeyGeneratorFactory) ctx
60: .lookup(keygenFactoryName);
61: return keygenFactory.getKeyGenerator();
62: } catch (NamingException e) {
63: throw new DeploymentException(
64: "Could not lookup key generator factory: "
65: + keygenFactoryName, e);
66: } catch (Exception e) {
67: throw new DeploymentException(
68: "Could not create KeyGenerator instance.", e);
69: }
70: }
71:
72: public void setFirstVersion(EntityEnterpriseContext ctx) {
73: Object version = keyGenerator.generateKey();
74: setInstanceValue(ctx, version);
75: }
76:
77: public Object updateVersion(EntityEnterpriseContext ctx) {
78: Object next = keyGenerator.generateKey();
79: setInstanceValue(ctx, next);
80: return next;
81: }
82: }
|