1
2
3
4
5
6
7
8
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
92
93
94
95
96
97 }
98 return xstream;
99 }
100
101 private static XStream xstream;
102 }