From: jihye424.kim Date: Wed, 27 May 2015 09:19:17 +0000 (+0900) Subject: EMPlugin: seperate plugin loader X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=533414a834e01df7a925b55e16061ffc9165d415;p=sdk%2Femulator%2Femulator-manager.git EMPlugin: seperate plugin loader - each EMPlugin class has loader - One loader load only the one plugin file Change-Id: Ic4b3385be2151ccd08932833a696cba30d93d0ee Signed-off-by: jihye424.kim --- diff --git a/src/org/tizen/emulator/manager/platform/Platform.java b/src/org/tizen/emulator/manager/platform/Platform.java index c7cbfae..fec8a77 100644 --- a/src/org/tizen/emulator/manager/platform/Platform.java +++ b/src/org/tizen/emulator/manager/platform/Platform.java @@ -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 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()); } } diff --git a/src/org/tizen/emulator/manager/plugin/EMPlugin.java b/src/org/tizen/emulator/manager/plugin/EMPlugin.java index d7f1e05..f433a98 100644 --- a/src/org/tizen/emulator/manager/plugin/EMPlugin.java +++ b/src/org/tizen/emulator/manager/plugin/EMPlugin.java @@ -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 exItemList - = new ArrayList(); - private Platform platform; private String profile; private String version; + private URLClassLoader loader; + private ArrayList exItemList + = new ArrayList(); - 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 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; } } diff --git a/src/org/tizen/emulator/manager/plugin/EMPluginLoader.java b/src/org/tizen/emulator/manager/plugin/EMPluginLoader.java index 6eaa26e..b5401b2 100644 --- a/src/org/tizen/emulator/manager/plugin/EMPluginLoader.java +++ b/src/org/tizen/emulator/manager/plugin/EMPluginLoader.java @@ -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 urlList = new ArrayList(); - - 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; - } - } diff --git a/src/org/tizen/emulator/manager/plugin/ExtensionItem.java b/src/org/tizen/emulator/manager/plugin/ExtensionItem.java index 48fb844..f75cc8e 100644 --- a/src/org/tizen/emulator/manager/plugin/ExtensionItem.java +++ b/src/org/tizen/emulator/manager/plugin/ExtensionItem.java @@ -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; } } diff --git a/src/org/tizen/emulator/manager/plugin/PluginImageResources.java b/src/org/tizen/emulator/manager/plugin/PluginImageResources.java index 562a43e..ea63c17 100644 --- a/src/org/tizen/emulator/manager/plugin/PluginImageResources.java +++ b/src/org/tizen/emulator/manager/plugin/PluginImageResources.java @@ -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; - } } diff --git a/src/org/tizen/emulator/manager/ui/detail/PInfoViewPage.java b/src/org/tizen/emulator/manager/ui/detail/PInfoViewPage.java index 7a0f105..d6f1bce 100644 --- a/src/org/tizen/emulator/manager/ui/detail/PInfoViewPage.java +++ b/src/org/tizen/emulator/manager/ui/detail/PInfoViewPage.java @@ -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(); if (f != null) { f.makeItemList(itemList, templateList, isCustom); diff --git a/src/org/tizen/emulator/manager/ui/detail/PModifyViewPage.java b/src/org/tizen/emulator/manager/ui/detail/PModifyViewPage.java index 744452f..fa3bebe 100644 --- a/src/org/tizen/emulator/manager/ui/detail/PModifyViewPage.java +++ b/src/org/tizen/emulator/manager/ui/detail/PModifyViewPage.java @@ -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(); // itemList = f.getModifyItemList(deviceItemList); if (f != null) { diff --git a/src/org/tizen/emulator/manager/ui/widgets/VMButton.java b/src/org/tizen/emulator/manager/ui/widgets/VMButton.java index 5e56549..ac052f3 100644 --- a/src/org/tizen/emulator/manager/ui/widgets/VMButton.java +++ b/src/org/tizen/emulator/manager/ui/widgets/VMButton.java @@ -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;