Coverage Report - org.mule.registry.store.XmlRegistryStore
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlRegistryStore
0%
0/29
0%
0/2
2.4
 
 1  
 /*
 2  
  * $Id: XmlRegistryStore.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.registry.store;
 12  
 
 13  
 import org.mule.ManagementContext;
 14  
 import org.mule.registry.Registry;
 15  
 import org.mule.registry.RegistryException;
 16  
 import org.mule.registry.RegistryFactory;
 17  
 import org.mule.registry.RegistryStore;
 18  
 import org.mule.registry.impl.AbstractRegistry;
 19  
 import org.mule.util.FileUtils;
 20  
 
 21  
 import com.thoughtworks.xstream.XStream;
 22  
 import com.thoughtworks.xstream.io.xml.StaxDriver;
 23  
 
 24  
 import java.io.FileReader;
 25  
 import java.io.FileWriter;
 26  
 import java.io.IOException;
 27  
 import java.io.Reader;
 28  
 import java.io.Writer;
 29  
 
 30  
 public class XmlRegistryStore implements RegistryStore
 31  
 {
 32  
 
 33  
     protected ManagementContext context;
 34  
 
 35  
     public XmlRegistryStore(ManagementContext context)
 36  0
     {
 37  0
         this.context = context;
 38  0
     }
 39  
 
 40  
     public void save(Registry registry) throws RegistryException
 41  
     {
 42  0
         synchronized (registry)
 43  
         {
 44  
             try
 45  
             {
 46  0
                 Writer w = new FileWriter(FileUtils.newFile(registry.getStoreLocation()));
 47  0
                 getXStream().toXML(registry, w);
 48  0
                 w.close();
 49  
             }
 50  0
             catch (Exception e)
 51  
             {
 52  0
                 throw new RegistryException("Could not save registry", e);
 53  0
             }
 54  0
         }
 55  0
     }
 56  
 
 57  
     public Registry load(String storeLocation) throws RegistryException
 58  
     {
 59  
         try
 60  
         {
 61  0
             Reader r = new FileReader(storeLocation);
 62  0
             AbstractRegistry reg = (AbstractRegistry)getXStream().fromXML(r);
 63  0
             reg.initialize();
 64  0
             reg.setStoreLocation(storeLocation);
 65  0
             r.close();
 66  0
             return reg;
 67  
         }
 68  0
         catch (IOException e)
 69  
         {
 70  0
             throw new RegistryException("Could not load registry", e);
 71  
         }
 72  
     }
 73  
 
 74  
     public Registry create(String store, RegistryFactory factory) throws RegistryException
 75  
     {
 76  0
         Registry reg = factory.create(this, context);
 77  0
         if (reg instanceof AbstractRegistry)
 78  
         {
 79  0
             ((AbstractRegistry)reg).initialize();
 80  0
             ((AbstractRegistry)reg).setStoreLocation(store);
 81  
         }
 82  0
         save(reg);
 83  0
         return reg;
 84  
     }
 85  
 
 86  
     private static XStream getXStream()
 87  
     {
 88  0
         if (xstream == null)
 89  
         {
 90  0
             xstream = new XStream(new StaxDriver());
 91  
             // xstream.alias("registry", BaseRegistry.class);
 92  
             // xstream.alias("engine", EngineImpl.class);
 93  
             // xstream.alias("binding", BindingImpl.class);
 94  
             // xstream.alias("library", LibraryImpl.class);
 95  
             // xstream.alias("assembly", AssemblyImpl.class);
 96  
             // xstream.alias("unit", UnitImpl.class);
 97  
         }
 98  0
         return xstream;
 99  
     }
 100  
 
 101  
     private static XStream xstream;
 102  
 }