1 /*
2 * Copyright (c) 2006 Stiftung Deutsches Elektronen-Synchroton,
3 * Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY.
4 *
5 * THIS SOFTWARE IS PROVIDED UNDER THIS LICENSE ON AN "../AS IS" BASIS.
6 * WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
7 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR PARTICULAR PURPOSE AND
8 * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
9 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
11 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. SHOULD THE SOFTWARE PROVE DEFECTIVE
12 * IN ANY RESPECT, THE USER ASSUMES THE COST OF ANY NECESSARY SERVICING, REPAIR OR
13 * CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE.
14 * NO USE OF ANY SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
15 * DESY HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
16 * OR MODIFICATIONS.
17 * THE FULL LICENSE SPECIFYING FOR THE SOFTWARE THE REDISTRIBUTION, MODIFICATION,
18 * USAGE AND OTHER RIGHTS AND OBLIGATIONS IS INCLUDED WITH THE DISTRIBUTION OF THIS
19 * PROJECT IN THE FILE LICENSE.HTML. IF THE LICENSE IS NOT INCLUDED YOU MAY FIND A COPY
20 * AT HTTP://WWW.DESY.DE/LEGAL/LICENSE.HTM
21 */
22
23 package de.desy.acop.transport.adapters;
24
25 /*
26 * Copyright (c) 2004 by Cosylab d.o.o.
27 *
28 * The full license specifying the redistribution, modification, usage and other
29 * rights and obligations is included with the distribution of this project in
30 * the file license.html. If the license is not included you may find a copy at
31 * http://www.cosylab.com/legal/abeans_license.htm or may write to Cosylab, d.o.o.
32 *
33 * THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND, NOT EVEN THE
34 * IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE, ASSUMES
35 * _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE RESULTING FROM THE USE, MODIFICATION,
36 * OR REDISTRIBUTION OF THIS SOFTWARE.
37 */
38
39 import java.util.HashMap;
40 import java.util.Iterator;
41 import java.util.Map;
42
43 import de.desy.acop.transport.ConnectionFailed;
44 import de.desy.acop.transport.ConnectionParameters;
45
46
47 /**
48 * Default implementaion of the AdapterFactory.
49 *
50 * @author Jaka Bobnar, Cosylab
51 *
52 */
53 public class DefaultAdapterFactory implements AdapterFactory
54 {
55 private Map<ConnectionParameters, AcopTransportDataSource> adapters = new HashMap<ConnectionParameters, AcopTransportDataSource>();
56 private boolean useCache = true;
57
58 protected DefaultAdapterFactory()
59 {
60 super();
61 }
62
63 /* (non-Javadoc)
64 * @see com.cosylab.adapters.dal.AdapterFactory#createDataSource(abeans.datatypes.DynamicValueProperty)
65 */
66 public synchronized AcopTransportDataSource createDataSource(
67 ConnectionParameters param) throws ConnectionFailed
68 {
69 if (useCache) {
70 AcopTransportDataSource ad = adapters.get(param);
71
72 if (ad == null) {
73 ad = createAdapter(param);
74 adapters.put(param, ad);
75 }
76
77 ad.useCount++;
78
79 return ad;
80 }
81
82 return createAdapter(param);
83 }
84
85 /* (non-Javadoc)
86 * @see com.cosylab.adapters.dal.AdapterFactory#getDataSource(abeans.datatypes.DynamicValueProperty)
87 */
88 public synchronized AcopTransportDataSource getDataSource(
89 ConnectionParameters param) throws ConnectionFailed
90 {
91 if (useCache) {
92 AcopTransportDataSource ad = adapters.get(param);
93
94 if (ad == null) {
95 ad = createDataSource(param);
96 }
97
98 return ad;
99 }
100
101 return createAdapter(param);
102 }
103
104 /* (non-Javadoc)
105 * @see com.cosylab.adapters.dal.AdapterFactory#releaseDataSource(com.cosylab.adapters.dal.DynamicValuePropertyAdapter)
106 */
107 public synchronized void releaseDataSource(AcopTransportDataSource ds)
108 {
109 ds.useCount--;
110
111 if (ds.useCount <= 0 || !useCache) {
112 adapters.remove(ds.getConnectionParameters());
113 ds.disconnect();
114 }
115 }
116
117 /*
118 * (non-Javadoc)
119 * @see de.desy.acop.transport.adapters.AdapterFactory#releaseDataSource(de.desy.acop.transport.ConnectionParameters)
120 */
121 public synchronized AcopTransportDataSource releaseDataSource(ConnectionParameters param) {
122 AcopTransportDataSource ds = adapters.get(param);
123
124 ds.useCount--;
125
126 if (ds.useCount <= 0 || !useCache) {
127 adapters.remove(param);
128 ds.disconnect();
129 }
130
131 return ds;
132
133 }
134
135 /* (non-Javadoc)
136 * @see com.cosylab.adapters.dal.AdapterFactory#clear()
137 */
138 public synchronized void clear()
139 {
140 Iterator<AcopTransportDataSource> it = adapters.values().iterator();
141
142 while (it.hasNext()) {
143 AcopTransportDataSource element = it.next();
144 element.disconnect();
145 }
146
147 adapters.clear();
148 }
149
150 protected AcopTransportDataSource createAdapter(ConnectionParameters param)
151 throws ConnectionFailed
152 {
153 if (param == null) {
154 throw new NullPointerException("param");
155 }
156
157 AcopTransportDataSource retVal = new AcopTransportDataSource();
158 retVal.connect(param);
159
160 return retVal;
161 }
162
163 /**
164 * Returns true if caching is enabled.
165 *
166 * @return
167 * @see #setUseCache(boolean)
168 */
169 public boolean isUseCache()
170 {
171 return useCache;
172 }
173
174 /**
175 * Enables/disables caching of adapters for multiple use. If adapters are
176 * cached there is only one adapter for one ConnectionParameters. If displayers
177 * are connected to the same channel, they share the adapter.
178 *
179 * @param useCache
180 */
181 public void setUseCache(boolean useCache)
182 {
183 this.useCache = useCache;
184 }
185
186 /*
187 * (non-Javadoc)
188 * @see de.desy.acop.transport.adapters.AdapterFactory#getCharacteristics(de.desy.acop.transport.ConnectionParameters)
189 */
190 public Map<String, Object> getCharacteristics(ConnectionParameters param) throws ConnectionFailed {
191 if (param == null) return new HashMap<String, Object>(0);
192 AcopTransportDataSource ds = adapters.get(param);
193 if (ds != null) {
194 return ds.getCharacteristics();
195 }
196 return DataSourceUtilities.getCharacteristics(param);
197 }
198
199
200 }
201
202 /* __oOo__ */