001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)Util.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.ui.common;
030:
031: import java.lang.reflect.Constructor;
032: import java.lang.reflect.InvocationTargetException;
033: import java.net.InetAddress;
034: import java.util.logging.Level;
035: import java.util.logging.Logger;
036:
037: /**
038: * This object provides utility methods common for client or server side code for tools.
039: * @author Sun Microsystems, Inc.
040: */
041: public class Util {
042: /**
043: * resource bundle
044: */
045: private static I18NBundle sI18NBundle = null;
046:
047: /** gives the I18N bundle
048: *@return I18NBundle object
049: */
050: public static I18NBundle getCommonI18NBundle() {
051: // lazzy initialize the JBI Client
052: if (sI18NBundle == null) {
053: sI18NBundle = new I18NBundle("com.sun.jbi.ui.common");
054: }
055: return sI18NBundle;
056: }
057:
058: /**
059: * prints the string to debug output
060: * @param msg text to print
061: */
062: public static void logDebug(String msg) {
063: ToolsLogManager.getCommonLogger().log(Level.FINER, msg);
064: }
065:
066: /**
067: * logs the message
068: * @param logger logger
069: * @param ex exception
070: */
071: public static void logDebug(Logger logger, Exception ex) {
072: logger.log(Level.FINER, ex.getMessage(), ex);
073: }
074:
075: /**
076: * logs the message
077: * @param ex exception
078: */
079: public static void logDebug(Exception ex) {
080: logDebug(ToolsLogManager.getCommonLogger(), ex);
081: }
082:
083: /**
084: * This method creates a new object of specified type using the reflection apis. It
085: * instantiate the object using its constructor that takes String as a argument.
086: * This is mainly used to convert the string value to a specified object type.
087: * @param type the fully qualified name of the desired class
088: * @param arg String value to be passed to the class constructor
089: * @throws java.lang.ClassNotFoundException on error
090: * @throws java.lang.NoSuchMethodException on error
091: * @throws java.lang.InstantiationException on error
092: * @throws java.lang.IllegalAccessException on error
093: * @throws java.lang.reflect.InvocationTargetException on error
094: * @return constrcuted object of the type specified with the string value
095: */
096: public static Object newInstance(String type, String arg)
097: throws ClassNotFoundException, NoSuchMethodException,
098: InstantiationException, IllegalAccessException,
099: InvocationTargetException {
100: Class objClass = null;
101: Object instance = null;
102:
103: // check for the primitive types
104: // boolean, byte, char, short, int, long, float, double and void
105: if (type.equals("boolean")) {
106: objClass = Boolean.class;
107: } else if (type.equals("byte")) {
108: objClass = Byte.class;
109: } else if (type.equals("char")) {
110: objClass = Character.class;
111: } else if (type.equals("short")) {
112: objClass = Short.class;
113: } else if (type.equals("int")) {
114: objClass = Integer.class;
115: } else if (type.equals("long")) {
116: objClass = Long.class;
117: } else if (type.equals("float")) {
118: objClass = Float.class;
119: } else if (type.equals("double")) {
120: objClass = Double.class;
121: } else if (type.equals("void")) {
122: objClass = Void.class;
123: } else {
124: objClass = Class.forName(type);
125: }
126:
127: Class[] argClass = new Class[] { String.class };
128: Constructor objConstructor = objClass.getConstructor(argClass);
129: String[] argValue = { arg };
130: instance = objConstructor.newInstance(argValue);
131: return instance;
132: }
133:
134: /**
135: * This method checks that the host name is a localhost or a remote host. This method
136: * compares the host name on which this method is getting executes with the host name
137: * that is passed to this method. if the comparison matches it returns true.
138: * @param host host name to check for local or remote host.
139: * @throws java.net.UnknownHostException on errror.
140: * @return true if the host name passed is a localhost else false.
141: */
142: public static boolean isLocalHost(String host)
143: throws java.net.UnknownHostException {
144:
145: String localhostName = "localhost";
146: String loopbackIP = "127.0.0.1";
147:
148: String remoteHost = null;
149:
150: if (host != null) {
151: remoteHost = host.trim().toLowerCase();
152: }
153:
154: logDebug("Checking for Remote HOST " + remoteHost);
155:
156: if (remoteHost == null || remoteHost.length() == 0
157: || remoteHost.equalsIgnoreCase(localhostName)
158: || remoteHost.equalsIgnoreCase(loopbackIP)) {
159: return true;
160: }
161:
162: InetAddress localInetAddr = InetAddress.getLocalHost();
163: String localHostIP = localInetAddr.getHostAddress();
164:
165: // logDebug("Local Host IP " + localHostIP);
166:
167: InetAddress[] remoteInetAddrs = InetAddress
168: .getAllByName(remoteHost);
169: // logDebug("Remote InetAddress Size " + remoteInetAddrs.length);
170:
171: for (int i = 0; i < remoteInetAddrs.length; ++i) {
172: String remoteHostIP = remoteInetAddrs[i].getHostAddress();
173: // logDebug("Comapring with Remote Host IP : " + remoteHostIP);
174: if (localHostIP.equalsIgnoreCase(remoteHostIP)) {
175: return true;
176: }
177: }
178: return false;
179: }
180:
181: /**
182: * This method will return the return the host address information for the given
183: * host name in the format of <host name> / <host ip address>.
184: * @param host the name of the host (ip address string or name of host).
185: * @throws java.net.UnknownHostException on error.
186: * @return the host name in the format of <host name> / <host ip address>
187: */
188: public static String getHostName(String aHostName)
189: throws java.net.UnknownHostException {
190: InetAddress ia = null;
191: if ((aHostName.equalsIgnoreCase("localhost"))
192: || (aHostName.equals(""))) {
193: ia = InetAddress.getLocalHost();
194: } else {
195: ia = InetAddress.getByName(aHostName);
196: }
197: String hostName = ia.getHostName();
198: String hostAddress = ia.getHostAddress();
199: String returnStr = hostName + "/" + hostAddress;
200: return returnStr;
201: }
202:
203: }
|