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.components.util.MarshalerSupport;
031    import org.apache.servicemix.jbi.util.DOMUtil;
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: 219 $
041     */
042    public class DefaultQuartzMarshaler extends MarshalerSupport implements QuartzMarshaler {
043    
044        public void populateNormalizedMessage(NormalizedMessage message, JobExecutionContext context) 
045            throws JobExecutionException, MessagingException {
046    
047            JobDetail detail = context.getJobDetail();
048            JobDataMap dataMap = detail.getJobDataMap();
049            for (Iterator iter = dataMap.entrySet().iterator(); iter.hasNext();) {
050                Map.Entry entry = (Map.Entry) iter.next();
051                String key = (String) entry.getKey();
052                if (!key.equals(ServiceMixJob.COMPONENT_NAME) && !key.equals(ServiceMixJob.ENDPOINT_NAME)) {
053                    Object value = entry.getValue();
054                    message.setProperty(key, value);
055                }
056            }
057            try {
058                Document document = getTransformer().createDocument();
059                Element root = document.createElement("timer");
060                document.appendChild(root);
061                DOMUtil.addChildElement(root, "name", detail.getName());
062                DOMUtil.addChildElement(root, "group", detail.getGroup());
063                DOMUtil.addChildElement(root, "fullname", detail.getFullName());
064                DOMUtil.addChildElement(root, "description", detail.getDescription());
065                DOMUtil.addChildElement(root, "fireTime", context.getFireTime());
066                message.setContent(new DOMSource(document));
067            } catch (ParserConfigurationException e) {
068                throw new MessagingException("Failed to create content: " + e, e);
069            }
070        }
071    
072    }