EMPlugin: seperate plugin loader
authorjihye424.kim <jihye424.kim@samsung.com>
Wed, 27 May 2015 09:19:17 +0000 (18:19 +0900)
committerjihye424.kim <jihye424.kim@samsung.com>
Wed, 27 May 2015 09:19:17 +0000 (18:19 +0900)
- each EMPlugin class has loader
- One loader load only the one plugin file

Change-Id: Ic4b3385be2151ccd08932833a696cba30d93d0ee
Signed-off-by: jihye424.kim <jihye424.kim@samsung.com>
src/org/tizen/emulator/manager/platform/Platform.java
src/org/tizen/emulator/manager/plugin/EMPlugin.java
src/org/tizen/emulator/manager/plugin/EMPluginLoader.java
src/org/tizen/emulator/manager/plugin/ExtensionItem.java
src/org/tizen/emulator/manager/plugin/PluginImageResources.java
src/org/tizen/emulator/manager/ui/detail/PInfoViewPage.java
src/org/tizen/emulator/manager/ui/detail/PModifyViewPage.java
src/org/tizen/emulator/manager/ui/widgets/VMButton.java

index c7cbfae..fec8a77 100644 (file)
@@ -95,7 +95,7 @@ public class Platform {
                        String ver = split[1];
                        return "tizen" + "-" + ver + File.separator + profile;
                }
-               EMLogger.getLogger().warning("Failed to get platformpath from platform name : " + platformName);
+               EMLogger.getLogger().warning("Failed to get platform path from platform name : " + platformName);
                return "";
        }
 
@@ -103,7 +103,7 @@ public class Platform {
        public List<IOption> loadOptionList(ItemList itemTemplate) {
                if (plugin != null) {
                        ExtensionItem exItem = getPlugin().getExtensionItem(PluginStringResources.OptionFactory);
-                       IOptionFactory f = (IOptionFactory)(exItem.createClass());
+                       IOptionFactory f = (IOptionFactory)(exItem.createInstance());
                        if (f != null) {
                                return f.makeOptionList(itemTemplate);
                        }
@@ -116,7 +116,7 @@ public class Platform {
                if (customOptionList == null) {
                        if (plugin != null) {
                                ExtensionItem exItem = getPlugin().getExtensionItem(PluginStringResources.OptionFactory);
-                               IOptionFactory f = (IOptionFactory)(exItem.createClass());
+                               IOptionFactory f = (IOptionFactory)(exItem.createInstance());
                                if (f != null) {
                                        customOptionList =  f.makeOptionList(getCustomItemList());
                                }
@@ -212,10 +212,14 @@ public class Platform {
                }
 
                if (pluginJar != null && pluginJar.exists()) {
-                       plugin = EMPluginLoader.makePlugin(pluginJar);
-                       if (plugin != null) {
-                               plugin.setPlatform(this);
+                       plugin = EMPluginLoader.makePlugin(pluginJar, this);
+                       if (plugin == null) {
+                               EMLogger.getLogger().warning("Making plug-in is failed." +
+                                               StringResources.NEW_LINE + "Plug-in file: " + pluginJar.getAbsolutePath());
                        }
+               } else {
+                       EMLogger.getLogger().warning("Plug-in file does not exist." +
+                                       StringResources.NEW_LINE + "Plug-in file: " + pluginJar.getAbsolutePath());
                }
        }
 
index d7f1e05..f433a98 100644 (file)
@@ -30,6 +30,7 @@
 package org.tizen.emulator.manager.plugin;
 
 import java.io.IOException;
+import java.net.URLClassLoader;
 import java.util.ArrayList;
 import java.util.jar.Attributes;
 
@@ -37,22 +38,32 @@ import org.tizen.emulator.manager.logging.EMLogger;
 import org.tizen.emulator.manager.platform.Platform;
 
 public class EMPlugin {
-       private ArrayList<ExtensionItem> exItemList
-               = new ArrayList<ExtensionItem>();
-       private Platform platform;
        private String profile;
        private String version;
+       private URLClassLoader loader;
+       private ArrayList<ExtensionItem> exItemList
+               = new ArrayList<ExtensionItem>();
 
-       public EMPlugin(Attributes attr) throws IOException {
+       public EMPlugin(Attributes attr, URLClassLoader loader, Platform platform) throws IOException {
                profile = attr.getValue(PluginStringResources.PlatformProfile);
                version = attr.getValue(PluginStringResources.PlatformVersion);
+
                if (profile == null) {
-                       throw new IOException("Not found " + "Platform-profile"+ " attribute.");
+                       throw new IOException("Can not found " + PluginStringResources.PlatformProfile + " attribute.");
                }
                if (version == null) {
-                       throw new IOException("Not found " + "Platform-version"+ " attribute.");
+                       throw new IOException("Can not found " + PluginStringResources.PlatformVersion + " attribute.");
+               }
+
+               if (!platform.getProfile().equals(profile)) {
+                       throw new IOException("Profile of plug-in does not correspond with profile of platform.");
+               }
+               if (!platform.getVersion().equals(version)) {
+                       throw new IOException("Version of plug-in does not correspond with version of platform.");
                }
 
+               this.loader = loader;
+
                for (String ex : EMPluginLoader.getExtensionList()) {
                        try {
                                exItemList.add(new ExtensionItem(ex, attr, this));
@@ -61,6 +72,7 @@ public class EMPlugin {
                                                + " extension point.");
                        }
                }
+
        }
 
        public ArrayList<ExtensionItem> getExItemList() {
@@ -80,27 +92,15 @@ public class EMPlugin {
                return null;
        }
 
-       public Platform getPlatform() {
-               return platform;
-       }
-
-       public void setPlatform(Platform platform) {
-               this.platform = platform;
-       }
-
        public String getProfile() {
                return profile;
        }
 
-       public void setProfile(String profile) {
-               this.profile = profile;
-       }
-
        public String getVersion() {
                return version;
        }
 
-       public void setVersion(String version) {
-               this.version = version;
+       public URLClassLoader getLoader() {
+               return loader;
        }
 }
index 6eaa26e..b5401b2 100644 (file)
@@ -38,8 +38,8 @@ import java.util.ArrayList;
 import java.util.jar.Attributes;
 import java.util.jar.JarFile;
 
-import org.tizen.emulator.manager.EmulatorManager;
 import org.tizen.emulator.manager.logging.EMLogger;
+import org.tizen.emulator.manager.platform.Platform;
 import org.tizen.emulator.manager.resources.StringResources;
 
 public class EMPluginLoader {
@@ -54,25 +54,9 @@ public class EMPluginLoader {
                return exPonitList;
        }
 
-       private static URLClassLoader loader;
-       private static ArrayList<URL> urlList = new ArrayList<URL>();
-
-       public static void completeLoadPlugin() throws Exception {
-               URL[] urls = new URL[urlList.size()];
-               loader = URLClassLoader.newInstance(urlList.toArray(urls));
-               if (loader == null) {
-                       EMLogger.getLogger().warning("URLClassLoader is null");
-                       throw new Exception("Can not use plugins file..");
-               }
-               if (!EmulatorManager.isConsoleMode()) {
-                       ImageRegistry.setLoader(loader);
-               }
-       }
-
-       public static EMPlugin makePlugin(File plugin) {
+       private static URLClassLoader makeLoader(File plugin) {
                URL url = null;
-               EMPlugin emPlugin = null;
-               // add URL
+               URLClassLoader loader;
                try {
                        url = plugin.toURI().toURL();
                } catch (MalformedURLException e) {
@@ -80,9 +64,38 @@ public class EMPluginLoader {
                        return null;
                }
 
-               if (url != null) {
-                       urlList.add(url);
+               if (url == null) {
+                       EMLogger.getLogger().warning("Failed to load url from jar file." +
+                                       StringResources.NEW_LINE + plugin.getAbsolutePath());
+                       return null;
+               }
+
+               URL[] urls = new URL[1];
+               urls[0] = url;
+               loader = URLClassLoader.newInstance(urls);
+               if (loader == null) {
+                       EMLogger.getLogger().warning("Failed to create instance of URLClassLoader.");
+                       return null;
+               }
+
+               return loader;
+       }
+
+       private static Attributes makeAttribute(JarFile jarFile) {
+               Attributes attr = null;
+               try {
+                       if (jarFile.getManifest() != null) {
+                               attr = jarFile.getManifest().getMainAttributes();
+                       }
+               } catch (IOException e) {
+                       EMLogger.addStaticLog("Failed to load Manifest file: " + e.getMessage());
+                       return null;
                }
+               return attr;
+       }
+
+       public static EMPlugin makePlugin(File plugin, Platform platform) {
+               EMPlugin emPlugin = null;
 
                JarFile jarFile = null;
                if (plugin.isFile()) {
@@ -93,23 +106,24 @@ public class EMPluginLoader {
                        }
 
                        if (jarFile != null) {
-                               Attributes attr = null;
+                               Attributes attr = EMPluginLoader.makeAttribute(jarFile);
+                               if (attr == null) {
+                                       return null;
+                               }
+
+                               URLClassLoader loader = EMPluginLoader.makeLoader(plugin);
+                               if (loader == null) {
+                                       return null;
+                               }
+
                                try {
-                                       if (jarFile.getManifest() != null) {
-                                               attr = jarFile.getManifest().getMainAttributes();
-                                       }
+                                       emPlugin = new EMPlugin (attr, loader, platform);
                                } catch (IOException e) {
-                                       EMLogger.addStaticLog("Failed to load Manifest file: " + e.getMessage());
-                               }
-                               if (attr != null) {
-                                       try {
-                                               emPlugin = new EMPlugin (attr);
-                                       } catch (IOException e) {
-                                               EMLogger.getLogger().warning("Failed to load em-plugin"
-                                                               + StringResources.NEW_LINE + "Path: " + plugin.getAbsolutePath()
-                                                               + StringResources.NEW_LINE + "Error: " + e.getMessage());
-                                       }
+                                       EMLogger.getLogger().warning("Failed to load em-plugin"
+                                                       + StringResources.NEW_LINE + "Path: " + plugin.getAbsolutePath()
+                                                       + StringResources.NEW_LINE + "Error: " + e.getMessage());
                                }
+
                        }
                }
 
@@ -123,35 +137,4 @@ public class EMPluginLoader {
 
                return emPlugin;
        }
-
-       public static Object createClass(String pointClass) {
-               if (loader == null) {
-                       try {
-                               completeLoadPlugin();
-                       } catch (Exception e) {
-                               EMLogger.getLogger().warning(e.getMessage());
-                       }
-               }
-
-               @SuppressWarnings("rawtypes")
-               Class classToLoad = null;
-
-               try {
-                       classToLoad = Class.forName(pointClass, true, loader);
-               } catch (ClassNotFoundException e) {
-                       EMLogger.getLogger().warning("Failed to load point class jar file: " + e.getMessage());
-                       return null;
-               }
-
-               try {
-                       return classToLoad.newInstance();
-               } catch (InstantiationException e) {
-                       EMLogger.getLogger().warning("Failed to create new instance from class: " + e.getMessage());
-               } catch (IllegalAccessException e) {
-                       EMLogger.getLogger().warning("Failed to create new instance from class: " + e.getMessage());
-               }
-
-               return null;
-       }
-
 }
index 48fb844..f75cc8e 100644 (file)
@@ -32,10 +32,13 @@ package org.tizen.emulator.manager.plugin;
 import java.io.IOException;
 import java.util.jar.Attributes;
 
+import org.tizen.emulator.manager.logging.EMLogger;
+
 public class ExtensionItem {
        private String exPoint;
        private String pointClass;
        private EMPlugin plugin;
+       private Object instance;
 
        public ExtensionItem(String ex, Attributes attr, EMPlugin plugin) throws IOException {
                this.exPoint = ex;
@@ -50,27 +53,32 @@ public class ExtensionItem {
                return exPoint;
        }
 
-       public String getPointClass() {
-               return pointClass;
-       }
-
-       private Object clazz;
-       public Object createClass() {
-               if (clazz == null) {
-                       clazz = EMPluginLoader.createClass(pointClass);
+       public Object createInstance() {
+               if (instance == null) {
+                       instance = loadingClass();
                }
-               return clazz;
+               return instance;
        }
 
-       public Object createInstance() {
-               return EMPluginLoader.createClass(pointClass);
-       }
+       private Object loadingClass() {
+               @SuppressWarnings("rawtypes")
+               Class classToLoad = null;
 
-       public EMPlugin getPlugin() {
-               return plugin;
-       }
+               try {
+                       classToLoad = Class.forName(pointClass, true, plugin.getLoader());
+               } catch (ClassNotFoundException e) {
+                       EMLogger.getLogger().warning("Failed to load point class jar file: " + e.getMessage());
+                       return null;
+               }
 
-       public void setPlugin(EMPlugin plugin) {
-               this.plugin = plugin;
+               try {
+                       return classToLoad.newInstance();
+               } catch (InstantiationException e) {
+                       EMLogger.getLogger().warning("Failed to create new instance from class: " + e.getMessage());
+               } catch (IllegalAccessException e) {
+                       EMLogger.getLogger().warning("Failed to create new instance from class: " + e.getMessage());
+               }
+
+               return null;
        }
 }
index 562a43e..ea63c17 100644 (file)
@@ -40,6 +40,9 @@ import org.eclipse.swt.graphics.ImageData;
 import org.eclipse.swt.graphics.ImageLoader;
 import org.eclipse.swt.widgets.Display;
 import org.tizen.emulator.manager.logging.EMLogger;
+import org.tizen.emulator.manager.platform.Platform;
+import org.tizen.emulator.manager.platform.Profile;
+import org.tizen.emulator.manager.platform.ProfileList;
 import org.tizen.emulator.manager.resources.ImageResources;
 import org.tizen.emulator.manager.resources.ImageType;
 
@@ -143,8 +146,19 @@ class ImageRegistry {
                return getImage(name, platformName, "gif");
        }
 
+       private static URLClassLoader findLoader(String platformName) {
+               for (Profile p : ProfileList.getProfileList()) {
+                       for (Platform pl : p.getPlatformList()) {
+                               if (pl.getName().equals(platformName)) {
+                                       return pl.getPlugin().getLoader();
+                               }
+                       }
+               }
+               return null;
+       }
        public static Image getImage(String name, String platformName, String extension) {
                Image i = map.get(name+platformName);
+               loader = findLoader(platformName);
                if (i == null && loader != null) {
                        InputStream is = loader.getResourceAsStream("res/" + platformName + "_" + name + "." + extension);
                        if (is != null) {
@@ -167,6 +181,7 @@ class ImageRegistry {
 
        public static ImageData[] getImageData(String name, String platformName, String extension) {
                ImageData[] frames = null;
+               loader = findLoader(platformName);
                if (loader != null) {
                        InputStream is = loader.getResourceAsStream("res/" + platformName + "_" + name + "." + extension);
                        if (is != null) {
@@ -184,12 +199,4 @@ class ImageRegistry {
                }
                return frames;
        }
-
-       public static ClassLoader getLoader() {
-               return loader;
-       }
-
-       public static void setLoader(URLClassLoader loader) {
-               ImageRegistry.loader = loader;
-       }
 }
index 7a0f105..d6f1bce 100644 (file)
@@ -339,7 +339,7 @@ class InfoViewItemList extends ViewItemList{
        }
 
        private void makeItemList(Composite parent, ItemList templateList, boolean isCustom) {
-               IItemListFactory f = (IItemListFactory)(exItem.createClass());
+               IItemListFactory f = (IItemListFactory)(exItem.createInstance());
                itemList = new ArrayList<IInfoViewItem>();
                if (f != null) {
                        f.makeItemList(itemList, templateList, isCustom);
index 744452f..fa3bebe 100644 (file)
@@ -472,7 +472,7 @@ class ModifyViewItemList extends ViewItemList{
        }
 
        private void makeItemList(PModifyViewPage page, ItemList deviceItemList, boolean isCustom) {
-               IItemListFactory f = (IItemListFactory)(exItem.createClass());
+               IItemListFactory f = (IItemListFactory)(exItem.createInstance());
                itemList = new ArrayList<IModifyViewItem>();
 //             itemList = f.getModifyItemList(deviceItemList);
                if (f != null) {
index 5e56549..ac052f3 100644 (file)
@@ -97,9 +97,9 @@ public class VMButton extends ImageButton {
                }
 
                if (item != null) {
-                       this.setPainListener((PaintListener)item.createClass());
-                       launchButtonLeftOffset = ((IVMButtonPainter)item.createClass()).getLaunchButtonLeftOffset();
-                       launchButtonBottomOffset = ((IVMButtonPainter)item.createClass()).getLaunchButtonBottomOffset();
+                       this.setPainListener((PaintListener)item.createInstance());
+                       launchButtonLeftOffset = ((IVMButtonPainter)item.createInstance()).getLaunchButtonLeftOffset();
+                       launchButtonBottomOffset = ((IVMButtonPainter)item.createInstance()).getLaunchButtonBottomOffset();
                } else {
                        this.setPainListener(VMButtonPaintListener);
                        launchButtonLeftOffset = 13;