Francesco Furfari | d8bdb64 | 2006-04-04 23:33:40 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2006 The Apache Software Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | package org.apache.felix.upnp.basedriver.export; |
| 19 | |
| 20 | |
| 21 | import java.util.Dictionary; |
| 22 | import java.util.Properties; |
| 23 | |
| 24 | import org.cybergarage.upnp.Action; |
| 25 | import org.cybergarage.upnp.Argument; |
| 26 | import org.cybergarage.upnp.ArgumentList; |
| 27 | import org.cybergarage.upnp.UPnPStatus; |
| 28 | import org.cybergarage.upnp.control.ActionListener; |
| 29 | |
| 30 | import org.osgi.framework.Constants; |
| 31 | import org.osgi.framework.InvalidSyntaxException; |
| 32 | import org.osgi.framework.ServiceEvent; |
| 33 | import org.osgi.framework.ServiceListener; |
| 34 | import org.osgi.framework.ServiceReference; |
| 35 | import org.osgi.service.upnp.UPnPAction; |
| 36 | import org.osgi.service.upnp.UPnPDevice; |
| 37 | import org.osgi.service.upnp.UPnPService; |
| 38 | |
| 39 | import org.apache.felix.upnp.basedriver.Activator; |
| 40 | import org.apache.felix.upnp.extra.util.Converter; |
| 41 | import org.apache.felix.upnp.extra.util.UPnPException; |
| 42 | |
| 43 | /** |
| 44 | * @author Stefano "Kismet" Lenzi |
| 45 | * |
| 46 | * |
| 47 | */ |
| 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() |
| 110 | upnpAct.setStatus(e.getErrorCode(),e.getErrorDescription()); |
| 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 | } |