/* * The contents of this file are subject to the Mozilla Public License Version 1.1 * (the "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at . * * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT * WARRANTY OF ANY KIND, either express or implied. See the License for the specific * language governing rights and limitations under the License. * * The Original Code is the Venice Web Communities System. * * The Initial Developer of the Original Code is Eric J. Bowersox , * for Silverwrist Design Studios. Portions created by Eric J. Bowersox are * Copyright (C) 2003 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. * * Contributor(s): */ package com.silverwrist.venice.menu; import org.apache.log4j.Logger; import org.w3c.dom.*; import com.silverwrist.util.*; import com.silverwrist.util.xml.*; import com.silverwrist.dynamo.db.NamespaceCache; import com.silverwrist.dynamo.except.*; import com.silverwrist.dynamo.iface.*; import com.silverwrist.dynamo.module.ModuleOperations; import com.silverwrist.dynamo.security.SecurityReferenceMonitor; import com.silverwrist.dynamo.util.*; import com.silverwrist.venice.VeniceNamespaces; import com.silverwrist.venice.iface.*; public class MenuManager implements NamedObject, ComponentInitialize, ComponentShutdown, MenuProvider { /*-------------------------------------------------------------------------------- * Static data members *-------------------------------------------------------------------------------- */ private static Logger logger = Logger.getLogger(MenuManager.class); /*-------------------------------------------------------------------------------- * Attributes *-------------------------------------------------------------------------------- */ private String m_name = null; private NamespaceCache m_ns_cache; private SecurityReferenceMonitor m_srm; private MenuDatabaseOps m_ops; private HardSoftCache m_menudef_cache; private ComponentShutdown m_installreg; /*-------------------------------------------------------------------------------- * Constructor *-------------------------------------------------------------------------------- */ public MenuManager() { // do nothing } // end constructor /*-------------------------------------------------------------------------------- * Internal operations *-------------------------------------------------------------------------------- */ private final MenuDefinition getMenuDefinition(String namespace, String name) throws DatabaseException { QualifiedNameKey key = new QualifiedNameKey(namespace,name); MenuDefinition mdef = (MenuDefinition)(m_menudef_cache.get(key)); if ((mdef!=null) && mdef.isDeleted()) { // discard deleted menu definitions m_menudef_cache.remove(key); mdef = null; } // end if if (mdef==null) { // get it and add it to the cache mdef = m_ops.getMenu(namespace,name); if (mdef==null) { // menu was not found DatabaseException de = new DatabaseException(MenuManager.class,"MenuMessages","menu.notFound"); de.setParameter(0,namespace); de.setParameter(1,name); throw de; } // end if m_menudef_cache.put(key,mdef); } // end if return mdef; } // end getMenuDefinition /*-------------------------------------------------------------------------------- * Implementations from interface NamedObject *-------------------------------------------------------------------------------- */ public synchronized String getName() { return m_name; } // end getName /*-------------------------------------------------------------------------------- * Implementations from interface ComponentInitialize *-------------------------------------------------------------------------------- */ /** * Initialize the component. * * @param config_root Pointer to the section of the Dynamo XML configuration file that configures this * particular component. This is to be considered "read-only" by the component. * @param services An implementation of {@link com.silverwrist.dynamo.iface.ServiceProvider ServiceProvider} * which provides initialization services to the component. This will include an implementation * of {@link com.silverwrist.dynamo.iface.ObjectProvider ObjectProvider} which may be used to * get information about other objects previously initialized by the application. * @exception com.silverwrist.dynamo.except.ConfigException If an error is encountered in the component * configuration. */ public void initialize(Element config_root, ServiceProvider services) throws ConfigException { logger.info("MenuManager initializing"); XMLLoader loader = XMLLoader.get(); String name_pool = null, name_cache = null, name_srm = null; try { // verify the right node name loader.verifyNodeName(config_root,"object"); // get the object's name m_name = loader.getAttribute(config_root,"name"); // get the name of the database pool and namespace cache DOMElementHelper config_root_h = new DOMElementHelper(config_root); Element foo = loader.getSubElement(config_root_h,"database"); name_pool = loader.getAttribute(foo,"connection"); name_cache = loader.getAttribute(foo,"namespaces"); // get the name of the security reference monitor foo = loader.getSubElement(config_root_h,"security"); name_srm = loader.getAttribute(foo,"object"); } // end try catch (XMLLoadException e) { // error loading XML config data logger.fatal("XML loader exception in StandardContentSupplier",e); throw new ConfigException(e); } // end catch // Get the database connection pool. DBConnectionPool pool = GetObjectUtils.getDatabaseConnection(services,name_pool); // Get the namespace cache. m_ns_cache = (NamespaceCache)(GetObjectUtils.getDynamoComponent(services,NamespaceCache.class,name_cache)); // Get the security reference monitor. m_srm = (SecurityReferenceMonitor)(GetObjectUtils.getDynamoComponent(services,SecurityReferenceMonitor.class, name_srm)); // Get the menu database operations. m_ops = MenuDatabaseOps.get(pool); m_ops.setNamespaceCache(m_ns_cache); // Create the menu definition cache. m_menudef_cache = new HardSoftCache(3,10); // Register our install service to let modules make changes to menus. ModuleOperations module_ops = (ModuleOperations)(services.queryService(ModuleOperations.class)); SingletonServiceProvider ssp = new SingletonServiceProvider("Menu install services",MenuProvider.class,this); m_installreg = module_ops.hookInstallServices(ssp); } // end initialize /*-------------------------------------------------------------------------------- * Implementations from interface ComponentShutdown *-------------------------------------------------------------------------------- */ public void shutdown() { m_installreg.shutdown(); m_installreg = null; m_ns_cache = null; m_srm = null; m_ops.dispose(); m_ops = null; m_menudef_cache.clear(); } // end shutdown /*-------------------------------------------------------------------------------- * Implementations from interface MenuProvider *-------------------------------------------------------------------------------- */ public MenuRenderObject getStandardMenu(DynamoUser caller, String namespace, String name, int[] acl_ids) throws DatabaseException { return new StandardMenuRendering(caller,getMenuDefinition(namespace,name),m_srm,acl_ids); } // end getStandardMenu public MenuRenderObject getLeftMenu(DynamoUser caller, String namespace, String name, int[] acl_ids) throws DatabaseException { return new LeftMenuRendering(caller,getMenuDefinition(namespace,name),m_srm,acl_ids); } // end getLeftMenu public MenuRenderObject getInlineMenu(DynamoUser caller, String namespace, String name, int[] acl_ids) throws DatabaseException { return new InlineMenuRendering(caller,getMenuDefinition(namespace,name),m_srm,acl_ids); } // end getInlineMenu public MenuEditObject getEditMenu(DynamoUser caller, String namespace, String name) throws DatabaseException, DynamoSecurityException { if (!(m_srm.getGlobalAcl().testPermission(caller,VeniceNamespaces.SYSTEM_PERMS_NAMESPACE,"edit.menus"))) throw new DynamoSecurityException(MenuManager.class,"MenuMessages","auth.editMenu"); return new MenuEditImpl(m_ops,getMenuDefinition(namespace,name)); } // end getEditMenu public MenuEditObject createMenu(DynamoUser caller, String namespace, String name, String title) throws DatabaseException, DynamoSecurityException { if (!(m_srm.getGlobalAcl().testPermission(caller,VeniceNamespaces.SYSTEM_PERMS_NAMESPACE,"edit.menus"))) throw new DynamoSecurityException(MenuManager.class,"MenuMessages","auth.createMenu"); int menuid = m_ops.createMenu(namespace,name,title); MenuDefinition mdef = new MenuDefinition(menuid,namespace,name,title,null); m_menudef_cache.put(new QualifiedNameKey(namespace,name),mdef); return new MenuEditImpl(m_ops,mdef); } // end createMenu } // end class MenuManager