
org.jinterop.dcom.common.JISystem Maven / Gradle / Ivy
/** j-Interop (Pure Java implementation of DCOM protocol)
* Copyright (C) 2006 Vikram Roopchand
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* Though a sincere effort has been made to deliver a professional,
* quality product,the library itself is distributed WITHOUT ANY WARRANTY;
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
*/
package org.jinterop.dcom.common;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
/**
* Class implemented for defining system wide changes.
*
* A note on logging: The framework exposes JRE based logger "org.jinterop".
* Applications need to attach their own handler to this logger.
*
* Note: Methods starting with internal_ keyword are internal to the framework and must not be called by the developer.
*
* @since 1.0
*/
public final class JISystem {
private JISystem() {
}
private static final Logger LOGGER = Logger.getLogger("org.jinterop");
private static final Properties mapOfProgIdsVsClsids = new Properties();
private static final List socketQueue = new ArrayList<>();
private static final Map mapOfHostnamesVsIPs = new HashMap<>();
private static String pathToDB = null;
private static Locale locale = Locale.getDefault();
private static ResourceBundle resourceBundle = null;
private static JIComVersion comVersion = new JIComVersion();
private static boolean autoRegister = false;
private static boolean autoCollection = true;
@Deprecated
public static Logger getLogger() {
return LOGGER;
}
/**
* Sets the COM version which the library would use for communicating with COM servers. Default is 5.2.
*
* @param comVersion new COM version
*/
public static void setCOMVersion(JIComVersion comVersion) {
JISystem.comVersion = comVersion;
}
/**
* Returns COM version currently being used by the library.
*
* @return
*/
public static JIComVersion getCOMVersion() {
return JISystem.comVersion;
}
/**
* Sets the locale, this locale will be used to retrieve the resource bundle
* for Error Messages.
*
* @param locale default is Locale.getDefault()
.
*/
public static void setLocale(Locale locale) {
JISystem.locale = locale;
}
/**
* Returns current locale associated with the library.
*
* @return
*/
public static Locale getLocale() {
return JISystem.locale;
}
/**
* Returns the ResourceBundle associated with current locale.
*
* @return
*/
public static ResourceBundle getErrorMessages() {
if (resourceBundle == null) {
synchronized (JISystem.class) {
try {
if (resourceBundle == null) {
resourceBundle = ResourceBundle.getBundle("org.jinterop.dcom.jierrormessages", locale);
}
} catch (MissingResourceException ex) {
//now use the parent US english bundle , which you already have
resourceBundle = ResourceBundle.getBundle("org.jinterop.dcom.jierrormessages");
}
}
}
return resourceBundle;
}
/**
* Returns the localized error messages for the error code.
*
* @param code error code
* @return
*/
public static String getLocalizedMessage(int code) {
String strKey = Integer.toHexString(code).toUpperCase();
char buffer[] = {'0', 'x', '0', '0', '0', '0', '0', '0', '0', '0'};
System.arraycopy(strKey.toCharArray(), 0, buffer, buffer.length - strKey.length(), strKey.length());
return getLocalizedMessage(String.valueOf(buffer));
}
private static String getLocalizedMessage(String key) {
String message;
try {
message = JISystem.getErrorMessages().getString(key);
message = message + " [" + key + "]";
} catch (MissingResourceException r) {
message = "Message not found for errorCode: " + key;
}
return message;
}
/**
* Queries the property file maintaining the PROGID
Vs
* CLSID
mappings and returns the CLSID
if found
* or null otherwise.
*
* @param progId user friendly string such as "Excel.Application".
* @return
*/
public static String getClsidFromProgId(String progId) {
if (progId == null) {
return null;
}
if (pathToDB == null) {
synchronized (JISystem.class) {
if (pathToDB == null) {
saveDBPathAndLoadFile();
}
}
}
return ((String) mapOfProgIdsVsClsids.get(progId));
}
private static void saveDBPathAndLoadFile() {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (loader == null) {
loader = JISystem.class.getClassLoader(); // fallback
}
Set locations = new HashSet<>();
if (loader != null) {
try {
Enumeration resources = loader.getResources("progIdVsClsidDB.properties");
while (resources.hasMoreElements()) {
locations.add(resources.nextElement());
break;
}
} catch (IOException ex) {
}
}
try {
if (locations.isEmpty()) {
Enumeration resources = ClassLoader.getSystemResources("progIdVsClsidDB.properties");
while (resources.hasMoreElements()) {
locations.add(resources.nextElement());
break;
}
}
} catch (IOException ex) {
}
Iterator iterator = locations.iterator();
while (iterator.hasNext()) {
try {
URL url = iterator.next();
pathToDB = url.getPath();
try {
if (!pathToDB.startsWith("file:")) {
url = new URL("file:" + pathToDB);
}
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.log(Level.INFO, "progIdVsClsidDB file located at: {0}", url);
}
URLConnection con = url.openConnection();
try (final InputStream inputStream = con.getInputStream()) {
mapOfProgIdsVsClsids.load(inputStream);
}
} catch (IOException e) {
}
//mapOfProgIdsVsClsids.load(new FileInputStream(pathToDB));
} catch (Exception ex) {
//ex.printStackTrace();
}
}
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.log(Level.INFO, "progIdVsClsidDB: {0}", mapOfProgIdsVsClsids);
}
}
//should be called from system shut down only
/**
* Should be called from system shut down only
*
* @exclude
*/
public static void internal_writeProgIdsToFile() {
if (pathToDB != null) {
try {
try (final FileOutputStream outputStream = new FileOutputStream(pathToDB)) {
mapOfProgIdsVsClsids.store(outputStream, "progId Vs ClsidDB");
}
} catch (FileNotFoundException e) {
LOGGER.throwing("JISystem", "writeProgIdsToFile", e);
} catch (IOException e) {
LOGGER.throwing("JISystem", "writeProgIdsToFile", e);
}
}
}
/**
* Stores it in a temporary hash map here, and this is later persisted when the library is shutdown
*
* @param progId
* @param clsid
*/
public static void internal_setClsidtoProgId(String progId, String clsid) {
mapOfProgIdsVsClsids.put(progId, clsid);
}
/**
* Synchronization will be performed by the oxid master
*
* @return
*/
public static Socket internal_getSocket() {
//synchronized (socketQueue)
{
return socketQueue.remove(0);
}
}
/**
* Synchronization will be performed by the oxid master
*
* @param socket
*/
public static void internal_setSocket(Socket socket) {
//synchronized (socketQueue)
{
socketQueue.add(socket);
}
}
public static synchronized void internal_initLogger() {
logSystemPropertiesAndVersion();
}
private static void logSystemPropertiesAndVersion() {
Properties pr = System.getProperties();
Iterator