Search in sources :

Example 1 with GeronimoEjbJarType

use of org.apache.openejb.jee.oejb2.GeronimoEjbJarType in project tomee by apache.

the class OpenEjb2Conversion method convertToGeronimoOpenejbXml.

/**
 * Actually called from ReadDescriptors as Geronimo needs this info early
 *
 * @param o2 OpenejbJarType
 * @return GeronimoEjbJarType
 */
public static GeronimoEjbJarType convertToGeronimoOpenejbXml(final OpenejbJarType o2) {
    final GeronimoEjbJarType g2 = new GeronimoEjbJarType();
    g2.setEnvironment(o2.getEnvironment());
    g2.setSecurity(o2.getSecurity());
    g2.getService().addAll(o2.getService());
    g2.getMessageDestination().addAll(o2.getMessageDestination());
    g2.getPersistence().addAll(o2.getPersistence());
    for (final org.apache.openejb.jee.oejb2.EnterpriseBean bean : o2.getEnterpriseBeans()) {
        g2.getAbstractNamingEntry().addAll(bean.getAbstractNamingEntry());
        g2.getPersistenceContextRef().addAll(bean.getPersistenceContextRef());
        g2.getPersistenceUnitRef().addAll(bean.getPersistenceUnitRef());
        g2.getEjbLocalRef().addAll(bean.getEjbLocalRef());
        g2.getEjbRef().addAll(bean.getEjbRef());
        g2.getResourceEnvRef().addAll(bean.getResourceEnvRef());
        g2.getResourceRef().addAll(bean.getResourceRef());
        g2.getServiceRef().addAll(bean.getServiceRef());
        if (bean instanceof RpcBean) {
            final RpcBean rpcBean = (RpcBean) bean;
            if (rpcBean.getTssLink() != null) {
                g2.getTssLink().add(new TssLinkType(rpcBean.getEjbName(), rpcBean.getTssLink(), rpcBean.getJndiName()));
            }
        }
        if (bean instanceof SessionBeanType) {
            final SessionBeanType sb = (SessionBeanType) bean;
            final WebServiceBindingType b = new WebServiceBindingType();
            b.setEjbName(sb.getEjbName());
            b.setWebServiceAddress(sb.getWebServiceAddress());
            b.setWebServiceVirtualHost(sb.getWebServiceVirtualHost());
            b.setWebServiceSecurity(sb.getWebServiceSecurity());
            if (b.containsData()) {
                g2.getWebServiceBinding().add(b);
            }
        }
    }
    return g2;
}
Also used : WebServiceBindingType(org.apache.openejb.jee.oejb2.WebServiceBindingType) RpcBean(org.apache.openejb.jee.oejb2.RpcBean) TssLinkType(org.apache.openejb.jee.oejb2.TssLinkType) SessionBeanType(org.apache.openejb.jee.oejb2.SessionBeanType) GeronimoEjbJarType(org.apache.openejb.jee.oejb2.GeronimoEjbJarType)

Example 2 with GeronimoEjbJarType

use of org.apache.openejb.jee.oejb2.GeronimoEjbJarType in project tomee by apache.

the class ReadDescriptors method readOpenejbJar.

private void readOpenejbJar(final EjbModule ejbModule) throws OpenEJBException {
    final Source source = getSource(ejbModule.getAltDDs().get("openejb-jar.xml"));
    if (source != null) {
        try {
            // Attempt to parse it first as a v3 descriptor
            final OpenejbJar openejbJar = JaxbOpenejbJar3.unmarshal(OpenejbJar.class, source.get()).postRead();
            ejbModule.setOpenejbJar(openejbJar);
        } catch (final Exception v3ParsingException) {
            // Attempt to parse it second as a v2 descriptor
            final OpenejbJar openejbJar = new OpenejbJar();
            ejbModule.setOpenejbJar(openejbJar);
            try {
                final JAXBElement element = (JAXBElement) JaxbOpenejbJar2.unmarshal(OpenejbJarType.class, source.get());
                final OpenejbJarType o2 = (OpenejbJarType) element.getValue();
                ejbModule.getAltDDs().put("openejb-jar.xml", o2);
                final GeronimoEjbJarType g2 = OpenEjb2Conversion.convertToGeronimoOpenejbXml(o2);
                ejbModule.getAltDDs().put("geronimo-openejb.xml", g2);
            } catch (final Exception v2ParsingException) {
                // Now we have to determine which error to throw; the v3 file exception or the fallback v2 file exception.
                final Exception[] realIssue = { v3ParsingException };
                try {
                    final SAXParserFactory factory = Saxs.namespaceAwareFactory();
                    final SAXParser parser = factory.newSAXParser();
                    parser.parse(source.get(), new DefaultHandler() {

                        public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException {
                            if (localName.equals("environment")) {
                                realIssue[0] = v2ParsingException;
                                throw new SAXException("Throw exception to stop parsing");
                            }
                            if (uri == null) {
                                return;
                            }
                            if (uri.contains("openejb-jar-2.") || uri.contains("geronimo.apache.org/xml/ns")) {
                                realIssue[0] = v2ParsingException;
                                throw new SAXException("Throw exception to stop parsing");
                            }
                        }
                    });
                } catch (final Exception dontCare) {
                // no-op
                }
                String filePath = "<error: could not be written>";
                try {
                    File tempFile;
                    try {
                        tempFile = File.createTempFile("openejb-jar-", ".xml");
                    } catch (final Throwable e) {
                        final File tmp = new File("tmp");
                        if (!tmp.exists() && !tmp.mkdirs()) {
                            throw new IOException("Failed to create local tmp directory: " + tmp.getAbsolutePath());
                        }
                        tempFile = File.createTempFile("openejb-jar-", ".xml", tmp);
                    }
                    try {
                        IO.copy(source.get(), tempFile);
                    } catch (final IOException e) {
                    // no-op
                    }
                    filePath = tempFile.getAbsolutePath();
                } catch (final IOException e) {
                // no-op
                }
                final Exception e = realIssue[0];
                if (e instanceof SAXException) {
                    throw new OpenEJBException("Cannot parse the openejb-jar.xml. Xml content written to: " + filePath, e);
                } else if (e instanceof JAXBException) {
                    throw new OpenEJBException("Cannot unmarshall the openejb-jar.xml. Xml content written to: " + filePath, e);
                } else if (e instanceof IOException) {
                    throw new OpenEJBException("Cannot read the openejb-jar.xml.", e);
                } else {
                    throw new OpenEJBException("Encountered unknown error parsing the openejb-jar.xml.", e);
                }
            }
        }
    }
    final Source source1 = getSource(ejbModule.getAltDDs().get("geronimo-openejb.xml"));
    if (source1 != null) {
        try {
            GeronimoEjbJarType geronimoEjbJarType = null;
            final Object o = JaxbOpenejbJar2.unmarshal(GeronimoEjbJarType.class, source1.get());
            if (o instanceof GeronimoEjbJarType) {
                geronimoEjbJarType = (GeronimoEjbJarType) o;
            } else if (o instanceof JAXBElement) {
                final JAXBElement element = (JAXBElement) o;
                geronimoEjbJarType = (GeronimoEjbJarType) element.getValue();
            }
            if (geronimoEjbJarType != null) {
                final Object nested = geronimoEjbJarType.getOpenejbJar();
                if (nested != null && nested instanceof OpenejbJar) {
                    final OpenejbJar existingOpenejbJar = ejbModule.getOpenejbJar();
                    if (existingOpenejbJar == null || existingOpenejbJar.getEjbDeploymentCount() <= 0) {
                        final OpenejbJar openejbJar = (OpenejbJar) nested;
                        ejbModule.getAltDDs().put("openejb-jar.xml", openejbJar);
                        ejbModule.setOpenejbJar(openejbJar);
                    }
                }
                ejbModule.getAltDDs().put("geronimo-openejb.xml", geronimoEjbJarType);
            }
        } catch (final Exception e) {
            throw new OpenEJBException("Failed parsing geronimo-openejb.xml", e);
        }
    }
}
Also used : OpenejbJarType(org.apache.openejb.jee.oejb2.OpenejbJarType) OpenEJBException(org.apache.openejb.OpenEJBException) JAXBException(javax.xml.bind.JAXBException) Attributes(org.xml.sax.Attributes) JAXBElement(javax.xml.bind.JAXBElement) IOException(java.io.IOException) InputSource(org.xml.sax.InputSource) OpenEJBException(org.apache.openejb.OpenEJBException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) GeronimoEjbJarType(org.apache.openejb.jee.oejb2.GeronimoEjbJarType) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) SAXParser(javax.xml.parsers.SAXParser) File(java.io.File) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 3 with GeronimoEjbJarType

use of org.apache.openejb.jee.oejb2.GeronimoEjbJarType in project tomee by apache.

the class OutputGeneratedDescriptors method writeGeronimoOpenejb.

private void writeGeronimoOpenejb(final EjbModule ejbModule) {
    try {
        final GeronimoEjbJarType geronimoEjbJarType = (GeronimoEjbJarType) ejbModule.getAltDDs().get("geronimo-openejb.xml");
        if (geronimoEjbJarType == null) {
            return;
        }
        final File tempFile = tempFile("geronimo-openejb-", ejbModule.getModuleId() + ".xml");
        final OutputStream out = IO.write(tempFile);
        try {
            JaxbOpenejbJar2.marshal(GeronimoEjbJarType.class, geronimoEjbJarType, out);
            logger.info("Dumping Generated geronimo-openejb.xml to: " + tempFile.getAbsolutePath());
        } catch (final JAXBException e) {
        // no-op
        } finally {
            IO.close(out);
        }
    } catch (final Exception e) {
    // no-op
    }
}
Also used : OutputStream(java.io.OutputStream) JAXBException(javax.xml.bind.JAXBException) File(java.io.File) OpenEJBException(org.apache.openejb.OpenEJBException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) GeronimoEjbJarType(org.apache.openejb.jee.oejb2.GeronimoEjbJarType)

Aggregations

GeronimoEjbJarType (org.apache.openejb.jee.oejb2.GeronimoEjbJarType)3 File (java.io.File)2 IOException (java.io.IOException)2 JAXBException (javax.xml.bind.JAXBException)2 OpenEJBException (org.apache.openejb.OpenEJBException)2 OutputStream (java.io.OutputStream)1 JAXBElement (javax.xml.bind.JAXBElement)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SAXParser (javax.xml.parsers.SAXParser)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1 OpenejbJarType (org.apache.openejb.jee.oejb2.OpenejbJarType)1 RpcBean (org.apache.openejb.jee.oejb2.RpcBean)1 SessionBeanType (org.apache.openejb.jee.oejb2.SessionBeanType)1 TssLinkType (org.apache.openejb.jee.oejb2.TssLinkType)1 WebServiceBindingType (org.apache.openejb.jee.oejb2.WebServiceBindingType)1 OpenejbJar (org.apache.openejb.jee.oejb3.OpenejbJar)1 Attributes (org.xml.sax.Attributes)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1 DefaultHandler (org.xml.sax.helpers.DefaultHandler)1