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.lwcontainer;
018    
019    import javax.jbi.component.ComponentContext;
020    import javax.jbi.messaging.MessageExchange;
021    import javax.jbi.messaging.MessageExchange.Role;
022    import javax.xml.namespace.QName;
023    
024    import org.apache.activemq.util.IdGenerator;
025    import org.apache.servicemix.common.ExchangeProcessor;
026    import org.apache.servicemix.common.endpoints.AbstractEndpoint;
027    import org.apache.servicemix.jbi.container.ActivationSpec;
028    import org.apache.servicemix.jbi.container.JBIContainer;
029    import org.apache.servicemix.jbi.framework.ComponentContextImpl;
030    
031    public class LwContainerEndpoint extends AbstractEndpoint {
032    
033        private static final QName SERVICE_NAME = new QName("http://lwcontainer.servicemix.org", "LwContainerComponent");
034    
035        private ActivationSpec activationSpec;
036    
037        public LwContainerEndpoint(ActivationSpec activationSpec) {
038            this.activationSpec = activationSpec;
039            this.service = SERVICE_NAME;
040            if (activationSpec.getId() != null) {
041                this.endpoint = activationSpec.getId();
042            } else if (activationSpec.getComponentName() != null) {
043                this.endpoint = activationSpec.getComponentName();
044            } else {
045                this.endpoint = new IdGenerator().generateId();
046            }
047        }
048    
049        public Role getRole() {
050            throw new UnsupportedOperationException();
051        }
052    
053        public void activate() throws Exception {
054            getContainer().activateComponent(activationSpec);
055        }
056    
057        public void deactivate() throws Exception {
058            getContainer().deactivateComponent(activationSpec.getId());
059        }
060    
061        public ExchangeProcessor getProcessor() {
062            throw new UnsupportedOperationException();
063        }
064        
065        public JBIContainer getContainer() {
066            ComponentContext context = getServiceUnit().getComponent().getComponentContext();
067            if (context instanceof ComponentContextImpl) {
068                return ((ComponentContextImpl) context).getContainer();
069            }
070            throw new IllegalStateException("LwContainer component can only be deployed in ServiceMix");
071        }
072    
073        @Override
074        public void process(MessageExchange exchange) throws Exception {
075            getProcessor().process(exchange);
076        }
077    
078        @Override
079        public void start() throws Exception {
080            // gracefully do nothing
081        }
082    
083        @Override
084        public void stop() throws Exception {
085            // gracefully do nothing
086            
087        }
088    
089    }