Search in sources :

Example 1 with UPnPException

use of org.osgi.service.upnp.UPnPException in project felix by apache.

the class SliderEditor method buildButtonPanel.

private void buildButtonPanel() {
    buttonPanel = new JPanel();
    JButton doAction = new JButton("Do Action");
    doAction.addActionListener(new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            outArgsModel.clearData();
            Dictionary params = null;
            Dictionary result = null;
            if (argsModel.getRowCount() != 0) {
                if (table.isEditing())
                    table.getCellEditor().stopCellEditing();
                params = new Hashtable();
                for (int i = 0; i < argsModel.getRowCount(); i++) {
                    String name = (String) argsModel.getValueAt(i, 0);
                    String value = (String) argsModel.getValueAt(i, 3);
                    try {
                        params.put(name, Converter.parseString(value, action.getStateVariable(name).getUPnPDataType()));
                    } catch (Exception ex) {
                        LogPanel.log("Error invoking action (bad parameter)");
                        return;
                    }
                }
            }
            try {
                result = action.invoke(params);
            } catch (UPnPException ex) {
                String error = "===== Action Failed =====" + "\nUPnP Error Code::\n    " + ex.getUPnPError_Code() + "\nUPnP Error Description::\n    " + ex.getMessage();
                printReport(params, error);
                JOptionPane.showMessageDialog(Mediator.getPropertiesViewer(), error);
            } catch (Exception ex) {
                printReport(params, ex.getMessage());
                JOptionPane.showMessageDialog(Mediator.getPropertiesViewer(), ex.getMessage());
            }
            if (result != null) {
                printReport(params, result);
                outArgsModel.setData(action, result);
                JOptionPane.showMessageDialog(Mediator.getPropertiesViewer(), "Action invoked!");
            }
        }

        private void printReport(Dictionary params, Object result) {
            String input = "";
            String output = "";
            if (params != null)
                input = params.toString();
            if (output != null)
                output = result.toString();
            String method = action.getName();
            String report = "\n==== Action:: " + method + " ====\n" + input + "\n----------- result ----------\n" + output + "\n-----------------------------";
            LogPanel.log(report);
        }
    });
    buttonPanel.add(doAction);
}
Also used : JPanel(javax.swing.JPanel) Dictionary(java.util.Dictionary) ActionEvent(java.awt.event.ActionEvent) Hashtable(java.util.Hashtable) JButton(javax.swing.JButton) UPnPException(org.osgi.service.upnp.UPnPException) AbstractAction(javax.swing.AbstractAction) UPnPException(org.osgi.service.upnp.UPnPException)

Example 2 with UPnPException

use of org.osgi.service.upnp.UPnPException in project felix by apache.

the class GeneralActionListener method actionControlReceived.

/**
 * @see org.cybergarage.upnp.control.ActionListener#actionControlReceived(org.cybergarage.upnp.Action)
 */
public synchronized boolean actionControlReceived(Action upnpAct) {
    if (!open)
        return false;
    UPnPService osgiServ = null;
    try {
        osgiServ = ((UPnPDevice) Activator.bc.getService(dev)).getService(id);
    } catch (Exception ignored) {
    }
    if (osgiServ == null)
        return exiting(false);
    UPnPAction osgiAct = osgiServ.getAction(upnpAct.getName());
    Properties inArgs = null;
    ArgumentList alIn = upnpAct.getInputArgumentList();
    ArgumentList alOut = upnpAct.getOutputArgumentList();
    String[] inArg = osgiAct.getInputArgumentNames();
    boolean invalidAction = false;
    if (inArg != null) {
        inArgs = new Properties();
        Argument arg;
        for (int j = 0; j < inArg.length; j++) {
            arg = alIn.getArgument(inArg[j]);
            try {
                inArgs.put(inArg[j], Converter.parseString(arg.getValue(), arg.getRelatedStateVariable().getDataType()));
            } catch (Exception e) {
                invalidAction = true;
                break;
            }
        }
    }
    Dictionary outArgs = null;
    try {
        outArgs = osgiAct.invoke(inArgs);
    } catch (UPnPException e) {
        // TODO Activator.logger.log()
        upnpAct.setStatus(e.getUPnPError_Code(), e.getMessage());
        invalidAction = true;
    } catch (Exception e) {
        // TODO Activator.logger.log()
        upnpAct.setStatus(UPnPStatus.ACTION_FAILED);
        invalidAction = true;
    }
    if (invalidAction)
        return exiting(false);
    String[] outArg = osgiAct.getOutputArgumentNames();
    if (outArg != null) {
        Argument arg;
        for (int j = 0; j < outArg.length; j++) {
            arg = alOut.getArgument(outArg[j]);
            try {
                arg.setValue(Converter.toString(outArgs.get(outArg[j]), arg.getRelatedStateVariable().getDataType()));
            } catch (Exception e) {
                e.printStackTrace();
                return exiting(false);
            }
        }
    }
    return exiting(true);
}
Also used : Dictionary(java.util.Dictionary) Argument(org.cybergarage.upnp.Argument) UPnPService(org.osgi.service.upnp.UPnPService) Properties(java.util.Properties) ArgumentList(org.cybergarage.upnp.ArgumentList) UPnPException(org.osgi.service.upnp.UPnPException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) UPnPException(org.osgi.service.upnp.UPnPException) UPnPAction(org.osgi.service.upnp.UPnPAction)

Example 3 with UPnPException

use of org.osgi.service.upnp.UPnPException in project felix by apache.

the class UPnPActionImpl method invoke.

/* (non-Javadoc)
	 * @see org.osgi.service.upnp.UPnPAction#invoke(java.util.Dictionary)
	 */
public Dictionary invoke(Dictionary args) throws Exception {
    /*TODO 
		check if I have understood wath this method should do
		*/
    /*
		 * I look for argument and value and then I add them to ArgumentList
		 */
    ArgumentList argsList = new ArgumentList();
    argsList = act.getInputArgumentList();
    for (int i = 0; i < argsList.size(); i++) {
        /*			
			 * I assert that .getArgument(i) will return to me an Argument with only the name of the
			 * Argument and not it's value. I'll set the associated value by myself and
			 * Also I assert that the Argument are ordered
			 */
        Argument argument = argsList.getArgument(i);
        String argumentName = argument.getName();
        // String relateVar=argument.getRelatedStateVariableName();
        UPnPStateVariable stateVar = this.getStateVariable(argumentName);
        String upnpType = stateVar.getUPnPDataType();
        /*Class javaClass=stateVar.getJavaDataType();*/
        // setting the value related to the input argument
        argument.setValue(Converter.toString(args.get(argumentName), upnpType));
    }
    act.setInArgumentValues(argsList);
    if (act.postControlAction() == true) {
        // TODO check what happen if I don't have any output argument
        Properties outDic = new Properties();
        ArgumentList outArgs = act.getOutputArgumentList();
        if (outArgs.size() == 0) {
            return null;
        }
        for (int i = 0; i < outArgs.size(); i++) {
            Argument argument = outArgs.getArgument(i);
            String argumentName = outArgs.getArgument(i).getName();
            // String relateVar=argument.getRelatedStateVariableName();
            UPnPStateVariable stateVar = getStateVariable(argumentName);
            // String javaType=stateVar.getJavaDataType().getName();
            // TODO rember to catch number exception
            String upnpType = stateVar.getUPnPDataType();
            outDic.put(argumentName, Converter.parseString(argument.getValue(), upnpType));
        }
        return outDic;
    } else {
        UPnPStatus controlStatus = act.getControlStatus();
        throw new UPnPException(controlStatus.getCode(), controlStatus.getDescription());
    }
}
Also used : Argument(org.cybergarage.upnp.Argument) UPnPStatus(org.cybergarage.upnp.UPnPStatus) UPnPStateVariable(org.osgi.service.upnp.UPnPStateVariable) ArgumentList(org.cybergarage.upnp.ArgumentList) Properties(java.util.Properties) UPnPException(org.osgi.service.upnp.UPnPException)

Aggregations

UPnPException (org.osgi.service.upnp.UPnPException)3 Dictionary (java.util.Dictionary)2 Properties (java.util.Properties)2 Argument (org.cybergarage.upnp.Argument)2 ArgumentList (org.cybergarage.upnp.ArgumentList)2 ActionEvent (java.awt.event.ActionEvent)1 Hashtable (java.util.Hashtable)1 AbstractAction (javax.swing.AbstractAction)1 JButton (javax.swing.JButton)1 JPanel (javax.swing.JPanel)1 UPnPStatus (org.cybergarage.upnp.UPnPStatus)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 UPnPAction (org.osgi.service.upnp.UPnPAction)1 UPnPService (org.osgi.service.upnp.UPnPService)1 UPnPStateVariable (org.osgi.service.upnp.UPnPStateVariable)1