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 // Implementation methods 121 // ------------------------------------------------------------------------- 122 123 @Override 124 protected void doStart() throws Exception { 125 super.doStart(); 126 postProcessContext(); 127 if (getCamelContexts().size() > 0) { 128 try { 129 getCamelContexts().get(0).start(); 130 // if we were veto started then mark as completed 131 } finally { 132 if (getCamelContexts().get(0).isVetoStarted()) { 133 completed(); 134 } 135 } 136 } 137 } 138 139 protected void doStop() throws Exception { 140 super.doStop(); 141 if (getCamelContexts().size() > 0) { 142 getCamelContexts().get(0).stop(); 143 } 144 } 145 146 protected ProducerTemplate findOrCreateCamelTemplate() { 147 if (getCamelContexts().size() > 0) { 148 return getCamelContexts().get(0).createProducerTemplate(); 149 } else { 150 return null; 151 } 152 } 153 154 protected Map<String, CamelContext> getCamelContextMap() { 155 Map<String, CamelContext> answer = new HashMap<String, CamelContext>(); 156 157 CamelContext camelContext = createContext(); 158 if (registry.size() > 0) { 159 // set the registry through which we've already bound some beans 160 if (DefaultCamelContext.class.isAssignableFrom(camelContext.getClass())) { 161 CompositeRegistry compositeRegistry = new CompositeRegistry(); 162 // make sure camel look up the Object from the registry first 163 compositeRegistry.addRegistry(registry); 164 // use the camel old registry as a fallback 165 compositeRegistry.addRegistry(((DefaultCamelContext) camelContext).getRegistry()); 166 ((DefaultCamelContext) camelContext).setRegistry(compositeRegistry); 167 } 168 } 169 170 answer.put("camel-1", camelContext); 171 return answer; 172 } 173 174 protected CamelContext createContext() { 175 return new DefaultCamelContext(); 176 } 177 178 /** 179 * A list of locations to load properties. You can use comma to separate multiple locations. 180 * This option will override any default locations and only use the locations from this option. 181 */ 182 protected void setPropertyPlaceholderLocations(String location) { 183 PropertiesComponent pc = new PropertiesComponent(); 184 pc.setLocation(location); 185 bind("properties", pc); 186 } 187}