PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/cmp/src/main/java/org/jboss/as/cmp/processors/CmpParsingProcessor.java

#
Java | 104 lines | 62 code | 14 blank | 28 comment | 8 complexity | a16ffc3f01ccdcda52edbc3e829f6b84 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat, Inc., 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.as.cmp.processors;
  23. import org.jboss.as.cmp.CmpMessages;
  24. import org.jboss.as.cmp.jdbc.metadata.JDBCApplicationMetaData;
  25. import org.jboss.as.cmp.jdbc.metadata.parser.JDBCMetaDataParser;
  26. import org.jboss.as.ejb3.deployment.EjbDeploymentAttachmentKeys;
  27. import org.jboss.as.server.deployment.DeploymentPhaseContext;
  28. import org.jboss.as.server.deployment.DeploymentUnit;
  29. import org.jboss.as.server.deployment.DeploymentUnitProcessingException;
  30. import org.jboss.as.server.deployment.DeploymentUnitProcessor;
  31. import org.jboss.metadata.ejb.spec.EjbJarMetaData;
  32. import org.jboss.metadata.parser.util.NoopXMLResolver;
  33. import org.jboss.modules.Module;
  34. import org.jboss.vfs.VFSUtils;
  35. import org.jboss.vfs.VirtualFile;
  36. import javax.xml.stream.XMLInputFactory;
  37. import javax.xml.stream.XMLStreamReader;
  38. import java.io.InputStream;
  39. /**
  40. * @author John Bailey
  41. */
  42. public class CmpParsingProcessor implements DeploymentUnitProcessor {
  43. public void deploy(final DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
  44. final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
  45. if (!CmpDeploymentMarker.isCmpDeployment(deploymentUnit)) {
  46. return;
  47. }
  48. final EjbJarMetaData jarMetaData = deploymentUnit.getAttachment(EjbDeploymentAttachmentKeys.EJB_JAR_METADATA);
  49. if (jarMetaData == null || jarMetaData.getEnterpriseBeans() == null) {
  50. throw CmpMessages.MESSAGES.invalidCmpDeployment(deploymentUnit);
  51. }
  52. final Module module = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.MODULE);
  53. final XMLInputFactory inputFactory = XMLInputFactory.newInstance();
  54. inputFactory.setXMLResolver(NoopXMLResolver.create());
  55. XMLStreamReader xmlReader = null;
  56. // 1. Create the initial JDBC App
  57. JDBCApplicationMetaData jdbcMetaData = new JDBCApplicationMetaData(jarMetaData, module.getClassLoader());
  58. // 2. Merge in the defaults from standardjbosscmp-jdbc.xml
  59. InputStream inputStream = null;
  60. try {
  61. inputStream = this.getClass().getClassLoader().getResourceAsStream("standardjbosscmp-jdbc.xml");
  62. xmlReader = inputFactory.createXMLStreamReader(inputStream);
  63. jdbcMetaData = JDBCMetaDataParser.parse(xmlReader, jdbcMetaData);
  64. } catch (Exception e) {
  65. throw CmpMessages.MESSAGES.failedToParse("standardjbosscmp-jdbc.xml", e);
  66. } finally {
  67. VFSUtils.safeClose(inputStream);
  68. }
  69. // 3. Merge in the app provided from jbosscmp-jdbc.xml
  70. final VirtualFile deploymentRoot = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.DEPLOYMENT_ROOT).getRoot();
  71. // Locate the descriptor
  72. final VirtualFile descriptor = deploymentRoot.getChild("META-INF/jbosscmp-jdbc.xml");
  73. JDBCApplicationMetaData deploymentJdbcApplicationMetaData = null;
  74. if (descriptor != null && descriptor.exists()) {
  75. try {
  76. inputStream = descriptor.openStream();
  77. xmlReader = inputFactory.createXMLStreamReader(inputStream);
  78. jdbcMetaData = JDBCMetaDataParser.parse(xmlReader, jdbcMetaData);
  79. } catch (Exception e) {
  80. throw CmpMessages.MESSAGES.failedToParse("jbosscmp-jdbc.xml", e);
  81. } finally {
  82. VFSUtils.safeClose(inputStream);
  83. }
  84. }
  85. deploymentUnit.putAttachment(Attachments.JDBC_APPLICATION_KEY, jdbcMetaData);
  86. }
  87. public void undeploy(DeploymentUnit deploymentUnit) {
  88. deploymentUnit.removeAttachment(Attachments.JDBC_APPLICATION_KEY);
  89. }
  90. }