KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > components > framework > SingletonFactory


1 /*******************************************************************************
2  * Copyright (c) 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.components.framework;
12
13 import java.lang.ref.WeakReference JavaDoc;
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 JavaDoc singletonInstance;
25     
26     private ClassIdentifier classId = null;
27     
28     /**
29      * Should only be called when this factory is created through an extension point.
30      * If this constructor is used, the caller MUST call setInitializationData before
31      * attempting to use the factory. Call one of the other constructors if instantiating
32      * this object programmatically.
33      */

34     public SingletonFactory() {
35     }
36    
37     /**
38      * Creates a factory that creates instances of the given class
39      *
40      * @param classId name of the class to instantiate
41      */

42     public SingletonFactory(ClassIdentifier classId) {
43         this.classId = classId;
44     }
45     
46     /* (non-Javadoc)
47      * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement, java.lang.String, java.lang.Object)
48      */

49     public void setInitializationData(IConfigurationElement config,
50             String JavaDoc propertyName, Object JavaDoc 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 JavaDoc result = singletonInstance.get();
60             if (result != null) {
61                 return new NonDisposingHandle(result);
62             }
63             singletonInstance = null;
64         }
65         
66         Class JavaDoc clazz = ComponentUtil.loadClass(classId);
67         try {
68             Object JavaDoc ref = clazz.newInstance();
69             singletonInstance = new WeakReference JavaDoc(ref);
70             return new NonDisposingHandle(ref);
71         } catch (InstantiationException JavaDoc e) {
72             throw new ComponentException(clazz, NLS.bind(ComponentMessages.Components_instantiationException, clazz.getName()), e);
73         } catch (Exception JavaDoc e) {
74             throw new ComponentException(clazz, e);
75         }
76     }
77
78 }
79
Popular Tags