📄 jahiausermanagerroutingservice.java
字号:
package org.jahia.services.usermanager;import java.io.File;import java.io.FileInputStream;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.Comparator;import java.util.Enumeration;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.Map;import java.util.Properties;import java.util.Set;import java.util.SortedSet;import java.util.TreeSet;import java.util.Vector;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.jahia.data.JahiaDOMObject;import org.jahia.exceptions.JahiaException;import org.jahia.exceptions.JahiaInitializationException;import org.jahia.settings.JahiaPrivateSettings;import org.jahia.utils.InsertionSortedMap;import org.jahia.utils.JahiaConsole;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;/** * <p>Title: Manages routing of user management processes to the corresponding * provider.</p> * <p>Description: This service is the heart of the "routing" process of the * user manager. It is also a configurable system where regexps are using to * define the routing process. These regexps are taken in a specific order, as * to give priority to one provider or another, such as : </p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: Jahia Ltd</p> * * @author Serge Huber * @version 3.0 */public class JahiaUserManagerRoutingService extends JahiaUserManagerService { private static final int ROUTE_CALL_ONE = 0; private static final int ROUTE_CALL_ALL = 1; private static final int ROUTE_CALL_ALL_UNTIL_SUCCESS = 2; /** * @todo FIXME we had to hardcode this reference, so when we change the * name or location of the JahiaUser class we must update this too :( */ private static final String JAHIAUSER_INTERFACE_NAME = "org.jahia.services.usermanager.JahiaUser"; private static final String XML_ROOT_TAG = "router-config"; private static final String XML_PROVIDER_ROOT_TAG = "provider"; private static final String XML_PROVIDER_KEY_TAG = "key"; private static final String XML_PROVIDER_CLASS_TAG = "class"; private static final String XML_PROVIDER_TITLE_TAG = "title"; private static final String XML_PROVIDER_DESCRIPTION_TAG = "description"; private static final String XML_PROVIDER_ISDEFAULT_TAG = "is-default"; private static final String XML_PROVIDER_ISREADONLY_TAG = "is-readonly"; private static final String XML_PROVIDER_PRIORITY_TAG = "priority"; private static final String XML_CRITERIA_ROOT_TAG = "criteria"; private static final String XML_CRITERIA_KEY_TAG = "key"; private static final String XML_CRITERIA_DESCRIPTION_TAG = "description"; private static final String XML_CRITERIA_CONDITION_TAG = "condition"; private static final String XML_CRITERIA_DESTINATION_TAG = "provider-destination"; private static final String XML_CRITERIA_CONDITION_PROPNAME_ATTR = "propertyName"; private static final String XML_CRITERIA_CONDITION_PROPVALUE_ATTR = "propertyValue"; private static final String CONFIG_PATH = File.separator + "services" + File.separator + "usermanager"; private static final String CONFIG_FILE_NAME = "router-config.xml"; private Vector criteriaList = null; private Map providersTable = null; private SortedSet sortedProviders = null; private JahiaUserManagerProvider defaultProviderInstance = null; static private JahiaUserManagerRoutingService mInstance = null; private Class stringClass = null; private Class propertiesClass = null; /** * Create an new instance of the User Manager Service if the instance do not * exist, or return the existing instance. * * @return Return the instance of the User Manager Service. */ public static JahiaUserManagerRoutingService getInstance () { if (mInstance == null) { mInstance = new JahiaUserManagerRoutingService (); } return mInstance; } private JahiaUserManagerRoutingService() { criteriaList = new Vector(); providersTable = new HashMap(); sortedProviders = new TreeSet (new Comparator () { public int compare (Object o1, Object o2) { return ((UserManagerProviderBean) o1).getPriority () - ((UserManagerProviderBean) o2).getPriority (); } }); // the following default classes are used to solve the problem of // null parameters being passed and us being unable to do .getClass() // calls on the real objects. Sad that Java doesn't offer another way // to do this. // for JahiaUser we cannot do this properly so we will just manually // add the test inside the methods that need one. (The problem being that // JahiaUser is an abstract class :( ) // for Integer we don't have to do it since we have Integer.TYPE that's // already done for us... stringClass = (new String()).getClass(); propertiesClass = (new Properties()).getClass(); } /** * Returns a vector of UserManagerProviderBean object describing the * available user management providers * * @return result a Vector of UserManagerProviderBean objects that describe * the providers. This will never be null but may be empty if no providers * are available. */ public Vector getProviderList() { Vector result = new Vector (sortedProviders);// Iterator providerIter = providersTable.keySet().iterator();// while (providerIter.hasNext()) {// String curProviderKey = (String) providerIter.next();// UserManagerProviderBean curProvider = (UserManagerProviderBean) providersTable.get(curProviderKey);// result.add(curProvider);// } return result; } public void init( JahiaPrivateSettings jSettings ) throws JahiaInitializationException { try { loadConfiguration(jSettings.jahiaEtcDiskPath + this.CONFIG_PATH + File.separator + this.CONFIG_FILE_NAME); } catch (JahiaException je) { JahiaConsole.printe("JahiaUserManagerRoutingService.init#" + "Error while trying to load configuration from " + jSettings.jahiaEtcDiskPath + this.CONFIG_PATH + File.separator + this.CONFIG_FILE_NAME, je); throw new JahiaInitializationException("Error while trying to load configuration from " + jSettings.jahiaEtcDiskPath + this.CONFIG_PATH + File.separator + this.CONFIG_FILE_NAME + " Exception:" + je.getMessage()); } Iterator providerIter = providersTable.keySet().iterator(); while (providerIter.hasNext()) { String curProviderKey = (String) providerIter.next(); UserManagerProviderBean curProvider = (UserManagerProviderBean) providersTable.get ( curProviderKey); curProvider.getInstance().init(jSettings); } } public JahiaUser createUser(String name, String password, String userKey, int siteID, Properties properties) { JahiaUser user = null; Class[] parameterTypes = new Class[5]; parameterTypes[0] = stringClass; parameterTypes[1] = stringClass; parameterTypes[2] = stringClass; parameterTypes[3] = Integer.TYPE; parameterTypes[4] = propertiesClass; Object[] args = new Object[5]; args[0] = name; args[1] = password; args[2] = userKey; args[3] = new Integer(siteID); args[4] = properties; Object result = null; try { result = routeCall("createUser", parameterTypes, properties, ROUTE_CALL_ONE, null, false, null, args); } catch (Throwable t) { t.printStackTrace(); return null; } user = (JahiaUser) result; return user; } public boolean deleteUser(JahiaUser user) { if (user == null) { return false; } Class[] parameterTypes = new Class[1]; // all this code just to get the Class of the interfaces, Java sucks here :( Class[] interfaces = user.getClass().getInterfaces(); Class jahiaUserClass = null; for (int i=0; i < interfaces.length; i++) { JahiaConsole.println("JahiaUserManagerRoutingService.deleteUser", "Interface[" + i + "].getName()=[" + interfaces[i].getName() + "]"); if (JAHIAUSER_INTERFACE_NAME.equals(interfaces[i].getName())) { jahiaUserClass = interfaces[i]; } } if (jahiaUserClass == null) { JahiaConsole.println("JahiaUserManagerRoutingService.deleteUser", "Cannot find interface JahiaUSer in passed user variables !! Aborting deleteUser call ! "); return false; } parameterTypes[0] = jahiaUserClass; Object[] args = new Object[1]; args[0] = user; Object result = null; try { result = routeCall("deleteUser", parameterTypes, user.getProperties(), ROUTE_CALL_ONE, null, false, null, args); } catch (Throwable t) { t.printStackTrace(); return false; } Boolean resultBool = (Boolean) result; return resultBool.booleanValue(); } public int getNbUsers() throws org.jahia.exceptions.JahiaException { Object result = null; try { result = routeCall("getNbUsers", null, null, ROUTE_CALL_ALL, null, false, null, null); } catch (Throwable t) { t.printStackTrace(); return -1; } Vector resultVector = (Vector) result; Enumeration resultEnum = resultVector.elements(); int nbUsers = 0; while (resultEnum.hasMoreElements()) { Integer curResult = (Integer) resultEnum.nextElement(); nbUsers += curResult.intValue(); } return nbUsers; } public int getNbUsers(int siteID) throws org.jahia.exceptions.JahiaException { Class[] parameterTypes = new Class[1]; parameterTypes[0] = Integer.TYPE; Object[] args = new Object[1]; args[0] = new Integer(siteID); Object result = null; try { result = routeCall("getNbUsers", parameterTypes, null, ROUTE_CALL_ALL, null, false, null, args); } catch (Throwable t) { t.printStackTrace(); return -1; } Vector resultVector = (Vector) result; Enumeration resultEnum = resultVector.elements(); int nbUsers = 0; while (resultEnum.hasMoreElements()) { Integer curResult = (Integer) resultEnum.nextElement(); nbUsers += curResult.intValue(); } return nbUsers; } public String getUniqueKey() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -