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