001 /*
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
005 *
006 * The contents of this file are subject to the terms of either the GNU
007 * General Public License Version 2 only ("GPL") or the Common Development
008 * and Distribution License("CDDL") (collectively, the "License"). You
009 * may not use this file except in compliance with the License. You can
010 * obtain a copy of the License at
011 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
012 * or packager/legal/LICENSE.txt. See the License for the specific
013 * language governing permissions and limitations under the License.
014 *
015 * When distributing the software, include this License Header Notice in each
016 * file and include the License file at packager/legal/LICENSE.txt.
017 *
018 * GPL Classpath Exception:
019 * Oracle designates this particular file as subject to the "Classpath"
020 * exception as provided by Oracle in the GPL Version 2 section of the License
021 * file that accompanied this code.
022 *
023 * Modifications:
024 * If applicable, add the following below the License Header, with the fields
025 * enclosed by brackets [] replaced by your own identifying information:
026 * "Portions Copyright [year] [name of copyright owner]"
027 *
028 * Contributor(s):
029 * If you wish your version of this file to be governed by only the CDDL or
030 * only the GPL Version 2, indicate your decision by adding "[Contributor]
031 * elects to include this software in this distribution under the [CDDL or GPL
032 * Version 2] license." If you don't indicate a single choice of license, a
033 * recipient has the option to distribute your version of this file under
034 * either the CDDL, the GPL Version 2 or to extend the choice of license to
035 * its licensees as provided above. However, if you add GPL Version 2 code
036 * and therefore, elected the GPL Version 2 license, then the option applies
037 * only if the new code is made subject to such option by the copyright
038 * holder.
039 */
040
041 package com.sun.enterprise.admin.cli.remote;
042
043 import java.io.*;
044 import java.net.*;
045 import java.util.logging.*;
046 import org.glassfish.api.admin.*;
047 import com.sun.enterprise.admin.cli.*;
048
049 /**
050 * Domain Admin Server utility method.
051 */
052 public class DASUtils {
053 private static final Logger logger =
054 Logger.getLogger("com.sun.enterprise.admin.cli.remote");
055
056 public enum Error {
057 NONE, AUTHENTICATION, CONNECTION, IO, UNKNOWN
058 };
059
060 private DASUtils() {
061 // can't instantiate
062 }
063
064 /**
065 * See if DAS is alive.
066 * Do not print out the results of the version command from the server.
067 *
068 * @return true if DAS can be reached and can handle commands,
069 * otherwise false.
070 */
071 public static boolean pingDASQuietly(ProgramOptions programOpts,
072 Environment env) {
073 try {
074 RemoteCommand cmd = new RemoteCommand("version", programOpts, env);
075 cmd.executeAndReturnOutput(new String[]{"version"});
076 return true;
077 }
078 catch (AuthenticationException aex) {
079 return true;
080 }
081 catch (Exception ex) {
082 ExceptionAnalyzer ea = new ExceptionAnalyzer(ex);
083 if(ea.getFirstInstanceOf(ConnectException.class) != null) {
084 logger.finer("Got java.net.ConnectException");
085 return false; // this definitely means server is not up
086 }
087 else if(ea.getFirstInstanceOf(IOException.class) != null) {
088 logger.finer("It appears that server has started, but for"
089 + " some reason the exception is thrown: "
090 + ex.getMessage());
091 return true;
092 }
093 else {
094 return false; // unknown error, shouldn't really happen
095 }
096 }
097 }
098
099 /**
100 * See if DAS is alive, but insist that athentication is correct.
101 * Do not print out the results of the version command from the server.
102 *
103 * @return Error code indicating status
104 */
105 public static Error pingDASWithAuth(ProgramOptions programOpts,
106 Environment env) throws CommandException {
107 try {
108 RemoteCommand cmd = new RemoteCommand("version", programOpts, env);
109 cmd.executeAndReturnOutput(new String[]{"version"});
110 }
111 catch (AuthenticationException aex) {
112 return Error.AUTHENTICATION;
113 }
114 catch (Exception ex) {
115 ExceptionAnalyzer ea = new ExceptionAnalyzer(ex);
116 if(ea.getFirstInstanceOf(ConnectException.class) != null) {
117 logger.finer("Got java.net.ConnectException");
118 return Error.CONNECTION;
119 }
120 else if(ea.getFirstInstanceOf(IOException.class) != null) {
121 logger.finer("It appears that server has started, but for"
122 + " some reason the exception is thrown: "
123 + ex.getMessage());
124 return Error.IO;
125 }
126 else {
127 return Error.UNKNOWN;
128 }
129 }
130 return Error.NONE;
131 }
132 }