001: package dalma.endpoints.jms.impl;
002:
003: import dalma.spi.EndPointFactory;
004: import dalma.spi.UrlQueryParser;
005: import dalma.EndPoint;
006: import dalma.endpoints.jms.JMSEndPoint;
007:
008: import javax.naming.InitialContext;
009: import javax.naming.Context;
010: import javax.naming.NamingException;
011: import javax.jms.ConnectionFactory;
012: import javax.jms.Destination;
013: import javax.jms.Connection;
014: import javax.jms.Session;
015: import javax.jms.JMSException;
016: import java.text.ParseException;
017: import java.net.URI;
018: import java.net.URISyntaxException;
019: import java.util.Map;
020: import java.util.HashMap;
021:
022: /**
023: * {@link EndPointFactory} for the JMS endpoint.
024: *
025: * URL structure is:
026: *
027: * <pre>
028: * jms://?factory=jndi://jms/QueueConnectionFactory&in=jndi://jms/request-queue&out=jndi://jms/response-topics
029: * </pre>
030: *
031: * @author Kohsuke Kawaguchi
032: */
033: public class JMSEndPointFactory implements EndPointFactory {
034: public EndPoint create(String endPointName, String endpointURL)
035: throws ParseException {
036: try {
037: Context jndic = new InitialContext();
038:
039: UrlQueryParser qp = new UrlQueryParser(new URI(endpointURL));
040:
041: ConnectionFactory cf = getJndiValue(
042: ConnectionFactory.class, jndic, qp, "factory");
043:
044: Destination in = getJndiValue(Destination.class, jndic, qp,
045: "in");
046:
047: Destination out = null;
048: if (qp.getValue("out") != null)
049: out = getJndiValue(Destination.class, jndic, qp, "out");
050:
051: String user = qp.getValue("username");
052: String pass = qp.getValue("password");
053: Connection con;
054: if (user == null || pass == null)
055: con = cf.createConnection();
056: else
057: con = cf.createConnection(user, pass);
058:
059: String modeStr = qp.getValue("mode", "AUTO").toUpperCase();
060: if (!MODE_TABLE.containsKey(modeStr))
061: throw new ParseException("Unknown mode: " + modeStr, -1);
062: int mode = MODE_TABLE.get(modeStr);
063:
064: return new JMSEndPoint(endPointName, con.createSession(qp
065: .has("transacted"), mode), out, in);
066: } catch (URISyntaxException e) {
067: ParseException pe = new ParseException(e.getMessage(), e
068: .getIndex());
069: pe.initCause(e);
070: throw pe;
071: } catch (NamingException e) {
072: ParseException pe = new ParseException(e.getMessage(), -1);
073: pe.initCause(e);
074: throw pe;
075: } catch (JMSException e) {
076: ParseException pe = new ParseException(e.getMessage(), -1);
077: pe.initCause(e);
078: throw pe;
079: }
080: }
081:
082: private <T> T getJndiValue(Class<T> type, Context context,
083: UrlQueryParser qp, String paramName) throws ParseException,
084: NamingException {
085: String jndiPath = qp.getValue(paramName);
086: if (jndiPath == null)
087: throw new ParseException(
088: "JMS endpoint URL is missing the '" + paramName
089: + "' parameter", -1);
090:
091: Object value = context.lookup(jndiPath);
092:
093: if (!type.isInstance(value))
094: throw new ParseException("JNDI name " + jndiPath
095: + " is an object of type "
096: + value.getClass().getName() + "\n"
097: + type.getName() + " expected", -1);
098:
099: return type.cast(value);
100: }
101:
102: private static final Map<String, Integer> MODE_TABLE = new HashMap<String, Integer>();
103:
104: static {
105: MODE_TABLE.put("AUTO_ACKNOWLEDGE", Session.AUTO_ACKNOWLEDGE);
106: MODE_TABLE
107: .put("CLIENT_ACKNOWLEDGE", Session.CLIENT_ACKNOWLEDGE);
108: MODE_TABLE.put("DUPS_OK_ACKNOWLEDGE",
109: Session.DUPS_OK_ACKNOWLEDGE);
110: MODE_TABLE.put("AUTO", Session.AUTO_ACKNOWLEDGE);
111: MODE_TABLE.put("CLIENT", Session.CLIENT_ACKNOWLEDGE);
112: MODE_TABLE.put("DUPS_OK", Session.DUPS_OK_ACKNOWLEDGE);
113: }
114: }
|