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;
}
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);
}
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);
}
}
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.");
}
}
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);
}
Aggregations