vendredi 24 mai 2019

Java Proxy Wrapper - Reflection

I created this proxy wrapper to handle invoking of methods. Seems to work, I was just wondering if anyone can check and tell me if something can be improved and why?

Cheers!

stackover is complaining I need to write more so this is just fillers...lol

IGNORE

Video provides a powerful way to help you prove your point. When you click Online Video, you can paste in the embed code for the video you want to add. You can also type a keyword to search online for the video that best fits your document. To make your document look professionally produced, Word provides header, footer, cover page, and text box designs that complement each other. For example, you can add a matching cover page, header, and sidebar. Click Insert and then choose the elements you want from the different galleries. Themes and styles also help keep your document coordinated. When you click Design and choose a new Theme, the pictures, charts, and SmartArt graphics change to match your new theme. When you apply styles, your headings change to match the new theme. Save time in Word with new buttons that show up where you need them. To change the way a picture fits in your document, click it and a button for layout options appears next to it. When you work on a table, click where you want to add a row or a column, and then click the plus sign. Reading is easier, too, in the new Reading view. You can collapse parts of the document and focus on the text you want. If you need to stop reading before you reach the end, Word remembers where you left off - even on another device.

Ok HERE IS THE CODE

import java.lang.reflect.Method;

public class ProxyWrapper
{
    Class<?> c = null;
    Object o = null;
    String namespace = null;
    String className = null;
    Thread tCleanup;
    HouseKeeping houseKeeping = null;
    Boolean closing = false;
    long idle_start = System.currentTimeMillis();
    int timeout = 10000;

    public ProxyWrapper()
    {
        CreateHouseKeeping();
    }

    public ProxyWrapper(String namespace, String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException, Exception
    {
        msg("new ProxyWrapper");
        this.namespace = namespace;
        this.className = className;

        msg("calling INIT");
        INIT();

        CreateHouseKeeping();
    }

    private void CreateHouseKeeping()
    {
        msg("creating housekeeping");
        houseKeeping = new HouseKeeping();
        houseKeeping.SetParent(this);
        tCleanup = new Thread(houseKeeping);
        tCleanup.start();
    }

    public void msg(Object e)
    {
        if(e.getClass() == String.class)
        {
            System.out.println((String)e);
        }
        if(e.getClass() == Exception.class)
        {
            System.out.println(((Exception)e).toString());
        }
    }

    public int GetTimeout()
    {
        return timeout;
    }

    public long GetIdleTime()
    {
        return idle_start;
    }

    public void SetIdleTime(long value)
    {
        idle_start = value;
    }

    public Boolean GetClosing()
    {
        return closing;
    }

    public void SetClosing(Boolean value)
    {
        closing = value;
    }

    private void INIT()throws ClassNotFoundException, InstantiationException, IllegalAccessException, Exception
    {
        if(namespace == null || namespace.length()==0)
            throw new Exception("Namespace property not set");
        if(className == null || className.length()==0)
            throw new Exception("Classname property not set");

        c = Class.forName(namespace+"."+className); 
        o= c.newInstance(); 
    }

    private class HouseKeeping implements Runnable
    {
        ProxyWrapper parent = null;

        public HouseKeeping()
        {

        }

        public void SetParent(ProxyWrapper parent)
        {
            this.parent = parent;
        }

        @Override
        public void run()
        {
            long t = 0;
            while (!parent.GetClosing())
            {
                try
                {
                    t = (System.currentTimeMillis() - parent.GetIdleTime()); 
                    msg("Housekeeping, checking idle session: " + t);
                    Thread.sleep(200);
                    if( t > parent.GetTimeout() )
                    {
                        msg("Session idle expired, finilizing");
                        parent.SetIdleTime(System.currentTimeMillis());
                        parent.finalize();
                        //break;
                    }
                }
                catch (Throwable e)
                {
                    msg(e);
                }
            }
        }
    }

    public Object Invoke(String method, Object[] args) throws Exception
    {
        //Method m = null;
        Method[] methods = null;

        try
        {
            if(method==null ||method.length()==0)
                throw new Exception("Method name is null/empty");

            msg("Invoking method " + method);

            if(o==null)
                INIT();

            SetIdleTime(System.currentTimeMillis());

            //m = o.getClass().getMethod(method);

            methods = c.getMethods();

            for (Method m : methods) {
                msg(m.getName());
                if (method.equals(m.getName())) {
                    return m.invoke(null, args);
                }
            }

            throw new ClassNotFoundException("Method not found " + method);
        }
        finally
        {
            methods = null;
        }

    }

    @Override
    public void finalize() throws Throwable
    {
        try
        {
            msg("entering finalize");
            closing = true;
            c = null;
            o = null;
        }
        catch(Throwable t)
        {
            msg(t);
            throw t;
        }
        finally
        {
            msg("leaving finalize");
            super.finalize();
        }
    }
}





Aucun commentaire:

Enregistrer un commentaire