Search in sources :

Example 61 with CqQuery

use of org.apache.geode.cache.query.CqQuery in project geode by apache.

the class CqServiceImpl method executeCqs.

@Override
public synchronized void executeCqs(Collection<? extends InternalCqQuery> cqs) throws CqException {
    if (cqs == null) {
        return;
    }
    String cqName = null;
    for (InternalCqQuery internalCq : cqs) {
        CqQuery cq = internalCq;
        if (!cq.isClosed() && cq.isStopped()) {
            try {
                cqName = cq.getName();
                cq.execute();
            } catch (QueryException | CqClosedException e) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Failed to execute the CQ, CqName : {} Error : {}", cqName, e.getMessage());
                }
            }
        }
    }
}
Also used : QueryException(org.apache.geode.cache.query.QueryException) CqClosedException(org.apache.geode.cache.query.CqClosedException) CqQuery(org.apache.geode.cache.query.CqQuery)

Example 62 with CqQuery

use of org.apache.geode.cache.query.CqQuery in project geode by apache.

the class ClientCQPostAuthorizationDUnitTest method createCQ.

private void createCQ(final int num) throws CqException, CqExistsException {
    for (int i = 0; i < num; i++) {
        QueryService cqService = getProxyCaches(i).getQueryService();
        String cqName = "CQ_" + i;
        String queryStr = cqNameToQueryStrings.get(cqName) + getProxyCaches(i).getRegion(REGION_NAME).getFullPath();
        // Create CQ Attributes.
        CqAttributesFactory cqf = new CqAttributesFactory();
        CqListener[] cqListeners = { new CqQueryTestListener(getLogWriter()) };
        ((CqQueryTestListener) cqListeners[0]).cqName = cqName;
        cqf.initCqListeners(cqListeners);
        CqAttributes cqa = cqf.create();
        // Create CQ.
        CqQuery cq1 = cqService.newCq(cqName, queryStr, cqa);
        assertTrue("newCq() state mismatch", cq1.getState().isStopped());
    }
}
Also used : CqQueryTestListener(org.apache.geode.cache.query.cq.dunit.CqQueryTestListener) QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) CqListener(org.apache.geode.cache.query.CqListener) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) InternalCqQuery(org.apache.geode.cache.query.internal.cq.InternalCqQuery) CqQuery(org.apache.geode.cache.query.CqQuery)

Example 63 with CqQuery

use of org.apache.geode.cache.query.CqQuery in project geode by apache.

the class ClientCQPostAuthorizationDUnitTest method executeCQ.

private void executeCQ(final int num, final boolean[] initialResults, final int expectedResultsSize, final String[] expectedErr, final boolean[] postAuthzAllowed) throws RegionNotFoundException, CqException {
    for (int i = 0; i < num; i++) {
        try {
            if (expectedErr[i] != null) {
                getLogWriter().info("<ExpectedException action=add>" + expectedErr[i] + "</ExpectedException>");
            }
            CqQuery cq1 = null;
            String cqName = "CQ_" + i;
            // String queryStr = cqNameToQueryStrings.get(cqName) +
            // getProxyCaches(i).getRegion(REGION_NAME).getFullPath();
            QueryService cqService = getProxyCaches(i).getQueryService();
            // Get CqQuery object.
            cq1 = cqService.getCq(cqName);
            if (cq1 == null) {
                getLogWriter().info("Failed to get CqQuery object for CQ name: " + cqName);
                fail("Failed to get CQ " + cqName);
            } else {
                getLogWriter().info("Obtained CQ, CQ name: " + cq1.getName());
                assertTrue("newCq() state mismatch", cq1.getState().isStopped());
            }
            if (initialResults[i]) {
                SelectResults cqResults = null;
                try {
                    cqResults = cq1.executeWithInitialResults();
                } catch (CqException ce) {
                    if (ce.getCause() instanceof NotAuthorizedException && !postAuthzAllowed[i]) {
                        getLogWriter().info("Got expected exception for CQ " + cqName);
                    } else {
                        getLogWriter().info("CqService is: " + cqService);
                        throw new AssertionError("Failed to execute CQ " + cqName, ce);
                    }
                }
                getLogWriter().info("initial result size = " + cqResults.size());
                assertTrue("executeWithInitialResults() state mismatch", cq1.getState().isRunning());
                if (expectedResultsSize >= 0) {
                    assertEquals("unexpected results size", expectedResultsSize, cqResults.size());
                }
            } else {
                try {
                    cq1.execute();
                } catch (CqException ce) {
                    if (ce.getCause() instanceof NotAuthorizedException && !postAuthzAllowed[i]) {
                        getLogWriter().info("Got expected exception for CQ " + cqName);
                    } else {
                        throw ce;
                    }
                }
                assertTrue("execute() state mismatch", cq1.getState().isRunning() == postAuthzAllowed[i]);
            }
        } finally {
            if (expectedErr[i] != null) {
                getLogWriter().info("<ExpectedException action=remove>" + expectedErr[i] + "</ExpectedException>");
            }
        }
    }
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) CqException(org.apache.geode.cache.query.CqException) InternalCqQuery(org.apache.geode.cache.query.internal.cq.InternalCqQuery) CqQuery(org.apache.geode.cache.query.CqQuery)

Example 64 with CqQuery

use of org.apache.geode.cache.query.CqQuery in project geode by apache.

the class MultiUserAPIDUnitTest method verifyDisallowedOps.

private void verifyDisallowedOps(final boolean multiUserMode) throws Exception {
    String op = "unknown";
    boolean success = false;
    if (!multiUserMode) {
        success = false;
        try {
            // Attempt cache.createAuthenticatedCacheView() and expect an exception, fail otherwise
            op = "Pool.createSecureUserCache()";
            GemFireCacheImpl.getInstance().createAuthenticatedView(new Properties(), "testPool");
        } catch (IllegalStateException uoe) {
            getLogWriter().info(op + ": Got expected exception: " + uoe);
            success = true;
        }
        if (!success) {
            fail("Did not get exception while doing " + op);
        }
    } else {
        // multiuser mode
        Region realRegion = GemFireCacheImpl.getInstance().getRegion(SecurityTestUtils.REGION_NAME);
        Region proxyRegion = SecurityTestUtils.getProxyCaches(0).getRegion(SecurityTestUtils.REGION_NAME);
        Pool pool = PoolManagerImpl.getPMI().find("testPool");
        for (int i = 0; i <= 27; i++) {
            success = false;
            try {
                switch(i) {
                    // and expect an exception, fail otherwise.
                    case 0:
                        op = "Region.create()";
                        realRegion.create("key", "value");
                        break;
                    case 1:
                        op = "Region.put()";
                        realRegion.put("key", "value");
                        break;
                    case 2:
                        op = "Region.get()";
                        realRegion.get("key");
                        break;
                    case 3:
                        op = "Region.containsKeyOnServer()";
                        realRegion.containsKeyOnServer("key");
                        break;
                    case 4:
                        op = "Region.remove()";
                        realRegion.remove("key");
                        break;
                    case 5:
                        op = "Region.destroy()";
                        realRegion.destroy("key");
                        break;
                    case 6:
                        op = "Region.destroyRegion()";
                        realRegion.destroyRegion();
                        break;
                    case 7:
                        op = "Region.registerInterest()";
                        realRegion.registerInterest("key");
                        break;
                    // break;
                    case 8:
                        op = "Region.clear()";
                        realRegion.clear();
                        break;
                    // and expect an exception, fail otherwise.
                    case 9:
                        op = "ProxyRegion.createSubregion()";
                        proxyRegion.createSubregion("subregion", null);
                        break;
                    case 10:
                        op = "ProxyRegion.forceRolling()";
                        proxyRegion.forceRolling();
                        break;
                    case 11:
                        op = "ProxyRegion.getAttributesMutator()";
                        proxyRegion.getAttributesMutator();
                        break;
                    case 12:
                        op = "ProxyRegion.registerInterest()";
                        proxyRegion.registerInterest("key");
                        break;
                    case 13:
                        op = "ProxyRegion.loadSnapshot()";
                        proxyRegion.loadSnapshot(null);
                        break;
                    case 14:
                        op = "ProxyRegion.saveSnapshot()";
                        proxyRegion.saveSnapshot(null);
                        break;
                    case 15:
                        op = "ProxyRegion.setUserAttribute()";
                        proxyRegion.setUserAttribute(null);
                        break;
                    case 16:
                        op = "ProxyRegion.unregisterInterestRegex()";
                        proxyRegion.unregisterInterestRegex("*");
                        break;
                    // exception, fail otherwise.
                    case 17:
                        op = "FunctionService.onRegion()";
                        FunctionService.onRegion(realRegion);
                        break;
                    case 18:
                        op = "FunctionService.onServer(pool)";
                        FunctionService.onServer(pool);
                        break;
                    case 19:
                        op = "FunctionService.onServers(pool)";
                        FunctionService.onServers(pool);
                        break;
                    // QueryService.newQuery().execute()/newCq().execute/executeWithInitialResults()
                    case 20:
                        op = "QueryService.newQuery.execute()";
                        Query query = pool.getQueryService().newQuery("SELECT * FROM /" + SecurityTestUtils.REGION_NAME);
                        query.execute();
                        break;
                    case 21:
                        op = "QueryService.newCq.execute()";
                        CqQuery cqQuery = pool.getQueryService().newCq("SELECT * FROM /" + SecurityTestUtils.REGION_NAME, new CqAttributesFactory().create());
                        try {
                            cqQuery.execute();
                        } catch (CqException ce) {
                            throw (Exception) ce.getCause();
                        }
                        break;
                    case 22:
                        op = "QueryService.newCq.executeWithInitialResults()";
                        cqQuery = pool.getQueryService().newCq("SELECT * FROM /" + SecurityTestUtils.REGION_NAME, new CqAttributesFactory().create());
                        try {
                            cqQuery.executeWithInitialResults();
                        } catch (CqException ce) {
                            throw (Exception) ce.getCause();
                        }
                        break;
                    // expect an exception, fail otherwise.
                    case 23:
                        op = "ProxyQueryService().getIndexes()";
                        SecurityTestUtils.getProxyCaches(0).getQueryService().getIndexes(null);
                        break;
                    case 24:
                        op = "ProxyQueryService().createIndex()";
                        SecurityTestUtils.getProxyCaches(0).getQueryService().createIndex(null, null, null);
                        break;
                    case 25:
                        op = "ProxyQueryService().removeIndexes()";
                        SecurityTestUtils.getProxyCaches(0).getQueryService().removeIndexes();
                        break;
                    case 26:
                        op = "ProxyRegion.localDestroy()";
                        proxyRegion.localDestroy("key");
                        break;
                    case 27:
                        op = "ProxyRegion.localInvalidate()";
                        proxyRegion.localInvalidate("key");
                        break;
                    default:
                        fail("Unknown op code: " + i);
                        break;
                }
            } catch (UnsupportedOperationException uoe) {
                getLogWriter().info(op + ": Got expected exception: " + uoe);
                success = true;
            }
            if (!success) {
                fail("Did not get exception while doing " + op);
            }
        }
    }
}
Also used : Query(org.apache.geode.cache.query.Query) CqQuery(org.apache.geode.cache.query.CqQuery) CqException(org.apache.geode.cache.query.CqException) Region(org.apache.geode.cache.Region) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) Pool(org.apache.geode.cache.client.Pool) CqQuery(org.apache.geode.cache.query.CqQuery) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties)

Aggregations

CqQuery (org.apache.geode.cache.query.CqQuery)64 QueryService (org.apache.geode.cache.query.QueryService)49 CacheException (org.apache.geode.cache.CacheException)38 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)36 CqAttributes (org.apache.geode.cache.query.CqAttributes)35 CqAttributesFactory (org.apache.geode.cache.query.CqAttributesFactory)31 IOException (java.io.IOException)27 CqExistsException (org.apache.geode.cache.query.CqExistsException)26 DefaultQueryService (org.apache.geode.cache.query.internal.DefaultQueryService)24 RegionNotFoundException (org.apache.geode.cache.query.RegionNotFoundException)22 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)21 Test (org.junit.Test)21 CqClosedException (org.apache.geode.cache.query.CqClosedException)20 Region (org.apache.geode.cache.Region)17 SelectResults (org.apache.geode.cache.query.SelectResults)17 Host (org.apache.geode.test.dunit.Host)14 VM (org.apache.geode.test.dunit.VM)14 CqListener (org.apache.geode.cache.query.CqListener)12 Struct (org.apache.geode.cache.query.Struct)11 CqEvent (org.apache.geode.cache.query.CqEvent)10