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 | 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 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
} |
98 | 0 | return xstream; |
99 | |
} |
100 | |
|
101 | |
private static XStream xstream; |
102 | |
} |