001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.transport.jms;
019:
020: import java.util.Enumeration;
021: import java.util.List;
022: import java.util.Properties;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025:
026: import javax.jms.JMSException;
027: import javax.jms.Message;
028: import javax.naming.Context;
029: import javax.naming.InitialContext;
030: import javax.naming.NamingException;
031:
032: import org.apache.cxf.common.logging.LogUtils;
033:
034: public final class JMSUtils {
035:
036: private static final Logger LOG = LogUtils
037: .getL7dLogger(JMSUtils.class);
038:
039: private JMSUtils() {
040:
041: }
042:
043: public static Context getInitialContext(AddressType addrType)
044: throws NamingException {
045: Properties env = new Properties();
046: populateContextEnvironment(addrType, env);
047:
048: if (LOG.isLoggable(Level.FINE)) {
049: Enumeration props = env.propertyNames();
050:
051: while (props.hasMoreElements()) {
052: String name = (String) props.nextElement();
053: String value = env.getProperty(name);
054: LOG.log(Level.FINE, "Context property: " + name + " | "
055: + value);
056: }
057: }
058:
059: Context context = new InitialContext(env);
060:
061: return context;
062: }
063:
064: protected static void populateContextEnvironment(
065: AddressType addrType, Properties env) {
066:
067: java.util.ListIterator listIter = addrType
068: .getJMSNamingProperty().listIterator();
069:
070: while (listIter.hasNext()) {
071: JMSNamingPropertyType propertyPair = (JMSNamingPropertyType) listIter
072: .next();
073:
074: if (null != propertyPair.getValue()) {
075: env.setProperty(propertyPair.getName(), propertyPair
076: .getValue());
077: }
078: }
079: }
080:
081: public static int getJMSDeliveryMode(JMSMessageHeadersType headers) {
082: int deliveryMode = Message.DEFAULT_DELIVERY_MODE;
083:
084: if (headers != null && headers.isSetJMSDeliveryMode()) {
085: deliveryMode = headers.getJMSDeliveryMode();
086: }
087: return deliveryMode;
088: }
089:
090: public static int getJMSPriority(JMSMessageHeadersType headers) {
091: int priority = Message.DEFAULT_PRIORITY;
092: if (headers != null && headers.isSetJMSPriority()) {
093: priority = headers.getJMSPriority();
094: }
095: return priority;
096: }
097:
098: public static long getTimeToLive(JMSMessageHeadersType headers) {
099: long ttl = -1;
100: if (headers != null && headers.isSetTimeToLive()) {
101: ttl = headers.getTimeToLive();
102: }
103: return ttl;
104: }
105:
106: public static String getCorrelationId(JMSMessageHeadersType headers) {
107: String correlationId = null;
108: if (headers != null && headers.isSetJMSCorrelationID()) {
109: correlationId = headers.getJMSCorrelationID();
110: }
111: return correlationId;
112: }
113:
114: public static void setMessageProperties(
115: JMSMessageHeadersType headers, Message message)
116: throws JMSException {
117:
118: if (headers != null && headers.isSetProperty()) {
119: List<JMSPropertyType> props = headers.getProperty();
120: for (int x = 0; x < props.size(); x++) {
121: message.setStringProperty(props.get(x).getName(), props
122: .get(x).getValue());
123: }
124: }
125: }
126:
127: }
|