summaryrefslogtreecommitdiffstats
blob: f6770a8968ef0fc8411a182e3550270af03d9d82 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
 * <copyright>
 *
 * Copyright (c) 2011-2012 Springsite BV (The Netherlands) and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Martin Taal - Initial API and implementation
 *
 * </copyright>
 *
 * $Id: EntityManagerProvider.java,v 1.7 2011/09/26 19:48:10 mtaal Exp $
 */
package org.eclipse.emf.texo.component;

import java.util.HashMap;
import java.util.Map;

/**
 * Central factory for internal components used by Texo. The registry in this class can be used to override the
 * implementation classes used by Texo internally. Initially the registry is empty, meaning that the requested
 * implementation class is used. User specific code can set the implementation class by calling
 * {@link #register(Class, Class)}.
 * 
 * @author <a href="mtaal@elver.org">Martin Taal</a>
 */
public class ComponentProvider implements TexoStaticSingleton {

  private static ComponentProvider instance = new ComponentProvider();

  public static ComponentProvider getInstance() {
    return instance;
  }

  public static void setInstance(ComponentProvider instance) {
    ComponentProvider.instance = instance;
  }

  private Map<Class<?>, Class<?>> registry = new HashMap<Class<?>, Class<?>>();

  /**
   * Create a new instance of the requested class using the internal registry.
   */
  public <T extends Object> T newInstance(Class<T> clz) {
    return newInstance(clz, null, null);
  }

  /**
   * Create a new instance of the requested class using the internal registry.
   */
  @SuppressWarnings("unchecked")
  public <T extends Object> T newInstance(Class<T> clz, Class<?>[] argTypes, Object[] args) {
    Class<?> implementationClass = getImplementation(clz);
    if (implementationClass == null) {
      implementationClass = clz;
    }
    try {
      if (args == null) {
        return (T) implementationClass.newInstance();
      }
      return (T) implementationClass.getConstructor(argTypes).newInstance(args);
    } catch (Exception e) {
      throw new IllegalArgumentException(e);
    }
  }

  /**
   * Register the implementation class for a certain texo class. Note, if there is already an entry in the registry for
   * the texoClass, then it will be overwritten.
   */
  public void register(Class<?> texoClass, Class<?> implementationClass) {
    // TODO: handle the case that there is already an entry...
    registry.put(texoClass, implementationClass);
  }

  /**
   * Return the current registered implementation.
   */
  @SuppressWarnings("unchecked")
  public <T extends Object> Class<T> getImplementation(Class<T> clz) {
    return (Class<T>) registry.get(clz);
  }
}