1
2
3
4
5 package org.epics.pvmanager.jca;
6
7 import gov.aps.jca.CAException;
8 import gov.aps.jca.Context;
9 import gov.aps.jca.JCALibrary;
10 import gov.aps.jca.Monitor;
11 import java.util.logging.Level;
12 import java.util.logging.Logger;
13 import org.epics.pvmanager.ChannelHandler;
14 import org.epics.pvmanager.DataSource;
15 import org.epics.pvmanager.vtype.DataTypeSupport;
16 import com.cosylab.epics.caj.CAJContext;
17 import gov.aps.jca.jni.JNIContext;
18 import java.lang.reflect.InvocationTargetException;
19 import java.lang.reflect.Method;
20 import java.security.AccessController;
21 import java.security.PrivilegedAction;
22 import java.util.concurrent.ExecutorService;
23 import java.util.concurrent.Executors;
24 import static org.epics.pvmanager.util.Executors.namedPool;
25
26
27
28
29
30
31
32
33
34 public class JCADataSource extends DataSource {
35
36 static {
37
38 DataTypeSupport.install();
39 }
40
41 private static final Logger log = Logger.getLogger(JCADataSource.class.getName());
42
43 private final Context ctxt;
44 private final int monitorMask;
45 private final boolean varArraySupported;
46 private final boolean dbePropertySupported;
47 private final JCATypeSupport typeSupport;
48 private final boolean rtypValueOnly;
49 private final boolean honorZeroPrecision;
50
51
52
53
54
55 public JCADataSource() {
56 this(new JCADataSourceBuilder());
57 }
58
59
60
61
62
63
64
65
66
67 @Deprecated
68 public JCADataSource(Context jcaContext, int monitorMask) {
69 this(new JCADataSourceBuilder().jcaContext(jcaContext).monitorMask(monitorMask));
70 }
71
72
73
74
75
76
77
78
79 @Deprecated
80 public JCADataSource(String className, int monitorMask) {
81 this(new JCADataSourceBuilder().jcaContextClass(className).monitorMask(monitorMask));
82 }
83
84
85
86
87
88
89
90
91
92
93
94 @Deprecated
95 public JCADataSource(Context jcaContext, int monitorMask, JCATypeSupport typeSupport) {
96 this(new JCADataSourceBuilder().jcaContext(jcaContext).monitorMask(monitorMask)
97 .typeSupport(typeSupport));
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111
112 @Deprecated
113 public JCADataSource(Context jcaContext, int monitorMask, JCATypeSupport typeSupport, boolean dbePropertySupported, boolean varArraySupported) {
114 this(new JCADataSourceBuilder().jcaContext(jcaContext).monitorMask(monitorMask)
115 .typeSupport(typeSupport).dbePropertySupported(dbePropertySupported)
116 .varArraySupported(varArraySupported));
117 }
118
119 protected JCADataSource(JCADataSourceBuilder builder) {
120 super(true);
121
122
123
124
125 if (builder.jcaContext == null) {
126 ctxt = JCADataSourceBuilder.createContext(JCALibrary.CHANNEL_ACCESS_JAVA);
127 } else {
128 ctxt = builder.jcaContext;
129 }
130
131 try {
132 if (ctxt instanceof CAJContext) {
133 ((CAJContext) ctxt).setDoNotShareChannels(true);
134 }
135 } catch (Throwable t) {
136 log.log(Level.WARNING, "Couldn't change CAJContext to doNotShareChannels: this may cause some rare notification problems.", t);
137 }
138
139
140 if (builder.typeSupport == null) {
141 typeSupport = new JCATypeSupport(new JCAVTypeAdapterSet());
142 } else {
143 typeSupport = builder.typeSupport;
144 }
145
146
147 if (builder.varArraySupported == null) {
148 varArraySupported = JCADataSourceBuilder.isVarArraySupported(ctxt);
149 } else {
150 varArraySupported = builder.varArraySupported;
151 }
152
153 monitorMask = builder.monitorMask;
154 dbePropertySupported = builder.dbePropertySupported;
155 rtypValueOnly = builder.rtypValueOnly;
156 honorZeroPrecision = builder.honorZeroPrecision;
157
158 if (useContextSwitchForAccessRightCallback()) {
159 contextSwitch = Executors.newSingleThreadExecutor(namedPool("PVMgr JCA Workaround "));
160 } else {
161 contextSwitch = null;
162 }
163 }
164
165 @Override
166 public void close() {
167 super.close();
168 ctxt.dispose();
169 }
170
171
172
173
174
175
176 public Context getContext() {
177 return ctxt;
178 }
179
180
181
182
183
184
185 public int getMonitorMask() {
186 return monitorMask;
187 }
188
189
190
191
192
193
194 public boolean isDbePropertySupported() {
195 return dbePropertySupported;
196 }
197
198 @Override
199 protected ChannelHandler createChannel(String channelName) {
200 return new JCAChannelHandler(channelName, this);
201 }
202
203 JCATypeSupport getTypeSupport() {
204 return typeSupport;
205 }
206
207
208
209
210
211
212
213 public boolean isVarArraySupported() {
214 return varArraySupported;
215 }
216
217
218
219
220
221
222 public boolean isRtypValueOnly() {
223 return rtypValueOnly;
224 }
225
226
227
228
229
230
231 public boolean isHonorZeroPrecision() {
232 return honorZeroPrecision;
233 }
234
235
236
237
238
239
240
241
242 @Deprecated
243 public static boolean isVarArraySupported(Context context) {
244 return JCADataSourceBuilder.isVarArraySupported(context);
245 }
246
247 final boolean useContextSwitchForAccessRightCallback() {
248 if (ctxt instanceof JNIContext) {
249 return true;
250 }
251 return false;
252 }
253
254 ExecutorService getContextSwitch() {
255 return contextSwitch;
256 }
257
258 private final ExecutorService contextSwitch;
259
260 }