Search in sources :

Example 1 with MutableObject

use of org.apache.commons.lang.mutable.MutableObject in project POL-POM-5 by PlayOnLinux.

the class UiMessageSenderJavaFXImplementation method run.

@Override
public <R> R run(Supplier<R> function) {
    // runBackground synchronously on JavaFX thread
    if (Platform.isFxApplicationThread()) {
        return function.get();
    }
    // queue on JavaFX thread and wait for completion
    final CountDownLatch doneLatch = new CountDownLatch(1);
    final MutableObject result = new MutableObject();
    Platform.runLater(() -> {
        try {
            result.setValue(function.get());
        } finally {
            doneLatch.countDown();
        }
    });
    try {
        doneLatch.await();
    } catch (InterruptedException e) {
    // ignore exception
    }
    return (R) result.getValue();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) MutableObject(org.apache.commons.lang.mutable.MutableObject)

Example 2 with MutableObject

use of org.apache.commons.lang.mutable.MutableObject in project uPortal by Jasig.

the class JpaBaseAggregationDaoTest method testUnclosedBaseAggregationRangeQuery.

@Test
public final void testUnclosedBaseAggregationRangeQuery() throws Exception {
    final IEntityGroup entityGroupA = mock(IEntityGroup.class);
    when(entityGroupA.getServiceName()).thenReturn(new CompositeName("local"));
    when(entityGroupA.getName()).thenReturn("Group A");
    when(compositeGroupService.findGroup("local.0")).thenReturn(entityGroupA);
    final IEntityGroup entityGroupB = mock(IEntityGroup.class);
    when(entityGroupB.getServiceName()).thenReturn(new CompositeName("local"));
    when(entityGroupB.getName()).thenReturn("Group B");
    when(compositeGroupService.findGroup("local.1")).thenReturn(entityGroupB);
    final MutableInt aggrs = new MutableInt();
    // Create 10 minutes of aggregations
    final DateTime start = new DateTime(1326734644000l, DateTimeZone.UTC).minuteOfDay().roundFloorCopy();
    final DateTime end = start.plusMinutes(10);
    final AggregationInterval interval = AggregationInterval.FIVE_MINUTE;
    final MutableObject startObj = new MutableObject();
    final MutableObject endObj = new MutableObject();
    this.executeInTransaction(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final Random r = new Random(0);
            final AggregatedGroupMapping groupA = aggregatedGroupLookupDao.getGroupMapping("local.0");
            final AggregatedGroupMapping groupB = aggregatedGroupLookupDao.getGroupMapping("local.1");
            populateDateTimeDimensions(start, end, new FunctionWithoutResult<Tuple<DateDimension, TimeDimension>>() {

                @Override
                protected void applyWithoutResult(Tuple<DateDimension, TimeDimension> input) {
                    final TimeDimension td = input.second;
                    final DateDimension dd = input.first;
                    final DateTime instant = td.getTime().toDateTime(dd.getDate());
                    if (startObj.getValue() == null) {
                        startObj.setValue(instant);
                    }
                    endObj.setValue(instant);
                    if (instant.equals(interval.determineStart(instant))) {
                        final AggregationIntervalInfo intervalInfo = aggregationIntervalHelper.getIntervalInfo(interval, instant);
                        final T baseAggregationA = getAggregationDao().createAggregation(createAggregationKey(intervalInfo, groupA));
                        final T baseAggregationB = getAggregationDao().createAggregation(createAggregationKey(intervalInfo, groupB));
                        for (int u = 0; u < r.nextInt(50); u++) {
                            updateAggregation(intervalInfo, baseAggregationA, r);
                            updateAggregation(intervalInfo, baseAggregationB, r);
                        }
                        if (aggrs.intValue() % 4 == 0) {
                            baseAggregationA.intervalComplete(5);
                        }
                        baseAggregationB.intervalComplete(5);
                        getAggregationDao().updateAggregation(baseAggregationA);
                        getAggregationDao().updateAggregation(baseAggregationB);
                        aggrs.add(2);
                    }
                }
            });
        }
    });
    // Verify all aggrs created
    assertEquals(4, aggrs.intValue());
    // Find unclosed 1 aggr
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final Collection<T> baseAggregations = getAggregationDao().getUnclosedAggregations(start.minusDays(1), end.plusDays(1), interval);
            assertEquals(1, baseAggregations.size());
            for (final T baseAggregationImpl : baseAggregations) {
                baseAggregationImpl.intervalComplete(5);
                getAggregationDao().updateAggregation(baseAggregationImpl);
            }
        }
    });
    // Find unclosed 0 aggr
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final Collection<T> baseAggregations = getAggregationDao().getUnclosedAggregations(start.minusDays(1), end.plusDays(1), interval);
            assertEquals(0, baseAggregations.size());
        }
    });
}
Also used : CompositeName(javax.naming.CompositeName) DateTime(org.joda.time.DateTime) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) IEntityGroup(org.apereo.portal.groups.IEntityGroup) FunctionWithoutResult(org.apereo.portal.concurrency.FunctionWithoutResult) AggregatedGroupMapping(org.apereo.portal.events.aggr.groups.AggregatedGroupMapping) Random(java.util.Random) MutableInt(org.apache.commons.lang.mutable.MutableInt) Collection(java.util.Collection) Tuple(org.apereo.portal.utils.Tuple) MutableObject(org.apache.commons.lang.mutable.MutableObject) Test(org.junit.Test) BaseAggrEventsJpaDaoTest(org.apereo.portal.test.BaseAggrEventsJpaDaoTest)

Example 3 with MutableObject

use of org.apache.commons.lang.mutable.MutableObject in project POL-POM-5 by PhoenicisOrg.

the class UiMessageSenderJavaFXImplementation method run.

@Override
public <R> R run(Supplier<R> function) {
    // runBackground synchronously on JavaFX thread
    if (Platform.isFxApplicationThread()) {
        return function.get();
    }
    // queue on JavaFX thread and wait for completion
    final CountDownLatch doneLatch = new CountDownLatch(1);
    final MutableObject result = new MutableObject();
    Platform.runLater(() -> {
        try {
            result.setValue(function.get());
        } finally {
            doneLatch.countDown();
        }
    });
    try {
        doneLatch.await();
    } catch (InterruptedException e) {
    // ignore exception
    }
    return (R) result.getValue();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) MutableObject(org.apache.commons.lang.mutable.MutableObject)

Example 4 with MutableObject

use of org.apache.commons.lang.mutable.MutableObject in project phoenicis by PhoenicisOrg.

the class UiMessageSenderJavaFXImplementation method run.

@Override
public <R> R run(Supplier<R> function) {
    // runBackground synchronously on JavaFX thread
    if (Platform.isFxApplicationThread()) {
        return function.get();
    }
    // queue on JavaFX thread and wait for completion
    final CountDownLatch doneLatch = new CountDownLatch(1);
    final MutableObject result = new MutableObject();
    Platform.runLater(() -> {
        try {
            result.setValue(function.get());
        } finally {
            doneLatch.countDown();
        }
    });
    try {
        doneLatch.await();
    } catch (InterruptedException e) {
    // ignore exception
    }
    return (R) result.getValue();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) MutableObject(org.apache.commons.lang.mutable.MutableObject)

Example 5 with MutableObject

use of org.apache.commons.lang.mutable.MutableObject in project uPortal by Jasig.

the class JpaBaseAggregationDaoTest method testBaseAggregationRangeQuery.

@Test
public final void testBaseAggregationRangeQuery() throws Exception {
    final IEntityGroup entityGroupA = mock(IEntityGroup.class);
    when(entityGroupA.getServiceName()).thenReturn(new CompositeName("local"));
    when(entityGroupA.getName()).thenReturn("Group A");
    when(compositeGroupService.findGroup("local.0")).thenReturn(entityGroupA);
    final IEntityGroup entityGroupB = mock(IEntityGroup.class);
    when(entityGroupB.getServiceName()).thenReturn(new CompositeName("local"));
    when(entityGroupB.getName()).thenReturn("Group B");
    when(compositeGroupService.findGroup("local.1")).thenReturn(entityGroupB);
    final MutableInt aggrs = new MutableInt();
    // Create 2 days of login aggregates ... every 5 minutes
    final DateTime start = new DateTime(1326734644000l, DateTimeZone.UTC).minuteOfDay().roundFloorCopy();
    final DateTime end = start.plusDays(2);
    final AggregationInterval interval = AggregationInterval.FIVE_MINUTE;
    final MutableObject startObj = new MutableObject();
    final MutableObject endObj = new MutableObject();
    this.executeInTransaction(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final Random r = new Random(0);
            final AggregatedGroupMapping groupA = aggregatedGroupLookupDao.getGroupMapping("local.0");
            final AggregatedGroupMapping groupB = aggregatedGroupLookupDao.getGroupMapping("local.1");
            populateDateTimeDimensions(start, end, new FunctionWithoutResult<Tuple<DateDimension, TimeDimension>>() {

                @Override
                protected void applyWithoutResult(Tuple<DateDimension, TimeDimension> input) {
                    final TimeDimension td = input.second;
                    final DateDimension dd = input.first;
                    final DateTime instant = td.getTime().toDateTime(dd.getDate());
                    if (startObj.getValue() == null) {
                        startObj.setValue(instant);
                    }
                    endObj.setValue(instant);
                    if (instant.equals(interval.determineStart(instant))) {
                        final AggregationIntervalInfo intervalInfo = aggregationIntervalHelper.getIntervalInfo(interval, instant);
                        final T baseAggregationA = getAggregationDao().createAggregation(createAggregationKey(intervalInfo, groupA));
                        final T baseAggregationB = getAggregationDao().createAggregation(createAggregationKey(intervalInfo, groupB));
                        for (int u = 0; u < r.nextInt(50); u++) {
                            updateAggregation(intervalInfo, baseAggregationA, r);
                            updateAggregation(intervalInfo, baseAggregationB, r);
                        }
                        baseAggregationA.intervalComplete(5);
                        baseAggregationB.intervalComplete(5);
                        getAggregationDao().updateAggregation(baseAggregationA);
                        getAggregationDao().updateAggregation(baseAggregationB);
                        aggrs.add(2);
                    }
                }
            });
        }
    });
    // Verify all aggrs created
    assertEquals(1152, aggrs.intValue());
    // Find aggrs for one day
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final AggregatedGroupMapping groupA = aggregatedGroupLookupDao.getGroupMapping("local.0");
            final AggregatedGroupMapping groupB = aggregatedGroupLookupDao.getGroupMapping("local.1");
            final DateTime queryStart = start.toDateMidnight().toDateTime();
            final DateTime queryEnd = queryStart.plusDays(1).minusSeconds(1);
            final List<T> baseAggregations = getAggregationDao().getAggregations(queryStart, queryEnd, createAggregationKey(interval, groupA), groupB);
            assertEquals(158, baseAggregations.size());
        }
    });
    // Find aggrs for second day
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final AggregatedGroupMapping groupA = aggregatedGroupLookupDao.getGroupMapping("local.0");
            final AggregatedGroupMapping groupB = aggregatedGroupLookupDao.getGroupMapping("local.1");
            final DateTime queryStart = start.toDateMidnight().minusDays(1).toDateTime();
            final DateTime queryEnd = queryStart.plusDays(2).minusSeconds(1);
            final List<T> baseAggregations = getAggregationDao().getAggregations(queryStart, queryEnd, createAggregationKey(interval, groupA), groupB);
            assertEquals(158, baseAggregations.size());
        }
    });
    // Find all aggrs
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final AggregatedGroupMapping groupA = aggregatedGroupLookupDao.getGroupMapping("local.0");
            final AggregatedGroupMapping groupB = aggregatedGroupLookupDao.getGroupMapping("local.1");
            final List<T> baseAggregations = getAggregationDao().getAggregations(start, end.plusDays(1), createAggregationKey(interval, groupA), groupB);
            assertEquals(1152, baseAggregations.size());
        }
    });
    // Find first days worth
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final AggregatedGroupMapping groupA = aggregatedGroupLookupDao.getGroupMapping("local.0");
            final AggregatedGroupMapping groupB = aggregatedGroupLookupDao.getGroupMapping("local.1");
            final List<T> baseAggregations = getAggregationDao().getAggregations(start, end, createAggregationKey(interval, groupA), groupB);
            assertEquals(1152, baseAggregations.size());
        }
    });
    // Find second days worth
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final AggregatedGroupMapping groupA = aggregatedGroupLookupDao.getGroupMapping("local.0");
            final AggregatedGroupMapping groupB = aggregatedGroupLookupDao.getGroupMapping("local.1");
            final List<T> baseAggregations = getAggregationDao().getAggregations(start.plusDays(1), end.plusDays(1), createAggregationKey(interval, groupA), groupB);
            assertEquals(576, baseAggregations.size());
        }
    });
    // Find first 12 hours worth
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final AggregatedGroupMapping groupA = aggregatedGroupLookupDao.getGroupMapping("local.0");
            final AggregatedGroupMapping groupB = aggregatedGroupLookupDao.getGroupMapping("local.1");
            final List<T> baseAggregations = getAggregationDao().getAggregations(start, start.plusHours(12), createAggregationKey(interval, groupA), groupB);
            assertEquals(288, baseAggregations.size());
        }
    });
    // Find middle 24 hours worth
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final AggregatedGroupMapping groupA = aggregatedGroupLookupDao.getGroupMapping("local.0");
            final AggregatedGroupMapping groupB = aggregatedGroupLookupDao.getGroupMapping("local.1");
            final List<T> baseAggregations = getAggregationDao().getAggregations(start.plusHours(12), end.plusHours(12), createAggregationKey(interval, groupA), groupB);
            assertEquals(864, baseAggregations.size());
        }
    });
    // Find middle 24 hours worth for one group
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final AggregatedGroupMapping groupA = aggregatedGroupLookupDao.getGroupMapping("local.0");
            final List<T> baseAggregations = getAggregationDao().getAggregations(start.plusHours(12), end.plusHours(12), createAggregationKey(interval, groupA));
            assertEquals(432, baseAggregations.size());
        }
    });
    // Find last 12 hours worth
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final AggregatedGroupMapping groupA = aggregatedGroupLookupDao.getGroupMapping("local.0");
            final AggregatedGroupMapping groupB = aggregatedGroupLookupDao.getGroupMapping("local.1");
            final List<T> baseAggregations = getAggregationDao().getAggregations(start.plusHours(36), end.plusDays(1), createAggregationKey(interval, groupA), groupB);
            assertEquals(288, baseAggregations.size());
        }
    });
    // TODO Query for intervals that are stored
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final Set<AggregatedGroupMapping> aggregatedGroupMappings = getAggregationDao().getAggregatedGroupMappings();
            assertEquals(2, aggregatedGroupMappings.size());
            final Set<AggregationInterval> aggregationIntervals = getAggregationDao().getAggregationIntervals();
            assertEquals(1, aggregationIntervals.size());
        }
    });
}
Also used : Set(java.util.Set) CompositeName(javax.naming.CompositeName) DateTime(org.joda.time.DateTime) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) IEntityGroup(org.apereo.portal.groups.IEntityGroup) FunctionWithoutResult(org.apereo.portal.concurrency.FunctionWithoutResult) AggregatedGroupMapping(org.apereo.portal.events.aggr.groups.AggregatedGroupMapping) Random(java.util.Random) MutableInt(org.apache.commons.lang.mutable.MutableInt) LinkedList(java.util.LinkedList) List(java.util.List) Tuple(org.apereo.portal.utils.Tuple) MutableObject(org.apache.commons.lang.mutable.MutableObject) Test(org.junit.Test) BaseAggrEventsJpaDaoTest(org.apereo.portal.test.BaseAggrEventsJpaDaoTest)

Aggregations

MutableObject (org.apache.commons.lang.mutable.MutableObject)7 MutableInt (org.apache.commons.lang.mutable.MutableInt)4 DateTime (org.joda.time.DateTime)4 Random (java.util.Random)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 CompositeName (javax.naming.CompositeName)3 CallableWithoutResult (org.apereo.portal.concurrency.CallableWithoutResult)3 FunctionWithoutResult (org.apereo.portal.concurrency.FunctionWithoutResult)3 AggregatedGroupMapping (org.apereo.portal.events.aggr.groups.AggregatedGroupMapping)3 IEntityGroup (org.apereo.portal.groups.IEntityGroup)3 BaseAggrEventsJpaDaoTest (org.apereo.portal.test.BaseAggrEventsJpaDaoTest)3 Tuple (org.apereo.portal.utils.Tuple)3 Test (org.junit.Test)3 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Collection (java.util.Collection)1 Set (java.util.Set)1 EntityManager (javax.persistence.EntityManager)1 EventSession (org.apereo.portal.events.aggr.session.EventSession)1 Cache (org.hibernate.Cache)1