1 11 package org.eclipse.ui.internal.components.framework; 12 13 import java.lang.ref.WeakReference ; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IConfigurationElement; 17 import org.eclipse.core.runtime.IExecutableExtension; 18 import org.eclipse.osgi.util.NLS; 19 import org.eclipse.ui.internal.components.ComponentMessages; 20 import org.eclipse.ui.internal.components.ComponentUtil; 21 22 public class SingletonFactory extends ComponentFactory implements IExecutableExtension { 23 24 private WeakReference singletonInstance; 25 26 private ClassIdentifier classId = null; 27 28 34 public SingletonFactory() { 35 } 36 37 42 public SingletonFactory(ClassIdentifier classId) { 43 this.classId = classId; 44 } 45 46 49 public void setInitializationData(IConfigurationElement config, 50 String propertyName, Object data) throws CoreException { 51 52 classId = ComponentUtil.getClassFromInitializationData(config, data); 53 } 54 55 public ComponentHandle createHandle(IServiceProvider availableServices) 56 throws ComponentException { 57 58 if (singletonInstance != null) { 59 Object result = singletonInstance.get(); 60 if (result != null) { 61 return new NonDisposingHandle(result); 62 } 63 singletonInstance = null; 64 } 65 66 Class clazz = ComponentUtil.loadClass(classId); 67 try { 68 Object ref = clazz.newInstance(); 69 singletonInstance = new WeakReference (ref); 70 return new NonDisposingHandle(ref); 71 } catch (InstantiationException e) { 72 throw new ComponentException(clazz, NLS.bind(ComponentMessages.Components_instantiationException, clazz.getName()), e); 73 } catch (Exception e) { 74 throw new ComponentException(clazz, e); 75 } 76 } 77 78 } 79 | Popular Tags |