001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.quartz.support;
018    
019    import java.util.Iterator;
020    import java.util.Map;
021    
022    import javax.jbi.messaging.MessagingException;
023    import javax.jbi.messaging.NormalizedMessage;
024    import javax.xml.parsers.ParserConfigurationException;
025    import javax.xml.transform.dom.DOMSource;
026    
027    import org.w3c.dom.Document;
028    import org.w3c.dom.Element;
029    
030    import org.apache.servicemix.common.util.DOMUtil;
031    import org.apache.servicemix.jbi.jaxp.SourceTransformer;
032    import org.quartz.JobDataMap;
033    import org.quartz.JobDetail;
034    import org.quartz.JobExecutionContext;
035    import org.quartz.JobExecutionException;
036    
037    /**
038     * The default implementation of the Quartz marshaler
039     *
040     * @version $Revision: 45185 $
041     */
042    public class DefaultQuartzMarshaler implements QuartzMarshaler {
043    
044        private SourceTransformer transformer;
045    
046        /**
047         * Converts the value to a String
048         *
049         * @param value the value to be coerced into a string
050         * @return the value as a string or null if it cannot be converted
051         */
052        protected String asString(Object value) {
053            return value != null ? value.toString() : null;
054        }
055    
056        // Properties
057        //-------------------------------------------------------------------------
058        public SourceTransformer getTransformer() {
059            if (transformer == null) {
060                transformer = new SourceTransformer();
061            }
062            return transformer;
063        }
064    
065        public void setTransformer(SourceTransformer transformer) {
066            this.transformer = transformer;
067        }
068    
069        public void populateNormalizedMessage(NormalizedMessage message, JobExecutionContext context)
070            throws JobExecutionException, MessagingException {
071    
072            JobDetail detail = context.getJobDetail();
073            JobDataMap dataMap = detail.getJobDataMap();
074            for (Iterator iter = dataMap.entrySet().iterator(); iter.hasNext();) {
075                Map.Entry entry = (Map.Entry) iter.next();
076                String key = (String) entry.getKey();
077                if (!key.equals(ServiceMixJob.COMPONENT_NAME) && !key.equals(ServiceMixJob.ENDPOINT_NAME)) {
078                    Object value = entry.getValue();
079                    message.setProperty(key, value);
080                }
081            }
082            try {
083                Document document = getTransformer().createDocument();
084                Element root = document.createElement("timer");
085                document.appendChild(root);
086                DOMUtil.addChildElement(root, "name", detail.getName());
087                DOMUtil.addChildElement(root, "group", detail.getGroup());
088                DOMUtil.addChildElement(root, "fullname", detail.getFullName());
089                DOMUtil.addChildElement(root, "description", detail.getDescription());
090                DOMUtil.addChildElement(root, "fireTime", context.getFireTime());
091                message.setContent(new DOMSource(document));
092            } catch (ParserConfigurationException e) {
093                throw new MessagingException("Failed to create content: " + e, e);
094            }
095        }
096    
097    }