Skip to content

Commit

Permalink
Merge pull request #102 from dizzzz/feature/add_errorcodes
Browse files Browse the repository at this point in the history
Introduce errorcodes
  • Loading branch information
dizzzz authored Nov 12, 2016
2 parents 0241c21 + 025b235 commit 706b62c
Show file tree
Hide file tree
Showing 18 changed files with 145 additions and 67 deletions.
9 changes: 5 additions & 4 deletions java/src/org/exist/jms/messaging/MessagingJmsListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.util.zip.GZIPInputStream;

import static org.exist.jms.shared.Constants.*;
import static org.exist.jms.shared.ErrorCodes.*;

/**
* JMS message receiver. Passes call to XQuery callback function.
Expand Down Expand Up @@ -254,7 +255,7 @@ private Sequence getContent(final Message msg) throws IOException, XPathExceptio
// Unsupported JMS message type
final String txt = String.format("Unsupported JMS Message type %s", msg.getClass().getCanonicalName());

final XPathException ex = new XPathException(txt);
final XPathException ex = new XPathException(JMS021, txt);
report.addListenerError(ex);
LOG.error(txt);
throw ex;
Expand Down Expand Up @@ -394,7 +395,7 @@ private Sequence handleObjectMessage(final ObjectMessage msg) throws JMSExceptio
} else {
final String txt = String.format("Unable to convert the object %s", obj.toString());

final XPathException ex = new XPathException(txt);
final XPathException ex = new XPathException(JMS022, txt);
report.addListenerError(ex);
LOG.error(txt);
throw ex;
Expand Down Expand Up @@ -445,12 +446,12 @@ private Sequence processXML(final byte[] data, final boolean isGzipped) throws X
} else {
final String txt = String.format("Received document is not valid: %s", validationReport.toString());
LOG.debug(txt);
throw new XPathException(txt);
throw new XPathException(JMS023, txt);
}

} catch (SAXException | ParserConfigurationException | IOException ex) {
report.addListenerError(ex);
throw new XPathException(ex.getMessage());
throw new XPathException(JMS003, ex.getMessage(), ex);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import org.exist.jms.replication.shared.TransportException;
import org.exist.jms.shared.Constants;

import javax.jms.DeliveryMode;
import javax.jms.Message;
import javax.naming.Context;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.exist.dom.persistent.DocumentImpl;
import org.exist.jms.replication.shared.MessageHelper;
import org.exist.jms.replication.shared.ReplicationGuard;
import org.exist.jms.replication.shared.TransportException;
import org.exist.jms.shared.eXistMessage;
import org.exist.storage.DBBroker;
import org.exist.storage.txn.Txn;
Expand Down Expand Up @@ -65,6 +64,7 @@ public class ReplicationTrigger extends SAXTrigger implements DocumentTrigger, C
private boolean isJMSOrigin(final Txn transaction) {

// Get originId.
@SuppressWarnings("deprecation")
final String originId = transaction.getOriginId();

return StringUtils.startsWith(originId, JMS_EXTENSION_PKG);
Expand Down Expand Up @@ -430,13 +430,9 @@ private void sendMessage(final eXistMessage msg) /* throws TriggerException */

sender.sendMessage(msg);

} catch (final TransportException ex) {
LOGGER.error(ex.getMessage(), ex);
//throw new TriggerException(ex.getMessage(), ex);

} catch (final Throwable ex) {
LOGGER.error(ex.getMessage(), ex);
//throw new TriggerException(ex.getMessage(), ex);
//throw new TriggerException(ex.getMessage(), ex); TransportException
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public abstract class ClientParameters {
// public static final String DESTINATION = Constants.DESTINATION; //"topic";
// public static final String CLIENT_ID = Constants.CLIENT_ID; //"client-id";
// public static final String PARAMETER_GROUPING = "..";

protected final Properties props = new Properties();
protected String connectionFactory = null;
protected String clientId = null;
protected String topic = null;
Expand All @@ -49,8 +49,6 @@ public abstract class ClientParameters {
protected String connectionUsername = null;
protected String connectionPassword = null;

protected final Properties props = new Properties();

/**
* Retrieve configuration value when available as String.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,6 @@ public static void retrieveFromDocument(final Map<String, Object> props, final D
}

public static void retrieveFromCollection(final Map<String, Object> props, final Collection collection) {
props.put(EXIST_RESOURCE_CREATIONTIME, collection.getCreationTime());
props.put(EXIST_RESOURCE_CREATIONTIME, collection.getMetadata().getCreated());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ public void onMessage(final Message msg) {
if (StringUtils.isNotEmpty(localID)) {
final String remoteID = msg.getStringProperty(Constants.EXIST_INSTANCE_ID);
if (localID.equals(remoteID)) {
LOG.info("Incoming JMS messsage was originally sent by this instance. Processing stopped.");
return; // TODO: throw exception? probably not because message does not need to be re-received
LOG.info("Incoming JMS messsage was originally sent by this instance. Stopped processing.");
msg.acknowledge();
return; // Do not throw exeception; Receive was OK
}
}

Expand Down
19 changes: 10 additions & 9 deletions java/src/org/exist/jms/send/Sender.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.util.zip.GZIPOutputStream;

import static org.exist.jms.shared.Constants.*;
import static org.exist.jms.shared.ErrorCodes.*;

//import org.exist.dom.persistent.NodeProxy;

Expand Down Expand Up @@ -119,7 +120,7 @@ public NodeImpl send(final JmsConfiguration jmsConfig, final JmsMessagePropertie
final ConnectionFactory cf = getConnectionFactoryInstance(context, jmsConfig);

if (cf == null) {
throw new XPathException("Unable to create connection factory");
throw new XPathException(JMS026, "Unable to create connection factory");
}

// Setup username/password when required
Expand Down Expand Up @@ -183,16 +184,16 @@ public NodeImpl send(final JmsConfiguration jmsConfig, final JmsMessagePropertie

Throwable cause = ex.getCause();

if("Error while attempting to add new Connection to the pool".contentEquals(ex.getMessage()) && cause!=null){
throw new XPathException(cause.getMessage());
if ("Error while attempting to add new Connection to the pool".contentEquals(ex.getMessage()) && cause != null) {
throw new XPathException(JMS004, cause.getMessage());

} else {
throw new XPathException(ex.getMessage());
throw new XPathException(JMS004, ex.getMessage());
}

} catch (final Throwable ex) {
LOG.error(ex.getMessage(), ex);
throw new XPathException(ex.getMessage());
throw new XPathException(JMS000, ex.getMessage());

} finally {
try {
Expand Down Expand Up @@ -278,8 +279,8 @@ private Message createMessageFromItem(final Session session, final Item item, fi
IOUtils.copy(is, os);

} catch (final IOException ex) {
LOG.error(ex);
throw new XPathException(ex);
LOG.error(ex.getMessage(), ex);
throw new XPathException(JMS001, ex.getMessage(), ex);

} finally {
IOUtils.closeQuietly(is);
Expand Down Expand Up @@ -323,7 +324,7 @@ private Message createMessageFromItem(final Session session, final Item item, fi

} catch (final IOException ex) {
LOG.error(ex);
throw new XPathException(ex);
throw new XPathException(JMS001, ex.getMessage(), ex);

} finally {
IOUtils.closeQuietly(is);
Expand Down Expand Up @@ -369,7 +370,7 @@ private Message createMessageFromItem(final Session session, final Item item, fi
objectMessage.setObject(booleanValue);
break;
default:
throw new XPathException(
throw new XPathException(JMS027,
String.format("Unable to convert '%s' of type '%s' into a JMS object.", item.getStringValue(), item.getType()));
}

Expand Down
63 changes: 63 additions & 0 deletions java/src/org/exist/jms/shared/ErrorCodes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* eXist Open Source Native XML Database
* Copyright (C) 2013 The eXist Project
* http://exist-db.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.exist.jms.shared;

import org.exist.dom.QName;
import org.exist.xquery.ErrorCodes.ErrorCode;

import static org.exist.jms.xquery.JmsModule.NAMESPACE_URI;
import static org.exist.jms.xquery.JmsModule.PREFIX;

/**
* ErrorCodes definition
*/
public class ErrorCodes {

public final static ErrorCode JMS000 = new JmsErrorCode("JMS000", "Generic exception.");
public final static ErrorCode JMS001 = new JmsErrorCode("JMS001", "Internal IO error.");
public final static ErrorCode JMS002 = new JmsErrorCode("JMS002", "Function does not exist.");
public final static ErrorCode JMS003 = new JmsErrorCode("JMS003", "XML parse(r) error.");
public final static ErrorCode JMS004 = new JmsErrorCode("JMS004", "Generic JMS Exception.");

public final static ErrorCode JMS010 = new JmsErrorCode("JMS010", "Unauthorized usage.");
public final static ErrorCode JMS011 = new JmsErrorCode("JMS011", "Configuration error.");

public final static ErrorCode JMS020 = new JmsErrorCode("JMS020", "Receiver does not exist.");
public final static ErrorCode JMS021 = new JmsErrorCode("JMS021", "Unsupported JMS message.");
public final static ErrorCode JMS022 = new JmsErrorCode("JMS022", "Unable to convert JMS ObjectMessage to object.");
public final static ErrorCode JMS023 = new JmsErrorCode("JMS023", "Received XML document is not valid.");

public final static ErrorCode JMS025 = new JmsErrorCode("JMS025", "JMS connection is not initialized.");
public final static ErrorCode JMS026 = new JmsErrorCode("JMS026", "No ConnectionFactory.");
public final static ErrorCode JMS027 = new JmsErrorCode("JMS027", "Unable to convert object to JMS ObjectMessage.");

public final static ErrorCode JMS030 = new JmsErrorCode("JMS030", "Missing collection trigger configuration.");
public final static ErrorCode JMS031 = new JmsErrorCode("JMS031", "Missing resource.");


protected final static class JmsErrorCode extends ErrorCode {

public JmsErrorCode(String code, String description) {
super(new QName(code, NAMESPACE_URI, PREFIX), description);
}

}

}
10 changes: 6 additions & 4 deletions java/src/org/exist/jms/shared/JmsConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import javax.jms.Message;
import javax.naming.Context;

import static org.exist.jms.shared.ErrorCodes.JMS011;


/**
* Wrapper for managing JMS configuration items.
Expand Down Expand Up @@ -215,22 +217,22 @@ public void validate() throws XPathException {

final String initialContextFactory = getInitialContextFactory();
if (initialContextFactory == null) {
throw new XPathException(String.format(CONFIG_ERROR_MSG, Context.INITIAL_CONTEXT_FACTORY));
throw new XPathException(JMS011, String.format(CONFIG_ERROR_MSG, Context.INITIAL_CONTEXT_FACTORY));
}

final String providerURL = getBrokerURL();
if (providerURL == null) {
throw new XPathException(String.format(CONFIG_ERROR_MSG, Context.PROVIDER_URL));
throw new XPathException(JMS011, String.format(CONFIG_ERROR_MSG, Context.PROVIDER_URL));
}

final String connectionFactory = getConnectionFactory();
if (connectionFactory == null) {
throw new XPathException(String.format(CONFIG_ERROR_MSG, Constants.CONNECTION_FACTORY));
throw new XPathException(JMS011, String.format(CONFIG_ERROR_MSG, Constants.CONNECTION_FACTORY));
}

final String destination = getDestination();
if (destination == null) {
throw new XPathException(String.format(CONFIG_ERROR_MSG, Constants.DESTINATION));
throw new XPathException(JMS011, String.format(CONFIG_ERROR_MSG, Constants.DESTINATION));
}

}
Expand Down
24 changes: 13 additions & 11 deletions java/src/org/exist/jms/shared/Receiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import javax.xml.datatype.Duration;
import java.util.Properties;

import static org.exist.jms.shared.ErrorCodes.*;

/**
* JMS messages receiver, represents a JMS connection.
* <p>
Expand Down Expand Up @@ -114,7 +116,7 @@ public void start() throws XPathException {
if (connection == null) {
final String txt = "JMS connection must be initialized first";
LOG.error(txt);
throw new XPathException(txt);
throw new XPathException(JMS025, txt);
}

try {
Expand All @@ -126,9 +128,9 @@ public void start() throws XPathException {
state = STATE.STARTED;

} catch (final JMSException ex) {
LOG.error(ex);
LOG.error(ex.getMessage(), ex);
messageListener.getReport().addReceiverError(ex);
throw new XPathException(ex.getMessage());
throw new XPathException(JMS004, ex.getMessage());
}
}

Expand Down Expand Up @@ -219,7 +221,7 @@ public void initialize() throws XPathException {
LOG.debug("" + jmsConfig.toString());

messageListener.getReport().addReceiverError(t);
throw new XPathException(t.getMessage());
throw new XPathException(JMS000, t.getMessage());
}

}
Expand All @@ -235,7 +237,7 @@ public void stop() throws XPathException {
if (connection == null) {
final String txt = "JMS connection must be initialized first";
LOG.error(txt);
throw new XPathException(txt);
throw new XPathException(JMS025, txt);
}

try {
Expand All @@ -247,10 +249,10 @@ public void stop() throws XPathException {
state = STATE.STOPPED;

} catch (final JMSException ex) {
LOG.error(ex);
LOG.error(ex.getMessage(), ex);

messageListener.getReport().addReceiverError(ex);
throw new XPathException(ex.getMessage());
throw new XPathException(JMS004, ex.getMessage());
}
}

Expand All @@ -265,7 +267,7 @@ public void close() throws XPathException {
if (connection == null) {
final String txt = "JMS connection must be initialized first";
LOG.error(txt);
throw new XPathException(txt);
throw new XPathException(JMS025, txt);
}

// If not stopped, try to stop first
Expand All @@ -285,7 +287,7 @@ public void close() throws XPathException {
try {
clientId = connection.getClientID();
} catch (final JMSException ex) {
LOG.debug(ex);
LOG.debug(ex.getMessage(), ex);
}

// Start listener
Expand All @@ -301,10 +303,10 @@ public void close() throws XPathException {
state = STATE.CLOSED;

} catch (final JMSException ex) {
LOG.error(ex);
LOG.error(ex.getMessage(), ex);

messageListener.getReport().addReceiverError(ex);
throw new XPathException(ex.getMessage());
throw new XPathException(JMS004, ex.getMessage());
}
}

Expand Down
2 changes: 1 addition & 1 deletion java/src/org/exist/jms/shared/ReportItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Throwable getThowable() {
}

public String getTimeStamp() {
return DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(timestamp);
return DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT.format(timestamp);
}

public String getMessage() {
Expand Down
Loading

0 comments on commit 706b62c

Please sign in to comment.