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