Search in sources :

Example 31 with Descriptor

use of org.apache.felix.service.command.Descriptor in project ecf by eclipse.

the class RSACommand method importservice.

@Descriptor("Import a remote service via Remote Service Admin.  If -e is used, the given endpoint URL is read to read the EndpointDescription.  If not used, an EndpointDescription is expected from the console input (e.g. copy and paste)")
public RemoteServiceAdmin.ImportReference importservice(CommandSession cs, @Descriptor("Optional URL indicating location of an Endpoint Description (EDEF format)") @Parameter(names = { "-e", "--edefurl" }, absentValue = "") String endpointurl) {
    InputStream ins = null;
    URL url = null;
    if ("".equals(endpointurl)) {
        ins = cs.getKeyboard();
        cs.getConsole().println("Waiting for console input.   To complete enter an empty line...");
    } else {
        try {
            url = new URL(endpointurl);
            ins = url.openStream();
        } catch (IOException e) {
            e.printStackTrace(cs.getConsole());
        }
    }
    BufferedReader br = new BufferedReader(new InputStreamReader(ins));
    StringBuffer buf = new StringBuffer();
    // read from the input stream until an empty line is encountered
    while (true) {
        try {
            String line = br.readLine();
            if (line != null && line.length() > 0)
                buf.append(line).append("\n");
            else
                break;
        } catch (IOException e) {
            e.printStackTrace(cs.getConsole());
            return null;
        }
    }
    // Close the input stream if this was from a url
    if (url != null)
        try {
            ins.close();
        } catch (IOException e) {
            e.printStackTrace(cs.getConsole());
        }
    ByteArrayInputStream bins = new ByteArrayInputStream(buf.toString().getBytes());
    EndpointDescriptionReader r = new EndpointDescriptionReader();
    org.osgi.service.remoteserviceadmin.EndpointDescription[] eds = null;
    try {
        eds = r.readEndpointDescriptions(bins);
    } catch (IOException e) {
        e.printStackTrace(cs.getConsole());
        return null;
    }
    // should be only one
    org.osgi.service.remoteserviceadmin.ImportRegistration reg = getRSA().importService(eds[0]);
    if (reg == null)
        return null;
    else {
        Throwable t = reg.getException();
        if (t != null) {
            t.printStackTrace(cs.getConsole());
            return null;
        } else {
            RemoteServiceAdmin.ImportReference ir = (RemoteServiceAdmin.ImportReference) reg.getImportReference();
            if (ir != null) {
                EndpointDescription ed = (EndpointDescription) ir.getImportedEndpoint();
                if (ed == null) {
                    cs.getConsole().println("Cannot get endpoint description for imported endpoint");
                    return null;
                }
                cs.getConsole().println("endpoint.id=" + ed.getId() + " with service.id=" + ir.getImportedService().getProperty(Constants.SERVICE_ID) + " successfully imported:");
                return ir;
            } else
                return null;
        }
    }
}
Also used : RemoteServiceAdmin(org.eclipse.ecf.osgi.services.remoteserviceadmin.RemoteServiceAdmin) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) EndpointDescriptionReader(org.eclipse.ecf.osgi.services.remoteserviceadmin.EndpointDescriptionReader) IOException(java.io.IOException) EndpointDescription(org.eclipse.ecf.osgi.services.remoteserviceadmin.EndpointDescription) URL(java.net.URL) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) Descriptor(org.apache.felix.service.command.Descriptor)

Aggregations

Descriptor (org.apache.felix.service.command.Descriptor)31 ArrayList (java.util.ArrayList)13 ServiceReference (org.osgi.framework.ServiceReference)8 IOException (java.io.IOException)6 Bundle (org.osgi.framework.Bundle)6 StartLevel (org.osgi.service.startlevel.StartLevel)5 URL (java.net.URL)4 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)4 PrintStream (java.io.PrintStream)3 Method (java.lang.reflect.Method)3 MalformedURLException (java.net.MalformedURLException)3 List (java.util.List)3 TreeMap (java.util.TreeMap)3 HandlerFactory (org.apache.felix.ipojo.HandlerFactory)3 TypeDeclaration (org.apache.felix.ipojo.extender.TypeDeclaration)3 BundleException (org.osgi.framework.BundleException)3 File (java.io.File)2 InputStream (java.io.InputStream)2 URI (java.net.URI)2 Factory (org.apache.felix.ipojo.Factory)2