blob: 002c958b98a57832344d9b77cc477de4e2dd473c [file] [log] [blame]
Francesco Furfariec7e1752006-10-02 13:37:04 +00001/*
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 Furfarid8bdb642006-04-04 23:33:40 +00009 *
Francesco Furfariec7e1752006-10-02 13:37:04 +000010 * http://www.apache.org/licenses/LICENSE-2.0
Francesco Furfarid8bdb642006-04-04 23:33:40 +000011 *
Francesco Furfariec7e1752006-10-02 13:37:04 +000012 * 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 Furfarid8bdb642006-04-04 23:33:40 +000018 */
19
20package org.apache.felix.upnp.basedriver.export;
21
22
23import java.util.Dictionary;
24import java.util.Properties;
25
26import org.cybergarage.upnp.Action;
27import org.cybergarage.upnp.Argument;
28import org.cybergarage.upnp.ArgumentList;
29import org.cybergarage.upnp.UPnPStatus;
30import org.cybergarage.upnp.control.ActionListener;
31
32import org.osgi.framework.Constants;
33import org.osgi.framework.InvalidSyntaxException;
34import org.osgi.framework.ServiceEvent;
35import org.osgi.framework.ServiceListener;
36import org.osgi.framework.ServiceReference;
37import org.osgi.service.upnp.UPnPAction;
38import org.osgi.service.upnp.UPnPDevice;
39import org.osgi.service.upnp.UPnPService;
40
41import org.apache.felix.upnp.basedriver.Activator;
42import org.apache.felix.upnp.extra.util.Converter;
Francesco Furfari33cb4382006-05-04 15:38:48 +000043import org.osgi.service.upnp.UPnPException;
Francesco Furfarid8bdb642006-04-04 23:33:40 +000044
Francesco Furfarif2a67912006-07-17 17:08:02 +000045/*
Karl Paulsd312acc2007-06-18 20:38:33 +000046* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
Francesco Furfarif2a67912006-07-17 17:08:02 +000047*/
Francesco Furfarid8bdb642006-04-04 23:33:40 +000048public 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 Furfari33cb4382006-05-04 15:38:48 +0000110 upnpAct.setStatus(e.getUPnPError_Code(),e.getMessage());
Francesco Furfarid8bdb642006-04-04 23:33:40 +0000111 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}