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