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.common.xbean;
018    
019    import java.util.Arrays;
020    
021    import org.apache.servicemix.common.Endpoint;
022    import org.apache.servicemix.common.ServiceMixComponent;
023    
024    import javax.jbi.management.DeploymentException;
025    
026    /**
027     * A useful XBean deployer which check that the endpoints inherit one of
028     * the given allowed endpoint classes.
029     * 
030     * @author gnodet
031     */
032    public class BaseXBeanDeployer extends AbstractXBeanDeployer {
033    
034        private final Class[] endpointClasses;
035        
036        public BaseXBeanDeployer(ServiceMixComponent component) {
037            this(component, new Class[0]);
038        }
039        
040        public BaseXBeanDeployer(ServiceMixComponent component, Class endpointClass) {
041            this(component, new Class[] { endpointClass });
042        }
043        
044        public BaseXBeanDeployer(ServiceMixComponent component, Class[] endpointClasses) {
045            super(component);
046            if (endpointClasses == null) {
047                throw new NullPointerException("endpointClasses must be non null");
048            }
049            this.endpointClasses = endpointClasses;
050        }
051        
052        protected void validate(Endpoint endpoint) throws DeploymentException {
053            for (int i = 0; i < endpointClasses.length; i++) {
054                if (endpointClasses[i].isInstance(endpoint)) {
055                    super.validate(endpoint);
056                    return;
057                }
058            }
059            throw new DeploymentException("Endpoint is not of type: " + Arrays.asList(endpointClasses) + " but is of type: " + endpoint.getClass());
060        }
061        
062    }