Coverage Report - org.jtheque.core.managers.module.loaders.SystemModuleLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
SystemModuleLoader
0 %
0/58
0 %
0/18
3.833
 
 1  
 package org.jtheque.core.managers.module.loaders;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jtheque.core.managers.Managers;
 20  
 import org.jtheque.core.managers.log.ILoggingManager;
 21  
 import org.jtheque.utils.io.FileUtils;
 22  
 
 23  
 import java.io.File;
 24  
 import java.io.IOException;
 25  
 import java.util.jar.JarFile;
 26  
 import java.util.jar.Manifest;
 27  
 
 28  
 /**
 29  
  * A module Loader. This class is responsible for the loading of all the modules of the application.
 30  
  *
 31  
  * @author Baptiste Wicht
 32  
  */
 33  
 public final class SystemModuleLoader implements IModuleLoader {
 34  0
     private static final IModuleLoader INSTANCE = new SystemModuleLoader();
 35  
 
 36  
     /**
 37  
      * Construct a new SystemModuleLoader. This constructor is private, all methods are static.
 38  
      */
 39  
     private SystemModuleLoader() {
 40  0
         super();
 41  0
     }
 42  
 
 43  
     /**
 44  
      * Return the loader.
 45  
      *
 46  
      * @return The loader.
 47  
      */
 48  
     public static IModuleLoader get() {
 49  0
         return INSTANCE;
 50  
     }
 51  
 
 52  
     @Override
 53  
     public Object installModule(File file) {
 54  0
         if (isValidModuleFile(file)) {
 55  0
             File modulesFolder = Managers.getCore().getFolders().getModulesFolder();
 56  
 
 57  0
             boolean inDirectory = FileUtils.putFileInDirectoryIfNot(file, modulesFolder);
 58  
 
 59  0
             if (inDirectory) {
 60  0
                 return loadModule(file);
 61  
             }
 62  
         }
 63  
 
 64  0
         return null;
 65  
     }
 66  
 
 67  
     /**
 68  
      * Indicate if the file contain a valid module.
 69  
      *
 70  
      * @param file The file to validate.
 71  
      * @return true if the file is valid else false.
 72  
      */
 73  
     private boolean isValidModuleFile(File file) {
 74  0
         boolean valid = true;
 75  
 
 76  0
         if (file.isFile()) {
 77  0
             JarFile jarFile = null;
 78  
             try {
 79  0
                 jarFile = new JarFile(file);
 80  
 
 81  0
                 Manifest manifest = jarFile.getManifest();
 82  
 
 83  0
                 if (manifest == null) {
 84  0
                     valid = false;
 85  
                 } else {
 86  0
                     String moduleClassName = manifest.getMainAttributes().getValue("ModuleContainer-Class");
 87  
 
 88  0
                     if (moduleClassName == null) {
 89  0
                         valid = false;
 90  
                     }
 91  
                 }
 92  0
             } catch (IOException e) {
 93  0
                 valid = false;
 94  
             } finally {
 95  0
                 if (jarFile != null) {
 96  
                     try {
 97  0
                         jarFile.close();
 98  0
                     } catch (IOException e) {
 99  0
                         Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e);
 100  0
                     }
 101  
                 }
 102  
             }
 103  0
         } else {
 104  0
             valid = false;
 105  
         }
 106  
 
 107  0
         return valid;
 108  
     }
 109  
 
 110  
     @Override
 111  
     public Object loadModule(File file) {
 112  0
         Object module = null;
 113  
 
 114  0
         JarFile jarFile = null;
 115  
         try {
 116  0
             jarFile = new JarFile(file);
 117  
 
 118  0
             Manifest manifest = jarFile.getManifest();
 119  
 
 120  0
             if (manifest != null) {
 121  0
                 String moduleClassName = manifest.getMainAttributes().getValue("Module-Context");
 122  
 
 123  0
                 if (moduleClassName != null) {
 124  0
                     ModuleLoader.addResource(file.toURI().toURL());
 125  
 
 126  0
                     module = loadModuleClass(moduleClassName);
 127  
                 }
 128  
             }
 129  0
         } catch (IOException e) {
 130  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e);
 131  
         } finally {
 132  0
             if (jarFile != null) {
 133  
                 try {
 134  0
                     jarFile.close();
 135  0
                 } catch (IOException e) {
 136  0
                     Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e);
 137  0
                 }
 138  
             }
 139  
         }
 140  
 
 141  0
         return module;
 142  
     }
 143  
 
 144  
     /**
 145  
      * Load a module class.
 146  
      *
 147  
      * @param moduleClassName The class name of the module.
 148  
      * @return The loaded module.
 149  
      */
 150  
     private Object loadModuleClass(String moduleClassName) {
 151  0
         Object module = null;
 152  
 
 153  
         try {
 154  0
             Class<?> moduleClass = Class.forName(moduleClassName, true, ClassLoader.getSystemClassLoader());
 155  
 
 156  0
             module = moduleClass.newInstance();
 157  0
         } catch (ClassNotFoundException e) {
 158  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e);
 159  0
         } catch (InstantiationException e) {
 160  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e);
 161  0
         } catch (IllegalAccessException e) {
 162  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e);
 163  0
         }
 164  
 
 165  0
         return module;
 166  
     }
 167  
 }