PageRenderTime 31ms CodeModel.GetById 22ms app.highlight 7ms RepoModel.GetById 1ms 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
 23package org.jboss.as.cmp.processors;
 24
 25import org.jboss.as.cmp.CmpMessages;
 26import org.jboss.as.cmp.jdbc.metadata.JDBCApplicationMetaData;
 27import org.jboss.as.cmp.jdbc.metadata.parser.JDBCMetaDataParser;
 28import org.jboss.as.ejb3.deployment.EjbDeploymentAttachmentKeys;
 29import org.jboss.as.server.deployment.DeploymentPhaseContext;
 30import org.jboss.as.server.deployment.DeploymentUnit;
 31import org.jboss.as.server.deployment.DeploymentUnitProcessingException;
 32import org.jboss.as.server.deployment.DeploymentUnitProcessor;
 33import org.jboss.metadata.ejb.spec.EjbJarMetaData;
 34import org.jboss.metadata.parser.util.NoopXMLResolver;
 35import org.jboss.modules.Module;
 36import org.jboss.vfs.VFSUtils;
 37import org.jboss.vfs.VirtualFile;
 38
 39import javax.xml.stream.XMLInputFactory;
 40import javax.xml.stream.XMLStreamReader;
 41import java.io.InputStream;
 42
 43/**
 44 * @author John Bailey
 45 */
 46public class CmpParsingProcessor implements DeploymentUnitProcessor {
 47
 48    public void deploy(final DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
 49        final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
 50        if (!CmpDeploymentMarker.isCmpDeployment(deploymentUnit)) {
 51            return;
 52        }
 53        final EjbJarMetaData jarMetaData = deploymentUnit.getAttachment(EjbDeploymentAttachmentKeys.EJB_JAR_METADATA);
 54        if (jarMetaData == null || jarMetaData.getEnterpriseBeans() == null) {
 55            throw CmpMessages.MESSAGES.invalidCmpDeployment(deploymentUnit);
 56        }
 57
 58        final Module module = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.MODULE);
 59
 60        final XMLInputFactory inputFactory = XMLInputFactory.newInstance();
 61        inputFactory.setXMLResolver(NoopXMLResolver.create());
 62        XMLStreamReader xmlReader = null;
 63
 64
 65        // 1.  Create the initial JDBC App
 66        JDBCApplicationMetaData jdbcMetaData = new JDBCApplicationMetaData(jarMetaData, module.getClassLoader());
 67
 68        // 2.  Merge in the defaults from standardjbosscmp-jdbc.xml
 69        InputStream inputStream = null;
 70        try {
 71            inputStream = this.getClass().getClassLoader().getResourceAsStream("standardjbosscmp-jdbc.xml");
 72            xmlReader = inputFactory.createXMLStreamReader(inputStream);
 73            jdbcMetaData = JDBCMetaDataParser.parse(xmlReader, jdbcMetaData);
 74        } catch (Exception e) {
 75            throw CmpMessages.MESSAGES.failedToParse("standardjbosscmp-jdbc.xml", e);
 76        } finally {
 77            VFSUtils.safeClose(inputStream);
 78        }
 79
 80        // 3.  Merge in the app provided from jbosscmp-jdbc.xml
 81        final VirtualFile deploymentRoot = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.DEPLOYMENT_ROOT).getRoot();
 82        // Locate the descriptor
 83        final VirtualFile descriptor = deploymentRoot.getChild("META-INF/jbosscmp-jdbc.xml");
 84        JDBCApplicationMetaData deploymentJdbcApplicationMetaData = null;
 85        if (descriptor != null && descriptor.exists()) {
 86            try {
 87                inputStream = descriptor.openStream();
 88                xmlReader = inputFactory.createXMLStreamReader(inputStream);
 89                jdbcMetaData = JDBCMetaDataParser.parse(xmlReader, jdbcMetaData);
 90            } catch (Exception e) {
 91                throw CmpMessages.MESSAGES.failedToParse("jbosscmp-jdbc.xml", e);
 92            } finally {
 93                VFSUtils.safeClose(inputStream);
 94            }
 95        }
 96
 97
 98        deploymentUnit.putAttachment(Attachments.JDBC_APPLICATION_KEY, jdbcMetaData);
 99    }
100
101    public void undeploy(DeploymentUnit deploymentUnit) {
102        deploymentUnit.removeAttachment(Attachments.JDBC_APPLICATION_KEY);
103    }
104}