/** * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies * * Please see distribution for license. */ package com.opengamma.component.factory.master; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import org.fudgemsg.AnnotationReflector; import org.joda.beans.BeanBuilder; import org.joda.beans.BeanDefinition; import org.joda.beans.JodaBeanUtils; import org.joda.beans.MetaBean; import org.joda.beans.MetaProperty; import org.joda.beans.impl.direct.DirectBeanBuilder; import org.joda.beans.impl.direct.DirectMetaPropertyMap; import org.joda.beans.ser.DefaultDeserializer; import org.joda.beans.ser.SerDeserializer; import org.joda.beans.ser.SerDeserializers; import org.reflections.Reflections; import com.opengamma.component.ComponentRepository; import com.opengamma.master.security.ManageableSecurity; import com.opengamma.master.security.SecurityMaster; import com.opengamma.master.security.impl.DataSecurityMasterResource; import com.opengamma.master.security.impl.DataTrackingSecurityMaster; import com.opengamma.master.security.impl.PermissionedSecurityMaster; import com.opengamma.master.security.impl.RemoteSecurityMaster; import com.opengamma.masterdb.security.DbSecurityBeanMaster; import com.opengamma.util.ClassUtils; import com.opengamma.util.metric.OpenGammaMetricRegistry; import com.opengamma.util.rest.AbstractDataResource; /** * Component factory for the bean-based database security master. */ @BeanDefinition public class DbSecurityBeanMasterComponentFactory extends AbstractDocumentDbMasterComponentFactory<SecurityMaster, DbSecurityBeanMaster> { /** * Joda-Bean deserializer to handle renamed property. */ private static final SerDeserializer DESERIALIZER = new DefaultDeserializer() { @Override public MetaProperty<?> findMetaProperty(Class<?> beanType, MetaBean metaBean, String propertyName) { if ("permissions".equals(propertyName)) { return metaBean.metaProperty("requiredPermissions"); } return super.findMetaProperty(beanType, metaBean, propertyName); } }; static { Reflections reflections = AnnotationReflector.getDefaultReflector().getReflector(); Set<String> subTypes = reflections.getStore().getSubTypesOf(ManageableSecurity.class.getName()); for (final String subType : subTypes) { Class<?> cls = ClassUtils.loadClassRuntime(subType, ManageableSecurity.class); SerDeserializers.INSTANCE.register(cls, DESERIALIZER); } SerDeserializers.INSTANCE.register(ManageableSecurity.class, DESERIALIZER); } //------------------------------------------------------------------------- /** * Creates an instance. */ public DbSecurityBeanMasterComponentFactory() { super("secb", SecurityMaster.class, RemoteSecurityMaster.class); } //------------------------------------------------------------------------- @Override public void init(ComponentRepository repo, LinkedHashMap<String, String> configuration) throws Exception { // hack to make this factory compatible with DbSecurityMasterComponentFactory configuration.remove("cacheManager"); configuration.remove("detailProvider"); super.init(repo, configuration); } @Override protected DbSecurityBeanMaster createDbDocumentMaster() throws Exception { DbSecurityBeanMaster master = new DbSecurityBeanMaster(getDbConnector()); master.registerMetrics(OpenGammaMetricRegistry.getSummaryInstance(), OpenGammaMetricRegistry.getDetailedInstance(), "DbSecurityMaster-" + getClassifier()); return master; } @Override protected AbstractDataResource createPublishedResource(DbSecurityBeanMaster dbMaster, SecurityMaster postProcessedMaster) { //only db instance is allowed by the constructor here: return new DataSecurityMasterResource(dbMaster); } @Override protected SecurityMaster wrapMasterWithTrackingInterface(SecurityMaster postProcessedMaster) { return new DataTrackingSecurityMaster(postProcessedMaster); } @Override protected SecurityMaster postProcess(DbSecurityBeanMaster master) { return PermissionedSecurityMaster.wrap(master); } //------------------------- AUTOGENERATED START ------------------------- ///CLOVER:OFF /** * The meta-bean for {@code DbSecurityBeanMasterComponentFactory}. * @return the meta-bean, not null */ public static DbSecurityBeanMasterComponentFactory.Meta meta() { return DbSecurityBeanMasterComponentFactory.Meta.INSTANCE; } static { JodaBeanUtils.registerMetaBean(DbSecurityBeanMasterComponentFactory.Meta.INSTANCE); } @Override public DbSecurityBeanMasterComponentFactory.Meta metaBean() { return DbSecurityBeanMasterComponentFactory.Meta.INSTANCE; } //----------------------------------------------------------------------- @Override public DbSecurityBeanMasterComponentFactory clone() { return JodaBeanUtils.cloneAlways(this); } @Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj != null && obj.getClass() == this.getClass()) { return super.equals(obj); } return false; } @Override public int hashCode() { int hash = 7; return hash ^ super.hashCode(); } @Override public String toString() { StringBuilder buf = new StringBuilder(32); buf.append("DbSecurityBeanMasterComponentFactory{"); int len = buf.length(); toString(buf); if (buf.length() > len) { buf.setLength(buf.length() - 2); } buf.append('}'); return buf.toString(); } @Override protected void toString(StringBuilder buf) { super.toString(buf); } //----------------------------------------------------------------------- /** * The meta-bean for {@code DbSecurityBeanMasterComponentFactory}. */ public static class Meta extends AbstractDocumentDbMasterComponentFactory.Meta<SecurityMaster, DbSecurityBeanMaster> { /** * The singleton instance of the meta-bean. */ static final Meta INSTANCE = new Meta(); /** * The meta-properties. */ private final Map<String, MetaProperty<?>> _metaPropertyMap$ = new DirectMetaPropertyMap( this, (DirectMetaPropertyMap) super.metaPropertyMap()); /** * Restricted constructor. */ protected Meta() { } @Override public BeanBuilder<? extends DbSecurityBeanMasterComponentFactory> builder() { return new DirectBeanBuilder<DbSecurityBeanMasterComponentFactory>(new DbSecurityBeanMasterComponentFactory()); } @Override public Class<? extends DbSecurityBeanMasterComponentFactory> beanType() { return DbSecurityBeanMasterComponentFactory.class; } @Override public Map<String, MetaProperty<?>> metaPropertyMap() { return _metaPropertyMap$; } //----------------------------------------------------------------------- } ///CLOVER:ON //-------------------------- AUTOGENERATED END -------------------------- }