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.Properties;
020    import java.util.concurrent.Executor;
021    
022    import org.apache.camel.Exchange;
023    import org.apache.camel.Processor;
024    import org.apache.camel.impl.DefaultConsumer;
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    import org.osgi.framework.ServiceRegistration;
028    import org.osgi.service.event.Event;
029    import org.osgi.service.event.EventConstants;
030    import org.osgi.service.event.EventHandler;
031    
032    public class EventAdminConsumer extends DefaultConsumer implements EventHandler {
033    
034        private static final transient Log LOG = LogFactory.getLog(EventAdminConsumer.class);
035        private final EventAdminEndpoint endpoint;
036        private ServiceRegistration registration;
037        private Executor executor;
038    
039        public EventAdminConsumer(EventAdminEndpoint endpoint, Processor processor) {
040            super(endpoint, processor);
041            this.endpoint = endpoint;
042        }
043    
044        public void handleEvent(Event event) {
045            Exchange exchange = endpoint.createExchange();
046            // TODO: populate exchange headers
047            exchange.getIn().setBody(event);
048    
049            if (LOG.isTraceEnabled()) {
050                LOG.trace("EventAdmin " + endpoint.getTopic() + " is firing");
051            }
052            try {
053                getProcessor().process(exchange);
054                // log exception if an exception occurred and was not handled
055                if (exchange.getException() != null) {
056                    getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
057                }
058            } catch (Exception e) {
059                getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
060            }
061        }
062    
063        @Override
064        protected void doStart() throws Exception {
065            super.doStart();
066            Properties props = new Properties();
067            props.put(EventConstants.EVENT_TOPIC, endpoint.getTopic());
068            registration = endpoint.getComponent().getBundleContext().registerService(EventHandler.class.getName(), this, props);
069        }
070    
071        @Override
072        protected void doStop() throws Exception {
073            if (registration != null) {
074                registration.unregister();
075            }
076            super.doStop();
077        }
078    }