Search in sources :

Example 16 with CallableWithoutResult

use of org.apereo.portal.concurrency.CallableWithoutResult in project uPortal by Jasig.

the class JaxbPortalDataHandlerService method importDataDirectory.

@Override
public void importDataDirectory(File directory, String pattern, final BatchImportOptions options) {
    if (!directory.exists()) {
        throw new IllegalArgumentException("The specified directory '" + directory + "' does not exist");
    }
    // Create the file filter to use when searching for files to import
    final FileFilter fileFilter;
    if (pattern != null) {
        fileFilter = new AntPatternFileFilter(true, false, pattern, this.dataFileExcludes);
    } else {
        fileFilter = new AntPatternFileFilter(true, false, this.dataFileIncludes, this.dataFileExcludes);
    }
    // Determine the parent directory to log to
    final File logDirectory = determineLogDirectory(options, "import");
    // Setup reporting file
    final File importReport = new File(logDirectory, "data-import.txt");
    final PrintWriter reportWriter;
    try {
        reportWriter = new PrintWriter(new PeriodicFlushingBufferedWriter(500, new FileWriter(importReport)));
    } catch (IOException e) {
        throw new RuntimeException("Failed to create FileWriter for: " + importReport, e);
    }
    // Convert directory to URI String to provide better logging output
    final URI directoryUri = directory.toURI();
    final String directoryUriStr = directoryUri.toString();
    IMPORT_BASE_DIR.set(directoryUriStr);
    try {
        // Scan the specified directory for files to import
        logger.info("Scanning for files to Import from: {}", directory);
        final PortalDataKeyFileProcessor fileProcessor = new PortalDataKeyFileProcessor(this.dataKeyTypes, options);
        this.directoryScanner.scanDirectoryNoResults(directory, fileFilter, fileProcessor);
        final long resourceCount = fileProcessor.getResourceCount();
        logger.info("Found {} files to Import from: {}", resourceCount, directory);
        // See if the import should fail on error
        final boolean failOnError = options != null ? options.isFailOnError() : true;
        // Map of files to import, grouped by type
        final ConcurrentMap<PortalDataKey, Queue<Resource>> dataToImport = fileProcessor.getDataToImport();
        // Import the data files
        for (final PortalDataKey portalDataKey : this.dataKeyImportOrder) {
            final Queue<Resource> files = dataToImport.remove(portalDataKey);
            if (files == null) {
                continue;
            }
            final Queue<ImportFuture<?>> importFutures = new LinkedList<>();
            final List<FutureHolder<?>> failedFutures = new LinkedList<>();
            final int fileCount = files.size();
            logger.info("Importing {} files of type {}", fileCount, portalDataKey);
            reportWriter.println(portalDataKey + "," + fileCount);
            while (!files.isEmpty()) {
                final Resource file = files.poll();
                // Check for completed futures on every iteration, needed to fail as fast as
                // possible on an import exception
                final List<FutureHolder<?>> newFailed = waitForFutures(importFutures, reportWriter, logDirectory, false);
                failedFutures.addAll(newFailed);
                final AtomicLong importTime = new AtomicLong(-1);
                // Create import task
                final Callable<Object> task = new CallableWithoutResult() {

                    @Override
                    protected void callWithoutResult() {
                        IMPORT_BASE_DIR.set(directoryUriStr);
                        importTime.set(System.nanoTime());
                        try {
                            importData(file, portalDataKey);
                        } finally {
                            importTime.set(System.nanoTime() - importTime.get());
                            IMPORT_BASE_DIR.remove();
                        }
                    }
                };
                // Submit the import task
                final Future<?> importFuture = this.importExportThreadPool.submit(task);
                // Add the future for tracking
                importFutures.offer(new ImportFuture(importFuture, file, portalDataKey, importTime));
            }
            // Wait for all of the imports on of this type to complete
            final List<FutureHolder<?>> newFailed = waitForFutures(importFutures, reportWriter, logDirectory, true);
            failedFutures.addAll(newFailed);
            if (failOnError && !failedFutures.isEmpty()) {
                throw new RuntimeException(failedFutures.size() + " " + portalDataKey + " entities failed to import.\n\n" + "\tPer-entity exception logs and a full report can be found in " + // location clickable in some shells
                logDirectory.toURI() + "\n");
            }
            reportWriter.flush();
        }
        if (!dataToImport.isEmpty()) {
            throw new IllegalStateException("The following PortalDataKeys are not listed in the dataTypeImportOrder List: " + dataToImport.keySet());
        }
        logger.info("For a detailed report on the data import see " + importReport);
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted while waiting for entities to import", e);
    } finally {
        IOUtils.closeQuietly(reportWriter);
        IMPORT_BASE_DIR.remove();
    }
}
Also used : FileWriter(java.io.FileWriter) URI(java.net.URI) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) AntPatternFileFilter(org.apereo.portal.utils.AntPatternFileFilter) AntPatternFileFilter(org.apereo.portal.utils.AntPatternFileFilter) FileFilter(java.io.FileFilter) PeriodicFlushingBufferedWriter(org.apereo.portal.utils.PeriodicFlushingBufferedWriter) Queue(java.util.Queue) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) PrintWriter(java.io.PrintWriter) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) LinkedList(java.util.LinkedList) AtomicLong(java.util.concurrent.atomic.AtomicLong) File(java.io.File)

Example 17 with CallableWithoutResult

use of org.apereo.portal.concurrency.CallableWithoutResult in project uPortal by Jasig.

the class JpaAggregatedPortletLookupDaoTest method testLoginAggregationLifecycle.

@Test
public void testLoginAggregationLifecycle() throws Exception {
    final IPortletDefinition portletDefinition = mock(IPortletDefinition.class);
    when(portletDefinition.getName()).thenReturn("PortletName");
    when(portletDefinitionDao.getPortletDefinitionByFname("fname")).thenReturn(portletDefinition);
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final AggregatedPortletMapping portletMapping = aggregatedPortletLookupDao.getMappedPortletForFname("fname");
            assertNotNull(portletMapping);
            assertEquals("fname", portletMapping.getFname());
            assertEquals("PortletName", portletMapping.getName());
        }
    });
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final AggregatedPortletMapping portletMapping = aggregatedPortletLookupDao.getMappedPortletForFname("fname");
            assertNotNull(portletMapping);
            assertEquals("fname", portletMapping.getFname());
            assertEquals("PortletName", portletMapping.getName());
        }
    });
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final AggregatedPortletMapping portletMapping = aggregatedPortletLookupDao.getMappedPortletForFname("fname_old");
            assertNotNull(portletMapping);
            assertEquals("fname_old", portletMapping.getFname());
            assertEquals("fname_old", portletMapping.getName());
        }
    });
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final AggregatedPortletMapping portletMapping = aggregatedPortletLookupDao.getMappedPortletForFname("fname_old");
            assertNotNull(portletMapping);
            assertEquals("fname_old", portletMapping.getFname());
            assertEquals("fname_old", portletMapping.getName());
        }
    });
}
Also used : IPortletDefinition(org.apereo.portal.portlet.om.IPortletDefinition) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) Test(org.junit.Test) BaseAggrEventsJpaDaoTest(org.apereo.portal.test.BaseAggrEventsJpaDaoTest)

Example 18 with CallableWithoutResult

use of org.apereo.portal.concurrency.CallableWithoutResult in project uPortal by Jasig.

the class JpaPortletDaoTest method testAllDefinitionDaoMethods.

@Test
public void testAllDefinitionDaoMethods() throws Exception {
    final IPortletDefinitionId portletDefinitionId = execute(new Callable<IPortletDefinitionId>() {

        @Override
        public IPortletDefinitionId call() {
            final IPortletType channelType = jpaChannelTypeDao.createPortletType("BaseType", "foobar");
            // Create a definition
            final IPortletDefinition chanDef1 = new PortletDefinitionImpl(channelType, "fname1", "Test Portlet 1", "Test Portlet 1 Title", "/context1", "portletName1", false);
            jpaPortletDefinitionDao.savePortletDefinition(chanDef1);
            // Try all of the retrieval options
            final IPortletDefinition portDef1a = jpaPortletDefinitionDao.getPortletDefinition(chanDef1.getPortletDefinitionId());
            jpaPortletDefinitionDao.savePortletDefinition(chanDef1);
            assertEquals(chanDef1, portDef1a);
            // Create a second definition with the same app/portlet
            final IPortletDefinition chanDef2 = new PortletDefinitionImpl(channelType, "fname2", "Test Portlet 2", "Test Portlet 2 Title", "/uPortal", "portletName2", true);
            jpaPortletDefinitionDao.savePortletDefinition(chanDef2);
            return chanDef2.getPortletDefinitionId();
        }
    });
    execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final IPortletDefinition chanDef2 = jpaPortletDefinitionDao.getPortletDefinitionByFname("fname2");
            // Add some preferences
            final List<IPortletPreference> prefsList2 = chanDef2.getPortletPreferences();
            prefsList2.add(new PortletPreferenceImpl("prefName1", false, "val1", "val2"));
            prefsList2.add(new PortletPreferenceImpl("prefName2", true, "val3", "val4"));
            jpaPortletDefinitionDao.savePortletDefinition(chanDef2);
        }
    });
    execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final IPortletDefinition chanDef2 = jpaPortletDefinitionDao.getPortletDefinitionByFname("fname2");
            // verify preferences
            final List<IPortletPreference> prefsList2 = chanDef2.getPortletPreferences();
            assertEquals(2, prefsList2.size());
        }
    });
    execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            // Check prefs, remove one and another
            final IPortletDefinition portDef3 = jpaPortletDefinitionDao.getPortletDefinitionByName("Test Portlet 2");
            final List<IPortletPreference> prefsList3 = portDef3.getPortletPreferences();
            final List<IPortletPreference> expectedPrefsList3 = new ArrayList<IPortletPreference>();
            expectedPrefsList3.add(new PortletPreferenceImpl("prefName1", false, "val1", "val2"));
            expectedPrefsList3.add(new PortletPreferenceImpl("prefName2", true, "val3", "val4"));
            assertEquals(expectedPrefsList3, prefsList3);
            prefsList3.remove(1);
            prefsList3.add(new PortletPreferenceImpl("prefName3", false, "val5", "val6"));
            jpaPortletDefinitionDao.savePortletDefinition(portDef3);
        }
    });
    execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            // Check prefs
            final IPortletDefinition portDef4 = jpaPortletDefinitionDao.getPortletDefinition(portletDefinitionId);
            final List<IPortletPreference> prefsList4 = portDef4.getPortletPreferences();
            final List<IPortletPreference> expectedPrefsList4 = new ArrayList<IPortletPreference>();
            expectedPrefsList4.add(new PortletPreferenceImpl("prefName1", false, "val1", "val2"));
            expectedPrefsList4.add(new PortletPreferenceImpl("prefName3", false, "val5", "val6"));
            assertEquals(expectedPrefsList4, prefsList4);
        }
    });
}
Also used : IPortletDefinitionId(org.apereo.portal.portlet.om.IPortletDefinitionId) IPortletPreference(org.apereo.portal.portlet.om.IPortletPreference) IPortletType(org.apereo.portal.portlet.om.IPortletType) ArrayList(java.util.ArrayList) List(java.util.List) IPortletDefinition(org.apereo.portal.portlet.om.IPortletDefinition) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) Test(org.junit.Test) BasePortalJpaDaoTest(org.apereo.portal.test.BasePortalJpaDaoTest)

Example 19 with CallableWithoutResult

use of org.apereo.portal.concurrency.CallableWithoutResult in project uPortal by Jasig.

the class JdbcAuthDaoTest method testSimpleGetCreate.

@Test
public void testSimpleGetCreate() throws UnknownHostException {
    final String service = "foo";
    // Create & Return
    final String token = this.execute(new Callable<String>() {

        @Override
        public String call() throws Exception {
            final String t = authDao.getAuthToken(service);
            assertEquals(1000, t.length());
            return t;
        }
    });
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final String t = authDao.getAuthToken(service);
            assertEquals(token, t);
        }
    });
}
Also used : UnknownHostException(java.net.UnknownHostException) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) BasePortalJpaDaoTest(org.apereo.portal.test.BasePortalJpaDaoTest) Test(org.junit.Test)

Example 20 with CallableWithoutResult

use of org.apereo.portal.concurrency.CallableWithoutResult in project uPortal by Jasig.

the class JpaClusterLockDaoTest method testConcurrentLocking.

@Test
public void testConcurrentLocking() throws InterruptedException {
    reset(portalInfoProvider);
    when(portalInfoProvider.getUniqueServerName()).thenReturn("ServerA");
    final String mutexName = "testConcurrentLocking";
    execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final ClusterMutex mutex = clusterLockDao.getClusterMutex(mutexName);
            assertNotNull(mutex);
        }
    });
    final ThreadGroupRunner threadGroupRunner = new ThreadGroupRunner("JpaClusterLockDaoTest-", true);
    final AtomicInteger lockCounter = new AtomicInteger();
    threadGroupRunner.addTask(3, new ThrowingRunnable() {

        @Override
        public void runWithException() throws Throwable {
            executeInTransaction(new CallableWithoutResult() {

                @Override
                protected void callWithoutResult() {
                    try {
                        threadGroupRunner.tick(1);
                        try {
                            final ClusterMutex mutex = clusterLockDao.getLock(mutexName);
                            if (mutex != null) {
                                lockCounter.incrementAndGet();
                            }
                        } finally {
                            threadGroupRunner.tick(3);
                        }
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
        }
    });
    threadGroupRunner.start();
    threadGroupRunner.join();
    assertEquals(1, lockCounter.intValue());
    ClusterMutex mutex = clusterLockDao.getClusterMutex(mutexName);
    assertTrue(mutex.isLocked());
    clusterLockDao.releaseLock(mutexName);
    mutex = clusterLockDao.getClusterMutex(mutexName);
    assertFalse(mutex.isLocked());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadGroupRunner(org.apereo.portal.test.ThreadGroupRunner) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) ThrowingRunnable(org.apereo.portal.utils.threading.ThrowingRunnable) Test(org.junit.Test) BasePortalJpaDaoTest(org.apereo.portal.test.BasePortalJpaDaoTest)

Aggregations

CallableWithoutResult (org.apereo.portal.concurrency.CallableWithoutResult)32 Test (org.junit.Test)28 BasePortalJpaDaoTest (org.apereo.portal.test.BasePortalJpaDaoTest)14 BaseAggrEventsJpaDaoTest (org.apereo.portal.test.BaseAggrEventsJpaDaoTest)12 DateTime (org.joda.time.DateTime)8 List (java.util.List)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 CompositeName (javax.naming.CompositeName)7 IEntityGroup (org.apereo.portal.groups.IEntityGroup)7 LinkedList (java.util.LinkedList)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 AggregatedGroupMapping (org.apereo.portal.events.aggr.groups.AggregatedGroupMapping)5 Ignore (org.junit.Ignore)5 Random (java.util.Random)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 MutableInt (org.apache.commons.lang.mutable.MutableInt)3 MutableObject (org.apache.commons.lang.mutable.MutableObject)3 FunctionWithoutResult (org.apereo.portal.concurrency.FunctionWithoutResult)3 File (java.io.File)2 FileWriter (java.io.FileWriter)2