📄 viewfactory.java
字号:
//// This file is part of the OpenNMS(R) Application.//// OpenNMS(R) is Copyright (C) 2002-2003 The OpenNMS Group, Inc. All rights reserved.// OpenNMS(R) is a derivative work, containing both original code, included code and modified// code that was published under the GNU General Public License. Copyrights for modified // and included code are below.//// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.//// Modifications://// 2003 Jan 31: Cleaned up some unused imports.//// Original code base Copyright (C) 1999-2001 Oculan Corp. All rights reserved.//// 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.// // For more information contact: // OpenNMS Licensing <license@opennms.org>// http://www.opennms.org/// http://www.opennms.com///// Tab Size = 8//package org.opennms.netmgt.config;import java.io.File;import java.io.FileInputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.StringWriter;import java.util.Collection;import java.util.Date;import java.util.Enumeration;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;import org.apache.regexp.RE;import org.apache.regexp.RESyntaxException;import org.exolab.castor.xml.MarshalException;import org.exolab.castor.xml.Marshaller;import org.exolab.castor.xml.Unmarshaller;import org.exolab.castor.xml.ValidationException;import org.opennms.netmgt.ConfigFileConstants;import org.opennms.netmgt.EventConstants;import org.opennms.netmgt.config.views.Categories;import org.opennms.netmgt.config.views.Header;import org.opennms.netmgt.config.views.Member;import org.opennms.netmgt.config.views.Membership;import org.opennms.netmgt.config.views.View;import org.opennms.netmgt.config.views.Viewinfo;import org.opennms.netmgt.config.views.Views;public class ViewFactory { /** * The static singleton instance of the ViewFactory */ private static ViewFactory instance; /** * File path of views.xml */ protected static File usersFile; /** * An input stream for the views configuration file */ protected static InputStream configIn; /** * A mapping of views ids to the View objects */ protected static HashMap m_views; /** * Boolean indicating if the init() method has been called */ private static boolean initialized = false; private static Header oldHeader; /** * Initializes the factory */ ViewFactory() { } public static synchronized void init() throws IOException, MarshalException, ValidationException { if (!initialized) { reload(); } } /** * Singleton static call to get the only instance that should exist for the * ViewFactory * * @return the single view factory instance */ static synchronized public ViewFactory getInstance() { if (!initialized) return null; if (instance == null) { instance = new ViewFactory(); } return instance; } /** * Parses the views.xml via the Castor classes */ public static synchronized void reload() throws IOException, MarshalException, ValidationException { configIn = new FileInputStream(ConfigFileConstants.getFile(ConfigFileConstants.VIEWS_CONF_FILE_NAME)); Viewinfo viewinfo = (Viewinfo) Unmarshaller.unmarshal(Viewinfo.class, new InputStreamReader(configIn)); Views views = viewinfo.getViews(); oldHeader = viewinfo.getHeader(); Collection viewsList = views.getViewCollection(); m_views = new HashMap(); Iterator i = viewsList.iterator(); while (i.hasNext()) { View curView = (View) i.next(); m_views.put(curView.getName(), curView); } initialized = true; } /** * Adds a new user and overwrites the "users.xml" */ public synchronized void saveView(String name, View details) throws Exception { if (name == null || details == null) { throw new Exception("UserFactory:saveUser null"); } else { m_views.put(name, details); } // Saves into "views.xml" file Views views = new Views(); Collection viewList = (Collection) m_views.values(); views.setViewCollection(viewList); saveViews(views); } /** * Removes the user from the list of users. Then overwrites to the * "users.xml" */ public synchronized void deleteUser(String name) throws Exception { // Check if the user exists if (name != null) { // Remove the user in the view. Set viewKeys = (Set) m_views.keySet(); Map map = new HashMap(); View view; Iterator iter = viewKeys.iterator(); while (iter.hasNext()) { View newView = new View(); view = (View) m_views.get((String) iter.next()); newView = view; Membership membership = new Membership(); Membership viewmembers = view.getMembership(); if (viewmembers != null) { Enumeration en = viewmembers.enumerateMember(); while (en.hasMoreElements()) { Member member = (Member) en.nextElement(); if (member.getType().equals("user")) { if (!member.getContent().equals(name)) { membership.addMember(member); } } else membership.addMember(member); } } newView.setMembership(membership); map.put(newView.getName(), newView); } // Saves into "views.xml" file m_views.clear(); Views views = new Views(); views.setViewCollection((Collection) map.values()); saveViews(views); } else { throw new Exception("ViewFactory:delete Invalid user name:" + name); } } /** * When this method is called users name is changed, so also is the username * belonging to the group and the view. Also overwrites the "users.xml" file */ public synchronized void renameUser(String oldName, String newName) throws Exception { // Get the old data if (oldName == null || newName == null || oldName == "" || newName == "") { throw new Exception("Group Factory: Rename user.. no value "); } else { Collection coll = (Collection) m_views.values(); Iterator iter = (Iterator) coll.iterator();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -