All Downloads are FREE. Search and download functionalities are using the official Maven repository.

brooklyn.entity.basic.BasicConfigurableEntityFactory Maven / Gradle / Ivy

There is a newer version: 0.7.0-M1
Show newest version
package brooklyn.entity.basic;

import static com.google.common.base.Preconditions.checkNotNull;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import brooklyn.entity.Entity;

import com.google.common.base.Objects;
import com.google.common.base.Throwables;

public class BasicConfigurableEntityFactory extends AbstractConfigurableEntityFactory {
    private transient Class clazz;
    private final String clazzName;

    public BasicConfigurableEntityFactory(Class clazz) {
        this(new HashMap(), clazz);
    }

    public BasicConfigurableEntityFactory(Map flags, Class clazz) {
        super(flags);
        this.clazz = checkNotNull(clazz, "clazz");
        this.clazzName = clazz.getName();
    }

    public T newEntity2(Map flags, Entity parent) {
        try {
            Constructor constructor = clazz.getConstructor(Map.class, Entity.class);
            return constructor.newInstance(flags, parent);
        } catch (InstantiationException e) {
            throw Throwables.propagate(e);
        } catch (IllegalAccessException e) {
            throw Throwables.propagate(e);
        } catch (InvocationTargetException e) {
            throw Throwables.propagate(e);
        } catch (NoSuchMethodException e) {
            throw Throwables.propagate(e);
        }
    }
    
    private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
        s.defaultReadObject();
        clazz = (Class) getClass().getClassLoader().loadClass(clazzName);
    }
    
    @Override
    public String toString() {
        return Objects.toStringHelper(this).add("type", clazzName).toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy