View Javadoc

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      {
37          this.context = context;
38      }
39  
40      public void save(Registry registry) throws RegistryException
41      {
42          synchronized (registry)
43          {
44              try
45              {
46                  Writer w = new FileWriter(FileUtils.newFile(registry.getStoreLocation()));
47                  getXStream().toXML(registry, w);
48                  w.close();
49              }
50              catch (Exception e)
51              {
52                  throw new RegistryException("Could not save registry", e);
53              }
54          }
55      }
56  
57      public Registry load(String storeLocation) throws RegistryException
58      {
59          try
60          {
61              Reader r = new FileReader(storeLocation);
62              AbstractRegistry reg = (AbstractRegistry)getXStream().fromXML(r);
63              reg.initialize();
64              reg.setStoreLocation(storeLocation);
65              r.close();
66              return reg;
67          }
68          catch (IOException e)
69          {
70              throw new RegistryException("Could not load registry", e);
71          }
72      }
73  
74      public Registry create(String store, RegistryFactory factory) throws RegistryException
75      {
76          Registry reg = factory.create(this, context);
77          if (reg instanceof AbstractRegistry)
78          {
79              ((AbstractRegistry)reg).initialize();
80              ((AbstractRegistry)reg).setStoreLocation(store);
81          }
82          save(reg);
83          return reg;
84      }
85  
86      private static XStream getXStream()
87      {
88          if (xstream == null)
89          {
90              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          return xstream;
99      }
100 
101     private static XStream xstream;
102 }