001    /**
002     * Copyright (C) 2009-2013 Barchart, Inc. <http://www.barchart.com/>
003     *
004     * All rights reserved. Licensed under the OSI BSD License.
005     *
006     * http://www.opensource.org/licenses/bsd-license.php
007     */
008    package com.barchart.udt;
009    
010    import org.slf4j.Logger;
011    import org.slf4j.LoggerFactory;
012    
013    /**
014     * Wrapper around the UDT CCCFactory class
015     * 
016     * @see <a href="http://udt.sourceforge.net/udt4/doc/ccc.htm">reference</a>
017     * @see <a href="http://udt.sourceforge.net/udt4/doc/t-cc.htm">tutorial</a>
018     * @see CCC
019     * 
020     * @author CCob
021     */
022    public class FactoryUDT<C> implements FactoryInterfaceUDT {
023    
024            C classType;
025            final Class<C> clazz;
026    
027            Logger log = LoggerFactory.getLogger(FactoryUDT.class);
028    
029            boolean doInit = false;
030            boolean doClose = false;
031            boolean doOnACK = false;
032            boolean doOnLoss = false;
033            boolean doOnTimeout = false;
034    
035            public FactoryUDT(final Class<C> clazz) {
036    
037                    this.clazz = clazz;
038    
039                    if (!CCC.class.isAssignableFrom(clazz)) {
040                            throw new IllegalArgumentException(
041                                            "Generic argument 'C' must be 'CCC' class or extension");
042                    }
043    
044                    try {
045    
046                            if (clazz.getMethod("init").getDeclaringClass() != CCC.class)
047                                    doInit = true;
048    
049                            if (clazz.getMethod("close").getDeclaringClass() != CCC.class)
050                                    doClose = true;
051    
052                            if (clazz.getMethod("onACK", int.class).getDeclaringClass() != CCC.class)
053                                    doOnACK = true;
054    
055                            if (clazz.getMethod("onLoss", int[].class).getDeclaringClass() != CCC.class)
056                                    doOnLoss = true;
057    
058                            if (clazz.getMethod("onTimeout").getDeclaringClass() != CCC.class)
059                                    doOnTimeout = true;
060    
061                    } catch (final SecurityException e) {
062                            log.error("Error setting up class factory", e);
063                    } catch (final NoSuchMethodException e) {
064                            log.error("Expected CCC method doesn't exsit", e);
065                    }
066            }
067    
068            @Override
069            public CCC create() {
070    
071                    try {
072                            final Object cccObj = clazz.newInstance();
073                            return (CCC) cccObj;
074                    } catch (final InstantiationException e) {
075                            log.error("Failed to instansiate CCC class", e);
076                    } catch (final IllegalAccessException e) {
077                            log.error("Failed to instansiate CCC class", e);
078                    }
079    
080                    return null;
081            }
082    
083            @Override
084            public FactoryInterfaceUDT cloneFactory() {
085                    return new FactoryUDT<C>(clazz);
086            }
087    
088    }