Skip to content

Commit

Permalink
Replaced all URLClassLoader constructor usages by GlassfishUrlClasslo…
Browse files Browse the repository at this point in the history
…ader

- Implements toString and may be extended later
- When I had issues with OSGI, the only information about some classloaders
  was just the class name and hash code. This should make it better.

Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Jan 8, 2023
1 parent 0fd71ce commit 587212e
Show file tree
Hide file tree
Showing 22 changed files with 444 additions and 691 deletions.
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -28,6 +28,7 @@
import java.util.Enumeration;

import org.glassfish.appclient.common.ClassPathUtils;
import org.glassfish.common.util.GlassfishUrlClassLoader;

/**
* Used as the system class loader during app client launch.
Expand All @@ -52,7 +53,7 @@ public ACCAgentClassLoader(ClassLoader parent) {


private static URLClassLoader prepareLoader(ClassLoader parent) {
PrivilegedAction<URLClassLoader> action = () -> new URLClassLoader(
PrivilegedAction<URLClassLoader> action = () -> new GlassfishUrlClassLoader(
new URL[] {ClassPathUtils.getGFClientJarURL()}, new ClassLoaderWrapper(parent));
return doPrivileged(action);
}
Expand Down
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,7 +17,18 @@

package org.glassfish.appclient.client;

import static org.glassfish.appclient.client.acc.CommandLaunchInfo.ClientLaunchType.UNKNOWN;
import com.sun.enterprise.container.common.spi.util.InjectionException;
import com.sun.enterprise.deployment.node.SaxParserHandlerBundled;
import com.sun.enterprise.universal.glassfish.TokenResolver;
import com.sun.enterprise.util.JDK;
import com.sun.enterprise.util.LocalStringManager;
import com.sun.enterprise.util.LocalStringManagerImpl;

import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;
import jakarta.xml.bind.ValidationEvent;
import jakarta.xml.bind.util.ValidationEventCollector;

import java.io.BufferedReader;
import java.io.CharArrayWriter;
Expand All @@ -34,7 +46,6 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -63,32 +74,21 @@
import org.glassfish.appclient.client.acc.config.MessageSecurityConfig;
import org.glassfish.appclient.client.acc.config.Property;
import org.glassfish.appclient.client.acc.config.TargetServer;
import org.glassfish.common.util.GlassfishUrlClassLoader;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import com.sun.enterprise.container.common.spi.util.InjectionException;
import com.sun.enterprise.deployment.node.SaxParserHandlerBundled;
import com.sun.enterprise.universal.glassfish.TokenResolver;
import com.sun.enterprise.util.JDK;
import com.sun.enterprise.util.LocalStringManager;
import com.sun.enterprise.util.LocalStringManagerImpl;

import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;
import jakarta.xml.bind.ValidationEvent;
import jakarta.xml.bind.util.ValidationEventCollector;
import static org.glassfish.appclient.client.acc.CommandLaunchInfo.ClientLaunchType.UNKNOWN;

/**
*
* @author tjquinn
*/
public class AppClientFacade {

private static final String ACC_CONFIG_CONTENT_PROPERTY_NAME = "glassfish-acc.xml.content";
private static final String MAN_PAGE_PATH = "/org/glassfish/appclient/client/acc/appclient.1m";
private static final String LINE_SEP = System.getProperty("line.separator");
private static final String LINE_SEP = System.lineSeparator();

private static final Class<?> stringsAnchor = ACCClassLoader.class;
private static LocalStringManager localStrings = new LocalStringManagerImpl(stringsAnchor);
Expand Down Expand Up @@ -249,9 +249,9 @@ private static void help() throws IOException {

private static String getUsage() {
return localStrings.getLocalString(stringsAnchor, "main.usage",
"appclient [ <classfile> | -client <appjar> ] [-mainclass <appClass-name>|-name <display-name>] [-xml <xml>] [-textauth] [-user <username>] [-password <password>|-passwordfile <password-file>] [-targetserver host[:port][,host[:port]...] [app-args]")
+ System.getProperty("line.separator") + localStrings.getLocalString(stringsAnchor, "main.usage.1",
" or :\n\tappclient [ <valid JVM options and valid ACC options> ] [ <appClass-name> | -jar <appjar> ] [app args]");
"appclient [ <classfile> | -client <appjar> ] [-mainclass <appClass-name>|-name <display-name>] [-xml <xml>] [-textauth] [-user <username>] [-password <password>|-passwordfile <password-file>] [-targetserver host[:port][,host[:port]...] [app-args]")
+ System.lineSeparator() + localStrings.getLocalString(stringsAnchor, "main.usage.1",
" or :\n\tappclient [ <valid JVM options and valid ACC options> ] [ <appClass-name> | -jar <appjar> ] [app args]");
}

private static ACCClassLoader initClassLoader(final boolean loaderShouldTransform) throws MalformedURLException {
Expand Down Expand Up @@ -383,7 +383,7 @@ private static AppClientContainer createContainerForClassName(Builder builder, S

private static ClassLoader prepareLoaderToFindClassFile(final ClassLoader currentLoader) throws MalformedURLException {
File currentDirPath = new File(System.getProperty("user.dir"));
ClassLoader newLoader = new URLClassLoader(new URL[] { currentDirPath.toURI().toURL() }, currentLoader);
ClassLoader newLoader = new GlassfishUrlClassLoader(new URL[] { currentDirPath.toURI().toURL() }, currentLoader);
return newLoader;
}

Expand Down Expand Up @@ -503,7 +503,7 @@ private static InputSource replaceTokensForParsing(final Reader reader) throws F
writer.close();
reader.close();

Map<String, String> mapping = new HashMap<String, String>();
Map<String, String> mapping = new HashMap<>();
Properties props = System.getProperties();
for (Enumeration e = props.propertyNames(); e.hasMoreElements();) {
String propName = (String) e.nextElement();
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022 Contributors to the Eclipse Foundation.
* Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation.
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -36,6 +36,7 @@

import org.glassfish.api.deployment.DeploymentContext;
import org.glassfish.api.naming.SimpleJndiName;
import org.glassfish.common.util.GlassfishUrlClassLoader;
import org.glassfish.deployment.common.RootDeploymentDescriptor;
import org.glassfish.persistence.jpa.ProviderContainerContractInfoBase;

Expand Down Expand Up @@ -79,14 +80,8 @@ public ClassLoader getClassLoader() {

@Override
public ClassLoader getTempClassloader() {
return AccessController.doPrivileged(new PrivilegedAction<URLClassLoader>() {

@Override
public URLClassLoader run() {
return new URLClassLoader(classLoader.getURLs());
}

});
PrivilegedAction<URLClassLoader> action = () -> new GlassfishUrlClassLoader(classLoader.getURLs());
return AccessController.doPrivileged(action);
}

@Override
Expand Down
Expand Up @@ -30,7 +30,7 @@
import org.glassfish.api.deployment.ApplicationContext;
import org.glassfish.api.deployment.DeploymentContext;
import org.glassfish.appclient.server.core.jws.JavaWebStartInfo;

import org.glassfish.common.util.GlassfishUrlClassLoader;
import org.jvnet.hk2.annotations.Service;
import org.glassfish.hk2.api.PerLookup;
import org.glassfish.hk2.api.ServiceLocator;
Expand Down Expand Up @@ -169,14 +169,8 @@ public ClassLoader getClassLoader() {
* This cannot be null or it prevents the framework from invoking unload
* on the deployer for this app.
*/
return AccessController.doPrivileged(new PrivilegedAction<URLClassLoader>() {

@Override
public URLClassLoader run() {
return new URLClassLoader(new URL[0]);
}

});
PrivilegedAction<URLClassLoader> action = () -> new GlassfishUrlClassLoader(new URL[0]);
return AccessController.doPrivileged(action);
}

public DeploymentContext dc() {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2009, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -442,43 +442,6 @@ private boolean isResType(Class cls, String resType) {
}


/**
* This method should be executed before driverLoaders are queries for
* classloader to load the classname.
* @param classname
* @return
*/
//TODO remove later
/*private boolean loadClass(File f, String classname, String resType) {
List<URL> urls = new ArrayList<URL>();
Class urlCls = null;
boolean isLoaded = false;
try {
urls.add(f.toURI().toURL());
} catch (MalformedURLException ex) {
}
ClassLoader loader = ConnectorRuntime.getRuntime().getConnectorClassLoader();
if (!urls.isEmpty()) {
ClassLoader urlClassLoader =
new URLClassLoader(urls.toArray(new URL[urls.size()]), loader);
try {
urlCls = urlClassLoader.loadClass(classname);
} catch (ClassNotFoundException ex) {
}
isLoaded = isResType(urlCls, resType);
if(isLoaded) {
//Loaded the class and verified it to be a jdbc driver implementing
//java.sql.Driver or implementing javax.sql.DataSource
//Register the url classloader for later use.
//For ojdbc14 n ojdbc5 (same class names different url class loaders
//will be added.
classLoaders.put(classname, urlClassLoader);
}
} else {
}
return isLoaded;
}*/

private String getClassName(String classname) {
classname = classname.replaceAll("/", ".");
classname = classname.substring(0, classname.lastIndexOf(".class"));
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -17,11 +17,18 @@

package com.sun.enterprise.connectors.util;

import com.sun.appserv.connectors.internal.api.ConnectorRuntimeException;
import com.sun.appserv.connectors.internal.api.ConnectorsUtil;
import com.sun.enterprise.connectors.ConnectorRuntime;
import com.sun.enterprise.deployment.EjbMessageBeanDescriptor;
import com.sun.enterprise.deployment.EnvironmentProperty;
import com.sun.enterprise.util.i18n.StringManager;
import com.sun.logging.LogDomains;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand All @@ -30,13 +37,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import com.sun.appserv.connectors.internal.api.ConnectorRuntimeException;
import com.sun.appserv.connectors.internal.api.ConnectorsUtil;
import com.sun.enterprise.connectors.ConnectorRuntime;
import com.sun.enterprise.deployment.EjbMessageBeanDescriptor;
import com.sun.enterprise.deployment.EnvironmentProperty;
import com.sun.enterprise.util.i18n.StringManager;
import com.sun.logging.LogDomains;
import org.glassfish.common.util.GlassfishUrlClassLoader;

/**
* This is a utility class to obtain the properties of a
Expand All @@ -51,87 +52,6 @@ public class RARUtils {
private final static Logger _logger = LogDomains.getLogger(RARUtils.class, LogDomains.RSR_LOGGER);
private static StringManager localStrings = StringManager.getManager(RARUtils.class);

/**
* Finds the properties of a RA JavaBean bundled in a RAR
* without exploding the RAR
*
* @param pathToDeployableUnit a physical,accessible location of the connector module.
* [either a RAR for RAR-based deployments or a directory for Directory based deployments]
* @return A Map that is of <String RAJavaBeanPropertyName, String defaultPropertyValue>
* An empty map is returned in the case of a 1.0 RAR
*/
/* TODO V3
public static Map getRABeanProperties (String pathToDeployableUnit) throws ConnectorRuntimeException {
File f = new File(pathToDeployableUnit);
if (!f.exists()){
String i18nMsg = I18N.getString(
"rar_archive_not_found", pathToDeployableUnit);
throw new ConnectorRuntimeException( i18nMsg );
}
if(f.isDirectory()) {
return getRABeanPropertiesForDirectoryBasedDeployment(pathToDeployableUnit);
} else {
return getRABeanPropertiesForRARBasedDeployment(pathToDeployableUnit);
}
}
*/

/*
private static Map getRABeanPropertiesForRARBasedDeployment(String rarLocation){
ConnectorRARClassLoader jarCL =
(new ConnectorRARClassLoader(rarLocation,
ApplicationServer.getServerContext().getCommonClassLoader()));
String raClassName = ConnectorDDTransformUtils.
getResourceAdapterClassName(rarLocation);
_logger.finer("RA class : " + raClassName);
Map hMap = new HashMap();
try {
hMap = extractRABeanProps(raClassName, jarCL);
} catch (ClassNotFoundException e) {
_logger.info(e.getMessage());
_logger.log(Level.FINE, "Error while trying to find class "
+ raClassName + "in RAR at " + rarLocation, e);
}
return hMap;
}
*/

/*
private static Map getRABeanPropertiesForDirectoryBasedDeployment(
String directoryLocation) {
Map hMap = new HashMap();
//Use the deployment APIs to get the name of the resourceadapter
//class through the connector descriptor
try {
ConnectorDescriptor cd = ConnectorDDTransformUtils.
getConnectorDescriptor(directoryLocation);
String raClassName = cd.getResourceAdapterClass();
File f = new File(directoryLocation);
URLClassLoader ucl = new URLClassLoader(new URL[]{f.toURI().toURL()},
ApplicationServer.getServerContext().getCommonClassLoader());
hMap = extractRABeanProps(raClassName, ucl);
} catch (IOException e) {
_logger.info(e.getMessage());
_logger.log(Level.FINE, "IO Error while trying to read connector" +
"descriptor to get resource-adapter properties", e);
} catch (ClassNotFoundException e) {
_logger.info(e.getMessage());
_logger.log(Level.FINE, "Unable to find class while trying to read connector" +
"descriptor to get resource-adapter properties", e);
} catch (ConnectorRuntimeException e) {
_logger.info(e.getMessage());
_logger.log(Level.FINE, "Error while trying to read connector" +
"descriptor to get resource-adapter properties", e);
} catch (Exception e) {
_logger.info(e.getMessage());
_logger.log(Level.FINE, "Error while trying to read connector" +
"descriptor to get resource-adapter properties", e);
}
return hMap;
}
*/

/**
* A valid resource adapter java bean property should either be one of the
* following
Expand Down Expand Up @@ -245,7 +165,6 @@ private static Class<?> loadClass(String pathToDeployableUnit, String beanClassN
* @throws ConnectorRuntimeException when unable to load the .rar
*/
private static ClassLoader getClassLoader(String file) throws ConnectorRuntimeException {
ClassLoader cl = null;
File f = new File(file);
validateRARLocation(f);
try {
Expand All @@ -255,21 +174,11 @@ private static ClassLoader getClassLoader(String file) throws ConnectorRuntimeEx
List<URL> urls = new ArrayList<>();
urls.add(f.toURI().toURL());
appendURLs(urls, f);
cl = new URLClassLoader(urls.toArray(new URL[urls.size()]), commonClassLoader);
} else {
cl = new ConnectorRARClassLoader(file, commonClassLoader);
return new GlassfishUrlClassLoader(urls.toArray(URL[]::new), commonClassLoader);
}
return cl;
return new ConnectorRARClassLoader(file, commonClassLoader);
} catch (IOException ioe) {
_logger.info(ioe.getMessage());
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "IO Error while trying to read connector "
+ "descriptor to get resource-adapter properties", ioe);
}
ConnectorRuntimeException cre = new ConnectorRuntimeException(
"unable to read connector descriptor from : " + file);
cre.setStackTrace(ioe.getStackTrace());
throw cre;
throw new ConnectorRuntimeException("unable to read connector descriptor from " + file, ioe);
}
}

Expand Down

0 comments on commit 587212e

Please sign in to comment.