Package de.odysseus.calyxo.base.misc

Source Code of de.odysseus.calyxo.base.misc.ModuleAccessor

/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.calyxo.base.misc;

import java.util.HashMap;
import java.util.Map;

import de.odysseus.calyxo.base.ModuleContext;
import de.odysseus.calyxo.base.ModuleSupport;
import de.odysseus.calyxo.base.util.MapFacade;
import de.odysseus.calyxo.base.access.StaticBeanAccessor;

/**
* Module accessor.
* Property <em>path</em> returns a map to lookup action paths. The
* <em>name</em> property returns the module context name.
* The <em>forName</em> property answers a map to lookup module
* accessors for other modules.
* <ul>
*   <li>name</li>
*   <li>attribute[key]</li>
*   <li>path[action]</li>
*   <li>forName[name]</li>
* </ul>
*
* @author Christoph Beck
*/
public class ModuleAccessor extends StaticBeanAccessor {

  /**
   * Map to serve module accessors by name. This is used to get
   * information about another module as the current, eg. to get
   * a path to an action in another module.
   */
  public class ForNameMap extends MapFacade {
    private ModuleSupport support;
    private HashMap cache = new HashMap();

    public ForNameMap() {
      support = ModuleSupport.getInstance(context.getServletContext());
    }
    /**
     * Get module accessor for specified module name
     */
    public Object get(Object key) {
      String name = key.toString();
      ModuleAccessor result = null;
      if (name.equals(ModuleAccessor.this.getName())) {
        return ModuleAccessor.this;
      } else {
        ModuleContext context = support.getModuleContext(name);
        if (context == null) {
          if (cache.containsKey(name)) {
            cache.remove(name);
          }
          return null;
        }
        result = (ModuleAccessor)cache.get(context.getName());
        if (result == null) {
          result = new ModuleAccessor(context);
          cache.put(context.getName(), result);
        }
      }
      return result;
    }

  }

  private Map path;
  private Map attribute;
  private Map forName;

  /**
   * Constructor
   * @param context the module context for this accessor
   */
  public ModuleAccessor(ModuleContext context) {
    super(context);
  }

  /**
   * Get the name of the context for this accessor
   */
  public String getName() {
    return context.getName();
  }

  /**
   * Get Map to serve context-relative paths by action identifier
   */
  public Map getPath() {
    if (path == null) {
      path = new MapFacade() {
        public Object get(Object key) {
          return context.getPath(key.toString());
        }
      };
    }
    return path;
  }

  /**
   * Get Map to serve attributes from module scope
   */
  public Map getAttribute() {
    if (attribute == null) {
      attribute = new MapFacade() {
        public Object get(Object key) {
          if (key == null) {
            return null;
          }
          return context.getAttribute(key.toString());
        }
      };
    }
    return attribute;
  }

  /**
   * Answer a <code>ForNameMap</code> to lookup module accessors by name.
   */
  public Map getForName() {
    if (forName == null) {
      forName = new ForNameMap();
    }
    return forName;
  }

  /**
   * Answer the module name
   */
  public String toString() {
    return getName();
  }
}
TOP

Related Classes of de.odysseus.calyxo.base.misc.ModuleAccessor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.