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.geronimo.system.main;
018    
019    import java.util.Collections;
020    
021    import org.apache.geronimo.cli.CLParserException;
022    import org.apache.geronimo.cli.client.ClientCLParser;
023    import org.apache.geronimo.gbean.AbstractName;
024    import org.apache.geronimo.gbean.AbstractNameQuery;
025    import org.apache.geronimo.kernel.Jsr77Naming;
026    import org.apache.geronimo.kernel.config.LifecycleException;
027    import org.apache.geronimo.kernel.config.NoSuchConfigException;
028    import org.apache.geronimo.kernel.repository.Artifact;
029    
030    /**
031     * @version $Revision: 718300 $ $Date: 2008-11-17 12:56:50 -0500 (Mon, 17 Nov 2008) $
032     */
033    public class ClientCommandLine extends CommandLine {
034    
035        /**
036         * Command line entry point called by executable jar
037         * @param args command line args
038         */
039        public static void main(String[] args) {
040            ClientCLParser parser = new ClientCLParser(System.out);
041            try {
042                parser.parse(args);
043            } catch (CLParserException e) {
044                System.err.println(e.getMessage());
045                parser.displayHelp();
046                System.exit(1);
047            }
048            
049            ClientCommandLine clientCommandLine = new ClientCommandLine();
050            int exitCode = clientCommandLine.execute(parser);
051            System.exit(exitCode);
052        }
053    
054        public ClientCommandLine(Artifact configuration, String[] args) throws Exception {
055            startClient(configuration, args);
056        }
057        
058        protected ClientCommandLine() {
059        }
060        
061        public int execute(ClientCLParser parser) {
062            log.info("Client startup begun");
063            try {
064                Artifact configuration = Artifact.create(parser.getApplicationClientConfiguration());
065                return startClient(configuration, parser.getApplicationClientArgs());
066            } catch (Exception e) {
067                ExceptionUtil.trimStackTrace(e);
068                if (e instanceof LifecycleException && ((LifecycleException)e).getCause() instanceof NoSuchConfigException) {
069                    log.error("The client is not found in server: " + ((NoSuchConfigException)((LifecycleException)e).getCause()).getConfigId());
070                } else {
071                    log.error("Client failed with exception: ", e);
072                }
073                return 2;
074            }
075        }
076        
077        protected int startClient(Artifact configuration, String[] args) throws Exception {
078            Jsr77Naming naming = new Jsr77Naming();
079            //this kinda sucks, but resource adapter modules deployed on the client insist on having a
080            //J2EEApplication name component
081            AbstractName baseName = naming.createRootName(configuration, configuration.toString(), "J2EEApplication");
082            AbstractNameQuery baseNameQuery = new AbstractNameQuery(baseName);
083            invokeMainGBean(Collections.singletonList(configuration), baseNameQuery, "main", args);
084            return 0;
085        }
086    }