1 package ch
.cyberduck
.core
;
4 * Copyright (c) 2009 David Kocher. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * Bug fixes, suggestions and comments should be sent to:
18 * dkocher@cyberduck.ch
21 import ch
.cyberduck
.core
.preferences
.Preferences
;
22 import ch
.cyberduck
.core
.preferences
.PreferencesFactory
;
23 import ch
.cyberduck
.core
.serializer
.Deserializer
;
25 import org
.apache
.commons
.lang3
.reflect
.ConstructorUtils
;
27 import java
.lang
.reflect
.Constructor
;
28 import java
.lang
.reflect
.InvocationTargetException
;
33 public class DeserializerFactory
<T
> extends Factory
<Deserializer
> {
35 private static final Preferences preferences
36 = PreferencesFactory
.get();
40 public DeserializerFactory() {
41 this.clazz
= preferences
.getProperty("factory.deserializer.class");
45 * @param clazz Implementation class name
47 public DeserializerFactory(final String clazz
) {
51 public Deserializer
create(final T dict
) {
53 throw new FactoryException(String
.format("No implementation given for factory %s", this.getClass().getSimpleName()));
56 final Class
<Deserializer
> name
= (Class
<Deserializer
>) Class
.forName(clazz
);
57 final Constructor
<Deserializer
> constructor
= ConstructorUtils
.getMatchingAccessibleConstructor(name
, dict
.getClass());
58 return constructor
.newInstance(dict
);
60 catch(InstantiationException
| IllegalAccessException
| InvocationTargetException
| ClassNotFoundException e
) {
61 throw new FactoryException(e
.getMessage(), e
);
65 public static <T
> Deserializer
get(final T dict
) {
66 return new DeserializerFactory
<T
>().create(dict
);