|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| package org.objectledge.cache; |
|
30 |
| |
|
31 |
| import org.jcontainer.dna.Configuration; |
|
32 |
| import org.objectledge.cache.spi.CacheFactorySPI; |
|
33 |
| import org.objectledge.cache.spi.ConfigurableValueFactory; |
|
34 |
| import org.objectledge.database.persistence.Persistence; |
|
35 |
| import org.objectledge.database.persistence.PersistenceException; |
|
36 |
| import org.objectledge.database.persistence.Persistent; |
|
37 |
| import org.objectledge.database.persistence.PersistentFactory; |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| public class PersistenceValueFactory |
|
46 |
| implements ConfigurableValueFactory |
|
47 |
| { |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| private PersistentFactory factory; |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| private Persistence persistence; |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
828
| public void init(final Class cl, Persistence persistence)
|
|
65 |
| { |
|
66 |
828
| this.persistence = persistence;
|
|
67 |
828
| if(!Persistent.class.isAssignableFrom(cl))
|
|
68 |
| { |
|
69 |
0
| throw new IllegalArgumentException(cl.getName()+" does not implement " +
|
|
70 |
| "Persistent interface"); |
|
71 |
| } |
|
72 |
828
| factory = new PersistentFactory()
|
|
73 |
| { |
|
74 |
0
| public Persistent newInstance()
|
|
75 |
| throws PersistenceException |
|
76 |
| { |
|
77 |
0
| try
|
|
78 |
| { |
|
79 |
0
| return (Persistent)cl.newInstance();
|
|
80 |
| } |
|
81 |
| catch(Exception e) |
|
82 |
| { |
|
83 |
0
| throw new PersistenceException("failed to instantiate "+cl.getName());
|
|
84 |
| } |
|
85 |
| } |
|
86 |
| }; |
|
87 |
| } |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
0
| public Object getValue(Object key)
|
|
99 |
| { |
|
100 |
0
| if(key instanceof Number)
|
|
101 |
| { |
|
102 |
0
| try
|
|
103 |
| { |
|
104 |
0
| return persistence.load(((Number)key).longValue(), factory);
|
|
105 |
| } |
|
106 |
| catch(PersistenceException e) |
|
107 |
| { |
|
108 |
0
| throw new RuntimeException("failed to produce value", e);
|
|
109 |
| } |
|
110 |
| } |
|
111 |
0
| throw new IllegalArgumentException(key.getClass().getName()+
|
|
112 |
| " does not extend java.lang.Number"); |
|
113 |
| } |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
782
| public void configure(CacheFactorySPI caching, String name, Configuration config)
|
|
119 |
| { |
|
120 |
782
| Configuration[] parameters = config.getChildren("parameter");
|
|
121 |
782
| String vClass = null;
|
|
122 |
782
| for(int i = 0; i < parameters.length; i++)
|
|
123 |
| { |
|
124 |
782
| if(parameters[i].getAttribute("name","").equals("valueClass"))
|
|
125 |
| { |
|
126 |
782
| vClass = parameters[i].getAttribute("value","");
|
|
127 |
| } |
|
128 |
| } |
|
129 |
782
| if(vClass == null || vClass.length()==0)
|
|
130 |
| { |
|
131 |
0
| throw new IllegalArgumentException("required parameter valueClass is missing");
|
|
132 |
| } |
|
133 |
782
| Class cl = null;
|
|
134 |
782
| try
|
|
135 |
| { |
|
136 |
782
| cl = Class.forName(vClass);
|
|
137 |
| } |
|
138 |
| catch(Exception e) |
|
139 |
| { |
|
140 |
0
| throw new IllegalArgumentException("cannot instantiate the class"+e.getMessage());
|
|
141 |
| } |
|
142 |
782
| init(cl, caching.getPersistence());
|
|
143 |
| } |
|
144 |
| } |