KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > registry > store > XmlRegistryStore


1 /*
2  * $Id: XmlRegistryStore.java 3937 2006-11-20 16:04:25Z lajos $
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 MuleSource MPL
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 com.thoughtworks.xstream.XStream;
14 import com.thoughtworks.xstream.io.xml.StaxDriver;
15
16 import org.mule.ManagementContext;
17 import org.mule.util.FileUtils;
18 import org.mule.registry.Registry;
19 import org.mule.registry.RegistryException;
20 import org.mule.registry.RegistryFactory;
21 import org.mule.registry.RegistryStore;
22 import org.mule.registry.impl.AbstractRegistry;
23
24 import java.io.FileReader JavaDoc;
25 import java.io.FileWriter JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.Reader JavaDoc;
28 import java.io.Writer JavaDoc;
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 JavaDoc w = new FileWriter JavaDoc(FileUtils.newFile(registry.getStoreLocation()));
47                 getXStream().toXML(registry, w);
48                 w.close();
49             }
50             catch (Exception JavaDoc e)
51             {
52                 throw new RegistryException("Could not save registry", e);
53             }
54         }
55     }
56
57     public Registry load(String JavaDoc storeLocation) throws RegistryException
58     {
59         try
60         {
61             Reader JavaDoc r = new FileReader JavaDoc(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 JavaDoc e)
69         {
70             throw new RegistryException("Could not load registry", e);
71         }
72     }
73
74     public Registry create(String JavaDoc 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 }
103
Popular Tags