Search in sources :

Example 1 with XMLMapper

use of org.jboss.staxmapper.XMLMapper in project wildfly by wildfly.

the class ServiceDeploymentParsingProcessor method deploy.

/**
     * Process a deployment for jboss-service.xml files. Will parse the xml file and attach a configuration discovered
     * during processing.
     *
     * @param phaseContext the deployment unit context
     * @throws DeploymentUnitProcessingException
     */
public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final VirtualFile deploymentRoot = phaseContext.getDeploymentUnit().getAttachment(Attachments.DEPLOYMENT_ROOT).getRoot();
    if (deploymentRoot == null || !deploymentRoot.exists())
        return;
    VirtualFile serviceXmlFile = null;
    if (deploymentRoot.isDirectory()) {
        serviceXmlFile = deploymentRoot.getChild(SERVICE_DESCRIPTOR_PATH);
    } else if (deploymentRoot.getName().toLowerCase(Locale.ENGLISH).endsWith(SERVICE_DESCRIPTOR_SUFFIX)) {
        serviceXmlFile = deploymentRoot;
    }
    if (serviceXmlFile == null || !serviceXmlFile.exists())
        return;
    final XMLMapper xmlMapper = XMLMapper.Factory.create();
    final JBossServiceXmlDescriptorParser jBossServiceXmlDescriptorParser = new JBossServiceXmlDescriptorParser(JBossDescriptorPropertyReplacement.propertyReplacer(phaseContext.getDeploymentUnit()));
    xmlMapper.registerRootElement(new QName("urn:jboss:service:7.0", "server"), jBossServiceXmlDescriptorParser);
    xmlMapper.registerRootElement(new QName(null, "server"), jBossServiceXmlDescriptorParser);
    InputStream xmlStream = null;
    try {
        xmlStream = serviceXmlFile.openStream();
        final XMLStreamReader reader = inputFactory.createXMLStreamReader(xmlStream);
        final ParseResult<JBossServiceXmlDescriptor> result = new ParseResult<JBossServiceXmlDescriptor>();
        xmlMapper.parseDocument(result, reader);
        final JBossServiceXmlDescriptor xmlDescriptor = result.getResult();
        if (xmlDescriptor != null)
            phaseContext.getDeploymentUnit().putAttachment(JBossServiceXmlDescriptor.ATTACHMENT_KEY, xmlDescriptor);
        else
            throw SarLogger.ROOT_LOGGER.failedXmlParsing(serviceXmlFile);
    } catch (Exception e) {
        throw SarLogger.ROOT_LOGGER.failedXmlParsing(e, serviceXmlFile);
    } finally {
        VFSUtils.safeClose(xmlStream);
    }
}
Also used : VirtualFile(org.jboss.vfs.VirtualFile) XMLMapper(org.jboss.staxmapper.XMLMapper) JBossServiceXmlDescriptor(org.jboss.as.service.descriptor.JBossServiceXmlDescriptor) XMLStreamReader(javax.xml.stream.XMLStreamReader) ParseResult(org.jboss.as.service.descriptor.ParseResult) QName(javax.xml.namespace.QName) InputStream(java.io.InputStream) JBossServiceXmlDescriptorParser(org.jboss.as.service.descriptor.JBossServiceXmlDescriptorParser) DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException)

Example 2 with XMLMapper

use of org.jboss.staxmapper.XMLMapper in project wildfly by wildfly.

the class EJBClientDescriptorParsingProcessor method createMapper.

private XMLMapper createMapper(final DeploymentUnit deploymentUnit) {
    final XMLMapper mapper = XMLMapper.Factory.create();
    final PropertyReplacer propertyReplacer = EjbClientDescriptorPropertyReplacement.propertyReplacer(deploymentUnit);
    final EJBClientDescriptor10Parser ejbClientDescriptor10Parser = new EJBClientDescriptor10Parser(propertyReplacer);
    mapper.registerRootElement(ROOT_1_0, ejbClientDescriptor10Parser);
    final EJBClientDescriptor11Parser ejbClientDescriptor11Parser = new EJBClientDescriptor11Parser(propertyReplacer);
    mapper.registerRootElement(ROOT_1_1, ejbClientDescriptor11Parser);
    final EJBClientDescriptor11Parser ejbClientDescriptor12Parser = new EJBClientDescriptor12Parser(propertyReplacer);
    mapper.registerRootElement(ROOT_1_2, ejbClientDescriptor12Parser);
    final EJBClientDescriptor13Parser ejbClientDescriptor13Parser = new EJBClientDescriptor13Parser(propertyReplacer);
    mapper.registerRootElement(ROOT_1_3, ejbClientDescriptor13Parser);
    mapper.registerRootElement(ROOT_NO_NAMESPACE, ejbClientDescriptor13Parser);
    return mapper;
}
Also used : XMLMapper(org.jboss.staxmapper.XMLMapper) PropertyReplacer(org.jboss.metadata.property.PropertyReplacer)

Example 3 with XMLMapper

use of org.jboss.staxmapper.XMLMapper in project wildfly by wildfly.

the class EJBClientDescriptorParsingProcessor method deploy.

@Override
public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    final XMLMapper mapper = createMapper(deploymentUnit);
    final ResourceRoot resourceRoot = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.DEPLOYMENT_ROOT);
    final ServiceModuleLoader moduleLoader = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.SERVICE_MODULE_LOADER);
    VirtualFile descriptorFile = null;
    for (final String loc : EJB_CLIENT_DESCRIPTOR_LOCATIONS) {
        final VirtualFile file = resourceRoot.getRoot().getChild(loc);
        if (file.exists()) {
            descriptorFile = file;
            break;
        }
    }
    if (descriptorFile == null) {
        return;
    }
    if (deploymentUnit.getParent() != null) {
        EeLogger.ROOT_LOGGER.subdeploymentIgnored(descriptorFile.getPathName());
        return;
    }
    final File ejbClientDeploymentDescriptorFile;
    try {
        ejbClientDeploymentDescriptorFile = descriptorFile.getPhysicalFile();
    } catch (IOException e) {
        throw EeLogger.ROOT_LOGGER.failedToProcessEJBClientDescriptor(e);
    }
    final EJBClientDescriptorMetaData ejbClientDescriptorMetaData = parse(ejbClientDeploymentDescriptorFile, mapper);
    EeLogger.ROOT_LOGGER.debugf("Successfully parsed jboss-ejb-client.xml for deployment unit %s", deploymentUnit);
    // attach the metadata
    deploymentUnit.putAttachment(Attachments.EJB_CLIENT_METADATA, ejbClientDescriptorMetaData);
}
Also used : VirtualFile(org.jboss.vfs.VirtualFile) ResourceRoot(org.jboss.as.server.deployment.module.ResourceRoot) XMLMapper(org.jboss.staxmapper.XMLMapper) EJBClientDescriptorMetaData(org.jboss.as.ee.metadata.EJBClientDescriptorMetaData) ServiceModuleLoader(org.jboss.as.server.moduleservice.ServiceModuleLoader) IOException(java.io.IOException) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) File(java.io.File) VirtualFile(org.jboss.vfs.VirtualFile)

Example 4 with XMLMapper

use of org.jboss.staxmapper.XMLMapper in project wildfly by wildfly.

the class FileTimerPersistence method loadTimersFromFile.

private Map<String, TimerImpl> loadTimersFromFile(String timedObjectId, TimerServiceImpl timerService) {
    Map<String, TimerImpl> timers = new HashMap<>();
    String directory = getDirectory(timedObjectId);
    timers.putAll(LegacyFileStore.loadTimersFromFile(timedObjectId, timerService, directory, factory, configuration));
    for (Map.Entry<String, TimerImpl> entry : timers.entrySet()) {
        //write legacy timers into the new format
        writeFile(entry.getValue());
    //the legacy code handling code will write a marker file, to make sure that the old timers will not be loaded on next restart.
    }
    final File file = new File(directory);
    if (!file.exists()) {
        //no timers exist yet
        return timers;
    } else if (!file.isDirectory()) {
        EJB3_TIMER_LOGGER.failToRestoreTimers(file);
        return timers;
    }
    final XMLMapper mapper = createMapper(timerService);
    for (File timerFile : file.listFiles()) {
        if (!timerFile.getName().endsWith(".xml")) {
            continue;
        }
        FileInputStream in = null;
        try {
            in = new FileInputStream(timerFile);
            final XMLInputFactory inputFactory = INPUT_FACTORY;
            setIfSupported(inputFactory, XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
            setIfSupported(inputFactory, XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
            final XMLStreamReader streamReader = inputFactory.createXMLStreamReader(in);
            try {
                List<TimerImpl> timerList = new ArrayList<>();
                mapper.parseDocument(timerList, streamReader);
                for (TimerImpl timer : timerList) {
                    if (timer.getId().equals("deleted-timer")) {
                        timerFile.delete();
                        break;
                    }
                    timers.put(timer.getId(), timer);
                }
            } finally {
                safeClose(in);
            }
        } catch (Exception e) {
            EJB3_TIMER_LOGGER.failToRestoreTimersFromFile(timerFile, e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    EJB3_TIMER_LOGGER.failToCloseFile(e);
                }
            }
        }
    }
    return timers;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) SystemException(javax.transaction.SystemException) XMLMapper(org.jboss.staxmapper.XMLMapper) TimerImpl(org.jboss.as.ejb3.timerservice.TimerImpl) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) File(java.io.File) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 5 with XMLMapper

use of org.jboss.staxmapper.XMLMapper in project wildfly by wildfly.

the class FileTimerPersistence method writeFile.

private void writeFile(TimerImpl timer) {
    final File file = fileName(timer.getTimedObjectId(), timer.getId());
    //if the timer is expired or cancelled delete the file
    if (timer.getState() == TimerState.CANCELED || timer.getState() == TimerState.EXPIRED) {
        if (file.exists()) {
            file.delete();
        }
        return;
    }
    try {
        FileOutputStream out = new FileOutputStream(file);
        try {
            XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(out);
            XMLMapper mapper = createMapper(timer.getTimerService());
            mapper.deparseDocument(new EjbTimerXmlPersister(factory, configuration), Collections.singletonList(timer), writer);
            writer.flush();
            writer.close();
        } finally {
            safeClose(out);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : XMLMapper(org.jboss.staxmapper.XMLMapper) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) FileOutputStream(java.io.FileOutputStream) File(java.io.File) IOException(java.io.IOException) SystemException(javax.transaction.SystemException)

Aggregations

XMLMapper (org.jboss.staxmapper.XMLMapper)7 File (java.io.File)4 IOException (java.io.IOException)3 XMLStreamReader (javax.xml.stream.XMLStreamReader)3 VirtualFile (org.jboss.vfs.VirtualFile)3 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 SystemException (javax.transaction.SystemException)2 QName (javax.xml.namespace.QName)2 XMLInputFactory (javax.xml.stream.XMLInputFactory)2 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)2 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)2 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1