Save This Page
Home » openjdk-7 » net » sf » rvpf » metadata » [javadoc | source]
    1   /**
    2    * Related Values Processing Framework.
    3    * 
    4    * Copyright (C) 2003 Serge Brisson.
    5    * 
    6    * This software is distributable under LGPL license.
    7    * See details at the bottom of this file.
    8    * 
    9    * $Header: /cvsroot/rvpf/RVPF/java/src/net/sf/rvpf/metadata/Metadata.java,v 1.10 2003/10/29 23:15:19 sfb Exp $
   10    */
   11   package net.sf.rvpf.metadata;
   12   
   13   import java.lang.ref.Reference;
   14   import java.lang.ref.WeakReference;
   15   import java.util.Collection;
   16   import java.util.HashMap;
   17   import java.util.Map;
   18   import java.util.WeakHashMap;
   19   
   20   import net.sf.rvpf.content.Content;
   21   import net.sf.rvpf.processor.behaviour.Behaviour;
   22   import net.sf.rvpf.processor.engine.Engine;
   23   import net.sf.rvpf.processor.filter.Filter;
   24   import net.sf.rvpf.store.Store;
   25   import net.sf.rvpf.util.Config;
   26   import net.sf.rvpf.util.KeyedValues;
   27   
   28   /** Metadata.
   29    * 
   30    * @author Serge Brisson
   31    * @version $Revision: 1.10 $
   32    */
   33   public class Metadata extends Config {
   34       
   35       // Constructors.
   36   
   37       /** Constructs a Metadata instance.
   38        * <p>
   39        * RVPF processes using the Metadata are also provided with a Config
   40        * file which may augment or override some parts included in
   41        * the Metadata resource.  The Metadata constructor is thus informed
   42        * of this Config to allow the override during such calls
   43        * as {@link #getPropertyValue}.
   44        * 
   45        * @param config The process Config.
   46        */
   47       public Metadata(Config config) {
   48           super(config.getClassLoader());
   49   
   50           setServiceName(config.getServiceName());
   51           getProperties().setOverrider(config.getProperties());
   52       }
   53   
   54       // Public Class Methods.
   55   
   56       /** Gets the properties from Metadata, overriden by groups specified in Params.
   57        * <p>
   58        * The values for the Param <code>"Properties"</code>, when present, should
   59        * contain the name of a group of properties which may be available
   60        * in the Metadata.  If so, these properties will override those in
   61        * the Metadata.  When there is more than one such group, they will be
   62        * used in order: the first one will override the second, etc.
   63        * 
   64        * @param config The Config.
   65        * @param params The Params (ignored if null).
   66        * @return The properties.
   67        */
   68       public static KeyedValues getProperties(Config config, Params params) {
   69           KeyedValues properties = config.getProperties();
   70   
   71           if (params != null) {
   72               String[] values = params.getValues(PROPERTIES_PARAM);
   73   
   74               for (int i = values.length; i > 0; ) {
   75                   KeyedValues group = config.getGroup(values[--i]);
   76   
   77                   if (group != null) {
   78                       group.setOverriden(properties);
   79                       properties = group;
   80                   }
   81               }
   82           }
   83   
   84           return properties;
   85       }
   86   
   87       // Public Instance Methods.
   88       
   89       /** Adds a Behaviour Entity to this Metadata.
   90        * 
   91        * @param behaviour The Behaviour Entity.
   92        */
   93       public void addBehaviourEntity(BehaviourEntity behaviour) {
   94           this.behavioursByName.put(behaviour.getName(), new WeakReference(behaviour));
   95       }
   96   
   97       /** Adds a Content Entity to this Metadata.
   98        * 
   99        * @param content The Content Entity.
  100        */
  101       public void addContentEntity(ContentEntity content) {
  102           this.contentsByName.put(content.getName(), new WeakReference(content));
  103       }
  104   
  105       /** Adds a Engine Entity to this Metadata.
  106        * 
  107        * @param engine The Engine Entity.
  108        */
  109       public void addEngineEntity(EngineEntity engine) {
  110           this.enginesByName.put(engine.getName(), new WeakReference(engine));
  111       }
  112   
  113       /** Adds a Filter Entity to this Metadata.
  114        * 
  115        * @param filter The Filter Entity.
  116        */
  117       public void addFilterEntity(FilterEntity filter) {
  118           this.filtersByName.put(filter.getName(), new WeakReference(filter));
  119       }
  120   
  121       /** Adds an Origin Entity to this Metadata.
  122        * 
  123        * @param origin The Origin Entity.
  124        */
  125       public void addOriginEntity(OriginEntity origin) {
  126           this.originsByName.put(origin.getName(), new WeakReference(origin));
  127       }
  128   
  129       /** Adds a Point Entity to this Metadata.
  130        * 
  131        * @param point The Point Entity.
  132        */
  133       public void addPointEntity(PointEntity point) {
  134           this.pointsByUUID.put(point.getUUID(), point);
  135           if (point.getName() != null)
  136               this.pointsByName.put(point.getName(), new WeakReference(point));
  137       }
  138   
  139       /** Adds a Store Entity to this Metadata.
  140        * 
  141        * @param store The Store Entity.
  142        */
  143       public void addStoreEntity(StoreEntity store) {
  144           this.storesByName.put(store.getName(), new WeakReference(store));
  145       }
  146   
  147       /** Adds a Transform Entity to this Metadata.
  148        * 
  149        * @param transform The Transform Entity.
  150        */
  151       public void addTransformEntity(TransformEntity transform) {
  152           this.transformsByName.put(transform.getName(), new WeakReference(transform));
  153       }
  154   
  155       /** Cleans up unreferenced entities.
  156        * <p>
  157        * This implementation expects a WeakHashMap to clean up upon access.
  158        */
  159       public void cleanUp() {
  160           this.pointsByName.get(null);
  161           this.originsByName.get(null);
  162           this.storesByName.get(null);
  163       }
  164   
  165       /** Gets the Behaviour with the specified name.
  166        * 
  167        * @param name The name.
  168        * @return The Behaviour.
  169        */
  170       public Behaviour getBehaviour(String name) {
  171           BehaviourEntity entity = getBehaviourEntity(name);
  172   
  173           if (entity == null) return null;
  174           entity.setUp(this);
  175   
  176           return (Behaviour) entity.getInstance();
  177       }
  178   
  179       /** Gets the Behaviour Entity with the specified name.
  180        * 
  181        * @param name The name.
  182        * @return The Behaviour Entity.
  183        */
  184       public BehaviourEntity getBehaviourEntity(String name) {
  185           Reference reference = (Reference) this.behavioursByName.get(name);
  186   
  187           return (BehaviourEntity) (reference == null? null: reference.get());
  188       }
  189   
  190       /** Gets the Content with the specified name.
  191        * 
  192        * @param name The name.
  193        * @return The Content.
  194        */
  195       public Content getContent(String name) {
  196           ContentEntity entity = getContentEntity(name);
  197   
  198           if (entity == null) return null;
  199           entity.setUp(this);
  200   
  201           return (Content) entity.getInstance();
  202       }
  203   
  204       /** Gets the Content Entity with the specified name.
  205        * 
  206        * @param name The name.
  207        * @return The Content Entity.
  208        */
  209       public ContentEntity getContentEntity(String name) {
  210           Reference reference = (Reference) this.contentsByName.get(name);
  211   
  212           return (ContentEntity) (reference == null? null: reference.get());
  213       }
  214   
  215       /** Gets the Engine with the specified name.
  216        * 
  217        * @param name The name.
  218        * @return The Engine.
  219        */
  220       public Engine getEngine(String name) {
  221           EngineEntity entity = getEngineEntity(name);
  222   
  223           if (entity == null) return null;
  224           entity.setUp(this);
  225   
  226           return (Engine) entity.getInstance();
  227       }
  228   
  229       /** Gets the Engine Entity with the specified name.
  230        * 
  231        * @param name The name.
  232        * @return The Engine Entity.
  233        */
  234       public EngineEntity getEngineEntity(String name) {
  235           Reference reference = (Reference) this.enginesByName.get(name);
  236   
  237           return (EngineEntity) (reference == null? null: reference.get());
  238       }
  239   
  240       /** Gets the Filter with the specified name.
  241        * 
  242        * @param name The name.
  243        * @return The Filter.
  244        */
  245       public Filter getFilter(String name) {
  246           FilterEntity entity = getFilterEntity(name);
  247   
  248           if (entity == null) return null;
  249           entity.setUp(this);
  250   
  251           return (Filter) entity.getInstance();
  252       }
  253   
  254       /** Gets the Filter Entity with the specified name.
  255        * 
  256        * @param name The name.
  257        * @return The Filter Entity.
  258        */
  259       public FilterEntity getFilterEntity(String name) {
  260           Reference reference = (Reference) this.filtersByName.get(name);
  261   
  262           return (FilterEntity) (reference == null? null: reference.get());
  263       }
  264   
  265       /** Gets the Origin Entity with the specified name.
  266        * 
  267        * @param name The name.
  268        * @return The Origin Entity.
  269        */
  270       public OriginEntity getOrigin(String name) {
  271           Reference reference = (Reference) this.originsByName.get(name);
  272   
  273           return (OriginEntity) (reference == null? null: reference.get());
  274       }
  275   
  276       /** Gets the Point with the specified name.
  277        * 
  278        * @param name The name.
  279        * @return The Point.
  280        */
  281       public PointEntity getPoint(String name) {
  282           Reference reference = (Reference) this.pointsByName.get(name);
  283   
  284           return (PointEntity) (reference == null? null: reference.get());
  285       }
  286   
  287       /** Gets the Point with the specified UUID.
  288        * 
  289        * @param uuid The UUID.
  290        * @return The Point.
  291        */
  292       public PointEntity getPoint(UUID uuid) {
  293           return (PointEntity) this.pointsByUUID.get(uuid);
  294       }
  295   
  296       /** Gets a Collection of the points registered in this Metadata.
  297        * 
  298        * @return A Collection of Points.
  299        */
  300       public Collection getPointsCollection() {
  301           return this.pointsByUUID.values();
  302       }
  303   
  304       /** Gets the Points Map.
  305        * 
  306        * @return The internal registry of points.
  307        */
  308       public Map getPointsMap() {
  309           return this.pointsByUUID;
  310       }
  311   
  312       /** Gets the Store with the specified name.
  313        * 
  314        * @param name The name.
  315        * @return The Store.
  316        */
  317       public Store getStore(String name) {
  318           StoreEntity entity = getStoreEntity(name);
  319   
  320           if (entity == null) return null;
  321           entity.setUp(this);
  322   
  323           return (Store) entity.getInstance();
  324       }
  325   
  326       /** Gets the Store Entity with the specified name.
  327        * 
  328        * @param name The name.
  329        * @return The Store Entity.
  330        */
  331       public StoreEntity getStoreEntity(String name) {
  332           Reference reference = (Reference) this.storesByName.get(name);
  333   
  334           return (StoreEntity) (reference == null? null: reference.get());
  335       }
  336   
  337       /** Gets the Transform Entity with the specified name.
  338        * 
  339        * @param name The name.
  340        * @return The Transform Entity.
  341        */
  342       public TransformEntity getTransform(String name) {
  343           Reference reference = (Reference) this.transformsByName.get(name);
  344   
  345           return (TransformEntity) (reference == null? null: reference.get());
  346       }
  347   
  348       /** Keeps the entities.
  349        * <p>
  350        * This is called by the MetadataDocument when the MetadataFilter wants
  351        * the entities to be kept, even if they are not referenced. 
  352        *
  353        * @param entities The object containing references to all the entities.
  354        */
  355       public void keepEntities(Object entities) {
  356           this.entities = entities;
  357       }
  358   
  359       // Protected Instance Methods.
  360   
  361       /**
  362        */
  363       protected void setUpProperties() {
  364           setProperties(new KeyedValues("metadata-property", false));
  365       }
  366   
  367       // Class Constants.
  368   
  369       public static final String PROPERTIES_PARAM = "Properties";
  370   
  371       // Instance Attributes.
  372   
  373       private Map behavioursByName = new WeakHashMap();
  374       private Map contentsByName = new WeakHashMap();
  375       private Map enginesByName = new WeakHashMap();
  376       private Object entities = null;
  377       private Map filtersByName = new WeakHashMap();
  378       private Map originsByName = new WeakHashMap();
  379       private Map pointsByName = new WeakHashMap();
  380       private Map pointsByUUID = new HashMap();
  381       private Map storesByName = new WeakHashMap();
  382       private Map transformsByName = new WeakHashMap();
  383   }
  384   
  385   // $Log: Metadata.java,v $
  386   // Revision 1.10  2003/10/29 23:15:19  sfb
  387   // Allowed use of 'getProperties' without Metadata.
  388   //
  389   // Revision 1.9  2003/10/29 15:28:11  sfb
  390   // Added 'keepEntities'.
  391   //
  392   // Revision 1.8  2003/10/24 16:11:00  sfb
  393   // Added Filter.
  394   //
  395   // Revision 1.7  2003/10/20 17:46:15  sfb
  396   // Made behaviours, contents, engines and transforms available by name.
  397   //
  398   
  399   /*
  400    * This is free software; you can redistribute it and/or modify
  401    * it under the terms of the GNU Lesser General Public License
  402    * as published by the Free Software Foundation; either version 2.1
  403    * of the License, or (at your option) any later version.
  404    *
  405    * This software is distributed in the hope that it will be useful,
  406    * but WITHOUT ANY WARRANTY; without even the implied warranty of
  407    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  408    * Lesser General Public License for more details.
  409    *
  410    * You should have received a copy of the GNU Lesser General Public
  411    * License along with this software; if not, write to the Free Software
  412    * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  413    */

Save This Page
Home » openjdk-7 » net » sf » rvpf » metadata » [javadoc | source]