📄 jawprmiclienthelper.java
字号:
/*
* JAWPRMIClientHelper.java
*
* Created on 17 pa焏ziernik 2005, 18:59
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/
package net.sf.jawp.client.rmi;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import net.sf.jawp.RMIConst;
import net.sf.jawp.api.service.JAWPGameService;
import net.sf.jawp.api.service.rmi.JAWPGameServiceRMI;
import net.sf.jawp.api.service.rmi.JAWPGameServiceRMIClient;
import net.sf.jawp.gf.api.services.UserService;
import net.sf.jawp.gf.api.services.rmi.GameServiceClientFactory;
import net.sf.jawp.gf.api.services.rmi.UserServiceRMI;
import net.sf.jawp.gf.api.services.rmi.UserServiceRMIClient;
import net.sf.jawp.util.Log;
/**
*
* @author jarek
*/
public class JAWPRMIClientHelper
{
private static final Log LOG = Log.getLog(JAWPRMIClientHelper.class);
private UserService<JAWPGameService > userService;
private final String host;
private final int port;
private final JAWPClientFactory factory = new JAWPClientFactory();
/** Creates a new instance of JAWPRMIClientHelper */
private JAWPRMIClientHelper(final String host, final int port)
{
this.host = host;
this.port = port;
getUserService();
}
public static JAWPRMIClientHelper create()
{
return create( "localhost");
}
public static JAWPRMIClientHelper create(final String host)
{
return create( host, RMIConst.RMI_PORT);
}
public static JAWPRMIClientHelper create(final String host, final int port)
{
return new JAWPRMIClientHelper( host, port);
}
@SuppressWarnings("unchecked")
public final synchronized UserService<JAWPGameService> getUserService()
{
if (this.userService == null)
{
try
{
final String hostString = "//" + this.host + ":" + this.port;
final UserServiceRMI<JAWPGameService> client =
(UserServiceRMI<JAWPGameService>)Naming.lookup(hostString + "/userService");
this.userService = new UserServiceRMIClient<JAWPGameService, JAWPClientFactory>( client, factory);
}
catch (final NotBoundException nbe)
{
LOG.error( nbe, nbe);
Throwable t = nbe.getCause();
while (t != null)
{
LOG.error( "caused by", t);
t = t.getCause();
}
throw new RuntimeException (nbe);
}
catch (final MalformedURLException mue)
{
LOG.error( mue, mue);
Throwable t = mue.getCause();
while (t != null)
{
LOG.error( "caused by", t);
t = t.getCause();
}
throw new RuntimeException (mue);
}
catch (final RemoteException re)
{
LOG.error( re, re);
Throwable t = re.getCause();
while (t != null)
{
LOG.error( "caused by", t);
t = t.getCause();
}
throw new RuntimeException (re);
}
}
return this.userService;
}
public final synchronized void closeConnection()
{
this.userService = null;
}
/**
* {@inheritDoc}
*/
@Override
protected void finalize() throws Throwable
{
closeConnection();
}
private static class JAWPClientFactory implements GameServiceClientFactory<JAWPGameService>
{
public JAWPGameService createClient(final java.rmi.Remote remote) throws RemoteException
{
return new JAWPGameServiceRMIClient( (JAWPGameServiceRMI) remote);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -