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.camel.component.eventadmin;
018    
019    import java.util.Dictionary;
020    import java.util.Hashtable;
021    import java.util.Map;
022    
023    import org.apache.camel.CamelContext;
024    import org.apache.camel.CamelExchangeException;
025    import org.apache.camel.Exchange;
026    import org.apache.camel.Message;
027    import org.apache.camel.impl.DefaultProducer;
028    import org.apache.camel.util.CamelContextHelper;
029    import org.osgi.service.event.Event;
030    import org.osgi.service.event.EventAdmin;
031    import org.osgi.util.tracker.ServiceTracker;
032    
033    /**
034     * EventAdmin producer
035     */
036    public class EventAdminProducer extends DefaultProducer {
037    
038        private final EventAdminEndpoint endpoint;
039        private ServiceTracker tracker;
040    
041        public EventAdminProducer(EventAdminEndpoint endpoint) {
042            super(endpoint);
043            this.endpoint = endpoint;
044            this.tracker = new ServiceTracker(endpoint.getComponent().getBundleContext(), EventAdmin.class.getName(), null);
045        }
046    
047        @Override
048        protected void doStart() throws Exception {
049            super.doStart();
050            this.tracker.open();
051        }
052    
053        @Override
054        protected void doStop() throws Exception {
055            this.tracker.close();
056            super.doStop();
057        }
058    
059        public void process(Exchange exchange) throws Exception {
060            EventAdmin admin = (EventAdmin) this.tracker.getService();
061            if (admin != null) {
062                Event event = getEvent(exchange);
063                if (endpoint.isSend()) {
064                    admin.sendEvent(event);
065                } else {
066                    admin.postEvent(event);
067                }
068            } else {
069                throw new CamelExchangeException("EventAdmin service not present", exchange);
070            }
071        }
072    
073        protected String getTopic(Exchange exchange) {
074            Message in = exchange.getIn();
075            String topic = in.getHeader(EventAdminConstants.EVENTADMIN_TOPIC, String.class);
076            if (topic != null) {
077                in.removeHeader(EventAdminConstants.EVENTADMIN_TOPIC);
078            }
079            if (topic == null) {
080                topic = endpoint.getTopic();
081            }
082            return topic;
083        }
084    
085        protected Event getEvent(Exchange exchange) {
086            Message in = exchange.getIn();
087            CamelContext context = endpoint.getCamelContext();
088            Event event = context.getTypeConverter().convertTo(Event.class, exchange, in.getBody());
089            if (event == null) {
090                String topic = getTopic(exchange);
091                Dictionary props = getProperties(exchange);
092                event = new Event(topic, props);
093            }
094            return event;
095        }
096    
097        protected Dictionary getProperties(Exchange exchange) {
098            Message in = exchange.getIn();
099            CamelContext context = endpoint.getCamelContext();
100            Map map = context.getTypeConverter().convertTo(Map.class, exchange, in.getBody());
101            Dictionary dict = new Hashtable();
102            for (Object key : map.keySet()) {
103                String keyString = CamelContextHelper.convertTo(context, String.class, key);
104                if (keyString != null) {
105                    Object val = map.get(key);
106                    // TODO: convert to acceptable value
107                    dict.put(keyString, val);
108                }
109            }
110            return dict;
111        }
112    
113    }