]> git.argeo.org Git - lgpl/argeo-commons.git/blob - i18n/ImplementationLoader.java
Prepare next development cycle
[lgpl/argeo-commons.git] / i18n / ImplementationLoader.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.demo.i18n;
17
18 import java.text.MessageFormat;
19
20 /**
21 * This class enable single sourcing between RAP and RCP. For this to run
22 * correctly, following conventions must be respected:
23 * <ul>
24 * <li>Given the fact that a common interface named Xxx is defined in package
25 * aa.bb.cc, corresponding implementation named XxxImpl must be found in package
26 * aa.bb.cc.specific of both RAP and RCP UI bundles.
27 *
28 * thanks to {@link http
29 * ://eclipsesource.com/en/info/rcp-rap-single-sourcing-guideline/}, chapter 7
30 */
31
32 public class ImplementationLoader {
33 // private final static Log log = LogFactory
34 // .getLog(ImplementationLoader.class);
35
36 public static Object newInstance(
37 @SuppressWarnings("rawtypes") final Class type) {
38 String name = type.getName();
39 // manually construct the implementation name for the given interface,
40 // assuming that convention have been respected.
41 String cName = type.getCanonicalName();
42 String pName = cName.substring(0, cName.lastIndexOf('.') + 1);
43 String sName = cName.substring(cName.lastIndexOf('.') + 1);
44 String implName = pName + "specific." + sName + "Impl";
45 // String implName = cName + "Impl";
46 Object result = null;
47 try {
48 result = type.getClassLoader().loadClass(implName).newInstance();
49 } catch (Throwable throwable) {
50 String txt = "Could not load implementation for {0}";
51 String msg = MessageFormat.format(txt, new Object[] { name });
52 throw new RuntimeException(msg, throwable);
53 }
54 return result;
55 }
56 }