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 | d8bdb64 | 2006-04-04 23:33:40 +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 | d8bdb64 | 2006-04-04 23:33:40 +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 | d8bdb64 | 2006-04-04 23:33:40 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | package org.apache.felix.upnp.basedriver.export; |
| 21 | |
| 22 | |
| 23 | import java.util.Dictionary; |
| 24 | import java.util.Properties; |
| 25 | |
| 26 | import org.cybergarage.upnp.Action; |
| 27 | import org.cybergarage.upnp.Argument; |
| 28 | import org.cybergarage.upnp.ArgumentList; |
| 29 | import org.cybergarage.upnp.UPnPStatus; |
| 30 | import org.cybergarage.upnp.control.ActionListener; |
| 31 | |
| 32 | import org.osgi.framework.Constants; |
| 33 | import org.osgi.framework.InvalidSyntaxException; |
| 34 | import org.osgi.framework.ServiceEvent; |
| 35 | import org.osgi.framework.ServiceListener; |
| 36 | import org.osgi.framework.ServiceReference; |
| 37 | import org.osgi.service.upnp.UPnPAction; |
| 38 | import org.osgi.service.upnp.UPnPDevice; |
| 39 | import org.osgi.service.upnp.UPnPService; |
| 40 | |
| 41 | import org.apache.felix.upnp.basedriver.Activator; |
| 42 | import org.apache.felix.upnp.extra.util.Converter; |
Francesco Furfari | 33cb438 | 2006-05-04 15:38:48 +0000 | [diff] [blame] | 43 | import org.osgi.service.upnp.UPnPException; |
Francesco Furfari | d8bdb64 | 2006-04-04 23:33:40 +0000 | [diff] [blame] | 44 | |
Francesco Furfari | f2a6791 | 2006-07-17 17:08:02 +0000 | [diff] [blame] | 45 | /* |
Karl Pauls | d312acc | 2007-06-18 20:38:33 +0000 | [diff] [blame^] | 46 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
Francesco Furfari | f2a6791 | 2006-07-17 17:08:02 +0000 | [diff] [blame] | 47 | */ |
Francesco Furfari | d8bdb64 | 2006-04-04 23:33:40 +0000 | [diff] [blame] | 48 | public class GeneralActionListener implements ServiceListener,ActionListener { |
| 49 | |
| 50 | private ServiceReference dev; |
| 51 | private String id; |
| 52 | private boolean open; |
| 53 | |
| 54 | /** |
| 55 | * @param osgiServ |
| 56 | */ |
| 57 | public GeneralActionListener(ServiceReference sr, String serviceId) { |
| 58 | try { |
| 59 | Activator.bc.addServiceListener(this, |
| 60 | "("+Constants.SERVICE_ID+"="+sr.getProperty(Constants.SERVICE_ID)+")"); |
| 61 | } catch (InvalidSyntaxException ingnored) {} |
| 62 | this.dev=sr; |
| 63 | this.id=serviceId; |
| 64 | this.open=true; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @see org.cybergarage.upnp.control.ActionListener#actionControlReceived(org.cybergarage.upnp.Action) |
| 69 | */ |
| 70 | public synchronized boolean actionControlReceived(Action upnpAct) { |
| 71 | if(!open) return false; |
| 72 | UPnPService osgiServ=null; |
| 73 | try{ |
| 74 | osgiServ=((UPnPDevice) Activator.bc.getService(dev)).getService(id); |
| 75 | }catch(Exception ignored){} |
| 76 | |
| 77 | if(osgiServ==null) |
| 78 | return exiting(false); |
| 79 | |
| 80 | UPnPAction osgiAct = osgiServ.getAction(upnpAct.getName()); |
| 81 | Properties inArgs = null; |
| 82 | ArgumentList al = upnpAct.getArgumentList(); |
| 83 | String[] inArg = osgiAct.getInputArgumentNames(); |
| 84 | boolean invalidAction=false; |
| 85 | if(inArg!=null){ |
| 86 | inArgs = new Properties(); |
| 87 | Argument arg; |
| 88 | for (int j = 0; j < inArg.length; j++) { |
| 89 | arg=al.getArgument(inArg[j]); |
| 90 | try { |
| 91 | inArgs.put( |
| 92 | inArg[j], |
| 93 | Converter.parseString( |
| 94 | arg.getValue(), |
| 95 | arg.getRelatedStateVariable().getDataType() |
| 96 | /*osgiServ.getStateVariable(arg.getRelatedStateVariableName()).getUPnPDataType()*/ |
| 97 | ) |
| 98 | ); |
| 99 | } catch (Exception e) { |
| 100 | invalidAction=true; |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | Dictionary outArgs=null; |
| 106 | try { |
| 107 | outArgs=osgiAct.invoke(inArgs); |
| 108 | } catch (UPnPException e) { |
| 109 | //TODO Activator.logger.log() |
Francesco Furfari | 33cb438 | 2006-05-04 15:38:48 +0000 | [diff] [blame] | 110 | upnpAct.setStatus(e.getUPnPError_Code(),e.getMessage()); |
Francesco Furfari | d8bdb64 | 2006-04-04 23:33:40 +0000 | [diff] [blame] | 111 | invalidAction=true; |
| 112 | } catch (Exception e){ |
| 113 | //TODO Activator.logger.log() |
| 114 | upnpAct.setStatus(UPnPStatus.ACTION_FAILED); |
| 115 | invalidAction=true; |
| 116 | } |
| 117 | if(invalidAction) |
| 118 | return exiting(false); |
| 119 | String[] outArg = osgiAct.getOutputArgumentNames(); |
| 120 | if(outArg!=null){ |
| 121 | Argument arg; |
| 122 | for (int j = 0; j < outArg.length; j++) { |
| 123 | arg = al.getArgument(outArg[j]); |
| 124 | try { |
| 125 | arg.setValue( |
| 126 | Converter.toString( |
| 127 | outArgs.get(outArg[j]), |
| 128 | arg.getRelatedStateVariable().getDataType() |
| 129 | /*osgiServ.getStateVariable(arg.getRelatedStateVariableName()).getUPnPDataType()*/ |
| 130 | ) |
| 131 | ); |
| 132 | } catch (Exception e) { |
| 133 | e.printStackTrace(); |
| 134 | return exiting(false); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | return exiting(true); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * @param b |
| 143 | * @return |
| 144 | */ |
| 145 | private boolean exiting(boolean b) { |
| 146 | Activator.bc.ungetService(dev); |
| 147 | return b; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @see org.osgi.framework.ServiceListener#serviceChanged(org.osgi.framework.ServiceEvent) |
| 152 | */ |
| 153 | public void serviceChanged(ServiceEvent e) { |
| 154 | if(e.getType()==ServiceEvent.UNREGISTERING){ |
| 155 | Activator.bc.removeServiceListener(this); |
| 156 | } |
| 157 | } |
| 158 | } |