Skip to content

Commit

Permalink
Add ResourcesLoader to wrap loading resources from classpath, remove …
Browse files Browse the repository at this point in the history
…or deprecate direct usage of class loaders from the engine
  • Loading branch information
riccardobl committed Aug 9, 2023
1 parent 177f7d3 commit d5b3c4b
Show file tree
Hide file tree
Showing 21 changed files with 302 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import com.jme3.system.*;
import com.jme3.system.JmeContext.Type;
import com.jme3.util.AndroidScreenshots;
import com.jme3.util.functional.VoidFunction;
import com.jme3.util.res.ResourcesLoader;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -52,7 +52,7 @@ public JmeAndroidSystem(){

@Override
public URL getPlatformAssetConfigURL() {
return Thread.currentThread().getContextClassLoader().getResource("com/jme3/asset/Android.cfg");
return ResourcesLoader.getResource("com/jme3/asset/Android.cfg");
}

@Override
Expand Down
6 changes: 4 additions & 2 deletions jme3-core/src/main/java/com/jme3/app/LegacyApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import com.jme3.renderer.ViewPort;
import com.jme3.system.*;
import com.jme3.system.JmeContext.Type;
import com.jme3.util.res.ResourcesLoader;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -206,14 +208,14 @@ private void initAssetManager() {
} catch (MalformedURLException ex) {
}
if (assetCfgUrl == null) {
assetCfgUrl = LegacyApplication.class.getClassLoader().getResource(assetCfg);
assetCfgUrl = ResourcesLoader.getResource(assetCfg);
if (assetCfgUrl == null) {
logger.log(Level.SEVERE, "Unable to access AssetConfigURL in asset config:{0}", assetCfg);
return;
}
}
}
}
}
if (assetCfgUrl == null) {
assetCfgUrl = JmeSystem.getPlatformAssetConfigURL();
}
Expand Down
4 changes: 3 additions & 1 deletion jme3-core/src/main/java/com/jme3/asset/AssetConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import com.jme3.util.res.ResourcesLoader;

/**
* <code>AssetConfig</code> loads a config file to configure the asset manager.
*
Expand Down Expand Up @@ -101,7 +103,7 @@ public static void loadText(AssetManager assetManager, URL configUrl) throws IOE
}
} else if (cmd.equals("INCLUDE")) {
String includedCfg = scan.nextLine().trim();
URL includedCfgUrl = Thread.currentThread().getContextClassLoader().getResource(includedCfg);
URL includedCfgUrl = ResourcesLoader.getResource(includedCfg);
if (includedCfgUrl != null) {
loadText(assetManager, includedCfgUrl);
} else {
Expand Down
21 changes: 17 additions & 4 deletions jme3-core/src/main/java/com/jme3/asset/AssetManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.jme3.texture.plugins.TGALoader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;

Expand Down Expand Up @@ -86,31 +87,43 @@
* so that modifications to one instance do not leak onto others.
*/
public interface AssetManager {

/**
* Adds a {@link ClassLoader} that is used to load {@link Class classes}
* that are needed for finding and loading Assets.
* This does <strong>not</strong> allow loading assets from that classpath,
* use registerLocator for that.
*
* @param loader A ClassLoader that Classes in asset files can be loaded from.
* @deprecated use {@link com.jme3.util.res.ResourcesLoader}
*/
public void addClassLoader(ClassLoader loader);
@Deprecated
public default void addClassLoader(ClassLoader loader) {

}

/**
* Remove a {@link ClassLoader} from the list of registered ClassLoaders
*
* @param loader the ClassLoader to be removed
* @deprecated use {@link com.jme3.util.res.ResourcesLoader}
*/
public void removeClassLoader(ClassLoader loader);
@Deprecated
public default void removeClassLoader(ClassLoader loader) {

}

/**
* Retrieve the list of registered ClassLoaders that are used for loading
* {@link Class classes} from asset files.
*
* @return an unmodifiable list
* @deprecated use {@link com.jme3.util.res.ResourcesLoader}
*/
public List<ClassLoader> getClassLoaders();
@Deprecated
public default List<ClassLoader> getClassLoaders() {
return new ArrayList<>();
}

/**
* Register an {@link AssetLoader} by using a class object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public class DesktopAssetManager implements AssetManager {
final private CopyOnWriteArrayList<AssetEventListener> eventListeners =
new CopyOnWriteArrayList<>();

final private List<ClassLoader> classLoaders =
Collections.synchronizedList(new ArrayList<>());
@Deprecated
final private List<ClassLoader> classLoaders = Collections.synchronizedList(new ArrayList<>());

public DesktopAssetManager() {
this(null);
Expand All @@ -99,21 +99,23 @@ private void loadConfigFile(URL configFile) {
}
}

@Deprecated
@Override
public void addClassLoader(ClassLoader loader) {
classLoaders.add(loader);
}

@Deprecated
@Override
public void removeClassLoader(ClassLoader loader) {
classLoaders.remove(loader);
}

@Deprecated
@Override
public List<ClassLoader> getClassLoaders() {
return Collections.unmodifiableList(classLoaders);
}

@Override
public void addAssetEventListener(AssetEventListener listener) {
eventListeners.add(listener);
Expand Down
4 changes: 4 additions & 0 deletions jme3-core/src/main/java/com/jme3/export/SavableClassUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ public static Savable fromName(String className)
}
}

/**
* @deprecated use {@link #fromName(java.lang.String)} instead
*/
@Deprecated
public static Savable fromName(String className, List<ClassLoader> loaders) throws InstantiationException,
InvocationTargetException, NoSuchMethodException,
IllegalAccessException, ClassNotFoundException, IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.jme3.util.res.ResourcesLoader;


/**
* Provides compatibility mapping to different joysticks
Expand Down Expand Up @@ -554,9 +556,9 @@ public static void loadMappingProperties(URL u) throws IOException {
}
}

protected static void loadMappings(ClassLoader cl, String path) throws IOException {
protected static void loadMappings(String path) throws IOException {
logger.log(Level.FINE, "Searching for mappings for path:{0}", path);
for (Enumeration<URL> en = cl.getResources(path); en.hasMoreElements(); ) {
for (Enumeration<URL> en = ResourcesLoader.getResources(path); en.hasMoreElements(); ) {
URL u = en.nextElement();
try {
loadMappingProperties(u);
Expand All @@ -574,7 +576,7 @@ protected static void loadMappings(ClassLoader cl, String path) throws IOExcepti
protected static void loadDefaultMappings() {
for (String s : searchPaths) {
try {
loadMappings(JoystickCompatibilityMappings.class.getClassLoader(), s);
loadMappings(s);
} catch (IOException e) {
logger.log(Level.SEVERE, "Error searching resource path:{0}", s);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.jme3.asset.DesktopAssetManager;
import com.jme3.audio.AudioRenderer;
import com.jme3.input.SoftTextDialogInput;
import com.jme3.util.res.ResourcesLoader;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -126,11 +127,11 @@ public String getFullName() {
}

public InputStream getResourceAsStream(String name) {
return this.getClass().getResourceAsStream(name);
return ResourcesLoader.getResourceAsStream(name,this.getClass());
}

public URL getResource(String name) {
return this.getClass().getResource(name);
return ResourcesLoader.getResource(name,this.getClass());
}

public boolean trackDirectMemory() {
Expand Down
4 changes: 3 additions & 1 deletion jme3-core/src/main/java/com/jme3/system/JmeVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import com.jme3.util.res.ResourcesLoader;

/**
* Pulls in version info from the version.properties file.
*
Expand All @@ -48,7 +50,7 @@ public class JmeVersion {

static {
try {
props.load(JmeVersion.class.getResourceAsStream("version.properties"));
props.load(ResourcesLoader.getResourceAsStream("version.properties",JmeVersion.class));
} catch (IOException | NullPointerException ex) {
logger.log(Level.WARNING, "Unable to read version info!", ex);
}
Expand Down
101 changes: 101 additions & 0 deletions jme3-core/src/main/java/com/jme3/util/res/ResourcesLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (c) 2009-2023 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.util.res;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;



/**
* This class is used to load resources from the default location
* usually the classpath.
*/
public class ResourcesLoader {
public static final String PROPERTY_RESOURCES_LOADER_IMPLEMENTATION = "com.jme3.ResourcesLoaderImplementation";
private static final String DEFAULT_IMPL = "com.jme3.util.res.ResourcesLoaderJImpl";

private static final Logger LOGGER = Logger.getLogger(ResourcesLoader.class.getName());
private static ResourcesLoaderImpl impl = null;

private static ResourcesLoaderImpl getImpl() {
if (impl != null) return impl;
String className = System.getProperty(PROPERTY_RESOURCES_LOADER_IMPLEMENTATION, DEFAULT_IMPL);
try {
impl = (ResourcesLoaderImpl) Class.forName(className).getDeclaredConstructor().newInstance();
} catch (final Throwable e) {
LOGGER.log(Level.WARNING, "Unable to access {0}", className);
try {
impl = (ResourcesLoaderImpl) Class.forName(DEFAULT_IMPL).getDeclaredConstructor().newInstance();
} catch (final Throwable e1) {
throw new RuntimeException("Unable to access default resources loader implementation", e1);
}
}
return impl;
}

public static void setImpl(ResourcesLoaderImpl impl) {
ResourcesLoader.impl = impl;
}


public static URL getResource(String path) {
return getImpl().getResource(path, null);
}


public static URL getResource(String path, Class<?> parent) {
return getImpl().getResource(path, parent);
}

public static InputStream getResourceAsStream(String path) {
return getImpl().getResourceAsStream(path, null);
}

public static InputStream getResourceAsStream(String path,Class<?> clazz) {
return getImpl().getResourceAsStream(path,clazz);
}


public static Enumeration<URL> getResources(String path ) throws IOException {
return getImpl().getResources(path,null);
}


public static Enumeration<URL> getResources(String path,Class<?> clazz) throws IOException {
return getImpl().getResources(path,clazz);
}
}
43 changes: 43 additions & 0 deletions jme3-core/src/main/java/com/jme3/util/res/ResourcesLoaderImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2009-2023 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.util.res;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;

public interface ResourcesLoaderImpl {
public URL getResource(String path, Class<?> clazz);
public InputStream getResourceAsStream(String path, Class<?> clazz);
public Enumeration<URL> getResources(String path, Class<?> clazz) throws IOException;
}
Loading

0 comments on commit d5b3c4b

Please sign in to comment.