OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 80 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 80 Rev 156
Line 12... Line 12...
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.utils.i18n;
14
 package org.openconcerto.utils.i18n;
15
 
15
 
16
import org.openconcerto.utils.Log;
16
import org.openconcerto.utils.Log;
-
 
17
import org.openconcerto.utils.ReflectUtils;
17
import org.openconcerto.utils.Tuple2;
18
import org.openconcerto.utils.Tuple2;
18
import org.openconcerto.utils.Value;
19
import org.openconcerto.utils.Value;
19
 
20
 
20
import java.io.IOException;
21
import java.io.IOException;
21
import java.lang.reflect.Constructor;
22
import java.lang.reflect.Constructor;
Line 86... Line 87...
86
        final List<T> l = new ArrayList<T>();
87
        final List<T> l = new ArrayList<T>();
87
        Locale localeRes = null;
88
        Locale localeRes = null;
88
        // test emptiness to not mix languages
89
        // test emptiness to not mix languages
89
        for (Locale targetLocale = locale; targetLocale != null && l.isEmpty(); targetLocale = this.cntrl.getFallbackLocale(baseName, targetLocale)) {
90
        for (Locale targetLocale = locale; targetLocale != null && l.isEmpty(); targetLocale = this.cntrl.getFallbackLocale(baseName, targetLocale)) {
90
            localeRes = targetLocale;
91
            localeRes = targetLocale;
-
 
92
            // e.g. "fr_FR", "fr"
91
            for (final Locale candidate : this.cntrl.getCandidateLocales(baseName, targetLocale)) {
93
            for (final Locale candidate : this.cntrl.getCandidateLocales(baseName, targetLocale)) {
-
 
94
                // e.g. org.acme.MyClass_fr
92
                final String bundleName = this.cntrl.toBundleName(baseName, candidate);
95
                final String bundleName = this.cntrl.toBundleName(baseName, candidate);
93
 
96
 
94
                // code first
97
                // code first
95
                final Class<?> loadedClass = loadClass(bundleName, cl);
98
                final Class<? extends T> subclazz = ReflectUtils.getSubclass(bundleName, this.clazz, cl.getClassLoader());
96
                if (loadedClass != null && this.clazz.isAssignableFrom(loadedClass)) {
99
                if (subclazz != null) {
97
                    final Class<? extends T> subclazz = loadedClass.asSubclass(this.clazz);
-
 
98
                    try {
100
                    try {
99
                        final Value<? extends T> instance = this.getInstance(subclazz);
101
                        final Value<? extends T> instance = this.getInstance(subclazz);
100
                        if (instance.hasValue())
102
                        if (instance.hasValue())
101
                            l.add(instance.getValue());
103
                            l.add(instance.getValue());
102
                        else
104
                        else
103
                            Log.get().warning(loadedClass + " exists but the constructor wasn't found");
105
                            Log.get().warning(subclazz + " exists but the constructor wasn't found");
104
                    } catch (Exception e) {
106
                    } catch (Exception e) {
105
                        Log.get().log(Level.WARNING, "Couldn't create an instance using " + subclazz, e);
107
                        Log.get().log(Level.WARNING, "Couldn't create an instance using " + subclazz, e);
106
                    }
108
                    }
107
                }
109
                }
108
 
110
 
Line 116... Line 118...
116
            }
118
            }
117
        }
119
        }
118
        return Tuple2.create(localeRes, l);
120
        return Tuple2.create(localeRes, l);
119
    }
121
    }
120
 
122
 
121
    private final Class<?> loadClass(final String name, final Class<?> cl) {
-
 
122
        try {
-
 
123
            return Class.forName(name, true, cl.getClassLoader());
-
 
124
        } catch (ClassNotFoundException e) {
-
 
125
            return null;
-
 
126
        }
-
 
127
    }
-
 
128
 
-
 
129
    /**
123
    /**
130
     * The no-arg static method to use.
124
     * The no-arg static method to use.
131
     * 
125
     * 
132
     * @param staticMethodName a static method name, <code>null</code> meaning the constructor.
126
     * @param staticMethodName a static method name, <code>null</code> meaning the constructor.
133
     * @return this.
127
     * @return this.