Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: Riven (posted 2014-05-18 18:24:22, viewed 50 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215  
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.zip.*;

public class ClassLoaderDummy {

   public static void searchResource(String path) throws IOException {
      List<Root> roots = new ArrayList<>();
      for (URL url : getContext().getURLs()) {
         roots.add(new Root(url));
      }

      System.out.println(prefix() + "SEARCHING: \"" + path + "\"");

      for (int attempt = 1; attempt <= 6; attempt++)
         for (Root root : roots)
            if (root.search(path, attempt))
               return;

      System.out.println(prefix() + "FAILED: failed to find anything like");
      System.out.println(prefix() + "               \"" + path + "\"");
      System.out.println(prefix() + "         in all classpath entries.");
   }















   private static class Root {
      final File entry;
      final List<String> resources = new ArrayList<>();

      public Root(URL entry) throws IOException {
         this.entry = visitRoot(entry, resources);
      }

      public boolean search(String path, int attempt) {
         path = path.replace('\\', '/');

         // ---

         if (attempt == 1)
            for (String resource : resources) {
               if (path.equals(resource)) {
                  System.out.println(prefix() + "SUCCESS: found resource \"" + path + "\" in root: " + entry);
                  return true;
               }
            }

         if (attempt == 2)
            for (String resource : resources) {
               if (path.toLowerCase().equals(resource.toLowerCase())) {
                  System.out.println(prefix() + "FOUND: similarly named resource:");
                  System.out.println(prefix() + "               \"" + resource + "\"");
                  System.out.println(prefix() + "         in classpath entry:");
                  System.out.println(prefix() + "               \"" + entry + "\"");
                  return true;
               }
            }

         // ---

         if (attempt == 3)
            for (String resource : resources) {
               String r1 = path;
               String r2 = resource;
               if (r1.contains("/"))
                  r1 = r1.substring(r1.lastIndexOf('/') + 1);
               if (r2.contains("/"))
                  r2 = r2.substring(r2.lastIndexOf('/') + 1);

               if (r1.equals(r2)) {
                  System.out.println(prefix() + "FOUND: mislocated resource:");
                  System.out.println(prefix() + "               \"" + resource + "\"");
                  System.out.println(prefix() + "         in classpath entry:");
                  System.out.println(prefix() + "               \"" + entry + "\"");
                  return true;
               }
            }

         if (attempt == 4)
            for (String resource : resources) {
               String r1 = path.toLowerCase();
               String r2 = resource.toLowerCase();
               if (r1.contains("/"))
                  r1 = r1.substring(r1.lastIndexOf('/') + 1);
               if (r2.contains("/"))
                  r2 = r2.substring(r2.lastIndexOf('/') + 1);

               if (r1.equals(r2)) {
                  System.out.println(prefix() + "FOUND: mislocated, similarly named resource:");
                  System.out.println(prefix() + "               \"" + resource + "\"");
                  System.out.println(prefix() + "         in classpath entry:");
                  System.out.println(prefix() + "               \"" + entry + "\"");
                  return true;
               }
            }

         // ---

         if (attempt == 5)
            for (String resource : resources) {
               String r1 = path;
               String r2 = resource;
               if (r1.contains("/"))
                  r1 = r1.substring(r1.lastIndexOf('/') + 1);
               if (r2.contains("/"))
                  r2 = r2.substring(r2.lastIndexOf('/') + 1);
               if (r1.contains("."))
                  r1 = r1.substring(0, r1.lastIndexOf('.'));
               if (r2.contains("."))
                  r2 = r2.substring(0, r2.lastIndexOf('.'));

               if (r1.equals(r2)) {
                  System.out.println(prefix() + "FOUND: resource with different extension:");
                  System.out.println(prefix() + "               \"" + resource + "\"");
                  System.out.println(prefix() + "         in classpath entry:");
                  System.out.println(prefix() + "               \"" + entry + "\"");
                  return true;
               }
            }

         if (attempt == 6)
            for (String resource : resources) {
               String r1 = path.toLowerCase();
               String r2 = resource.toLowerCase();
               if (r1.contains("/"))
                  r1 = r1.substring(r1.lastIndexOf('/') + 1);
               if (r2.contains("/"))
                  r2 = r2.substring(r2.lastIndexOf('/') + 1);
               if (r1.contains("."))
                  r1 = r1.substring(0, r1.lastIndexOf('.'));
               if (r2.contains("."))
                  r2 = r2.substring(0, r2.lastIndexOf('.'));

               if (r1.equals(r2)) {
                  System.out.println(prefix() + "FOUND: similarly named resource with different extension:");
                  System.out.println(prefix() + "               \"" + resource + "\"");
                  System.out.println(prefix() + "         in classpath entry:");
                  System.out.println(prefix() + "               \"" + entry + "\"");
                  return true;
               }
            }

         return false;
      }
   }

   private static URLClassLoader getContext() {
      return (URLClassLoader) Thread.currentThread().getContextClassLoader();
   }

   private static File visitRoot(URL url, List<String> resources) throws IOException {
      if (!url.getProtocol().equals("file")) {
         throw new IllegalStateException();
      }
      String path = url.getPath();

      if (System.getProperty("os.name").toLowerCase().contains("win"))
         if (path.startsWith("/"))
            path = path.substring(1);

      File root = new File(path);
      if (!root.exists()) {
         System.err.println(prefix() + "failed to find classpath entry in filesystem: " + path);
         return null;
      }

      if (root.isDirectory()) {
         visitDir(root.getAbsolutePath().replace('\\', '/'), root, resources);
      } else if (root.getName().toLowerCase().endsWith(".zip")) {
         visitZip(root, resources);
      } else if (root.getName().toLowerCase().endsWith(".jar")) {
         visitZip(root, resources);
      } else {
         System.err.println(prefix() + "unknown classpath entry type: " + path);
         return null;
      }
      return root;
   }

   private static void visitDir(String root, File dir, Collection<String> out) {
      for (File file : dir.listFiles()) {
         if (file.isDirectory())
            visitDir(root, file, out);
         out.add(file.getAbsolutePath().replace('\\', '/').substring(root.length() + 1));
      }
   }

   private static void visitZip(File jar, Collection<String> out) throws IOException {
      ZipInputStream zis = new ZipInputStream(new FileInputStream(jar));
      while (true) {
         ZipEntry entry = zis.getNextEntry();
         if (entry == null)
            break;
         out.add(entry.getName().replace('\\', '/'));
      }
      zis.close();
   }

   private static String prefix() {
      return "[" + ClassLoaderDummy.class.getSimpleName() + "] ";
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

ctomni231 (34 views)
2014-07-18 06:55:21

Zero Volt (30 views)
2014-07-17 23:47:54

danieldean (25 views)
2014-07-17 23:41:23

MustardPeter (27 views)
2014-07-16 23:30:00

Cero (42 views)
2014-07-16 00:42:17

Riven (44 views)
2014-07-14 18:02:53

OpenGLShaders (32 views)
2014-07-14 16:23:47

Riven (33 views)
2014-07-14 11:51:35

quew8 (30 views)
2014-07-13 13:57:52

SHC (66 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!