You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by ra...@apache.org on 2001/05/08 21:09:19 UTC

cvs commit: jakarta-jetspeed/src/java/org/apache/jetspeed/services/registry CastorRegistryService.java RegistryFragment.java

raphael     01/05/08 12:09:18

  Modified:    src/java/org/apache/jetspeed/services/registry
                        CastorRegistryService.java RegistryFragment.java
  Log:
  add persistence support to registry implementation
  
  Revision  Changes    Path
  1.7       +185 -62   jakarta-jetspeed/src/java/org/apache/jetspeed/services/registry/CastorRegistryService.java
  
  Index: CastorRegistryService.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/services/registry/CastorRegistryService.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- CastorRegistryService.java	2001/05/08 10:04:04	1.6
  +++ CastorRegistryService.java	2001/05/08 19:09:10	1.7
  @@ -68,6 +68,9 @@
   import org.exolab.castor.xml.Unmarshaller;
   import org.exolab.castor.xml.Marshaller;
   import org.xml.sax.InputSource;
  +import org.apache.xml.serialize.Serializer;
  +import org.apache.xml.serialize.XMLSerializer;
  +import org.apache.xml.serialize.OutputFormat;
   
   //java stuff
   import java.util.*;
  @@ -98,7 +101,7 @@
    * </p>
    *
    * @author <a href="mailto:raphael@apache.org">Rapha�l Luta</a>
  - * @version $Id: CastorRegistryService.java,v 1.6 2001/05/08 10:04:04 raphael Exp $
  + * @version $Id: CastorRegistryService.java,v 1.7 2001/05/08 19:09:10 raphael Exp $
    */
   public class CastorRegistryService
       extends TurbineBaseService
  @@ -108,18 +111,27 @@
       public static final String DEFAULT_EXTENSION = ".xreg";
       public static final String DEFAULT_MAPPING = "${webapp.dir}/WEB-INF/conf/mapping.xml";
   
  -    /** Known registries */
  +    /** regsitry type keyed list of entries */
       private Hashtable registries = new Hashtable();
       
       /** The Castor generated RegsitryFragment objects */
       private Hashtable fragments = new Hashtable();
   
  +    /** The list of default fragments stores for newly created objects */
  +    private Hashtable defaults = new Hashtable();
  +
  +    /** Associates entries with their fragments name for quick lookup */
  +    private Hashtable entryIndex = new Hashtable();
  +
       /** the Watcher object which monitors the regsitry directory */
       private RegistryWatcher watcher = null;
           
       /** the Castor mapping file name */
       private Mapping mapping = null;
           
  +    /** the output format for pretty printing when saving registries */
  +    private OutputFormat format = null;
  +        
       /** the base regsitry directory */
       private String directory = null;
       
  @@ -180,25 +192,42 @@
        */
       public void addEntry(String regName, RegistryEntry entry) throws RegistryException
       {
  -        try
  +        if (entry == null) 
  +        {
  +            return;
  +        }
  +        
  +        Registry registry = (Registry)registries.get( regName );
  +        
  +        if (registry!=null)
           {
  -            Registry registry = (Registry)registries.get( regName );
  +            String fragmentName = (String)entryIndex.get( entry.getName() );
               
  -            if (registry.getEntry(entry.getName())!=null)
  +            if (fragmentName == null)
               {
  -                registry.setEntry(entry);
  +                // either the entry was deleted or it does not exist
  +                // in both cases, use the default fragment                
  +                fragmentName = (String)defaults.get( regName );
               }
  -            else
  +                
  +            RegistryFragment fragment = (RegistryFragment)fragments.get( fragmentName );
  +            
  +            synchronized (entryIndex)
               {
  -                registry.addEntry(entry);
  +                if (registry.getEntry(entry.getName())!=null)
  +                {
  +                    fragment.setEntry(regName,entry);
  +                    registry.setEntry(entry);
  +                }
  +                else
  +                {
  +                    fragment.addEntry(regName,entry);
  +                    registry.addEntry(entry);
  +                }
  +                
  +                entryIndex.put(entry,fragmentName);
               }
           }
  -        catch ( NullPointerException e )
  -        {
  -            Log.error(regName + "registry is not known ");
  -            Log.error( e );
  -        }
  -
       }
       
       /**
  @@ -211,15 +240,31 @@
        */
       public void removeEntry( String regName, String entryName )
       {
  -        try
  +        if (entryName==null)
           {
  -            ((Registry)registries.get( regName )).removeEntry( entryName );
  +            return;
           }
  -        catch ( NullPointerException e )
  +        
  +        Registry registry = (Registry)registries.get( regName );
  +        
  +        if (registry!=null)
           {
  -            Log.error(regName + "registry is not known ");
  -            Log.error( e );
  -        }
  +            String fragmentName = (String)entryIndex.get( entryName );
  +            
  +            if (fragmentName != null)
  +            {
  +                RegistryFragment fragment = (RegistryFragment)fragments.get( fragmentName );
  +            
  +                synchronized (entryIndex)
  +                {
  +                    fragment.removeEntry(regName,entryName);
  +                    entryIndex.remove( entryName );
  +                }
  +            }
  +            
  +            // the entry is physically removed, remove the dangling reference
  +            registry.removeEntry( entryName );
  +        }        
       }
           
       /**
  @@ -232,22 +277,52 @@
           ResourceService serviceConf = ((TurbineServices)TurbineServices.getInstance())
                                                        .getResources(RegistryService.SERVICE_NAME);
           String mapFile = null;
  -        Vector names = null;
  +        Vector names = new Vector();
           int refreshRate = 0;
           
  +        // read the configuration keys
           try
           {
               directory = serviceConf.getString("directory");
               mapFile = serviceConf.getString("mapping",DEFAULT_MAPPING);
               extension = serviceConf.getString("extension",DEFAULT_EXTENSION);
               refreshRate = serviceConf.getInt("refreshRate",DEFAULT_REFRESH);
  -            names = serviceConf.getVector("registries");
           }
           catch ( Throwable t )
           {
               throw new InitializationException( "Unable to initialize CastorRegistryService, missing config keys");
           }
               
  +        // build the map of default fragments, eahc registry must be associated 
  +        // with at least one fragment
  +        try
  +        {
  +            ResourceService defaults = serviceConf.getResources("default");
  +            Iterator i = defaults.getKeys();
  +            while(i.hasNext())
  +            {
  +                String name = (String)i.next();
  +                String fragmentFileName = defaults.getString(name);
  +                
  +                String absFileName = new File(directory,fragmentFileName+extension).getAbsolutePath();
  +                // add this name in the list of available registries
  +                names.add(name);
  +                
  +                // store the default file mapping
  +                this.defaults.put(name,absFileName);
  +            }
  +        }
  +        catch (Exception e)
  +        {
  +            Log.error("Registry init error", e);
  +            throw new InitializationException( "Unable to initialize CastorRegistryService, invalid registries definition");
  +        }
  +        
  +        // create the serializer output format        
  +        this.format = new OutputFormat();
  +        format.setIndenting(true);
  +        format.setIndent(4);
  +
           // test the mapping file and create the mapping object
           
           if (mapFile != null)
  @@ -311,22 +386,28 @@
           //Mark that we are done
           setInit(true);
   
  -        Log.note( "Registry early init()....end!");
  +        Log.debug( "Registry early init()....end!");
       }
   
   
  -    public void init( ) throws InitializationException {
  -        Log.note( "Late init for Registry called" );
  -        while( !getInit() ) {
  +    /** Late init method from Turbine Service model */
  +    public void init( ) throws InitializationException
  +    {
  +        Log.debug( "Late init for Registry called" );
  +        while( !getInit() )
  +        {
               //Not yet...
  -            try {
  +            try
  +            {
                   Thread.sleep( 500 );
  -                Log.note( "Waiting for init of Registry..." );
  -            } catch (InterruptedException ie ) {
  +                Log.debug( "Waiting for init of Registry..." );
  +            }
  +            catch (InterruptedException ie )
  +            {
                   Log.error( ie );
               }
           }
  -        Log.note( "We are done! (Registry)" );
  +        Log.debug( "We are done! (Registry)" );
       }
   
       
  @@ -334,16 +415,15 @@
        * This is the shutdown method called by the 
        * Turbine <code>Service</code> framework
        */
  -    public void shutdown() {
  +    public void shutdown()
  +    {
           this.watcher.setDone();
           
  -/*
  -        Enumeration en = fragments.keys();
  -        while (en.hasMoreElements())
  +        Iterator i = fragments.keySet().iterator();
  +        while (i.hasNext())
           {
  -            saveFragment((String)en.nextElement());
  +            saveFragment((String)i.next());
           }
  -*/
       }
   
       // FileRegistry interface
  @@ -351,7 +431,8 @@
       /** Refresh the state of the registry implementation. Should be called 
        *   whenever the underlying fragments are modified
        */
  -    public void refresh() {
  +    public void refresh()
  +    {
           Enumeration en = getNames();
           while(en.hasMoreElements())
           {
  @@ -371,7 +452,10 @@
               Unmarshaller unmarshaller = new Unmarshaller(this.mapping);
               reader = new FileReader(file);
               RegistryFragment fragment = (RegistryFragment)unmarshaller.unmarshal(reader);
  -            fragments.put(file,fragment);
  +
  +            // if we get here, we successfully loaded the new fragment            
  +            updateFragment(file, fragment);
  +            
           }
           catch ( Throwable t )
           {
  @@ -400,14 +484,11 @@
               Unmarshaller unmarshaller = new Unmarshaller(this.mapping);
               RegistryFragment fragment = (RegistryFragment)unmarshaller.unmarshal(reader);
   
  -            fragments.put(file,fragment);
  +            updateFragment(file,fragment);
   
               if (persistent)
               {
  -                writer = new FileWriter(file);
  -                Marshaller marshaller = new Marshaller(writer);
  -                marshaller.setMapping(this.mapping);
  -                marshaller.marshal(fragment);
  +                saveFragment(file);
               }
           }
           catch ( Throwable t )
  @@ -437,7 +518,8 @@
               try
               {
                   writer = new FileWriter(file);
  -                Marshaller marshaller = new Marshaller(writer);
  +                Serializer serializer = new XMLSerializer(writer, format);                
  +                Marshaller marshaller = new Marshaller(serializer.asDocumentHandler());
                   marshaller.setMapping(this.mapping);
                   marshaller.marshal(fragment);
               }
  @@ -461,26 +543,74 @@
           
           if (fragment != null)
           {
  -            // make sure the keys & entries are freed for this fragment
  -            // only the entries not replaced by the next registry refresh will
  -            // stay in memory
  -            fragment.clear();
  -            // remove the actual fragment from memory
  -            fragments.remove(file);
  +            synchronized (entryIndex)
  +            {
  +                // clear the entry index
  +                Iterator i = entryIndex.keySet().iterator();
  +                while(i.hasNext())
  +                {
  +                    if (file.equals(i.next()))
  +                    {
  +                        i.remove();
  +                    }
  +                }
  +                
  +                // make sure the keys & entries are freed for this fragment
  +                // only the entries not replaced by the next registry refresh will
  +                // stay in memory
  +                fragment.clear();
  +                // remove the actual fragment from memory
  +                fragments.remove(file);
  +            }
           }
       }    
   
       // Implementation specific methods
       
       /**
  +     * Updates a fragment in storage and the associated entryIndex
  +     */
  +    protected void updateFragment(String name, RegistryFragment fragment)
  +    {
  +        synchronized (entryIndex)
  +        {
  +            // remove the old keys
  +            Iterator i = entryIndex.keySet().iterator();
  +            while(i.hasNext())
  +            {
  +                if (name.equals(i.next()))
  +                {
  +                    i.remove();
  +                }
  +            }
  +            
  +            // store the new fragment
  +            fragments.put(name,fragment);
  +            
  +            // recreate the index entries
  +            
  +            Iterator reg = registries.keySet().iterator();
  +            while(reg.hasNext())
  +            {
  +                i = fragment.getEntries((String)reg.next()).iterator();
  +                while(i.hasNext())
  +                {
  +                    entryIndex.put(i.next(), name);
  +                }
  +            }
  +        }            
  +    }
  +    
  +    /**
        * Scan all the registry fragments for new entries relevant to
        * this registry and update its definition.
        *
        * @param name the name of the Registry to refresh
        */
  -    protected void refresh( String regName ) {
  +    protected void refresh( String regName )
  +    {
   
  -        Log.note( "Updating the "+regName+" registry" ); 
  +        Log.debug( "Updating the "+regName+" registry" ); 
   
           int count = 0;
           Registry registry = get(regName);
  @@ -497,6 +627,7 @@
               String location = (String)en.nextElement();
               RegistryFragment fragment = (RegistryFragment)fragments.get(location);
               Vector entries = fragment.getEntries(regName);
  +
               //... if it has entries related to this regsistry,
               if (entries != null)
               {
  @@ -508,14 +639,6 @@
                       // update or add the entry in the registry
                       try 
                       {
  -                        if (entry instanceof PortletEntry)
  -                        {
  -                            if (PortletEntry.TYPE_REF.equals(((PortletEntry)entry).getType()))
  -                            {
  -                                entry = new NormalizedPortletEntry((PortletEntry)entry);
  -                            }
  -                        }
  -
                           Log.debug("Adding entry "+entry.getName()+" of class "+entry.getClass()+" to registry "+name);
   
                           if (registry.hasEntry(entry.getName()))
  @@ -526,6 +649,7 @@
                           {
                               registry.addEntry(entry);
                           }
  +
                           ++count;
                       }
                       catch (RegistryException e)
  @@ -536,8 +660,7 @@
               }
           }
           
  -        Log.note( "Merged " + count + " entries into the " + name ); 
  -
  +        Log.debug( "Merged " + count + " entries into the " + name ); 
       }        
   
       
  
  
  
  1.4       +58 -1     jakarta-jetspeed/src/java/org/apache/jetspeed/services/registry/RegistryFragment.java
  
  Index: RegistryFragment.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/services/registry/RegistryFragment.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- RegistryFragment.java	2001/05/08 14:23:50	1.3
  +++ RegistryFragment.java	2001/05/08 19:09:13	1.4
  @@ -55,15 +55,17 @@
   package org.apache.jetspeed.services.registry;
   
   import org.apache.jetspeed.services.Registry;
  +import org.apache.jetspeed.om.newregistry.RegistryEntry;
   import java.util.Vector;
   import java.util.Hashtable;
  +import java.util.Iterator;
   
   /**
    * Bean like implementation of a multi-object registry usable
    * by Castor XML serialization
    *
    * @author <a href="mailto:raphael@apache.org">Rapha�l Luta</a>
  - * @version $Id: RegistryFragment.java,v 1.3 2001/05/08 14:23:50 raphael Exp $
  + * @version $Id: RegistryFragment.java,v 1.4 2001/05/08 19:09:13 raphael Exp $
    */
   public class RegistryFragment extends Hashtable implements java.io.Serializable {
   
  @@ -88,6 +90,61 @@
           return new Vector();
       }
                
  +    /** Add a new entry in the fragment. It does not check for name
  +     *  duplication
  +     *  @param name a valid Registry name.
  +     *  @param entry the entry to add
  +     */
  +    public void addEntry(String name, RegistryEntry entry)
  +    {
  +        if ( (name != null) && (entry != null) )
  +        {
  +            Vector registry = (Vector)get(name);
  +            
  +            if (registry != null)
  +            {
  +                registry.add(entry);
  +            }
  +        }
  +    }
  +             
  +    /** Remove an existing entry in the fragment.
  +     *  @param name a valid Registry name.
  +     *  @param entryName the name of the entry to remove
  +     */
  +    public void removeEntry(String name, String entryName)
  +    {
  +        if ( (name != null) && (entryName != null) )
  +        {
  +            Vector registry = (Vector)get(name);
  +            if (registry != null)
  +            {
  +                Iterator i = registry.iterator();
  +                while(i.hasNext())
  +                {
  +                    RegistryEntry regEntry = (RegistryEntry)i.next();
  +                    if (entryName.equals(regEntry.getName()))
  +                    {
  +                        i.remove();
  +                    }
  +                }
  +            }
  +        }
  +    }
  +    
  +    /** Modify an existing entry in the fragment.
  +     *  @param name a valid Registry name.
  +     *  @param entry the entry to add
  +     */
  +    public void setEntry(String name, RegistryEntry entry)
  +    {
  +        if (entry!=null)
  +        {
  +            removeEntry(name,entry.getName());
  +            addEntry(name,entry);
  +        }
  +    }
  +                          
       // Castor serialization support methods
       
       public Vector getPortlets()
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jetspeed-dev-help@jakarta.apache.org