Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Pierre De Rop | e8c339f | 2009-12-04 22:51:51 +0000 | [diff] [blame] | 19 | package org.apache.felix.dm.impl.dependencies; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 20 | |
Marcel Offermans | 26081d3 | 2010-07-12 12:43:42 +0000 | [diff] [blame] | 21 | import java.lang.reflect.InvocationTargetException; |
Marcel Offermans | a8c213f | 2010-07-02 17:27:20 +0000 | [diff] [blame] | 22 | import java.net.URL; |
Marcel Offermans | 203bdad | 2009-12-04 09:23:04 +0000 | [diff] [blame] | 23 | import java.util.ArrayList; |
Marcel Offermans | 117aa2f | 2009-12-10 09:48:17 +0000 | [diff] [blame] | 24 | import java.util.Dictionary; |
Marcel Offermans | 203bdad | 2009-12-04 09:23:04 +0000 | [diff] [blame] | 25 | import java.util.List; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 26 | import java.util.Properties; |
| 27 | |
Marcel Offermans | 8b93efa | 2010-07-02 18:27:21 +0000 | [diff] [blame] | 28 | import org.apache.felix.dm.Dependency; |
| 29 | import org.apache.felix.dm.ResourceDependency; |
| 30 | import org.apache.felix.dm.ResourceHandler; |
| 31 | import org.apache.felix.dm.Service; |
| 32 | import org.apache.felix.dm.ServiceComponentDependency; |
Marcel Offermans | 26081d3 | 2010-07-12 12:43:42 +0000 | [diff] [blame] | 33 | import org.apache.felix.dm.impl.InvocationUtil; |
Pierre De Rop | e8c339f | 2009-12-04 22:51:51 +0000 | [diff] [blame] | 34 | import org.apache.felix.dm.impl.Logger; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 35 | import org.osgi.framework.BundleContext; |
| 36 | import org.osgi.framework.ServiceRegistration; |
Marcel Offermans | 26081d3 | 2010-07-12 12:43:42 +0000 | [diff] [blame] | 37 | import org.osgi.service.log.LogService; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 38 | |
Marcel Offermans | b1959f4 | 2010-07-01 12:23:51 +0000 | [diff] [blame] | 39 | public class ResourceDependencyImpl extends DependencyBase implements ResourceDependency, ResourceHandler, DependencyActivation, ServiceComponentDependency { |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 40 | private volatile BundleContext m_context; |
| 41 | private volatile ServiceRegistration m_registration; |
Marcel Offermans | b99e7e4 | 2009-12-10 09:11:32 +0000 | [diff] [blame] | 42 | // private long m_resourceCounter; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 43 | |
| 44 | private Object m_callbackInstance; |
| 45 | private String m_callbackAdded; |
| 46 | private String m_callbackChanged; |
| 47 | private String m_callbackRemoved; |
| 48 | private boolean m_autoConfig; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 49 | private String m_autoConfigInstance; |
Marcel Offermans | 203bdad | 2009-12-04 09:23:04 +0000 | [diff] [blame] | 50 | protected List m_services = new ArrayList(); |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 51 | private String m_resourceFilter; |
Marcel Offermans | a8c213f | 2010-07-02 17:27:20 +0000 | [diff] [blame] | 52 | private URL m_trackedResource; |
Marcel Offermans | 203bdad | 2009-12-04 09:23:04 +0000 | [diff] [blame] | 53 | private boolean m_isStarted; |
Marcel Offermans | b99e7e4 | 2009-12-10 09:11:32 +0000 | [diff] [blame] | 54 | private List m_resources = new ArrayList(); |
Marcel Offermans | a8c213f | 2010-07-02 17:27:20 +0000 | [diff] [blame] | 55 | private URL m_resourceInstance; |
Marcel Offermans | 117aa2f | 2009-12-10 09:48:17 +0000 | [diff] [blame] | 56 | private boolean m_propagate; |
Marcel Offermans | 26081d3 | 2010-07-12 12:43:42 +0000 | [diff] [blame] | 57 | private Object m_propagateCallbackInstance; |
| 58 | private String m_propagateCallbackMethod; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 59 | |
Pierre De Rop | e8c339f | 2009-12-04 22:51:51 +0000 | [diff] [blame] | 60 | public ResourceDependencyImpl(BundleContext context, Logger logger) { |
Marcel Offermans | 61a8114 | 2010-04-02 15:16:50 +0000 | [diff] [blame] | 61 | super(logger); |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 62 | m_context = context; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 63 | m_autoConfig = true; |
| 64 | } |
| 65 | |
Marcel Offermans | b1959f4 | 2010-07-01 12:23:51 +0000 | [diff] [blame] | 66 | public ResourceDependencyImpl(ResourceDependencyImpl prototype) { |
| 67 | super(prototype); |
| 68 | m_context = prototype.m_context; |
| 69 | m_autoConfig = prototype.m_autoConfig; |
| 70 | m_callbackInstance = prototype.m_callbackInstance; |
| 71 | m_callbackAdded = prototype.m_callbackAdded; |
| 72 | m_callbackChanged = prototype.m_callbackChanged; |
| 73 | m_callbackRemoved = prototype.m_callbackRemoved; |
| 74 | m_autoConfigInstance = prototype.m_autoConfigInstance; |
| 75 | m_resourceFilter = prototype.m_resourceFilter; |
| 76 | m_trackedResource = prototype.m_trackedResource; |
| 77 | m_propagate = prototype.m_propagate; |
| 78 | } |
| 79 | |
| 80 | public Dependency createCopy() { |
| 81 | return new ResourceDependencyImpl(this); |
| 82 | } |
| 83 | |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 84 | public synchronized boolean isAvailable() { |
Marcel Offermans | b99e7e4 | 2009-12-10 09:11:32 +0000 | [diff] [blame] | 85 | return m_resources.size() > 0; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 88 | public void start(DependencyService service) { |
Marcel Offermans | 203bdad | 2009-12-04 09:23:04 +0000 | [diff] [blame] | 89 | boolean needsStarting = false; |
| 90 | synchronized (this) { |
| 91 | m_services.add(service); |
| 92 | if (!m_isStarted) { |
| 93 | m_isStarted = true; |
| 94 | needsStarting = true; |
| 95 | } |
| 96 | } |
| 97 | if (needsStarting) { |
Marcel Offermans | 266eee5 | 2009-12-08 10:56:09 +0000 | [diff] [blame] | 98 | Properties props = null; |
| 99 | if (m_resourceFilter != null) { |
| 100 | props = new Properties(); |
Marcel Offermans | a8c213f | 2010-07-02 17:27:20 +0000 | [diff] [blame] | 101 | props.setProperty(ResourceHandler.FILTER, m_resourceFilter); |
Marcel Offermans | 266eee5 | 2009-12-08 10:56:09 +0000 | [diff] [blame] | 102 | } |
Marcel Offermans | 203bdad | 2009-12-04 09:23:04 +0000 | [diff] [blame] | 103 | m_registration = m_context.registerService(ResourceHandler.class.getName(), this, props); |
| 104 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | public void stop(DependencyService service) { |
Marcel Offermans | 203bdad | 2009-12-04 09:23:04 +0000 | [diff] [blame] | 108 | boolean needsStopping = false; |
| 109 | synchronized (this) { |
| 110 | if (m_services.size() == 1 && m_services.contains(service)) { |
| 111 | m_isStarted = false; |
| 112 | needsStopping = true; |
| 113 | m_services.remove(service); |
| 114 | } |
| 115 | } |
| 116 | if (needsStopping) { |
| 117 | m_registration.unregister(); |
| 118 | m_registration = null; |
| 119 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Marcel Offermans | a8c213f | 2010-07-02 17:27:20 +0000 | [diff] [blame] | 122 | public void added(URL resource) { |
Marcel Offermans | b99e7e4 | 2009-12-10 09:11:32 +0000 | [diff] [blame] | 123 | if (m_trackedResource == null || m_trackedResource.equals(resource)) { |
| 124 | long counter; |
| 125 | Object[] services; |
| 126 | synchronized (this) { |
| 127 | m_resources.add(resource); |
| 128 | counter = m_resources.size(); |
| 129 | services = m_services.toArray(); |
| 130 | } |
| 131 | for (int i = 0; i < services.length; i++) { |
| 132 | DependencyService ds = (DependencyService) services[i]; |
| 133 | if (counter == 1) { |
| 134 | ds.dependencyAvailable(this); |
| 135 | if (!isRequired()) { |
| 136 | invokeAdded(ds, resource); |
| 137 | } |
| 138 | } |
| 139 | else { |
| 140 | ds.dependencyChanged(this); |
Marcel Offermans | 203bdad | 2009-12-04 09:23:04 +0000 | [diff] [blame] | 141 | invokeAdded(ds, resource); |
| 142 | } |
| 143 | } |
Marcel Offermans | b99e7e4 | 2009-12-10 09:11:32 +0000 | [diff] [blame] | 144 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Marcel Offermans | a8c213f | 2010-07-02 17:27:20 +0000 | [diff] [blame] | 147 | public void changed(URL resource) { |
Marcel Offermans | b99e7e4 | 2009-12-10 09:11:32 +0000 | [diff] [blame] | 148 | if (m_trackedResource == null || m_trackedResource.equals(resource)) { |
| 149 | Object[] services; |
| 150 | synchronized (this) { |
| 151 | services = m_services.toArray(); |
| 152 | } |
| 153 | for (int i = 0; i < services.length; i++) { |
| 154 | DependencyService ds = (DependencyService) services[i]; |
| 155 | invokeChanged(ds, resource); |
| 156 | } |
Marcel Offermans | 203bdad | 2009-12-04 09:23:04 +0000 | [diff] [blame] | 157 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Marcel Offermans | a8c213f | 2010-07-02 17:27:20 +0000 | [diff] [blame] | 160 | public void removed(URL resource) { |
Marcel Offermans | b99e7e4 | 2009-12-10 09:11:32 +0000 | [diff] [blame] | 161 | if (m_trackedResource == null || m_trackedResource.equals(resource)) { |
| 162 | long counter; |
| 163 | Object[] services; |
| 164 | synchronized (this) { |
| 165 | m_resources.remove(resource); |
| 166 | counter = m_resources.size(); |
| 167 | services = m_services.toArray(); |
| 168 | } |
| 169 | for (int i = 0; i < services.length; i++) { |
| 170 | DependencyService ds = (DependencyService) services[i]; |
| 171 | if (counter == 0) { |
| 172 | ds.dependencyUnavailable(this); |
| 173 | if (!isRequired()) { |
| 174 | invokeRemoved(ds, resource); |
| 175 | } |
| 176 | } |
| 177 | else { |
| 178 | ds.dependencyChanged(this); |
Marcel Offermans | 203bdad | 2009-12-04 09:23:04 +0000 | [diff] [blame] | 179 | invokeRemoved(ds, resource); |
| 180 | } |
| 181 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Marcel Offermans | a8c213f | 2010-07-02 17:27:20 +0000 | [diff] [blame] | 185 | public void invokeAdded(DependencyService ds, URL serviceInstance) { |
Marcel Offermans | ea89b86 | 2010-06-24 13:14:43 +0000 | [diff] [blame] | 186 | invoke(ds, serviceInstance, m_callbackAdded); |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Marcel Offermans | a8c213f | 2010-07-02 17:27:20 +0000 | [diff] [blame] | 189 | public void invokeChanged(DependencyService ds, URL serviceInstance) { |
Marcel Offermans | ea89b86 | 2010-06-24 13:14:43 +0000 | [diff] [blame] | 190 | invoke(ds, serviceInstance, m_callbackChanged); |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Marcel Offermans | a8c213f | 2010-07-02 17:27:20 +0000 | [diff] [blame] | 193 | public void invokeRemoved(DependencyService ds, URL serviceInstance) { |
Marcel Offermans | ea89b86 | 2010-06-24 13:14:43 +0000 | [diff] [blame] | 194 | invoke(ds, serviceInstance, m_callbackRemoved); |
| 195 | } |
| 196 | |
Marcel Offermans | a8c213f | 2010-07-02 17:27:20 +0000 | [diff] [blame] | 197 | private void invoke(DependencyService ds, URL serviceInstance, String name) { |
Marcel Offermans | ea89b86 | 2010-06-24 13:14:43 +0000 | [diff] [blame] | 198 | if (name != null) { |
| 199 | ds.invokeCallbackMethod(getCallbackInstances(ds), name, |
Marcel Offermans | a8c213f | 2010-07-02 17:27:20 +0000 | [diff] [blame] | 200 | new Class[][] {{ Service.class, URL.class }, { Service.class, Object.class }, { Service.class }, { URL.class }, { Object.class }, {}}, |
Marcel Offermans | ea89b86 | 2010-06-24 13:14:43 +0000 | [diff] [blame] | 201 | new Object[][] {{ ds.getServiceInterface(), serviceInstance }, { ds.getServiceInterface(), serviceInstance }, { ds.getServiceInterface() }, { serviceInstance }, { serviceInstance }, {}} |
Marcel Offermans | 61a8114 | 2010-04-02 15:16:50 +0000 | [diff] [blame] | 202 | ); |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
Marcel Offermans | ea89b86 | 2010-06-24 13:14:43 +0000 | [diff] [blame] | 205 | |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 206 | /** |
| 207 | * Sets the callbacks for this service. These callbacks can be used as hooks whenever a |
| 208 | * dependency is added or removed. When you specify callbacks, the auto configuration |
| 209 | * feature is automatically turned off, because we're assuming you don't need it in this |
| 210 | * case. |
| 211 | * |
| 212 | * @param added the method to call when a service was added |
| 213 | * @param removed the method to call when a service was removed |
| 214 | * @return this service dependency |
| 215 | */ |
| 216 | public synchronized ResourceDependency setCallbacks(String added, String removed) { |
| 217 | return setCallbacks(null, added, null, removed); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Sets the callbacks for this service. These callbacks can be used as hooks whenever a |
| 222 | * dependency is added, changed or removed. When you specify callbacks, the auto |
| 223 | * configuration feature is automatically turned off, because we're assuming you don't |
| 224 | * need it in this case. |
| 225 | * |
| 226 | * @param added the method to call when a service was added |
| 227 | * @param changed the method to call when a service was changed |
| 228 | * @param removed the method to call when a service was removed |
| 229 | * @return this service dependency |
| 230 | */ |
| 231 | public synchronized ResourceDependency setCallbacks(String added, String changed, String removed) { |
| 232 | return setCallbacks(null, added, changed, removed); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Sets the callbacks for this service. These callbacks can be used as hooks whenever a |
| 237 | * dependency is added or removed. They are called on the instance you provide. When you |
| 238 | * specify callbacks, the auto configuration feature is automatically turned off, because |
| 239 | * we're assuming you don't need it in this case. |
| 240 | * |
| 241 | * @param instance the instance to call the callbacks on |
| 242 | * @param added the method to call when a service was added |
| 243 | * @param removed the method to call when a service was removed |
| 244 | * @return this service dependency |
| 245 | */ |
| 246 | public synchronized ResourceDependency setCallbacks(Object instance, String added, String removed) { |
| 247 | return setCallbacks(instance, added, null, removed); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Sets the callbacks for this service. These callbacks can be used as hooks whenever a |
| 252 | * dependency is added, changed or removed. They are called on the instance you provide. When you |
| 253 | * specify callbacks, the auto configuration feature is automatically turned off, because |
| 254 | * we're assuming you don't need it in this case. |
| 255 | * |
| 256 | * @param instance the instance to call the callbacks on |
| 257 | * @param added the method to call when a service was added |
| 258 | * @param changed the method to call when a service was changed |
| 259 | * @param removed the method to call when a service was removed |
| 260 | * @return this service dependency |
| 261 | */ |
| 262 | public synchronized ResourceDependency setCallbacks(Object instance, String added, String changed, String removed) { |
| 263 | ensureNotActive(); |
| 264 | // if at least one valid callback is specified, we turn off auto configuration |
| 265 | if (added != null || removed != null || changed != null) { |
| 266 | setAutoConfig(false); |
| 267 | } |
| 268 | m_callbackInstance = instance; |
| 269 | m_callbackAdded = added; |
| 270 | m_callbackChanged = changed; |
| 271 | m_callbackRemoved = removed; |
| 272 | return this; |
| 273 | } |
| 274 | |
| 275 | private void ensureNotActive() { |
| 276 | if (m_registration != null) { |
| 277 | throw new IllegalStateException("Cannot modify state while active."); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Sets auto configuration for this service. Auto configuration allows the |
| 283 | * dependency to fill in any attributes in the service implementation that |
| 284 | * are of the same type as this dependency. Default is on. |
| 285 | * |
| 286 | * @param autoConfig the value of auto config |
| 287 | * @return this service dependency |
| 288 | */ |
| 289 | public synchronized ResourceDependency setAutoConfig(boolean autoConfig) { |
| 290 | ensureNotActive(); |
| 291 | m_autoConfig = autoConfig; |
| 292 | return this; |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Sets auto configuration for this service. Auto configuration allows the |
| 297 | * dependency to fill in the attribute in the service implementation that |
| 298 | * has the same type and instance name. |
| 299 | * |
| 300 | * @param instanceName the name of attribute to auto config |
| 301 | * @return this service dependency |
| 302 | */ |
| 303 | public synchronized ResourceDependency setAutoConfig(String instanceName) { |
| 304 | ensureNotActive(); |
| 305 | m_autoConfig = (instanceName != null); |
| 306 | m_autoConfigInstance = instanceName; |
| 307 | return this; |
| 308 | } |
| 309 | |
Marcel Offermans | 61a8114 | 2010-04-02 15:16:50 +0000 | [diff] [blame] | 310 | // private void invokeCallbackMethod(Object[] instances, String methodName, Object service) { |
| 311 | // for (int i = 0; i < instances.length; i++) { |
| 312 | // try { |
| 313 | // invokeCallbackMethod(instances[i], methodName, service); |
| 314 | // } |
| 315 | // catch (NoSuchMethodException e) { |
| 316 | // m_logger.log(Logger.LOG_DEBUG, "Method '" + methodName + "' does not exist on " + instances[i] + ". Callback skipped."); |
| 317 | // } |
| 318 | // } |
| 319 | // } |
| 320 | // |
| 321 | // private void invokeCallbackMethod(Object instance, String methodName, Object service) throws NoSuchMethodException { |
| 322 | // Class currentClazz = instance.getClass(); |
| 323 | // boolean done = false; |
| 324 | // while (!done && currentClazz != null) { |
| 325 | // done = invokeMethod(instance, currentClazz, methodName, |
| 326 | // new Class[][] {{Resource.class}, {Object.class}, {}}, |
| 327 | // new Object[][] {{service}, {service}, {}}, |
| 328 | // false); |
| 329 | // if (!done) { |
| 330 | // currentClazz = currentClazz.getSuperclass(); |
| 331 | // } |
| 332 | // } |
| 333 | // if (!done && currentClazz == null) { |
| 334 | // throw new NoSuchMethodException(methodName); |
| 335 | // } |
| 336 | // } |
| 337 | // |
| 338 | // private boolean invokeMethod(Object object, Class clazz, String name, Class[][] signatures, Object[][] parameters, boolean isSuper) { |
| 339 | // Method m = null; |
| 340 | // for (int i = 0; i < signatures.length; i++) { |
| 341 | // Class[] signature = signatures[i]; |
| 342 | // try { |
| 343 | // m = clazz.getDeclaredMethod(name, signature); |
| 344 | // if (!(isSuper && Modifier.isPrivate(m.getModifiers()))) { |
| 345 | // m.setAccessible(true); |
| 346 | // try { |
| 347 | // m.invoke(object, parameters[i]); |
| 348 | // } |
| 349 | // catch (InvocationTargetException e) { |
| 350 | // m_logger.log(Logger.LOG_ERROR, "Exception while invoking method " + m + ".", e); |
| 351 | // } |
| 352 | // // we did find and invoke the method, so we return true |
| 353 | // return true; |
| 354 | // } |
| 355 | // } |
| 356 | // catch (NoSuchMethodException e) { |
| 357 | // // ignore this and keep looking |
| 358 | // } |
| 359 | // catch (Exception e) { |
| 360 | // // could not even try to invoke the method |
| 361 | // m_logger.log(Logger.LOG_ERROR, "Exception while trying to invoke method " + m + ".", e); |
| 362 | // } |
| 363 | // } |
| 364 | // return false; |
| 365 | // } |
Marcel Offermans | 203bdad | 2009-12-04 09:23:04 +0000 | [diff] [blame] | 366 | |
| 367 | private synchronized Object[] getCallbackInstances(DependencyService ds) { |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 368 | if (m_callbackInstance == null) { |
Marcel Offermans | 203bdad | 2009-12-04 09:23:04 +0000 | [diff] [blame] | 369 | return ds.getCompositionInstances(); |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 370 | } |
Marcel Offermans | 203bdad | 2009-12-04 09:23:04 +0000 | [diff] [blame] | 371 | else { |
| 372 | return new Object[] { m_callbackInstance }; |
| 373 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Marcel Offermans | a8c213f | 2010-07-02 17:27:20 +0000 | [diff] [blame] | 376 | public ResourceDependency setResource(URL resource) { |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 377 | m_trackedResource = resource; |
| 378 | return this; |
| 379 | } |
| 380 | |
| 381 | public synchronized ResourceDependency setRequired(boolean required) { |
| 382 | ensureNotActive(); |
Marcel Offermans | 61a8114 | 2010-04-02 15:16:50 +0000 | [diff] [blame] | 383 | setIsRequired(required); |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 384 | return this; |
| 385 | } |
| 386 | |
| 387 | public ResourceDependency setFilter(String resourceFilter) { |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 388 | ensureNotActive(); |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 389 | m_resourceFilter = resourceFilter; |
| 390 | return this; |
| 391 | } |
| 392 | public synchronized boolean isAutoConfig() { |
| 393 | return m_autoConfig; |
| 394 | } |
Marcel Offermans | b99e7e4 | 2009-12-10 09:11:32 +0000 | [diff] [blame] | 395 | |
Marcel Offermans | a8c213f | 2010-07-02 17:27:20 +0000 | [diff] [blame] | 396 | public URL getResource() { |
Marcel Offermans | b99e7e4 | 2009-12-10 09:11:32 +0000 | [diff] [blame] | 397 | return lookupResource(); |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 398 | } |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 399 | |
Marcel Offermans | a8c213f | 2010-07-02 17:27:20 +0000 | [diff] [blame] | 400 | private URL lookupResource() { |
Marcel Offermans | b99e7e4 | 2009-12-10 09:11:32 +0000 | [diff] [blame] | 401 | try { |
Marcel Offermans | a8c213f | 2010-07-02 17:27:20 +0000 | [diff] [blame] | 402 | return (URL) m_resources.get(0); |
Marcel Offermans | b99e7e4 | 2009-12-10 09:11:32 +0000 | [diff] [blame] | 403 | } |
| 404 | catch (IndexOutOfBoundsException e) { |
| 405 | return null; |
| 406 | } |
| 407 | } |
| 408 | |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 409 | public Object getAutoConfigInstance() { |
Marcel Offermans | b99e7e4 | 2009-12-10 09:11:32 +0000 | [diff] [blame] | 410 | return lookupResource(); |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | public String getAutoConfigName() { |
Marcel Offermans | b99e7e4 | 2009-12-10 09:11:32 +0000 | [diff] [blame] | 414 | return m_autoConfigInstance; |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | public Class getAutoConfigType() { |
Marcel Offermans | a8c213f | 2010-07-02 17:27:20 +0000 | [diff] [blame] | 418 | return URL.class; |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | public void invokeAdded(DependencyService service) { |
Marcel Offermans | b99e7e4 | 2009-12-10 09:11:32 +0000 | [diff] [blame] | 422 | // we remember these for future reference, needed for required callbacks |
| 423 | m_resourceInstance = lookupResource(); |
| 424 | invokeAdded(service, m_resourceInstance); |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | public void invokeRemoved(DependencyService service) { |
Marcel Offermans | b99e7e4 | 2009-12-10 09:11:32 +0000 | [diff] [blame] | 428 | invokeRemoved(service, m_resourceInstance); |
| 429 | m_resourceInstance = null; |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 430 | } |
Marcel Offermans | 117aa2f | 2009-12-10 09:48:17 +0000 | [diff] [blame] | 431 | |
| 432 | public ResourceDependency setPropagate(boolean propagate) { |
| 433 | ensureNotActive(); |
| 434 | m_propagate = propagate; |
| 435 | return this; |
| 436 | } |
| 437 | |
Marcel Offermans | 26081d3 | 2010-07-12 12:43:42 +0000 | [diff] [blame] | 438 | public ResourceDependency setPropagate(Object instance, String method) { |
| 439 | setPropagate(instance != null && method != null); |
| 440 | m_propagateCallbackInstance = instance; |
| 441 | m_propagateCallbackMethod = method; |
| 442 | return this; |
| 443 | } |
| 444 | |
Marcel Offermans | 117aa2f | 2009-12-10 09:48:17 +0000 | [diff] [blame] | 445 | public Dictionary getProperties() { |
Marcel Offermans | a8c213f | 2010-07-02 17:27:20 +0000 | [diff] [blame] | 446 | URL resource = lookupResource(); |
Marcel Offermans | 117aa2f | 2009-12-10 09:48:17 +0000 | [diff] [blame] | 447 | if (resource != null) { |
Marcel Offermans | 26081d3 | 2010-07-12 12:43:42 +0000 | [diff] [blame] | 448 | if (m_propagateCallbackInstance != null && m_propagateCallbackMethod != null) { |
| 449 | try { |
| 450 | return (Dictionary) InvocationUtil.invokeCallbackMethod(m_propagateCallbackInstance, m_propagateCallbackMethod, new Class[][] {{ URL.class }}, new Object[][] {{ resource }}); |
| 451 | } |
| 452 | catch (InvocationTargetException e) { |
| 453 | m_logger.log(LogService.LOG_WARNING, "Exception while invoking callback method", e.getCause()); |
| 454 | } |
| 455 | catch (Exception e) { |
| 456 | m_logger.log(LogService.LOG_WARNING, "Exception while trying to invoke callback method", e); |
| 457 | } |
| 458 | throw new IllegalStateException("Could not invoke callback"); |
| 459 | } |
| 460 | else { |
| 461 | Properties props = new Properties(); |
| 462 | props.setProperty(ResourceHandler.HOST, resource.getHost()); |
| 463 | props.setProperty(ResourceHandler.PATH, resource.getPath()); |
| 464 | props.setProperty(ResourceHandler.PROTOCOL, resource.getProtocol()); |
| 465 | props.setProperty(ResourceHandler.PORT, Integer.toString(resource.getPort())); |
| 466 | return props; |
| 467 | } |
Marcel Offermans | 117aa2f | 2009-12-10 09:48:17 +0000 | [diff] [blame] | 468 | } |
| 469 | else { |
| 470 | throw new IllegalStateException("cannot find resource"); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | public boolean isPropagated() { |
| 475 | return m_propagate; |
| 476 | } |
Marcel Offermans | 61a8114 | 2010-04-02 15:16:50 +0000 | [diff] [blame] | 477 | |
| 478 | public ResourceDependency setInstanceBound(boolean isInstanceBound) { |
| 479 | setIsInstanceBound(isInstanceBound); |
| 480 | return this; |
| 481 | } |
Marcel Offermans | b1959f4 | 2010-07-01 12:23:51 +0000 | [diff] [blame] | 482 | |
| 483 | public String getName() { |
| 484 | StringBuilder sb = new StringBuilder(); |
| 485 | if (m_resourceFilter != null) { |
| 486 | sb.append(m_resourceFilter); |
| 487 | } |
| 488 | if (m_trackedResource != null) { |
Marcel Offermans | a8c213f | 2010-07-02 17:27:20 +0000 | [diff] [blame] | 489 | sb.append(m_trackedResource.toString()); |
Marcel Offermans | b1959f4 | 2010-07-01 12:23:51 +0000 | [diff] [blame] | 490 | } |
| 491 | return sb.toString(); |
| 492 | } |
| 493 | |
| 494 | public int getState() { |
| 495 | return (isAvailable() ? 1 : 0) + (isRequired() ? 2 : 0); |
| 496 | } |
| 497 | |
| 498 | public String getType() { |
| 499 | return "resource"; |
| 500 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 501 | } |