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