Francesco Furfari | ec7e175 | 2006-10-02 13:37:04 +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
|
Francesco Furfari | d1072b5 | 2006-05-03 07:44:48 +0000 | [diff] [blame] | 9 | *
|
Francesco Furfari | ec7e175 | 2006-10-02 13:37:04 +0000 | [diff] [blame] | 10 | * http://www.apache.org/licenses/LICENSE-2.0
|
Francesco Furfari | d1072b5 | 2006-05-03 07:44:48 +0000 | [diff] [blame] | 11 | *
|
Francesco Furfari | ec7e175 | 2006-10-02 13:37:04 +0000 | [diff] [blame] | 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.
|
Francesco Furfari | d1072b5 | 2006-05-03 07:44:48 +0000 | [diff] [blame] | 18 | */
|
Francesco Furfari | ec7e175 | 2006-10-02 13:37:04 +0000 | [diff] [blame] | 19 |
|
Francesco Furfari | d1072b5 | 2006-05-03 07:44:48 +0000 | [diff] [blame] | 20 | package org.apache.felix.upnp.sample.clock;
|
| 21 |
|
| 22 | import java.beans.PropertyChangeEvent;
|
| 23 | import java.beans.PropertyChangeListener;
|
| 24 | import java.util.Dictionary;
|
| 25 | import java.util.Iterator;
|
| 26 | import java.util.Properties;
|
| 27 | import java.util.Vector;
|
| 28 |
|
| 29 | import org.osgi.framework.BundleContext;
|
| 30 | import org.osgi.framework.Constants;
|
| 31 | import org.osgi.framework.Filter;
|
| 32 | import org.osgi.framework.ServiceEvent;
|
| 33 | import org.osgi.framework.ServiceListener;
|
| 34 | import org.osgi.framework.ServiceReference;
|
| 35 | import org.osgi.service.upnp.UPnPDevice;
|
| 36 | import org.osgi.service.upnp.UPnPEventListener;
|
| 37 | import org.osgi.service.upnp.UPnPService;
|
| 38 | import org.osgi.service.upnp.UPnPStateVariable;
|
| 39 |
|
| 40 | /*
|
| 41 | * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
|
| 42 | */
|
| 43 |
|
| 44 | public class UPnPEventNotifier implements PropertyChangeListener,ServiceListener {
|
Francesco Furfari | bc767e5 | 2006-11-12 23:10:53 +0000 | [diff] [blame^] | 45 | private BundleContext context;
|
| 46 | private UPnPDevice device;
|
| 47 | private UPnPService service;
|
| 48 | private EventSource source;
|
Francesco Furfari | d1072b5 | 2006-05-03 07:44:48 +0000 | [diff] [blame] | 49 |
|
Francesco Furfari | bc767e5 | 2006-11-12 23:10:53 +0000 | [diff] [blame^] | 50 | private Properties UPnPTargetListener;
|
| 51 | private String deviceId;
|
| 52 | private String serviceId;
|
| 53 | private Vector upnpListeners = new Vector();
|
| 54 | final private Object LOCK = new Object();
|
Francesco Furfari | d1072b5 | 2006-05-03 07:44:48 +0000 | [diff] [blame] | 55 |
|
| 56 | public UPnPEventNotifier(BundleContext context,UPnPDevice device,UPnPService service,EventSource source){
|
| 57 | this.context=context;
|
| 58 | this.device=device;
|
| 59 | this.service=service;
|
| 60 | this.source=source;
|
| 61 | this.serviceId=service.getId();
|
| 62 | setupUPnPListenerHouseKeeping(device);
|
| 63 | }
|
| 64 |
|
| 65 | /**
|
| 66 | * @param deviceId
|
| 67 | */
|
| 68 | private void setupUPnPListenerHouseKeeping(UPnPDevice device) {
|
| 69 | UPnPTargetListener = new Properties();
|
| 70 | Dictionary dict = device.getDescriptions(null);
|
| 71 | deviceId = (String) dict.get(UPnPDevice.ID);
|
| 72 | UPnPTargetListener.put(UPnPDevice.ID,deviceId);
|
| 73 | UPnPTargetListener.put(UPnPService.ID,serviceId);
|
| 74 | UPnPTargetListener.put(UPnPDevice.TYPE,dict.get(UPnPDevice.TYPE));
|
| 75 | UPnPTargetListener.put(UPnPService.TYPE,service.getType());
|
| 76 | String ANY_UPnPEventListener = "("+Constants.OBJECTCLASS+"="+UPnPEventListener.class.getName()+")";
|
| 77 |
|
Francesco Furfari | bc767e5 | 2006-11-12 23:10:53 +0000 | [diff] [blame^] | 78 | synchronized (LOCK) {
|
| 79 | try {
|
| 80 | String filter = ANY_UPnPEventListener;
|
| 81 | context.addServiceListener(this,filter);
|
| 82 | } catch (Exception ex) {
|
| 83 | System.out.println(ex);
|
| 84 | }
|
| 85 |
|
| 86 |
|
| 87 | ServiceReference[] listeners = null;
|
| 88 | try {
|
| 89 | listeners = context.getServiceReferences(UPnPEventListener.class.getName(),null);
|
| 90 | if (listeners != null){
|
| 91 | for (int i = 0;i<listeners.length;i++){
|
| 92 | ServiceReference sr = listeners[i];
|
| 93 | Filter filter = (Filter) sr.getProperty(UPnPEventListener.UPNP_FILTER);
|
| 94 | if (filter == null) upnpListeners.add(sr);
|
| 95 | else {
|
| 96 | if (filter.match(UPnPTargetListener))
|
| 97 | addNewListener(sr);
|
| 98 | }
|
Francesco Furfari | d1072b5 | 2006-05-03 07:44:48 +0000 | [diff] [blame] | 99 | }
|
| 100 | }
|
Francesco Furfari | bc767e5 | 2006-11-12 23:10:53 +0000 | [diff] [blame^] | 101 | } catch (Exception ex) {
|
| 102 | System.out.println(ex);
|
Francesco Furfari | d1072b5 | 2006-05-03 07:44:48 +0000 | [diff] [blame] | 103 | }
|
Francesco Furfari | d1072b5 | 2006-05-03 07:44:48 +0000 | [diff] [blame] | 104 |
|
Francesco Furfari | d1072b5 | 2006-05-03 07:44:48 +0000 | [diff] [blame] | 105 | }
|
Francesco Furfari | bc767e5 | 2006-11-12 23:10:53 +0000 | [diff] [blame^] | 106 |
|
Francesco Furfari | d1072b5 | 2006-05-03 07:44:48 +0000 | [diff] [blame] | 107 | if (source!=null){
|
| 108 | UPnPStateVariable[] vars = service.getStateVariables();
|
| 109 | if (vars != null){
|
| 110 | for (int i=0;i<vars.length;i++)
|
| 111 | if(vars[i].sendsEvents())
|
| 112 | source.addPropertyChangeListener(vars[i].getName(),this);
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 |
|
| 117 | }
|
| 118 |
|
| 119 | /* (non-Javadoc)
|
| 120 | * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
|
| 121 | */
|
| 122 | public void propertyChange(PropertyChangeEvent evt) {
|
Francesco Furfari | bc767e5 | 2006-11-12 23:10:53 +0000 | [diff] [blame^] | 123 |
|
| 124 | synchronized (LOCK) {
|
| 125 |
|
| 126 | Iterator list = upnpListeners.iterator();
|
| 127 | String property = evt.getPropertyName();
|
| 128 | Object value = evt.getNewValue();
|
| 129 | String valueString = value.toString();
|
| 130 | final Properties events = new Properties();
|
| 131 | events.put(property,valueString);
|
| 132 | while (list.hasNext()){
|
| 133 | final ServiceReference sr = (ServiceReference)list.next();
|
| 134 | String[] props =sr.getPropertyKeys();
|
| 135 | new Thread(){
|
| 136 | public void run(){
|
| 137 | try {
|
| 138 | UPnPEventListener listener = (UPnPEventListener) context.getService(sr);
|
| 139 | listener.notifyUPnPEvent(deviceId,serviceId,events);
|
| 140 | context.ungetService(sr);
|
| 141 | } catch (Exception ex){
|
| 142 | System.out.println("Clock UPnPEventNotifier Err: " +ex);
|
| 143 | System.out.println("context: " +context);
|
| 144 | System.out.println("listener: " +context.getService(sr));
|
| 145 | System.out.println("sr: " +sr);
|
| 146 | System.out.println();
|
| 147 | }
|
Francesco Furfari | d1072b5 | 2006-05-03 07:44:48 +0000 | [diff] [blame] | 148 | }
|
Francesco Furfari | bc767e5 | 2006-11-12 23:10:53 +0000 | [diff] [blame^] | 149 | }.start();
|
| 150 | }
|
Francesco Furfari | d1072b5 | 2006-05-03 07:44:48 +0000 | [diff] [blame] | 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | /* (non-Javadoc)
|
| 155 | * @see org.osgi.framework.ServiceListener#serviceChanged(org.osgi.framework.ServiceEvent)
|
| 156 | */
|
| 157 | public void serviceChanged(ServiceEvent e) {
|
Francesco Furfari | bc767e5 | 2006-11-12 23:10:53 +0000 | [diff] [blame^] | 158 | ServiceReference sr = e.getServiceReference();
|
| 159 | synchronized (LOCK) {
|
| 160 | switch(e.getType()){
|
| 161 | case ServiceEvent.REGISTERED:{
|
| 162 | Filter filter = (Filter) sr.getProperty(UPnPEventListener.UPNP_FILTER);
|
| 163 | if (filter == null) addNewListener(sr);
|
| 164 | else {
|
| 165 | if (filter.match(UPnPTargetListener))
|
| 166 | addNewListener(sr);
|
| 167 | }
|
| 168 | };break;
|
Francesco Furfari | d1072b5 | 2006-05-03 07:44:48 +0000 | [diff] [blame] | 169 |
|
Francesco Furfari | bc767e5 | 2006-11-12 23:10:53 +0000 | [diff] [blame^] | 170 | case ServiceEvent.MODIFIED:{
|
| 171 | Filter filter = (Filter) sr.getProperty(UPnPEventListener.UPNP_FILTER);
|
| 172 | removeListener(sr);
|
| 173 | if (filter == null)
|
| 174 | addNewListener(sr);
|
| 175 | else {
|
| 176 | if (filter.match(UPnPTargetListener))
|
| 177 | addNewListener(sr);
|
| 178 | }
|
| 179 | };break;
|
| 180 |
|
| 181 | case ServiceEvent.UNREGISTERING:{
|
| 182 | removeListener(sr);
|
| 183 | };break;
|
| 184 | }
|
| 185 | }//end LOCK
|
Francesco Furfari | d1072b5 | 2006-05-03 07:44:48 +0000 | [diff] [blame] | 186 |
|
| 187 | }
|
| 188 |
|
| 189 | /**
|
| 190 | * @param reference
|
| 191 | */
|
| 192 | private void removeListener(ServiceReference reference) {
|
| 193 | upnpListeners.remove(reference);
|
| 194 | }
|
| 195 |
|
| 196 | /**
|
| 197 | * @param reference
|
| 198 | */
|
| 199 | private void addNewListener(ServiceReference reference) {
|
| 200 | upnpListeners.add(reference);
|
| 201 | }
|
| 202 |
|
| 203 | public void destroy(){
|
| 204 | context.removeServiceListener(this);
|
| 205 | upnpListeners.removeAllElements();
|
| 206 | }
|
| 207 | }
|