Search in sources :

Example 1 with InjectionTarget$JAXB.writeInjectionTarget

use of org.apache.openejb.jee.InjectionTarget$JAXB.writeInjectionTarget in project tomee by apache.

the class LegacyProcessor method process.

public static void process(final Class<?> clazz, final EnterpriseBean bean) {
    if (bean instanceof SessionBean) {
        final SessionBean sessionBean = (SessionBean) bean;
        if (sessionBean.getSessionType() == STATEFUL && SessionSynchronization.class.isAssignableFrom(clazz)) {
            try {
                sessionBean.getAfterBegin().add(new LifecycleCallback(clazz.getMethod("afterBegin")));
                sessionBean.getBeforeCompletion().add(new LifecycleCallback(clazz.getMethod("beforeCompletion")));
                sessionBean.getAfterCompletion().add(new LifecycleCallback(clazz.getMethod("afterCompletion", boolean.class)));
            } catch (final NoSuchMethodException e) {
            // Ignore, should never happen
            }
        }
        if (javax.ejb.SessionBean.class.isAssignableFrom(clazz)) {
            final ResourceEnvRef ref = new ResourceEnvRef("javax.ejb.SessionBean/sessionContext", SessionContext.class);
            final InjectionTarget target = new InjectionTarget();
            target.setInjectionTargetClass(clazz);
            target.setInjectionTargetName("sessionContext");
            ref.getInjectionTarget().add(target);
            sessionBean.getResourceEnvRef().add(ref);
        }
    }
    if (bean instanceof MessageDrivenBean) {
        final MessageDrivenBean messageDrivenBean = (MessageDrivenBean) bean;
        if (javax.ejb.MessageDrivenBean.class.isAssignableFrom(clazz)) {
            final ResourceEnvRef ref = new ResourceEnvRef("javax.ejb.MessageDrivenBean/messageDrivenContext", MessageDrivenContext.class);
            final InjectionTarget target = new InjectionTarget();
            target.setInjectionTargetClass(clazz);
            target.setInjectionTargetName("messageDrivenContext");
            ref.getInjectionTarget().add(target);
            messageDrivenBean.getResourceEnvRef().add(ref);
        }
    }
}
Also used : SessionSynchronization(javax.ejb.SessionSynchronization) MessageDrivenBean(org.apache.openejb.jee.MessageDrivenBean) InjectionTarget(org.apache.openejb.jee.InjectionTarget) LifecycleCallback(org.apache.openejb.jee.LifecycleCallback) SessionBean(org.apache.openejb.jee.SessionBean) ResourceEnvRef(org.apache.openejb.jee.ResourceEnvRef)

Example 2 with InjectionTarget$JAXB.writeInjectionTarget

use of org.apache.openejb.jee.InjectionTarget$JAXB.writeInjectionTarget in project tomee by apache.

the class AutoConfig method getType.

private String getType(final JndiReference ref, final ClassLoader classLoader) throws OpenEJBException {
    final String refType = ref.getType();
    if (refType != null) {
        return refType;
    }
    if (classLoader != null) {
        final Set<InjectionTarget> injections = ref.getInjectionTarget();
        for (final InjectionTarget injection : injections) {
            try {
                final Class target = classLoader.loadClass(injection.getInjectionTargetClass().trim());
                final Class type = IntrospectionSupport.getPropertyType(target, injection.getInjectionTargetName().trim());
                return type.getName();
            } catch (final ClassNotFoundException | NoSuchFieldException e) {
            // ignore
            }
        }
    }
    throw new OpenEJBException("Unable to infer type for " + ref.getKey());
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) InjectionTarget(org.apache.openejb.jee.InjectionTarget)

Example 3 with InjectionTarget$JAXB.writeInjectionTarget

use of org.apache.openejb.jee.InjectionTarget$JAXB.writeInjectionTarget in project tomee by apache.

the class CleanEnvEntries method fillInMissingType.

private void fillInMissingType(final ClassLoader loader, final EnvEntry entry) {
    if (entry.getType() != null) {
        return;
    }
    // If it has the lookup supplied we don't care if there is no type
    if (entry.getLookupName() != null) {
        return;
    }
    // We can't imply type without at least one injection point
    if (entry.getInjectionTarget().size() == 0) {
        return;
    }
    final Set<Class> types = new HashSet<Class>();
    for (final InjectionTarget target : entry.getInjectionTarget()) {
        if (target.getInjectionTargetClass() == null) {
            continue;
        }
        if (target.getInjectionTargetName() == null) {
            continue;
        }
        types.add(getType(loader, target));
    }
    normalize(types);
    final Class<?> type = types.size() == 1 ? types.iterator().next() : String.class;
    entry.setType(type.getName());
}
Also used : InjectionTarget(org.apache.openejb.jee.InjectionTarget) HashSet(java.util.HashSet)

Example 4 with InjectionTarget$JAXB.writeInjectionTarget

use of org.apache.openejb.jee.InjectionTarget$JAXB.writeInjectionTarget in project tomee by apache.

the class CheckInjectionTargets method validate.

public void validate(final EjbModule ejbModule) {
    for (final EnterpriseBean bean : ejbModule.getEjbJar().getEnterpriseBeans()) {
        final List<JndiReference> entries = new ArrayList<JndiReference>();
        entries.addAll(bean.getEjbLocalRef());
        entries.addAll(bean.getEjbRef());
        entries.addAll(bean.getEnvEntry());
        entries.addAll(bean.getMessageDestinationRef());
        entries.addAll(bean.getPersistenceContextRef());
        entries.addAll(bean.getPersistenceUnitRef());
        entries.addAll(bean.getResourceEnvRef());
        entries.addAll(bean.getResourceRef());
        entries.addAll(bean.getServiceRef());
        for (final JndiReference reference : entries) {
            // check injection target
            for (final InjectionTarget target : reference.getInjectionTarget()) {
                boolean classPrefix = false;
                String name = target.getInjectionTargetName();
                if (name.startsWith(target.getInjectionTargetClass() + "/")) {
                    classPrefix = true;
                    name = name.substring(target.getInjectionTargetClass().length() + 1);
                }
                final String shortNameInvalid = name;
                if (name.startsWith("set") && name.length() >= 4 && Character.isUpperCase(name.charAt(3))) {
                    final StringBuilder correctName = new StringBuilder(name);
                    correctName.delete(0, 3);
                    correctName.setCharAt(0, Character.toLowerCase(correctName.charAt(0)));
                    final String shortNameCorrect = correctName.toString();
                    if (classPrefix) {
                        correctName.insert(0, target.getInjectionTargetClass() + "/");
                    }
                    warn(bean, "injectionTarget.nameContainsSet", target.getInjectionTargetName(), shortNameInvalid, shortNameCorrect, correctName, reference.getName(), reference.getClass().getSimpleName());
                    target.setInjectionTargetName(correctName.toString());
                }
            }
        }
    }
}
Also used : EnterpriseBean(org.apache.openejb.jee.EnterpriseBean) JndiReference(org.apache.openejb.jee.JndiReference) ArrayList(java.util.ArrayList) InjectionTarget(org.apache.openejb.jee.InjectionTarget)

Example 5 with InjectionTarget$JAXB.writeInjectionTarget

use of org.apache.openejb.jee.InjectionTarget$JAXB.writeInjectionTarget in project tomee by apache.

the class EjbLocalRef$JAXB method _write.

public static final void _write(final XoXMLStreamWriter writer, final EjbLocalRef ejbLocalRef, RuntimeContext context) throws Exception {
    if (ejbLocalRef == null) {
        writer.writeXsiNil();
        return;
    }
    if (context == null) {
        context = new RuntimeContext();
    }
    final String prefix = writer.getUniquePrefix("http://java.sun.com/xml/ns/javaee");
    if (EjbLocalRef.class != ejbLocalRef.getClass()) {
        context.unexpectedSubclass(writer, ejbLocalRef, EjbLocalRef.class);
        return;
    }
    context.beforeMarshal(ejbLocalRef, LifecycleCallback.NONE);
    // ATTRIBUTE: id
    final String idRaw = ejbLocalRef.id;
    if (idRaw != null) {
        String id = null;
        try {
            id = Adapters.collapsedStringAdapterAdapter.marshal(idRaw);
        } catch (final Exception e) {
            context.xmlAdapterError(ejbLocalRef, "id", CollapsedStringAdapter.class, String.class, String.class, e);
        }
        writer.writeAttribute("", "", "id", id);
    }
    // ELEMENT: descriptions
    Text[] descriptions = null;
    try {
        descriptions = ejbLocalRef.getDescriptions();
    } catch (final Exception e) {
        context.getterError(ejbLocalRef, "descriptions", EjbLocalRef.class, "getDescriptions", e);
    }
    if (descriptions != null) {
        for (final Text descriptionsItem : descriptions) {
            if (descriptionsItem != null) {
                writer.writeStartElement(prefix, "description", "http://java.sun.com/xml/ns/javaee");
                writeText(writer, descriptionsItem, context);
                writer.writeEndElement();
            } else {
                context.unexpectedNullValue(ejbLocalRef, "descriptions");
            }
        }
    }
    // ELEMENT: ejbRefName
    final String ejbRefNameRaw = ejbLocalRef.ejbRefName;
    String ejbRefName = null;
    try {
        ejbRefName = Adapters.collapsedStringAdapterAdapter.marshal(ejbRefNameRaw);
    } catch (final Exception e) {
        context.xmlAdapterError(ejbLocalRef, "ejbRefName", CollapsedStringAdapter.class, String.class, String.class, e);
    }
    if (ejbRefName != null) {
        writer.writeStartElement(prefix, "ejb-ref-name", "http://java.sun.com/xml/ns/javaee");
        writer.writeCharacters(ejbRefName);
        writer.writeEndElement();
    } else {
        context.unexpectedNullValue(ejbLocalRef, "ejbRefName");
    }
    // ELEMENT: ejbRefType
    final EjbRefType ejbRefType = ejbLocalRef.ejbRefType;
    if (ejbRefType != null) {
        writer.writeStartElement(prefix, "ejb-ref-type", "http://java.sun.com/xml/ns/javaee");
        writer.writeCharacters(toStringEjbRefType(ejbLocalRef, null, context, ejbRefType));
        writer.writeEndElement();
    }
    // ELEMENT: localHome
    final String localHomeRaw = ejbLocalRef.localHome;
    String localHome = null;
    try {
        localHome = Adapters.collapsedStringAdapterAdapter.marshal(localHomeRaw);
    } catch (final Exception e) {
        context.xmlAdapterError(ejbLocalRef, "localHome", CollapsedStringAdapter.class, String.class, String.class, e);
    }
    if (localHome != null) {
        writer.writeStartElement(prefix, "local-home", "http://java.sun.com/xml/ns/javaee");
        writer.writeCharacters(localHome);
        writer.writeEndElement();
    }
    // ELEMENT: local
    final String localRaw = ejbLocalRef.local;
    String local = null;
    try {
        local = Adapters.collapsedStringAdapterAdapter.marshal(localRaw);
    } catch (final Exception e) {
        context.xmlAdapterError(ejbLocalRef, "local", CollapsedStringAdapter.class, String.class, String.class, e);
    }
    if (local != null) {
        writer.writeStartElement(prefix, "local", "http://java.sun.com/xml/ns/javaee");
        writer.writeCharacters(local);
        writer.writeEndElement();
    }
    // ELEMENT: ejbLink
    final String ejbLinkRaw = ejbLocalRef.ejbLink;
    String ejbLink = null;
    try {
        ejbLink = Adapters.collapsedStringAdapterAdapter.marshal(ejbLinkRaw);
    } catch (final Exception e) {
        context.xmlAdapterError(ejbLocalRef, "ejbLink", CollapsedStringAdapter.class, String.class, String.class, e);
    }
    if (ejbLink != null) {
        writer.writeStartElement(prefix, "ejb-link", "http://java.sun.com/xml/ns/javaee");
        writer.writeCharacters(ejbLink);
        writer.writeEndElement();
    }
    // ELEMENT: mappedName
    final String mappedNameRaw = ejbLocalRef.mappedName;
    String mappedName = null;
    try {
        mappedName = Adapters.collapsedStringAdapterAdapter.marshal(mappedNameRaw);
    } catch (final Exception e) {
        context.xmlAdapterError(ejbLocalRef, "mappedName", CollapsedStringAdapter.class, String.class, String.class, e);
    }
    if (mappedName != null) {
        writer.writeStartElement(prefix, "mapped-name", "http://java.sun.com/xml/ns/javaee");
        writer.writeCharacters(mappedName);
        writer.writeEndElement();
    }
    // ELEMENT: injectionTarget
    final Set<InjectionTarget> injectionTarget = ejbLocalRef.injectionTarget;
    if (injectionTarget != null) {
        for (final InjectionTarget injectionTargetItem : injectionTarget) {
            if (injectionTargetItem != null) {
                writer.writeStartElement(prefix, "injection-target", "http://java.sun.com/xml/ns/javaee");
                writeInjectionTarget(writer, injectionTargetItem, context);
                writer.writeEndElement();
            } else {
                context.unexpectedNullValue(ejbLocalRef, "injectionTarget");
            }
        }
    }
    // ELEMENT: lookupName
    final String lookupNameRaw = ejbLocalRef.lookupName;
    String lookupName = null;
    try {
        lookupName = Adapters.collapsedStringAdapterAdapter.marshal(lookupNameRaw);
    } catch (final Exception e) {
        context.xmlAdapterError(ejbLocalRef, "lookupName", CollapsedStringAdapter.class, String.class, String.class, e);
    }
    if (lookupName != null) {
        writer.writeStartElement(prefix, "lookup-name", "http://java.sun.com/xml/ns/javaee");
        writer.writeCharacters(lookupName);
        writer.writeEndElement();
    }
    context.afterMarshal(ejbLocalRef, LifecycleCallback.NONE);
}
Also used : EjbRefType$JAXB.parseEjbRefType(org.apache.openejb.jee.EjbRefType$JAXB.parseEjbRefType) EjbRefType$JAXB.toStringEjbRefType(org.apache.openejb.jee.EjbRefType$JAXB.toStringEjbRefType) CollapsedStringAdapter(javax.xml.bind.annotation.adapters.CollapsedStringAdapter) Text$JAXB.readText(org.apache.openejb.jee.Text$JAXB.readText) Text$JAXB.writeText(org.apache.openejb.jee.Text$JAXB.writeText) InjectionTarget$JAXB.writeInjectionTarget(org.apache.openejb.jee.InjectionTarget$JAXB.writeInjectionTarget) InjectionTarget$JAXB.readInjectionTarget(org.apache.openejb.jee.InjectionTarget$JAXB.readInjectionTarget) RuntimeContext(org.metatype.sxc.jaxb.RuntimeContext)

Aggregations

CollapsedStringAdapter (javax.xml.bind.annotation.adapters.CollapsedStringAdapter)18 InjectionTarget$JAXB.readInjectionTarget (org.apache.openejb.jee.InjectionTarget$JAXB.readInjectionTarget)18 InjectionTarget$JAXB.writeInjectionTarget (org.apache.openejb.jee.InjectionTarget$JAXB.writeInjectionTarget)18 Text$JAXB.readText (org.apache.openejb.jee.Text$JAXB.readText)18 Text$JAXB.writeText (org.apache.openejb.jee.Text$JAXB.writeText)18 RuntimeContext (org.metatype.sxc.jaxb.RuntimeContext)18 QName (javax.xml.namespace.QName)10 Attribute (org.metatype.sxc.util.Attribute)9 XoXMLStreamReader (org.metatype.sxc.util.XoXMLStreamReader)9 InjectionTarget (org.apache.openejb.jee.InjectionTarget)8 LinkedHashSet (java.util.LinkedHashSet)5 ArrayList (java.util.ArrayList)4 EjbRefType$JAXB.parseEjbRefType (org.apache.openejb.jee.EjbRefType$JAXB.parseEjbRefType)4 EjbRefType$JAXB.toStringEjbRefType (org.apache.openejb.jee.EjbRefType$JAXB.toStringEjbRefType)4 EjbJar (org.apache.openejb.jee.EjbJar)3 EnvEntry (org.apache.openejb.jee.EnvEntry)3 Assembler (org.apache.openejb.assembler.classic.Assembler)2 ProxyFactoryInfo (org.apache.openejb.assembler.classic.ProxyFactoryInfo)2 SecurityServiceInfo (org.apache.openejb.assembler.classic.SecurityServiceInfo)2 StatelessSessionContainerInfo (org.apache.openejb.assembler.classic.StatelessSessionContainerInfo)2