You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by 0 8 <01...@gmail.com> on 2006/07/20 18:38:02 UTC

How do I create and re-create an object for the entire application?

Hi,

I've created a ServletContextListener object to run a piece of code
once a web app of mine starts up -- and thanks to this list for
pointing me in that direction. The core code is pretty simple:

ClientServices cs =
com.mycompany.searchapp.ClientServicesFactory.getInstance("RMI",
parms); // Get the ClientServices
cs.Login(strDomain, strUser, strPassword); // Login

But, the login will expire after 30 minutes and I cannot change that.

How do I recreate the login after it expires?

If I have to re-create it at the JSP level, that's fine.  But, in that
case, how can I store it so all users who use a particular JSP will
use the same login?

Thanks!

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How do I create and re-create an object for the entire application?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
To whom it may concern,

> ClientServices cs =
> com.mycompany.searchapp.ClientServicesFactory.getInstance("RMI",
> parms); // Get the ClientServices
> cs.Login(strDomain, strUser, strPassword); // Login

I assume you put this thing into the application scope, so that your
code can get to it at some point, yes?

> But, the login will expire after 30 minutes and I cannot change that.
> 
> How do I recreate the login after it expires?

What is the usage pattern for this object? Do you fetch it, and then do
something to it (like query it for information)? Assuming that is the
case, you could wrap your object in a helper object that does something
like this:

public synchronized Object getSomeData(...)
{
    if(!cs.isConnected())
    {
         // Do whatever needs to be done to re-connect
    }

    return cs.getSomeData();
}

This is even easier if ClientServices is an interface, 'cause you can
create a new class that implements that interface and wraps an object of
the same type:

public class ClientServicesWrapper
    implements ClientServices
{
    private ClientServices _cs;

    public ClientServicesWrapper(ClientServices cs)
    {
        _cs = cs;
    }

    public synchronized Object getSomeData(...)
    {
        if(!_cs.isConnected())
        {
             // Do whatever needs to be done to re-connect
        }

        return _cs.getSomeData();
    }

    // Implement the remaining methods in a similar way
}

But the Coup de Gras is using Java's Proxy stuff to save typing and look
like a 1337 h4x0r in front of all your friends:

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

// ...

ClientServices cs = // get from RMI
ClientServices wrapper = (ClientServices)Proxy.newProxyInstance(
                 ClientServices.class.getClassLoader(),
                 new Class[] { ClientServices.class },
                 new ClientServicesWrapper(cs)
                 );

// ...

public class ClientServicesWrapper
    implements InvocationHandler
{
    private ClientServices _cs;

    public ClientServicesWrapper(ClientServices cs)
    {
        _cs = cs;
    }

    public Object invoke(Object proxy, Method method, Object[] args)
        throws Throwable
    {
        if(!_cs.isConnected())
        {
            // reconnect the _cs object
        }

        method.invoke(_cs, args);
    }
}

How sexy is /that/?

Hope that helps. At least it was a pretty good example of using Java
proxies ;)

-chris