/projects/jboss-5.1.0/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/keygen/JDBCKeyGeneratorCreateCommand.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 90 lines · 51 code · 9 blank · 30 comment · 2 complexity · ef07be89cd63d450c5b6cdf0f8402efb MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2008, Red Hat Middleware LLC, and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * 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.keygen;
  23. import javax.ejb.CreateException;
  24. import javax.naming.InitialContext;
  25. import javax.naming.NamingException;
  26. import org.jboss.deployment.DeploymentException;
  27. import org.jboss.ejb.EntityEnterpriseContext;
  28. import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMPFieldBridge;
  29. import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCEntityCommandMetaData;
  30. import org.jboss.ejb.plugins.cmp.jdbc.JDBCInsertPKCreateCommand;
  31. import org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager;
  32. import org.jboss.ejb.plugins.keygenerator.KeyGenerator;
  33. import org.jboss.ejb.plugins.keygenerator.KeyGeneratorFactory;
  34. /**
  35. * JDBCKeyGeneratorCreateCommand executes an INSERT INTO query.
  36. * This command will ask the corresponding key generator for a
  37. * value for the primary key before inserting the row.
  38. *
  39. * @author <a href="mailto:loubyansky@hotmail.com">Alex Loubyansky</a>
  40. *
  41. * @version $Revision: 81030 $
  42. */
  43. public class JDBCKeyGeneratorCreateCommand extends JDBCInsertPKCreateCommand
  44. {
  45. protected KeyGenerator keyGenerator;
  46. protected JDBCCMPFieldBridge pkField;
  47. public void init(JDBCStoreManager manager) throws DeploymentException
  48. {
  49. super.init(manager);
  50. pkField = getGeneratedPKField();
  51. }
  52. protected void initEntityCommand(JDBCEntityCommandMetaData entityCommand) throws DeploymentException
  53. {
  54. super.initEntityCommand(entityCommand);
  55. String factoryName = entityCommand.getAttribute("key-generator-factory");
  56. if(factoryName == null)
  57. {
  58. throw new DeploymentException("key-generator-factory attribute must be set for entity " + entity.getEntityName());
  59. }
  60. try
  61. {
  62. KeyGeneratorFactory keyGeneratorFactory = (KeyGeneratorFactory) new InitialContext().lookup(factoryName);
  63. keyGenerator = keyGeneratorFactory.getKeyGenerator();
  64. }
  65. catch(NamingException e)
  66. {
  67. throw new DeploymentException("Error: can't find key generator factory: " + factoryName, e);
  68. }
  69. catch(Exception e)
  70. {
  71. throw new DeploymentException("Error: can't create key generator instance; key generator factory: " + factoryName, e);
  72. }
  73. }
  74. protected void generateFields(EntityEnterpriseContext ctx) throws CreateException
  75. {
  76. super.generateFields(ctx);
  77. Object pk = keyGenerator.generateKey();
  78. log.debug("Generated new pk: " + pk);
  79. pkField.setInstanceValue(ctx, pk);
  80. }
  81. }