1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.connector.infinispan;
25
26 import java.io.ByteArrayInputStream;
27 import java.io.ObjectInputStream;
28 import java.util.Enumeration;
29 import java.util.HashMap;
30 import java.util.Hashtable;
31 import java.util.List;
32 import java.util.Map;
33 import javax.naming.BinaryRefAddr;
34 import javax.naming.Context;
35 import javax.naming.RefAddr;
36 import javax.naming.Reference;
37 import javax.naming.Referenceable;
38 import javax.naming.StringRefAddr;
39 import net.jcip.annotations.ThreadSafe;
40 import org.infinispan.client.hotrod.RemoteCacheManager;
41 import org.infinispan.manager.CacheContainer;
42 import org.modeshape.common.annotation.Category;
43 import org.modeshape.common.annotation.Description;
44 import org.modeshape.common.annotation.Label;
45 import org.modeshape.common.util.HashCode;
46 import org.modeshape.common.util.StringUtil;
47 import org.modeshape.graph.cache.CachePolicy;
48 import org.modeshape.graph.connector.RepositorySource;
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 @ThreadSafe
64 public class RemoteInfinispanSource extends BaseInfinispanSource {
65 private static final long serialVersionUID = 1L;
66
67 protected static final String INFINISPAN_SERVER_LIST = "remoteInfinispanServerList";
68
69 @Description( i18n = InfinispanConnectorI18n.class, value = "remoteInfinispanServerListPropertyDescription" )
70 @Label( i18n = InfinispanConnectorI18n.class, value = "remoteInfinispanServerListPropertyLabel" )
71 @Category( i18n = InfinispanConnectorI18n.class, value = "remoteInfinispanServerListPropertyCategory" )
72 private volatile String remoteInfinispanServerList;
73
74
75
76
77
78
79
80
81 public String getRemoteInfinispanServerList() {
82 return remoteInfinispanServerList;
83 }
84
85
86
87
88
89
90
91
92 public synchronized void setRemoteInfinispanServerList( String remoteInfinispanServerList ) {
93 if (this.remoteInfinispanServerList == remoteInfinispanServerList || this.remoteInfinispanServerList != null
94 && this.remoteInfinispanServerList.equals(remoteInfinispanServerList)) return;
95 this.remoteInfinispanServerList = remoteInfinispanServerList;
96 }
97
98 @Override
99 protected CacheContainer createCacheContainer() {
100 if (this.getRemoteInfinispanServerList() == null || this.getRemoteInfinispanServerList().equals("")) {
101 return new RemoteCacheManager();
102 }
103 return new RemoteCacheManager(this.getRemoteInfinispanServerList());
104 }
105
106
107
108
109 @Override
110 public synchronized Reference getReference() {
111 Reference ref = super.getReference();
112 ref.add(new StringRefAddr(INFINISPAN_SERVER_LIST, getRemoteInfinispanServerList()));
113 return ref;
114 }
115
116
117
118
119 @Override
120 public Object getObjectInstance( Object obj,
121 javax.naming.Name name,
122 Context nameCtx,
123 Hashtable<?, ?> environment ) throws Exception {
124 if (obj instanceof Reference) {
125 Map<String, Object> values = new HashMap<String, Object>();
126 Reference ref = (Reference)obj;
127 Enumeration<?> en = ref.getAll();
128 while (en.hasMoreElements()) {
129 RefAddr subref = (RefAddr)en.nextElement();
130 if (subref instanceof StringRefAddr) {
131 String key = subref.getType();
132 Object value = subref.getContent();
133 if (value != null) values.put(key, value.toString());
134 } else if (subref instanceof BinaryRefAddr) {
135 String key = subref.getType();
136 Object value = subref.getContent();
137 if (value instanceof byte[]) {
138
139 ByteArrayInputStream bais = new ByteArrayInputStream((byte[])value);
140 ObjectInputStream ois = new ObjectInputStream(bais);
141 value = ois.readObject();
142 values.put(key, value);
143 }
144 }
145 }
146 String sourceName = (String)values.get(SOURCE_NAME);
147 String rootNodeUuidString = (String)values.get(ROOT_NODE_UUID);
148 String remoteServerList = (String)values.get(INFINISPAN_SERVER_LIST);
149 Object defaultCachePolicy = values.get(DEFAULT_CACHE_POLICY);
150 String retryLimit = (String)values.get(RETRY_LIMIT);
151 String defaultWorkspace = (String)values.get(DEFAULT_WORKSPACE);
152 String createWorkspaces = (String)values.get(ALLOW_CREATING_WORKSPACES);
153 String updatesAllowed = (String)values.get(UPDATES_ALLOWED);
154
155 String combinedWorkspaceNames = (String)values.get(PREDEFINED_WORKSPACE_NAMES);
156 String[] workspaceNames = null;
157 if (combinedWorkspaceNames != null) {
158 List<String> paths = StringUtil.splitLines(combinedWorkspaceNames);
159 workspaceNames = paths.toArray(new String[paths.size()]);
160 }
161
162
163 RemoteInfinispanSource source = new RemoteInfinispanSource();
164 if (sourceName != null) source.setName(sourceName);
165 if (rootNodeUuidString != null) source.setRootNodeUuid(rootNodeUuidString);
166 if (remoteServerList != null) source.setRemoteInfinispanServerList(remoteServerList);
167 if (defaultCachePolicy instanceof CachePolicy) {
168 source.setDefaultCachePolicy((CachePolicy)defaultCachePolicy);
169 }
170 if (retryLimit != null) source.setRetryLimit(Integer.parseInt(retryLimit));
171 if (defaultWorkspace != null) source.setDefaultWorkspaceName(defaultWorkspace);
172 if (createWorkspaces != null) source.setCreatingWorkspacesAllowed(Boolean.parseBoolean(createWorkspaces));
173 if (workspaceNames != null && workspaceNames.length != 0) source.setPredefinedWorkspaceNames(workspaceNames);
174 if (updatesAllowed != null) source.setUpdatesAllowed(Boolean.valueOf(updatesAllowed));
175 return source;
176 }
177 return null;
178 }
179
180
181
182
183 @Override
184 public boolean equals( Object obj ) {
185 if (obj == this) return true;
186 if (obj instanceof RemoteInfinispanSource) {
187 RemoteInfinispanSource that = (RemoteInfinispanSource)obj;
188 if (this.getName() == null) {
189 if (that.getName() != null) return false;
190 } else {
191 if (!this.getName().equals(that.getName())) return false;
192 }
193 return true;
194 }
195 return false;
196 }
197
198 @Override
199 public int hashCode() {
200 return HashCode.compute(getName());
201 }
202
203 }