Merge pull request #64 in ITERATE/cyberduck from feature/windows/9074 to master
[cyberduck.git] / source / ch / cyberduck / core / DeserializerFactory.java
blob47036cd19f9e9bce103d8f9a06d29549d710ce03
1 package ch.cyberduck.core;
3 /*
4 * Copyright (c) 2009 David Kocher. All rights reserved.
5 * http://cyberduck.ch/
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;
30 /**
31 * @version $Id$
33 public class DeserializerFactory<T> extends Factory<Deserializer> {
35 private static final Preferences preferences
36 = PreferencesFactory.get();
38 private String clazz;
40 public DeserializerFactory() {
41 this.clazz = preferences.getProperty("factory.deserializer.class");
44 /**
45 * @param clazz Implementation class name
47 public DeserializerFactory(final String clazz) {
48 this.clazz = clazz;
51 public Deserializer create(final T dict) {
52 if(null == clazz) {
53 throw new FactoryException(String.format("No implementation given for factory %s", this.getClass().getSimpleName()));
55 try {
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);