Package ca.simplegames.micro

Examples of ca.simplegames.micro.Extension


      }
    }
  }

  public ExtensionsManager require(String name) throws Exception { //todo: improve the Exceptions
    Extension extension = null;
    Map<String, Object> yaml = extensionsConfigMap.get(name);
    File extensionLibDir;

    if (yaml != null && !registeredExtensions.contains(name)) {
      final String extensionLibFolderName = name + "/lib";

      if (extensionsFolder == null) {
        extensionLibDir = new File(site.getApplicationConfigPath(), "/extensions/" + extensionLibFolderName);
      } else {
        extensionLibDir = new File(extensionsFolder, extensionLibFolderName);
      }

      Class[] parameters = new Class[]{URL.class};
      URLClassLoader microClassLoader = (URLClassLoader) Micro.class.getClassLoader();
      Class sysclass = URLClassLoader.class;

      if (extensionLibDir.exists() && extensionLibDir.isDirectory()) {
        for (File file : site.files(extensionLibDir, ".jar")) {
          try {
            Method method = sysclass.getDeclaredMethod("addURL", parameters);
            method.setAccessible(true);
            method.invoke(microClassLoader, file.toURI().toURL());
          } catch (Throwable t) {
            t.printStackTrace();
            throw new IOException("Error, could not add URL to system classloader");
          }
        }

        try {
          Class classToLoad = Class.forName((String) yaml.get("class"), true, microClassLoader);
          extension = (Extension) classToLoad.newInstance();
        } catch (ClassNotFoundException e) {
          e.printStackTrace();
          throw new Exception(String.format("Class: %s, not found.", yaml.get("class")));
        }
      }

      if (extension == null) {
        // check if the Extension is using a Micro class...
        final Class klass = ClassUtilities.loadClass((String) yaml.get("class"));
        if (klass != null) {
          extension = (Extension) klass.newInstance();
        }
      }

      if (extension != null) {
        extension.register(name, site, yaml);

        if (registeredExtensions.isEmpty()) { //cosmetics
          site.getLog().info("Extensions:");
        }
        registeredExtensions.add(extension.getName());
        site.getLog().info(String.format(" - %s, loaded.", extension.getName()));
      } else {
        site.getLog().error(String.format("  %s, not loaded.", name));
      }
    }
    return this;
View Full Code Here



  public void shutdown() {
    if (!CollectionUtils.isEmpty(registeredExtensions)) {
      for (String extensionName : registeredExtensions) {
        Extension extension = (Extension) site.get(extensionName);
        if (extension != null) {
          extension.shutdown();
        }
      }
    }
  }
View Full Code Here

TOP

Related Classes of ca.simplegames.micro.Extension

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.