📄 adminbean.java
字号:
/** * Copyright (C) 2003-2004 Funambol * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package sync4j.server.admin.ejb;import javax.ejb.*;import javax.naming.InitialContext;import java.util.HashMap;import java.util.ArrayList;import java.util.List;import java.util.Arrays;import java.util.Vector;import java.sql.*;import java.io.*;import java.lang.reflect.Method;import java.util.logging.Logger;import java.util.logging.Level;import java.net.URL;import java.net.MalformedURLException;import org.kxml.wap.SyncMLWriter;import org.kxml.parser.XmlParser;import org.kxml.parser.ParseEvent;import org.kxml.Xml;import sync4j.framework.core.*;import sync4j.framework.logging.Sync4jLogger;import sync4j.framework.config.Configuration;import sync4j.framework.config.ConfigClassLoader;import sync4j.framework.config.ConfigurationException;import sync4j.framework.core.Sync4jException;import sync4j.framework.server.error.*;import sync4j.framework.tools.WBXMLTools;import sync4j.framework.tools.beans.BeanFactory;import sync4j.framework.tools.beans.BeanException;import sync4j.framework.tools.DBTools;import sync4j.server.admin.DBUserManager;import sync4j.server.engine.Sync4jSource;import sync4j.framework.engine.source.SyncSource;import sync4j.framework.engine.source.SyncSourceException;import sync4j.framework.security.Sync4jPrincipal;import sync4j.framework.server.SyncUser;import sync4j.framework.server.Sync4jDevice;import sync4j.framework.server.Sync4jModule;import sync4j.framework.server.Sync4jSourceType;import sync4j.framework.server.Sync4jConnector;import sync4j.framework.server.error.ServerException;import sync4j.framework.server.store.Clause;import sync4j.framework.server.store.WhereClause;import sync4j.framework.server.store.LogicalClause;import sync4j.framework.server.store.BasePersistentStore;import sync4j.framework.server.store.PersistentStore;import sync4j.framework.server.store.PersistentStoreException;import sync4j.framework.server.store.NotFoundException;import sync4j.framework.server.store.ConfigPersistentStoreException;import java.beans.XMLEncoder;import sync4j.server.admin.AdminException;/** * This is the session enterprise java bean that handles the administration * console. It is designed to be a stateless session bean. * <p> * This server accepts the requests addressed to the hostname * indicated by the configuration property pointed by {CONFIG_SERVER_URI} (see * Sync4j.properties). * <p> * AdminBean uses two environment properties: * <ul> * <li><i>{ENV_SERVER_CONFIG_URI}</i> contains the URI of the server * configuration. It can be a file or any other kind of URI that returns * a stream readable from the <i>java.util.Properties</i> class. * </ul> * * @author Luigia Fassina @ Funambol * * @version $Id: AdminBean.java,v 1.1 2004/05/19 16:27:07 luigiafassina Exp $ * */public class AdminBean implements javax.ejb.SessionBean{ // ------------------------------------------------------- Private constants private static final String ENV_SERVER_CONFIG_URI = "java:comp/env/server/config_uri"; private static final String ENV_SERVER_CONFIG_PATH = "java:comp/env/server/config_path"; private static final String CFG_PERSISTENT_STORE = "engine.store"; private static final String CONFIG_USER_MANAGER = "user.manager"; private static final String OPT_INSERT = "INSERT"; private static final String OPT_UPDATE = "UPDATE"; private static final String SEARCH_COUNT_DEVICES = "SCD"; private static final String SEARCH_COUNT_PRINCIPALS = "SCP"; //This role is used only for authentication private static final String ROLE_SPECIAL = "special_sync_admin"; // ------------------------------------------------------------ Private data private transient Logger log = null; private SessionContext sessionContext = null; private Configuration config = null; private PersistentStore ps = null; private DBUserManager dbUserManager = null; private URL configURI = null, configPath = null; // ------------------------------------------------------------- EJB methods public void ejbCreate() throws CreateException { log = Sync4jLogger.getLogger("server"); loadConfiguration(); // // Set the underlying persistent store // HashMap psConfig = new HashMap(1); psConfig.put("class-loader", config.getClassLoader()); ps = (PersistentStore)config.getBeanInstance(CFG_PERSISTENT_STORE); try { ps.configure(psConfig); } catch (ConfigPersistentStoreException e) { log.severe("Error configuring the persistent store: " + e); log.throwing(getClass().getName(), "<init>", e); throw new CreateException( "Error " + e.getClass().getName() + " creating the SyncBean: " + e.getMessage() ); } dbUserManager = (DBUserManager)config.getBeanInstance(CONFIG_USER_MANAGER); } public Configuration getConfig() { return config; } public void ejbRemove() { log = null; } public void ejbActivate() { log = Sync4jLogger.getLogger(); } public void ejbPassivate() { log = null; } public void setSessionContext (SessionContext sessionContext) throws EJBException { this.sessionContext = sessionContext; } /** * Read the list of roles available. * * @return names of roles available * * @throws ServerException * @throws AdminException */ public String[] getRoles() throws ServerException, AdminException { log.fine("AdminBean method getRoles"); String[] roles = null; try { roles = dbUserManager.getRoles(); if (roles != null) { List l = Arrays.asList(roles); Vector v = new Vector(l); int size = v.size(); for (int i=0; i<size; i++) { String role = (String)v.get(i); if (role.startsWith(ROLE_SPECIAL)) { v.remove(i); size = v.size(); } } roles = (String[])v.toArray(new String[0]); } } catch (PersistentStoreException e) { String msg = "Error reading roles: " + e.getMessage(); if (log.isLoggable(Level.SEVERE)) { log.severe(msg); } log.throwing(getClass().getName(), "getRoles", e); throw new ServerException(msg, e); } return roles; } /** * Read all users that satisfy the parameter of search. * * @param clause array of conditions for the query * * @return array of SyncUser * * @throws ServerException * @throws AdminException */ public SyncUser[] getUsers(Clause clause) throws ServerException, AdminException { log.fine("AdminBean method getUsers"); SyncUser[] users = null; try { users = dbUserManager.getUsers(clause); for (int i=0; (users != null) && i<users.length; i++) { dbUserManager.getUserRoles(users[i]); } } catch (PersistentStoreException e) { String msg = "Error reading Users: " + e.getMessage(); if (log.isLoggable(Level.SEVERE)) { log.severe(msg); } log.throwing(getClass().getName(), "getUsers", e); throw new ServerException(msg, e); } return users; } /** * Insert a new user and the assigned role to it * * @param user the user to insert * * @throws ServerException * @throws AdminException */ public void insertUser(SyncUser user) throws ServerException, AdminException { log.fine("AdminBean method insertUser"); try { dbUserManager.insertUser(user); } catch (PersistentStoreException e) { String msg = "Error inserting User: " + e.getMessage(); if (log.isLoggable(Level.SEVERE)) { log.severe(msg); } log.throwing(getClass().getName(), "insertUser", e); throw new ServerException(msg, e); } } /** * Update the information of the specific user * * @param user the user with informations updated * * @throws ServerException * @throws AdminException */ public void setUser(SyncUser user) throws ServerException, AdminException { log.fine("AdminBean method setUser"); try { dbUserManager.setUser(user); } catch (PersistentStoreException e) { String msg = "Error updating User: " + e.getMessage(); if (log.isLoggable(Level.SEVERE)) { log.severe(msg); } log.throwing(getClass().getName(), "setUser", e); throw new ServerException(msg, e); } } /** * Delete the user * * @param userName the name of user to delete * * @throws ServerException * @throws AdminException */ public void deleteUser(String userName) throws ServerException, AdminException { log.fine("AdminBean method deleteUser"); try { SyncUser user = new SyncUser(userName,null,null,null,null,null); dbUserManager.deleteUser(user); } catch (PersistentStoreException e) { String msg = "Error deleting User: " + e.getMessage(); if (log.isLoggable(Level.SEVERE)) { log.severe(msg); } log.throwing(getClass().getName(), "deleteUser", e); throw new ServerException(msg, e); } } public void importUser(SyncUser user) throws ServerException, AdminException { log.fine("AdminBean method importUser"); throw new AdminException("Not implemented yet"); } /** * Count the number of users that satisfy the specif clauses. * * @param clause the conditions of the search * * @return number of users * * @throws ServerException * @throws AdminException */ public int countUsers(Clause clause) throws ServerException, AdminException { log.fine("AdminBean method countUsers"); int n = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -