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