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 */
017
018 package org.apache.camel.spring.javaconfig;
019
020 import java.util.ArrayList;
021 import java.util.LinkedList;
022 import java.util.List;
023 import java.util.Map;
024
025 import org.apache.camel.CamelContext;
026 import org.apache.camel.spring.SpringCamelContext;
027 import org.apache.camel.util.ObjectHelper;
028 import org.springframework.context.ApplicationContext;
029 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
030 import org.springframework.context.support.AbstractApplicationContext;
031
032 /**
033 * The Main class which takes the spring java config parameter
034 */
035 public class Main extends org.apache.camel.spring.Main {
036
037 private String basedPackages;
038
039 private String configClassesString;
040
041 public Main() {
042
043 addOption(new ParameterOption("bp", "basedPackages",
044 "Sets the based packages of spring java config ApplicationContext", "basedPackages") {
045 protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
046 setBasedPackages(parameter);
047 }
048 });
049
050 addOption(new ParameterOption("cc", "configClasses",
051 "Sets the config Class of spring java config ApplicationContext", "configureClasses") {
052 protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
053 setConfigClassesString(parameter);
054 }
055 });
056 }
057
058 public static void main(String... args) throws Exception {
059 Main main = new Main();
060 instance = main;
061 main.enableHangupSupport();
062 main.run(args);
063 }
064
065 public void setBasedPackages(String config) {
066 basedPackages = config;
067 }
068
069 public String getBasedPackages() {
070 return basedPackages;
071 }
072
073 public void setConfigClassesString(String config) {
074 configClassesString = config;
075 }
076
077 public String getConfigClassesString() {
078 return configClassesString;
079 }
080
081 private Class<?>[] getConfigClasses(String configureClasses) {
082 List<Class<?>> answer = new ArrayList<Class<?>>();
083 String[] classes = configureClasses.split(";");
084 for (String className : classes) {
085 Class<?> configClass = ObjectHelper.loadClass(className);
086 if (configClass != null) {
087 answer.add(configClass);
088 }
089 }
090 return answer.toArray(new Class<?>[answer.size()]);
091 }
092
093 protected AbstractApplicationContext createDefaultApplicationContext() {
094 ApplicationContext parentContext = getParentApplicationContext();
095 AnnotationConfigApplicationContext acApplicationContext = new AnnotationConfigApplicationContext();
096 if (parentContext != null) {
097 acApplicationContext.setParent(parentContext);
098 }
099 if (getConfigClassesString() != null) {
100 Class<?>[] configClasses = getConfigClasses(getConfigClassesString());
101 for (Class<?> cls : configClasses) {
102 acApplicationContext.register(cls);
103 }
104 }
105 if (getBasedPackages() != null) {
106 String[] basePackages = getBasedPackages().split(";");
107 for (String basePackage : basePackages) {
108 acApplicationContext.scan(basePackage);
109 }
110 }
111 acApplicationContext.refresh();
112 return acApplicationContext;
113
114 }
115
116 }