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.test.blueprint;
018    
019    import java.util.HashMap;
020    import java.util.LinkedList;
021    import java.util.Map;
022    import javax.xml.bind.JAXBException;
023    
024    import org.apache.camel.CamelContext;
025    import org.apache.camel.ProducerTemplate;
026    import org.apache.camel.impl.MainSupport;
027    import org.apache.camel.view.ModelFileGenerator;
028    import org.osgi.framework.BundleContext;
029    
030    /**
031     * A command line tool for booting up a CamelContext using an OSGi Blueprint XML file
032     */
033    public class Main extends MainSupport {
034    
035        protected static Main instance;
036        private BundleContext bundleContext;
037        private String descriptors = "OSGI-INF/blueprint/*.xml";
038        private CamelContext camelContext;
039        private String bundleName = "MyBundle";
040        private boolean includeSelfAsBundle;
041    
042        public Main() {
043    
044            addOption(new ParameterOption("ac", "applicationContext",
045                    "Sets the classpath based OSGi Blueprint", "applicationContext") {
046                protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
047                    setDescriptors(parameter);
048                }
049            });
050    
051            addOption(new ParameterOption("fa", "fileApplicationContext",
052                    "Sets the filesystem based OSGi Blueprint", "fileApplicationContext") {
053                protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
054                    setDescriptors(parameter);
055                }
056            });
057    
058        }
059    
060        public static void main(String... args) throws Exception {
061            Main main = new Main();
062            main.enableHangupSupport();
063            main.run(args);
064        }
065    
066        /**
067         * Returns the currently executing main
068         *
069         * @return the current running instance
070         */
071        public static Main getInstance() {
072            return instance;
073        }
074    
075        @Override
076        protected void doStart() throws Exception {
077            super.doStart();
078            if (bundleContext == null) {
079                String descriptors = getDescriptors();
080                if (descriptors == null) {
081                    throw new IllegalArgumentException("Descriptors must be provided, with the name of the blueprint XML file");
082                }
083                LOG.debug("Starting Blueprint XML file: " + descriptors);
084                bundleContext = createBundleContext(bundleName);
085    
086                camelContext = CamelBlueprintHelper.getOsgiService(bundleContext, CamelContext.class);
087                if (camelContext == null) {
088                    throw new IllegalArgumentException("Cannot find CamelContext in blueprint XML file: " + descriptors);
089                }
090            }
091        }
092    
093        @Override
094        protected void doStop() throws Exception {
095            // stop camel context
096            if (camelContext != null) {
097                camelContext.stop();
098            }
099            // and then stop blueprint
100            LOG.debug("Stopping Blueprint XML file: " + descriptors);
101            CamelBlueprintHelper.disposeBundleContext(bundleContext);
102            // call completed to properly stop as we count down the waiting latch
103            completed();
104        }
105    
106        @Override
107        protected ProducerTemplate findOrCreateCamelTemplate() {
108            if (camelContext != null) {
109                return camelContext.createProducerTemplate();
110            } else {
111                return null;
112            }
113        }
114    
115        protected BundleContext createBundleContext() throws Exception {
116            return createBundleContext(getClass().getSimpleName());
117        }
118    
119        protected BundleContext createBundleContext(String name) throws Exception {
120            return CamelBlueprintHelper.createBundleContext(name, descriptors, isIncludeSelfAsBundle());
121        }
122    
123        @Override
124        protected Map<String, CamelContext> getCamelContextMap() {
125            Map<String, CamelContext> map = new HashMap<String, CamelContext>(1);
126            if (camelContext != null) {
127                map.put(camelContext.getName(), camelContext);
128            }
129            return map;
130        }
131    
132        @Override
133        protected ModelFileGenerator createModelFileGenerator() throws JAXBException {
134            throw new UnsupportedOperationException("This method is not supported");
135        }
136    
137        public String getDescriptors() {
138            return descriptors;
139        }
140    
141        public void setDescriptors(String descriptors) {
142            this.descriptors = descriptors;
143        }
144    
145        public String getBundleName() {
146            return bundleName;
147        }
148    
149        public void setBundleName(String bundleName) {
150            this.bundleName = bundleName;
151        }
152    
153        public boolean isIncludeSelfAsBundle() {
154            return includeSelfAsBundle;
155        }
156    
157        public void setIncludeSelfAsBundle(boolean includeSelfAsBundle) {
158            this.includeSelfAsBundle = includeSelfAsBundle;
159        }
160    }