Search in sources :

Example 6 with EISException

use of org.eclipse.persistence.eis.EISException in project eclipselink by eclipse-ee4j.

the class MongoPlatform method extractValueFromExpression.

/**
 * Extract the field or constant value from the comparison expression.
 */
protected Object extractValueFromExpression(Expression expression, DatabaseQuery query) {
    Object value = null;
    expression.getBuilder().setSession(query.getSession());
    if (expression.isQueryKeyExpression()) {
        QueryKeyExpression queryKeyExpression = (QueryKeyExpression) expression;
        value = queryKeyExpression.getField();
        if ((queryKeyExpression.getMapping() != null) && queryKeyExpression.getMapping().getDescriptor().isDescriptorTypeAggregate()) {
            String name = queryKeyExpression.getField().getName();
            while (queryKeyExpression.getBaseExpression().isQueryKeyExpression() && (((QueryKeyExpression) queryKeyExpression.getBaseExpression()).getMapping().isAbstractCompositeObjectMapping() || ((QueryKeyExpression) queryKeyExpression.getBaseExpression()).getMapping().isAbstractCompositeCollectionMapping() || ((QueryKeyExpression) queryKeyExpression.getBaseExpression()).getMapping().isAbstractCompositeDirectCollectionMapping())) {
                queryKeyExpression = (QueryKeyExpression) queryKeyExpression.getBaseExpression();
                if (queryKeyExpression.getMapping().isAbstractCompositeObjectMapping()) {
                    name = queryKeyExpression.getMapping().getField().getName() + "." + name;
                } else if (queryKeyExpression.getMapping().isAbstractCompositeCollectionMapping()) {
                    name = queryKeyExpression.getMapping().getField().getName() + "." + name;
                } else if (queryKeyExpression.getMapping().isAbstractCompositeDirectCollectionMapping()) {
                    name = queryKeyExpression.getMapping().getField().getName() + "." + name;
                }
            }
            DatabaseField field = new DatabaseField();
            field.setName(name);
            value = field;
        }
    } else if (expression.isFieldExpression()) {
        value = ((FieldExpression) expression).getField();
    } else if (expression.isConstantExpression()) {
        value = ((ConstantExpression) expression).getValue();
        if (((ConstantExpression) expression).getLocalBase() != null) {
            value = ((ConstantExpression) expression).getLocalBase().getFieldValue(value, query.getSession());
        }
    } else if (expression.isParameterExpression()) {
        value = query.getTranslationRow().get(((ParameterExpression) expression).getField());
        if (((ParameterExpression) expression).getLocalBase() != null) {
            value = ((ParameterExpression) expression).getLocalBase().getFieldValue(value, query.getSession());
        }
    } else {
        throw new EISException("Query too complex for Mongo translation, comparison of [" + expression + "] not supported in query: " + query);
    }
    if (value instanceof List) {
        @SuppressWarnings({ "unchecked" }) List<Object> values = (List<Object>) value;
        for (int index = 0; index < values.size(); index++) {
            Object element = values.get(index);
            if (element instanceof Expression) {
                element = extractValueFromExpression((Expression) element, query);
                values.set(index, element);
            }
        }
    }
    return value;
}
Also used : ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) EISException(org.eclipse.persistence.eis.EISException) QueryKeyExpression(org.eclipse.persistence.internal.expressions.QueryKeyExpression) FieldExpression(org.eclipse.persistence.internal.expressions.FieldExpression) RelationExpression(org.eclipse.persistence.internal.expressions.RelationExpression) FieldExpression(org.eclipse.persistence.internal.expressions.FieldExpression) QueryKeyExpression(org.eclipse.persistence.internal.expressions.QueryKeyExpression) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) LogicalExpression(org.eclipse.persistence.internal.expressions.LogicalExpression) FunctionExpression(org.eclipse.persistence.internal.expressions.FunctionExpression) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) Expression(org.eclipse.persistence.expressions.Expression) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) List(java.util.List)

Example 7 with EISException

use of org.eclipse.persistence.eis.EISException in project eclipselink by eclipse-ee4j.

the class MongoConnectionSpec method connectToDataSource.

/**
 * Connect with the specified properties and return the Connection.
 */
@Override
public Connection connectToDataSource(EISAccessor accessor, Properties properties) throws DatabaseException, ValidationException {
    if ((this.connectionFactory == null) && (this.name == null)) {
        this.connectionFactory = createMongoConnectionFactory();
    }
    if (!properties.isEmpty()) {
        if (this.connectionSpec == null) {
            this.connectionSpec = new MongoJCAConnectionSpec();
        }
        MongoJCAConnectionSpec spec = (MongoJCAConnectionSpec) this.connectionSpec;
        String host = (String) properties.get(HOST);
        String port = (String) properties.get(PORT);
        String db = (String) properties.get(DB);
        if (host != null) {
            if (host.indexOf(',') == -1) {
                spec.getHosts().add(host);
                if (port != null) {
                    spec.getPorts().add(Integer.valueOf(port));
                }
            } else {
                int startIndex = 0;
                while (startIndex < (host.length() - 1)) {
                    int endIndex = host.indexOf(',', startIndex);
                    if (endIndex == -1) {
                        endIndex = host.length();
                    }
                    String nextHost = host.substring(startIndex, endIndex);
                    spec.getHosts().add(nextHost);
                    startIndex = endIndex + 1;
                }
                while (startIndex < (port.length() - 1)) {
                    int endIndex = port.indexOf(',', startIndex);
                    if (endIndex == -1) {
                        endIndex = port.length();
                    }
                    String nextPort = port.substring(startIndex, endIndex);
                    spec.getPorts().add(Integer.valueOf(nextPort));
                    startIndex = endIndex + 1;
                }
            }
        }
        if (db != null) {
            spec.setDB(db);
        }
        String user = (String) properties.get("user");
        Object password = properties.get("password");
        if (password instanceof String) {
            password = ((String) password).toCharArray();
        }
        if ((user != null) && (user.length() != 0)) {
            spec.setUser(user);
            spec.setPassword((char[]) password);
        }
        // Allows setting of read preference as a property.
        Object preference = properties.get(READ_PREFERENCE);
        if (preference instanceof ReadPreference) {
            spec.setReadPreference((ReadPreference) preference);
        } else if (preference instanceof String) {
            String constant = (String) preference;
            if (constant.equals("PRIMARY")) {
                spec.setReadPreference(ReadPreference.primary());
            } else if (constant.equals("SECONDARY")) {
                spec.setReadPreference(ReadPreference.secondary());
            } else {
                throw new EISException("Invalid read preference property value: " + constant);
            }
        }
        // Allows setting of write concern as a property.
        Object concern = properties.get(WRITE_CONCERN);
        if (concern instanceof WriteConcern) {
            spec.setWriteConcern((WriteConcern) concern);
        } else if (concern instanceof String) {
            String constant = (String) concern;
            if (constant.equals("FSYNC_SAFE")) {
                spec.setWriteConcern(WriteConcern.FSYNC_SAFE);
            } else if (constant.equals("JOURNAL_SAFE")) {
                spec.setWriteConcern(WriteConcern.JOURNAL_SAFE);
            } else if (constant.equals("MAJORITY")) {
                spec.setWriteConcern(WriteConcern.MAJORITY);
            } else if (constant.equals("NONE")) {
                spec.setWriteConcern(/*FIXME: WriteConcern.NONE*/
                new WriteConcern("none"));
            } else if (constant.equals("NORMAL")) {
                spec.setWriteConcern(WriteConcern.NORMAL);
            } else if (constant.equals("REPLICAS_SAFE")) {
                spec.setWriteConcern(WriteConcern.REPLICAS_SAFE);
            } else if (constant.equals("SAFE")) {
                spec.setWriteConcern(WriteConcern.SAFE);
            } else {
                throw new EISException("Invalid read preference property value: " + constant);
            }
        }
        // Allows setting of options as a property.
        Object options = properties.get(OPTIONS);
        if (options instanceof Number) {
            spec.setOptions(((Number) options).intValue());
        } else if (options instanceof String) {
            spec.setOptions(Integer.parseInt(((String) options)));
        }
        // Allows setting of serverSelectionTimeout as a property.
        Object serverSelectionTimeout = properties.get(SERVER_SELECTION_TIMEOUT);
        if (serverSelectionTimeout instanceof Number) {
            spec.setServerSelectionTimeout(((Number) serverSelectionTimeout).intValue());
        } else if (serverSelectionTimeout instanceof String) {
            spec.setServerSelectionTimeout(Integer.parseInt(((String) serverSelectionTimeout)));
        }
    }
    return super.connectToDataSource(accessor, properties);
}
Also used : MongoJCAConnectionSpec(org.eclipse.persistence.internal.nosql.adapters.mongo.MongoJCAConnectionSpec) ReadPreference(com.mongodb.ReadPreference) WriteConcern(com.mongodb.WriteConcern) EISException(org.eclipse.persistence.eis.EISException)

Example 8 with EISException

use of org.eclipse.persistence.eis.EISException in project eclipselink by eclipse-ee4j.

the class CciJMSInteraction method executeSendReceiveInteraction.

/**
 * Execute the send/receive message interaction.
 * Only text messages of XML content are supported.
 *
 * @param spec - the send/receive interaction spec
 * @param input - the input record
 * @param output - the output record
 */
protected void executeSendReceiveInteraction(CciJMSSendReceiveInteractionSpec spec, CciJMSRecord input, CciJMSRecord output) throws EISException {
    // verify input record
    if (input.size() != 1) {
        throw EISException.invalidInput();
    }
    try {
        Queue sendQueue;
        Queue replyToQueue;
        QueueSession qSession = (QueueSession) connection.getSession();
        // set the request queue
        if (spec.hasDestinationURL()) {
            sendQueue = (Queue) new InitialContext().lookup(spec.getDestinationURL());
        } else {
            sendQueue = qSession.createQueue(spec.getDestination());
        }
        // set the replyTo queue
        if (spec.hasReplyToDestinationURL()) {
            replyToQueue = (Queue) new InitialContext().lookup(spec.getReplyToDestinationURL());
        } else {
            replyToQueue = qSession.createQueue(spec.getReplyToDestination());
        }
        Message msg = createMessage(input.get(0), qSession);
        msg.setJMSReplyTo(replyToQueue);
        // set the user-defined message selector, if one exists, else use the JMSMessageID
        if (spec.hasMessageSelector()) {
            msg.setJMSCorrelationID(spec.getMessageSelector());
            sendMessageAndCommit(qSession, sendQueue, msg);
        } else {
            sendMessageAndCommit(qSession, sendQueue, msg);
            spec.setMessageSelector(msg.getJMSMessageID());
        }
        // at this point the message selector set in the spec is either user-defined or the JMSMessageID
        // perform the receive portion of the interaction
        Queue receiveQueue;
        if (spec.hasReplyToDestinationURL()) {
            receiveQueue = (Queue) new InitialContext().lookup(spec.getReplyToDestinationURL());
        } else {
            receiveQueue = qSession.createQueue(spec.getReplyToDestination());
        }
        msg = qSession.createReceiver(receiveQueue, spec.getFormattedMessageSelector()).receive(spec.getTimeout());
        // check for timeout
        if (msg == null) {
            throw EISException.timeoutOccurred();
        }
        output.add(msg);
    } catch (Exception ex) {
        throw EISException.createException(ex);
    }
}
Also used : InitialContext(javax.naming.InitialContext) EISException(org.eclipse.persistence.eis.EISException)

Example 9 with EISException

use of org.eclipse.persistence.eis.EISException in project eclipselink by eclipse-ee4j.

the class OrderQueueTest method testReadTimeout.

/**
 * Reading with a dequeue timeout test.
 */
@Test
public void testReadTimeout() throws Exception {
    final DatabaseSession session = SessionHelper.createDatabaseSession(AQTestSuite.project);
    XMLInteraction interaction = new XMLInteraction();
    AQDequeueOption options = new AQDequeueOption();
    options.setWaitTime(1);
    interaction.setProperty(AQPlatform.QUEUE_OPERATION, AQPlatform.DEQUEUE);
    interaction.setProperty(AQPlatform.DEQUEUE_OPTIONS, options);
    boolean timeout = false;
    try {
        session.readObject(org.eclipse.persistence.testing.models.order.Order.class, interaction);
    } catch (EISException exception) {
        timeout = true;
        if (exception.getMessage().indexOf("timeout") == -1) {
            throw exception;
        }
    } finally {
        session.logout();
    }
    if (!timeout) {
        throw new TestErrorException("Timeout exception did not occur, was a message in the queue.");
    }
}
Also used : DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) EISException(org.eclipse.persistence.eis.EISException) XMLInteraction(org.eclipse.persistence.eis.interactions.XMLInteraction) AQDequeueOption(oracle.AQ.AQDequeueOption) Test(org.junit.Test)

Example 10 with EISException

use of org.eclipse.persistence.eis.EISException in project eclipselink by eclipse-ee4j.

the class MongoPlatform method buildCallFromStatement.

/**
 * INTERNAL:
 * Override this method to throw an exception by default.
 * Platforms that support dynamic querying can override this to generate an EISInteraction.
 */
@Override
public DatasourceCall buildCallFromStatement(SQLStatement statement, DatabaseQuery query, AbstractSession session) {
    if (query.isObjectLevelReadQuery()) {
        ObjectLevelReadQuery readQuery = (ObjectLevelReadQuery) query;
        MappedInteraction interaction = new MappedInteraction();
        interaction.setProperty(OPERATION, MongoOperation.FIND);
        interaction.setProperty(COLLECTION, ((EISDescriptor) query.getDescriptor()).getDataTypeName());
        if (readQuery.getFirstResult() > 0) {
            interaction.setProperty(SKIP, readQuery.getFirstResult());
        }
        if (readQuery.getMaxRows() > 0) {
            interaction.setProperty(LIMIT, readQuery.getMaxRows());
        }
        if (readQuery.getFetchSize() > 0) {
            interaction.setProperty(BATCH_SIZE, readQuery.getMaxRows());
        }
        DatabaseRecord row = new DatabaseRecord();
        if (statement.getWhereClause() != null) {
            appendExpressionToQueryRow(statement.getWhereClause(), row, query);
        }
        if (readQuery.hasOrderByExpressions()) {
            DatabaseRecord sort = new DatabaseRecord();
            for (Expression orderBy : readQuery.getOrderByExpressions()) {
                appendExpressionToSortRow(orderBy, sort, query);
            }
            row.put(MongoRecord.SORT, sort);
        }
        if (readQuery.isReportQuery()) {
            DatabaseRecord select = new DatabaseRecord();
            for (Object field : ((SQLSelectStatement) statement).getFields()) {
                if (field instanceof DatabaseField) {
                    select.put((DatabaseField) field, 1);
                } else if (field instanceof Expression) {
                    Object value = extractValueFromExpression((Expression) field, readQuery);
                    if (!(value instanceof DatabaseField)) {
                        throw new EISException("Query too complex for Mongo translation, only field selects are supported in query: " + query);
                    }
                    select.put((DatabaseField) value, 1);
                }
            }
            row.put("$select", select);
        }
        interaction.setInputRow(row);
        return interaction;
    }
    throw new EISException("Query too complex for Mongo translation, only select queries are supported in query: " + query);
}
Also used : ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) MappedInteraction(org.eclipse.persistence.eis.interactions.MappedInteraction) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) RelationExpression(org.eclipse.persistence.internal.expressions.RelationExpression) FieldExpression(org.eclipse.persistence.internal.expressions.FieldExpression) QueryKeyExpression(org.eclipse.persistence.internal.expressions.QueryKeyExpression) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) LogicalExpression(org.eclipse.persistence.internal.expressions.LogicalExpression) FunctionExpression(org.eclipse.persistence.internal.expressions.FunctionExpression) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) Expression(org.eclipse.persistence.expressions.Expression) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) SQLSelectStatement(org.eclipse.persistence.internal.expressions.SQLSelectStatement) EISException(org.eclipse.persistence.eis.EISException)

Aggregations

EISException (org.eclipse.persistence.eis.EISException)12 FunctionExpression (org.eclipse.persistence.internal.expressions.FunctionExpression)4 InitialContext (javax.naming.InitialContext)3 Expression (org.eclipse.persistence.expressions.Expression)3 ConstantExpression (org.eclipse.persistence.internal.expressions.ConstantExpression)3 FieldExpression (org.eclipse.persistence.internal.expressions.FieldExpression)3 LogicalExpression (org.eclipse.persistence.internal.expressions.LogicalExpression)3 ParameterExpression (org.eclipse.persistence.internal.expressions.ParameterExpression)3 QueryKeyExpression (org.eclipse.persistence.internal.expressions.QueryKeyExpression)3 RelationExpression (org.eclipse.persistence.internal.expressions.RelationExpression)3 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)3 ReadPreference (com.mongodb.ReadPreference)2 WriteConcern (com.mongodb.WriteConcern)2 InteractionSpec (jakarta.resource.cci.InteractionSpec)2 List (java.util.List)2 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 DatabaseRecord (org.eclipse.persistence.sessions.DatabaseRecord)2 AQDequeueOption (oracle.AQ.AQDequeueOption)1 Consistency (oracle.kv.Consistency)1 Durability (oracle.kv.Durability)1