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 */
017package org.apache.camel.main;
018
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.camel.CamelContext;
023import org.apache.camel.ProducerTemplate;
024import org.apache.camel.impl.CompositeRegistry;
025import org.apache.camel.impl.DefaultCamelContext;
026import org.apache.camel.impl.SimpleRegistry;
027import org.apache.camel.spi.Registry;
028
029/**
030 * A command line tool for booting up a CamelContext
031 *
032 * @version 
033 */
034public class Main extends MainSupport {
035
036    protected static Main instance;
037    protected final SimpleRegistry registry = new SimpleRegistry();
038
039    public Main() {
040    }
041
042    public static void main(String... args) throws Exception {
043        Main main = new Main();
044        instance = main;
045        main.run(args);
046
047        System.exit(main.getExitCode());
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    /**
103     * 
104     * Gets or creates the {@link org.apache.camel.CamelContext} this main class is using.
105     * 
106     * It just create a new CamelContextMap per call, please don't use it to access the camel context that will be ran by main.
107     * If you want to setup the CamelContext please use MainListener to get the new created camel context.
108     */
109    public CamelContext getOrCreateCamelContext() {
110        // force init
111        Map<String, CamelContext> map = getCamelContextMap();
112        if (map.size() >= 1) {
113            return map.values().iterator().next();
114        } else {
115            throw new IllegalStateException("Error creating CamelContext");
116        }
117    }
118    
119    // Implementation methods
120    // -------------------------------------------------------------------------
121
122    @Override
123    protected void doStart() throws Exception {
124        super.doStart();
125        postProcessContext();
126        if (getCamelContexts().size() > 0) {
127            getCamelContexts().get(0).start();
128        }
129    }
130
131    protected void doStop() throws Exception {
132        super.doStop();
133        if (getCamelContexts().size() > 0) {
134            getCamelContexts().get(0).stop();
135        }
136    }
137
138    protected ProducerTemplate findOrCreateCamelTemplate() {
139        if (getCamelContexts().size() > 0) {
140            return getCamelContexts().get(0).createProducerTemplate();
141        } else {
142            return null;
143        }
144    }
145
146    protected Map<String, CamelContext> getCamelContextMap() {
147        Map<String, CamelContext> answer = new HashMap<String, CamelContext>();
148
149        CamelContext camelContext = createContext();
150        if (registry.size() > 0) {
151            // set the registry through which we've already bound some beans
152            if (DefaultCamelContext.class.isAssignableFrom(camelContext.getClass())) {
153                CompositeRegistry compositeRegistry = new CompositeRegistry();
154                // make sure camel look up the Object from the registry first
155                compositeRegistry.addRegistry(registry);
156                // use the camel old registry as a fallback
157                compositeRegistry.addRegistry(((DefaultCamelContext) camelContext).getRegistry());
158                ((DefaultCamelContext) camelContext).setRegistry(compositeRegistry);
159            }
160        }
161
162        answer.put("camel-1", camelContext);
163        return answer;
164    }
165
166    protected CamelContext createContext() {
167        return new DefaultCamelContext();
168    }
169
170}