📄 rmiclient.java
字号:
/******************************************************************************* * Copyright (C) 2002, 2003 * ingenieurbuero fuer innovative informationstechnik (iiit) * Dipl.-Ing. Joerg Beckmann, Dortmund, Germany * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * or look at http://www.gnu.org/copleft/lesser.html. * * version $Id: RMIClient.java,v 1.6 2003/04/13 21:13:38 joerg Exp $ ******************************************************************************/package de.iiit.access.common.rmi;import de.iiit.access.common.api.*;import de.iiit.access.common.cache.*;import de.iiit.cache.*;import de.iiit.util.*;import java.rmi.*;import java.rmi.registry.*;import java.util.*;import org.apache.log4j.Logger;/** This class implements the client side of AccessServer's RMI interface. * It incorporates its own caching for already requested expressions. * @version $Revision: 1.6 $ $Date: 2003/04/13 21:13:38 $ */public class RMIClient{ /** CVS Version Tag */ private static final String vcid = "$Id: RMIClient.java,v 1.6 2003/04/13 21:13:38 joerg Exp $"; private static Logger logger = Logger.getLogger(RMIClient.class); private static UserRightsCache cache = null; private static Vector hosts = new Vector(); private static Vector ports = new Vector(); private static Vector registries = new Vector(); private static int maxServer = 0; private static int roundRobin = 0; private static boolean initialized = false; private String user; private RMISessionIf remoteSession; /** Creates a new instance of Session * @param user the user for whom the session shall be used. * @throws RMIClientException */ private RMIClient(String user, RMISessionIf remoteSession) throws RMIClientException { this.user = user; this.remoteSession = remoteSession; } /** Returns a new RMIClient object representing a session with the remote AccessServer * @param user user the user for whom the session shall be used. * @throws RMIClientException if anything goes wrong * @return the new session, or null if the user doesn't exist in the user database */ public static RMIClient getSession(String user) throws RMIClientException { RMIClient result = null; if (! initialized) throw new RMIClientException("Not initialized!"); try { if (roundRobin >= maxServer) roundRobin = 0; int start = roundRobin; RMIServerIf sessionManager = null; do { Registry registry = (Registry) registries.get(roundRobin); if (registry == null) { registry = LocateRegistry.getRegistry((String) hosts.get(roundRobin), ((Integer) ports.get(roundRobin)).intValue()); registries.set(roundRobin, registry); } if (registry != null) sessionManager = (RMIServerIf) registry.lookup("RMIServer"); roundRobin++; if (roundRobin > maxServer) roundRobin = 0; } while (sessionManager == null && roundRobin != start); if (sessionManager == null) throw new RMIClientException("No server found"); else { RMISessionIf remoteSession = sessionManager.getSession(user); if (remoteSession != null) result = new RMIClient(user, remoteSession); } } catch (RemoteException re) { throw new RMIClientException(re.getMessage(), re); } catch (NotBoundException nbe) { throw new RMIClientException(nbe.getMessage(), nbe); } return result; } /** Initializes the logging system. If the application where this client is included * already uses Log4J for logging, this method should not be called at all, * otherwise it must be called before any other method of this class can be used. * @param level the name of the log-level. Valid are values are <CODE>ALL</CODE>, <CODE>DEBUG</CODE>, * <CODE>INFO</CODE>, <CODE>WARN</CODE>, <CODE>ERROR</CODE>, <CODE>FATAL</CODE> and * <CODE>OFF</CODE>. * @param logFile the name of the the log output shall be send to. If it is null or empty * <CODE>System.out</CODE> will be used. */ public static void initLogSystem(String level, String logFile) { LogUtil.init(level, logFile); } /** Initializes the RMI client system. The time values (in milliseconds) are used to * initialize the local cache. * @param server A list of servers to be requested. Each entry must have the format * <server>:<port>, for example <CODE>localhost:54322</CODE>. If the port number is * ommited, <CODE>RMIServerIf.DEFAULT_RMI_PORT</CODE> will be used. * @param invalidationTimeout after this period of time any elements in the cache will be ignored to assure * that changes in the rights will be recognized. * @param lruTimeout after this period of time an unused cache for a specific user will be deleted. * It does not make any sense to set this timeout higher than the invalidation timeout. * @param sleepTime the time to sleep between to runs of the cache cleaner. */ public static void initialize(String[] server, int invalidationTimeout, int lruTimeout, int sleepTime) { if (server == null || server.length == 0) throw new IllegalArgumentException("At least one server must be defined"); maxServer = server.length; for (int i = 0; i < maxServer; i++) { String[] parts = server[i].split(":", 2); if (parts[0].equals("")) throw new IllegalArgumentException("Hostname must not be empty"); int port = RMIServerIf.DEFAULT_RMI_PORT; if (! parts[1].equals("")) port = Integer.parseInt(parts[1]); logger.info("Found host <" + parts[0] + ">, port <" + port + ">"); hosts.add(parts[0]); ports.add(new Integer(port)); registries.add(null); } cache = new UserRightsCache(lruTimeout * 1000, invalidationTimeout * 1000, sleepTime * 1000); initialized = true; } /** Shuts down the cleaner thread of the cache. This method should be called when * the surrounding application finishes. */ public static void shutdown() { cache.shutdown(); } /** Evaluates one expression. * @param expression the expression to evaluate * @throws RMIClientException if anything goes wrong * @throws RMIParserException if a ParserException occurs on the server side. * @return true if the user is member of the set described by the expression and false * otherwise. */ public boolean resolveExpr(String expression) throws RMIClientException, ParserException { boolean answer = false; try { if (cache != null) { try { answer = cache.getUserRight(user, expression); } catch(CacheFaultException e) { answer = remoteSession.resolveExpr(expression); cache.addUserRight(user, expression, answer); } } else { answer = remoteSession.resolveExpr(expression); } } catch (RemoteException re) { throw new RMIClientException(re.getMessage(), re); } return answer; } }/** * $Log: RMIClient.java,v $ * Revision 1.6 2003/04/13 21:13:38 joerg * Copyright statements changed to LGPL * * Revision 1.5 2003/04/13 21:09:56 joerg * Package structure modified * * Revision 1.4 2003/04/13 20:16:41 joerg * Package structure modified * * Revision 1.3 2003/04/07 20:08:14 joerg * New static method getSession() * * Revision 1.2 2003/04/07 17:03:57 joerg * Method resolveExpr() now throws RMIParserException, too. * * Revision 1.1 2003/04/07 16:39:23 joerg * New classes RMIClient and RMIClientException * */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -