Search in sources :

Example 1 with TimerSchedule

use of org.apache.openejb.jee.TimerSchedule in project tomee by apache.

the class Timer$JAXB method _read.

public static final Timer _read(final XoXMLStreamReader reader, RuntimeContext context) throws Exception {
    // Check for xsi:nil
    if (reader.isXsiNil()) {
        return null;
    }
    if (context == null) {
        context = new RuntimeContext();
    }
    final Timer timer = new Timer();
    context.beforeUnmarshal(timer, LifecycleCallback.NONE);
    ArrayList<Text> descriptions = null;
    // Check xsi:type
    final QName xsiType = reader.getXsiType();
    if (xsiType != null) {
        if (("timerType" != xsiType.getLocalPart()) || ("http://java.sun.com/xml/ns/javaee" != xsiType.getNamespaceURI())) {
            return context.unexpectedXsiType(reader, Timer.class);
        }
    }
    // Read attributes
    for (final Attribute attribute : reader.getAttributes()) {
        if (("id" == attribute.getLocalName()) && (("" == attribute.getNamespace()) || (attribute.getNamespace() == null))) {
            // ATTRIBUTE: id
            final String id = Adapters.collapsedStringAdapterAdapter.unmarshal(attribute.getValue());
            context.addXmlId(reader, id, timer);
            timer.id = id;
        } else if (XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI != attribute.getNamespace()) {
            context.unexpectedAttribute(attribute, new QName("", "id"));
        }
    }
    // Read elements
    for (final XoXMLStreamReader elementReader : reader.getChildElements()) {
        if (("description" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
            // ELEMENT: descriptions
            final Text descriptionsItem = readText(elementReader, context);
            if (descriptions == null) {
                descriptions = new ArrayList<Text>();
            }
            descriptions.add(descriptionsItem);
        } else if (("schedule" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
            // ELEMENT: schedule
            final TimerSchedule schedule = readTimerSchedule(elementReader, context);
            timer.schedule = schedule;
        } else if (("start" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
            // ELEMENT: start
            final XMLGregorianCalendar start = datatypeFactory.newXMLGregorianCalendar(elementReader.getElementAsString());
            timer.start = start;
        } else if (("end" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
            // ELEMENT: end
            final XMLGregorianCalendar end = datatypeFactory.newXMLGregorianCalendar(elementReader.getElementAsString());
            timer.end = end;
        } else if (("timeout-method" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
            // ELEMENT: timeoutMethod
            final NamedMethod timeoutMethod = readNamedMethod(elementReader, context);
            timer.timeoutMethod = timeoutMethod;
        } else if (("persistent" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
            // ELEMENT: persistent
            final Boolean persistent = ("1".equals(elementReader.getElementAsString()) || "true".equals(elementReader.getElementAsString()));
            timer.persistent = persistent;
        } else if (("timezone" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
            // ELEMENT: timezone
            final String timezoneRaw = elementReader.getElementAsString();
            final String timezone;
            try {
                timezone = Adapters.collapsedStringAdapterAdapter.unmarshal(timezoneRaw);
            } catch (final Exception e) {
                context.xmlAdapterError(elementReader, CollapsedStringAdapter.class, String.class, String.class, e);
                continue;
            }
            timer.timezone = timezone;
        } else if (("info" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
            // ELEMENT: info
            final String infoRaw = elementReader.getElementAsString();
            final String info;
            try {
                info = Adapters.collapsedStringAdapterAdapter.unmarshal(infoRaw);
            } catch (final Exception e) {
                context.xmlAdapterError(elementReader, CollapsedStringAdapter.class, String.class, String.class, e);
                continue;
            }
            timer.info = info;
        } else {
            context.unexpectedElement(elementReader, new QName("http://java.sun.com/xml/ns/javaee", "description"), new QName("http://java.sun.com/xml/ns/javaee", "schedule"), new QName("http://java.sun.com/xml/ns/javaee", "start"), new QName("http://java.sun.com/xml/ns/javaee", "end"), new QName("http://java.sun.com/xml/ns/javaee", "timeout-method"), new QName("http://java.sun.com/xml/ns/javaee", "persistent"), new QName("http://java.sun.com/xml/ns/javaee", "timezone"), new QName("http://java.sun.com/xml/ns/javaee", "info"));
        }
    }
    if (descriptions != null) {
        try {
            timer.setDescriptions(descriptions.toArray(new Text[descriptions.size()]));
        } catch (final Exception e) {
            context.setterError(reader, Timer.class, "setDescriptions", Text[].class, e);
        }
    }
    context.afterUnmarshal(timer, LifecycleCallback.NONE);
    return timer;
}
Also used : CollapsedStringAdapter(javax.xml.bind.annotation.adapters.CollapsedStringAdapter) Attribute(org.metatype.sxc.util.Attribute) QName(javax.xml.namespace.QName) Text$JAXB.readText(org.apache.openejb.jee.Text$JAXB.readText) Text$JAXB.writeText(org.apache.openejb.jee.Text$JAXB.writeText) NamedMethod$JAXB.readNamedMethod(org.apache.openejb.jee.NamedMethod$JAXB.readNamedMethod) NamedMethod$JAXB.writeNamedMethod(org.apache.openejb.jee.NamedMethod$JAXB.writeNamedMethod) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) TimerSchedule$JAXB.readTimerSchedule(org.apache.openejb.jee.TimerSchedule$JAXB.readTimerSchedule) TimerSchedule$JAXB.writeTimerSchedule(org.apache.openejb.jee.TimerSchedule$JAXB.writeTimerSchedule) RuntimeContext(org.metatype.sxc.jaxb.RuntimeContext) XoXMLStreamReader(org.metatype.sxc.util.XoXMLStreamReader)

Example 2 with TimerSchedule

use of org.apache.openejb.jee.TimerSchedule in project tomee by apache.

the class ScheduleTest method testSchedule.

public void testSchedule() throws Exception {
    System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, LocalInitialContextFactory.class.getName());
    final Assembler assembler = new Assembler();
    final ConfigurationFactory config = new ConfigurationFactory();
    assembler.createProxyFactory(config.configureService(ProxyFactoryInfo.class));
    assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
    assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
    final EjbJar ejbJar = new EjbJar();
    // Configure schedule by deployment plan
    final StatelessBean subBeanA = new StatelessBean(SubBeanA.class);
    final Timer subBeanATimer = new Timer();
    subBeanATimer.setTimeoutMethod(new NamedMethod("subBeanA", "javax.ejb.Timer"));
    final TimerSchedule timerScheduleA = new TimerSchedule();
    timerScheduleA.setSecond("2");
    timerScheduleA.setMinute("*");
    timerScheduleA.setHour("*");
    subBeanATimer.setSchedule(timerScheduleA);
    subBeanATimer.setInfo("SubBeanAInfo");
    subBeanA.getTimer().add(subBeanATimer);
    ejbJar.addEnterpriseBean(subBeanA);
    // Configure schedule by annotation
    final StatelessBean subBeanB = new StatelessBean(SubBeanB.class);
    ejbJar.addEnterpriseBean(subBeanB);
    // Override aroundTimeout annotation by deployment plan
    final StatelessBean subBeanC = new StatelessBean(SubBeanC.class);
    final Timer subBeanCTimer = new Timer();
    subBeanCTimer.setTimeoutMethod(new NamedMethod("subBeanC", "javax.ejb.Timer"));
    final TimerSchedule timerScheduleC = new TimerSchedule();
    timerScheduleC.setSecond("2");
    timerScheduleC.setMinute("*");
    timerScheduleC.setHour("*");
    subBeanCTimer.setSchedule(timerScheduleC);
    subBeanCTimer.setInfo("SubBeanCInfo");
    subBeanC.getTimer().add(subBeanCTimer);
    ejbJar.addEnterpriseBean(subBeanC);
    final StatefulBean subBeanM = new StatefulBean(SubBeanM.class);
    ejbJar.addEnterpriseBean(subBeanM);
    final EjbJarInfo ejbJarInfo = config.configureApplication(ejbJar);
    assembler.createApplication(ejbJarInfo);
    countDownLatch.await(1L, TimeUnit.MINUTES);
    // A better way for validation ?
    int beforeAroundInvocationCount = 0;
    int afterAroundInvocationCount = 0;
    int timeoutInvocationCount = 0;
    final int size;
    synchronized (result) {
        size = result.size();
        for (final Call call : result) {
            switch(call) {
                case BEAN_BEFORE_AROUNDTIMEOUT:
                    beforeAroundInvocationCount++;
                    break;
                case BEAN_AFTER_AROUNDTIMEOUT:
                    afterAroundInvocationCount++;
                    break;
                case TIMEOUT:
                    timeoutInvocationCount++;
                    break;
            }
        }
    }
    assertEquals(3, beforeAroundInvocationCount);
    assertEquals(3, afterAroundInvocationCount);
    assertEquals(3, timeoutInvocationCount);
    assertEquals(9, size);
}
Also used : StatefulBean(org.apache.openejb.jee.StatefulBean) LocalInitialContextFactory(org.apache.openejb.core.LocalInitialContextFactory) NamedMethod(org.apache.openejb.jee.NamedMethod) ProxyFactoryInfo(org.apache.openejb.assembler.classic.ProxyFactoryInfo) TimerSchedule(org.apache.openejb.jee.TimerSchedule) TransactionServiceInfo(org.apache.openejb.assembler.classic.TransactionServiceInfo) StatelessBean(org.apache.openejb.jee.StatelessBean) Timer(org.apache.openejb.jee.Timer) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) Assembler(org.apache.openejb.assembler.classic.Assembler) SecurityServiceInfo(org.apache.openejb.assembler.classic.SecurityServiceInfo) EjbJarInfo(org.apache.openejb.assembler.classic.EjbJarInfo) EjbJar(org.apache.openejb.jee.EjbJar)

Example 3 with TimerSchedule

use of org.apache.openejb.jee.TimerSchedule in project tomee by apache.

the class EjbJarInfoBuilder method copySchedules.

private void copySchedules(final List<Timer> timers, final List<MethodScheduleInfo> scheduleInfos) {
    final Map<NamedMethod, MethodScheduleInfo> methodScheduleInfoMap = new HashMap<>();
    for (final Timer timer : timers) {
        final NamedMethod timeoutMethod = timer.getTimeoutMethod();
        MethodScheduleInfo methodScheduleInfo = methodScheduleInfoMap.get(timer.getTimeoutMethod());
        if (methodScheduleInfo == null) {
            methodScheduleInfo = new MethodScheduleInfo();
            methodScheduleInfoMap.put(timeoutMethod, methodScheduleInfo);
            methodScheduleInfo.method = toInfo(timeoutMethod);
        }
        final ScheduleInfo scheduleInfo = new ScheduleInfo();
        // Copy TimerSchedule
        final TimerSchedule timerSchedule = timer.getSchedule();
        if (timerSchedule != null) {
            scheduleInfo.second = timerSchedule.getSecond();
            scheduleInfo.minute = timerSchedule.getMinute();
            scheduleInfo.hour = timerSchedule.getHour();
            scheduleInfo.dayOfWeek = timerSchedule.getDayOfWeek();
            scheduleInfo.dayOfMonth = timerSchedule.getDayOfMonth();
            scheduleInfo.month = timerSchedule.getMonth();
            scheduleInfo.year = timerSchedule.getYear();
        }
        // Copy other attributes
        scheduleInfo.timezone = timer.getTimezone();
        if (timer.getStart() != null) {
            scheduleInfo.start = timer.getStart().toGregorianCalendar().getTime();
        }
        if (timer.getEnd() != null) {
            scheduleInfo.end = timer.getEnd().toGregorianCalendar().getTime();
        }
        scheduleInfo.info = timer.getInfo();
        if (timer.getPersistent() != null) {
            scheduleInfo.persistent = timer.getPersistent();
        }
        methodScheduleInfo.schedules.add(scheduleInfo);
    }
    scheduleInfos.addAll(methodScheduleInfoMap.values());
}
Also used : MethodScheduleInfo(org.apache.openejb.assembler.classic.MethodScheduleInfo) TimerSchedule(org.apache.openejb.jee.TimerSchedule) Timer(org.apache.openejb.jee.Timer) HashMap(java.util.HashMap) NamedMethod(org.apache.openejb.jee.NamedMethod) MethodScheduleInfo(org.apache.openejb.assembler.classic.MethodScheduleInfo) ScheduleInfo(org.apache.openejb.assembler.classic.ScheduleInfo)

Example 4 with TimerSchedule

use of org.apache.openejb.jee.TimerSchedule in project tomee by apache.

the class Timer$JAXB method _write.

public static final void _write(final XoXMLStreamWriter writer, final Timer timer, RuntimeContext context) throws Exception {
    if (timer == null) {
        writer.writeXsiNil();
        return;
    }
    if (context == null) {
        context = new RuntimeContext();
    }
    final String prefix = writer.getUniquePrefix("http://java.sun.com/xml/ns/javaee");
    if (Timer.class != timer.getClass()) {
        context.unexpectedSubclass(writer, timer, Timer.class);
        return;
    }
    context.beforeMarshal(timer, LifecycleCallback.NONE);
    // ATTRIBUTE: id
    final String idRaw = timer.id;
    if (idRaw != null) {
        String id = null;
        try {
            id = Adapters.collapsedStringAdapterAdapter.marshal(idRaw);
        } catch (final Exception e) {
            context.xmlAdapterError(timer, "id", CollapsedStringAdapter.class, String.class, String.class, e);
        }
        writer.writeAttribute("", "", "id", id);
    }
    // ELEMENT: descriptions
    Text[] descriptions = null;
    try {
        descriptions = timer.getDescriptions();
    } catch (final Exception e) {
        context.getterError(timer, "descriptions", Timer.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(timer, "descriptions");
            }
        }
    }
    // ELEMENT: schedule
    final TimerSchedule schedule = timer.schedule;
    if (schedule != null) {
        writer.writeStartElement(prefix, "schedule", "http://java.sun.com/xml/ns/javaee");
        writeTimerSchedule(writer, schedule, context);
        writer.writeEndElement();
    } else {
        context.unexpectedNullValue(timer, "schedule");
    }
    // ELEMENT: start
    final XMLGregorianCalendar start = timer.start;
    if (start != null) {
        writer.writeStartElement(prefix, "start", "http://java.sun.com/xml/ns/javaee");
        writer.writeCharacters(start.toXMLFormat());
        writer.writeEndElement();
    }
    // ELEMENT: end
    final XMLGregorianCalendar end = timer.end;
    if (end != null) {
        writer.writeStartElement(prefix, "end", "http://java.sun.com/xml/ns/javaee");
        writer.writeCharacters(end.toXMLFormat());
        writer.writeEndElement();
    }
    // ELEMENT: timeoutMethod
    final NamedMethod timeoutMethod = timer.timeoutMethod;
    if (timeoutMethod != null) {
        writer.writeStartElement(prefix, "timeout-method", "http://java.sun.com/xml/ns/javaee");
        writeNamedMethod(writer, timeoutMethod, context);
        writer.writeEndElement();
    } else {
        context.unexpectedNullValue(timer, "timeoutMethod");
    }
    // ELEMENT: persistent
    final Boolean persistent = timer.persistent;
    if (persistent != null) {
        writer.writeStartElement(prefix, "persistent", "http://java.sun.com/xml/ns/javaee");
        writer.writeCharacters(Boolean.toString(persistent));
        writer.writeEndElement();
    }
    // ELEMENT: timezone
    final String timezoneRaw = timer.timezone;
    String timezone = null;
    try {
        timezone = Adapters.collapsedStringAdapterAdapter.marshal(timezoneRaw);
    } catch (final Exception e) {
        context.xmlAdapterError(timer, "timezone", CollapsedStringAdapter.class, String.class, String.class, e);
    }
    if (timezone != null) {
        writer.writeStartElement(prefix, "timezone", "http://java.sun.com/xml/ns/javaee");
        writer.writeCharacters(timezone);
        writer.writeEndElement();
    }
    // ELEMENT: info
    final String infoRaw = timer.info;
    String info = null;
    try {
        info = Adapters.collapsedStringAdapterAdapter.marshal(infoRaw);
    } catch (final Exception e) {
        context.xmlAdapterError(timer, "info", CollapsedStringAdapter.class, String.class, String.class, e);
    }
    if (info != null) {
        writer.writeStartElement(prefix, "info", "http://java.sun.com/xml/ns/javaee");
        writer.writeCharacters(info);
        writer.writeEndElement();
    }
    context.afterMarshal(timer, LifecycleCallback.NONE);
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) TimerSchedule$JAXB.readTimerSchedule(org.apache.openejb.jee.TimerSchedule$JAXB.readTimerSchedule) TimerSchedule$JAXB.writeTimerSchedule(org.apache.openejb.jee.TimerSchedule$JAXB.writeTimerSchedule) 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) RuntimeContext(org.metatype.sxc.jaxb.RuntimeContext) NamedMethod$JAXB.readNamedMethod(org.apache.openejb.jee.NamedMethod$JAXB.readNamedMethod) NamedMethod$JAXB.writeNamedMethod(org.apache.openejb.jee.NamedMethod$JAXB.writeNamedMethod) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException)

Aggregations

CollapsedStringAdapter (javax.xml.bind.annotation.adapters.CollapsedStringAdapter)2 DatatypeConfigurationException (javax.xml.datatype.DatatypeConfigurationException)2 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)2 NamedMethod (org.apache.openejb.jee.NamedMethod)2 NamedMethod$JAXB.readNamedMethod (org.apache.openejb.jee.NamedMethod$JAXB.readNamedMethod)2 NamedMethod$JAXB.writeNamedMethod (org.apache.openejb.jee.NamedMethod$JAXB.writeNamedMethod)2 Text$JAXB.readText (org.apache.openejb.jee.Text$JAXB.readText)2 Text$JAXB.writeText (org.apache.openejb.jee.Text$JAXB.writeText)2 Timer (org.apache.openejb.jee.Timer)2 TimerSchedule (org.apache.openejb.jee.TimerSchedule)2 TimerSchedule$JAXB.readTimerSchedule (org.apache.openejb.jee.TimerSchedule$JAXB.readTimerSchedule)2 TimerSchedule$JAXB.writeTimerSchedule (org.apache.openejb.jee.TimerSchedule$JAXB.writeTimerSchedule)2 RuntimeContext (org.metatype.sxc.jaxb.RuntimeContext)2 HashMap (java.util.HashMap)1 QName (javax.xml.namespace.QName)1 Assembler (org.apache.openejb.assembler.classic.Assembler)1 EjbJarInfo (org.apache.openejb.assembler.classic.EjbJarInfo)1 MethodScheduleInfo (org.apache.openejb.assembler.classic.MethodScheduleInfo)1 ProxyFactoryInfo (org.apache.openejb.assembler.classic.ProxyFactoryInfo)1 ScheduleInfo (org.apache.openejb.assembler.classic.ScheduleInfo)1