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.main;
018    
019    import java.util.HashMap;
020    import java.util.Map;
021    import javax.xml.bind.JAXBException;
022    
023    import org.apache.camel.CamelContext;
024    import org.apache.camel.ProducerTemplate;
025    import org.apache.camel.impl.DefaultCamelContext;
026    import org.apache.camel.impl.SimpleRegistry;
027    import org.apache.camel.spi.Registry;
028    import org.apache.camel.view.ModelFileGenerator;
029    
030    /**
031     * A command line tool for booting up a CamelContext
032     *
033     * @version 
034     */
035    public class Main extends MainSupport {
036    
037        protected static Main instance;
038        protected final SimpleRegistry registry = new SimpleRegistry();
039    
040        public Main() {
041        }
042    
043        public static void main(String... args) throws Exception {
044            Main main = new Main();
045            instance = main;
046            main.enableHangupSupport();
047            main.run(args);
048        }
049    
050        /**
051         * Returns the currently executing main
052         *
053         * @return the current running instance
054         */
055        public static Main getInstance() {
056            return instance;
057        }
058    
059        /**
060         * Binds the given <code>name</code> to the <code>bean</code> object, so
061         * that it can be looked up inside the CamelContext this command line tool
062         * runs with.
063         * 
064         * @param name the used name through which we do bind
065         * @param bean the object to bind
066         */
067        public void bind(String name, Object bean) {
068            registry.put(name, bean);
069        }
070    
071        /**
072         * Using the given <code>name</code> does lookup for the bean being already
073         * bound using the {@link #bind(String, Object)} method.
074         * 
075         * @see Registry#lookupByName(String)
076         */
077        public Object lookup(String name) {
078            return registry.get(name);
079        }
080    
081        /**
082         * Using the given <code>name</code> and <code>type</code> does lookup for
083         * the bean being already bound using the {@link #bind(String, Object)}
084         * method.
085         * 
086         * @see Registry#lookupByNameAndType(String, Class)
087         */
088        public <T> T lookup(String name, Class<T> type) {
089            return registry.lookupByNameAndType(name, type);
090        }
091    
092        /**
093         * Using the given <code>type</code> does lookup for the bean being already
094         * bound using the {@link #bind(String, Object)} method.
095         * 
096         * @see Registry#findByTypeWithName(Class)
097         */
098        public <T> Map<String, T> lookupByType(Class<T> type) {
099            return registry.findByTypeWithName(type);
100        }    
101        
102        // Implementation methods
103        // -------------------------------------------------------------------------
104    
105        @Override
106        protected void doStart() throws Exception {
107            super.doStart();
108            postProcessContext();
109            getCamelContexts().get(0).start();
110        }
111    
112        protected void doStop() throws Exception {
113            super.doStop();
114            if (getCamelContexts().size() > 0) {
115                getCamelContexts().get(0).stop();
116            }
117        }
118    
119        protected ProducerTemplate findOrCreateCamelTemplate() {
120            return getCamelContexts().get(0).createProducerTemplate();
121        }
122    
123        protected Map<String, CamelContext> getCamelContextMap() {
124            Map<String, CamelContext> answer = new HashMap<String, CamelContext>();
125    
126            CamelContext camelContext = createContext();
127            if (registry.size() > 0) {
128                // set the registry through which we've already bound some beans
129                if (DefaultCamelContext.class.isAssignableFrom(camelContext.getClass())) {
130                    ((DefaultCamelContext) camelContext).setRegistry(registry);
131                }
132            }
133    
134            answer.put("camel-1", camelContext);
135            return answer;
136        }
137    
138        protected CamelContext createContext() {
139            return new DefaultCamelContext();
140        }
141    
142        protected ModelFileGenerator createModelFileGenerator() throws JAXBException {
143            return null;
144        }
145    }