]> git.argeo.org Git - lgpl/argeo-commons.git/blob - demo/plugins/org.argeo.demo.i18n/src/main/java/org/argeo/demo/i18n/ImplementationLoader.java
Improve CSV and tabular
[lgpl/argeo-commons.git] / demo / plugins / org.argeo.demo.i18n / src / main / java / org / argeo / demo / i18n / ImplementationLoader.java
1 package org.argeo.demo.i18n;
2
3 import java.text.MessageFormat;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7
8 /**
9 * This class enable single sourcing between RAP and RCP. For this to run
10 * correctly, following conventions must be respected:
11 * <ul>
12 * <li>Given the fact that a common interface named Xxx is defined in package
13 * aa.bb.cc, corresponding implementation named XxxImpl must be found in package
14 * aa.bb.cc.specific of both RAP and RCP UI bundles.
15 *
16 * thanks to {@link http
17 * ://eclipsesource.com/en/info/rcp-rap-single-sourcing-guideline/}, chapter 7
18 */
19
20 public class ImplementationLoader {
21 private final static Log log = LogFactory
22 .getLog(ImplementationLoader.class);
23
24 public static Object newInstance(
25 @SuppressWarnings("rawtypes") final Class type) {
26 String name = type.getName();
27 // manually construct the implementation name for the given interface,
28 // assuming that convention have been respected.
29 String cName = type.getCanonicalName();
30 String pName = cName.substring(0, cName.lastIndexOf('.') + 1);
31 String sName = cName.substring(cName.lastIndexOf('.') + 1);
32 String implName = pName + "specific." + sName + "Impl";
33 // String implName = cName + "Impl";
34 Object result = null;
35 try {
36 result = type.getClassLoader().loadClass(implName).newInstance();
37 } catch (Throwable throwable) {
38 String txt = "Could not load implementation for {0}";
39 String msg = MessageFormat.format(txt, new Object[] { name });
40 throw new RuntimeException(msg, throwable);
41 }
42 return result;
43 }
44 }