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    package org.apache.servicemix;
018    
019    import java.io.File;
020    import java.util.ArrayList;
021    import java.util.List;
022    
023    import org.apache.servicemix.jbi.container.SpringJBIContainer;
024    import org.apache.servicemix.xbean.ClassLoaderXmlPreprocessor;
025    import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
026    import org.apache.xbean.spring.context.FileSystemXmlApplicationContext;
027    import org.springframework.beans.factory.DisposableBean;
028    import org.springframework.context.ApplicationContext;
029    
030    /**
031     * A simple stand alone application which runs ServiceMix from the command line.
032     *
033     * @version $Revision: 641065 $
034     */
035    public final class Main {
036        
037        private Main() {
038        }
039    
040        public static void main(String args[]) {
041            try {
042                String version = "";
043                Package p = Package.getPackage("org.apache.servicemix");
044                if (p != null) {
045                    version = ": " + p.getImplementationVersion();
046                }
047                System.out.println("Starting Apache ServiceMix ESB" + version);
048                System.out.println();
049    
050                final ApplicationContext context;
051                if (args.length <= 0) {
052                    System.out.println("Loading Apache ServiceMix from servicemix.xml on the CLASSPATH");
053                    context = new ClassPathXmlApplicationContext("servicemix.xml");
054                } else {
055                    String file = args[0];
056    
057                    if ("-?".equals(file) || "?".equals(file) || "--help".equals(file) || "-h".equals(file)) {
058                        System.out.println("Usage: Main [-v1] [xmlConfigFile]");
059                        System.out.println("If an XML config file is not specified then servicemix.xml is used from the CLASSPATH");
060                        return;
061                    }
062                    
063                    List processors = new ArrayList();
064                    processors.add(new ClassLoaderXmlPreprocessor(new File(".")));
065                    System.out.println("Loading Apache ServiceMix from file: " + file);
066                    context = new FileSystemXmlApplicationContext(file, processors);
067                }
068                SpringJBIContainer container = (SpringJBIContainer) context.getBean("jbi");            
069                container.onShutDown(new Runnable() {
070                    public void run() {
071                        if (context instanceof DisposableBean) {
072                            try {
073                                ((DisposableBean) context).destroy();
074                            } catch (Exception e) {
075                                System.out.println("Caught: " + e);
076                                e.printStackTrace();
077                            }
078                        }
079                    }
080                });
081                
082            } catch (Exception e) {
083                System.out.println("Caught: " + e);
084                e.printStackTrace();
085            }
086        }
087    }