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.enableHangupSupport();
046        main.run(args);
047    }
048
049    /**
050     * Returns the currently executing main
051     *
052     * @return the current running instance
053     */
054    public static Main getInstance() {
055        return instance;
056    }
057
058    /**
059     * Binds the given <code>name</code> to the <code>bean</code> object, so
060     * that it can be looked up inside the CamelContext this command line tool
061     * runs with.
062     * 
063     * @param name the used name through which we do bind
064     * @param bean the object to bind
065     */
066    public void bind(String name, Object bean) {
067        registry.put(name, bean);
068    }
069
070    /**
071     * Using the given <code>name</code> does lookup for the bean being already
072     * bound using the {@link #bind(String, Object)} method.
073     * 
074     * @see Registry#lookupByName(String)
075     */
076    public Object lookup(String name) {
077        return registry.get(name);
078    }
079
080    /**
081     * Using the given <code>name</code> and <code>type</code> does lookup for
082     * the bean being already bound using the {@link #bind(String, Object)}
083     * method.
084     * 
085     * @see Registry#lookupByNameAndType(String, Class)
086     */
087    public <T> T lookup(String name, Class<T> type) {
088        return registry.lookupByNameAndType(name, type);
089    }
090
091    /**
092     * Using the given <code>type</code> does lookup for the bean being already
093     * bound using the {@link #bind(String, Object)} method.
094     * 
095     * @see Registry#findByTypeWithName(Class)
096     */
097    public <T> Map<String, T> lookupByType(Class<T> type) {
098        return registry.findByTypeWithName(type);
099    }
100
101    /**
102     * 
103     * Gets or creates the {@link org.apache.camel.CamelContext} this main class is using.
104     * 
105     * It just create a new CamelContextMap per call, please don't use it to access the camel context that will be ran by main.
106     * If you want to setup the CamelContext please use MainListener to get the new created camel context.
107     */
108    public CamelContext getOrCreateCamelContext() {
109        // force init
110        Map<String, CamelContext> map = getCamelContextMap();
111        if (map.size() >= 1) {
112            return map.values().iterator().next();
113        } else {
114            throw new IllegalStateException("Error creating CamelContext");
115        }
116    }
117    
118    // Implementation methods
119    // -------------------------------------------------------------------------
120
121    @Override
122    protected void doStart() throws Exception {
123        super.doStart();
124        postProcessContext();
125        if (getCamelContexts().size() > 0) {
126            getCamelContexts().get(0).start();
127        }
128    }
129
130    protected void doStop() throws Exception {
131        super.doStop();
132        if (getCamelContexts().size() > 0) {
133            getCamelContexts().get(0).stop();
134        }
135    }
136
137    protected ProducerTemplate findOrCreateCamelTemplate() {
138        if (getCamelContexts().size() > 0) {
139            return getCamelContexts().get(0).createProducerTemplate();
140        } else {
141            return null;
142        }
143    }
144
145    protected Map<String, CamelContext> getCamelContextMap() {
146        Map<String, CamelContext> answer = new HashMap<String, CamelContext>();
147
148        CamelContext camelContext = createContext();
149        if (registry.size() > 0) {
150            // set the registry through which we've already bound some beans
151            if (DefaultCamelContext.class.isAssignableFrom(camelContext.getClass())) {
152                CompositeRegistry compositeRegistry = new CompositeRegistry();
153                // make sure camel look up the Object from the registry first
154                compositeRegistry.addRegistry(registry);
155                // use the camel old registry as a fallback
156                compositeRegistry.addRegistry(((DefaultCamelContext) camelContext).getRegistry());
157                ((DefaultCamelContext) camelContext).setRegistry(compositeRegistry);
158            }
159        }
160
161        answer.put("camel-1", camelContext);
162        return answer;
163    }
164
165    protected CamelContext createContext() {
166        return new DefaultCamelContext();
167    }
168
169}