diff options
Diffstat (limited to 'osgi/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin.console/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/console/RSACommand.java')
-rw-r--r-- | osgi/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin.console/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/console/RSACommand.java | 403 |
1 files changed, 246 insertions, 157 deletions
diff --git a/osgi/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin.console/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/console/RSACommand.java b/osgi/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin.console/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/console/RSACommand.java index 439d553d1..adcd76031 100644 --- a/osgi/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin.console/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/console/RSACommand.java +++ b/osgi/bundles/org.eclipse.ecf.osgi.services.remoteserviceadmin.console/src/org/eclipse/ecf/osgi/services/remoteserviceadmin/console/RSACommand.java @@ -24,6 +24,7 @@ import java.util.Map; import org.apache.felix.service.command.CommandSession; import org.apache.felix.service.command.Converter; +import org.apache.felix.service.command.Descriptor; import org.apache.felix.service.command.Parameter; import org.eclipse.ecf.console.AbstractCommand; import org.eclipse.ecf.core.IContainerManager; @@ -46,20 +47,30 @@ import org.osgi.service.component.annotations.Reference; import org.osgi.service.remoteserviceadmin.RemoteConstants; @Component(immediate = true, property = { "osgi.command.scope=ecf", "osgi.command.function=listexports", - "osgi.command.function=lex", "osgi.command.function=listimports", "osgi.command.function=lim", - "osgi.command.function=unexport", "osgi.command.function=une", "osgi.command.function=unimport", - "osgi.command.function=uni", - "osgi.command.function=rsadebug", - "osgi.command.function=rsexport", - "osgi.command.function=rsimport" }, service = { RSACommand.class, Converter.class }) + "osgi.command.function=lexps", "osgi.command.function=listimports", "osgi.command.function=limps", + "osgi.command.function=unexportservice", "osgi.command.function=unexpsvc", + "osgi.command.function=unimportservice", "osgi.command.function=unimpsvc", "osgi.command.function=rsadebug", + "osgi.command.function=exportservice", "osgi.command.function=expsvc", "osgi.command.function=importservice", + "osgi.command.function=impsvc", "osgi.command.function=updateservice", + "osgi.command.function=updsvc" }, service = { RSACommand.class, Converter.class }) public class RSACommand extends AbstractCommand implements Converter { - private static final boolean DEBUGON = Boolean - .parseBoolean(System.getProperty("org.eclipse.ecf.osgi.services.remoteserviceadmin.debug", "true")); + private static final String DEFAULT_EXPORT_CONFIG = System.getProperty( + "org.eclipse.ecf.osgi.services.remoteserviceadmin.console.defaultconfig", "ecf.generic.server"); + + private static final boolean DEBUGON = Boolean.parseBoolean( + System.getProperty("org.eclipse.ecf.osgi.services.remoteserviceadmin.console.rsadebug", "true")); + + private static final String EXPORT_LINE_FORMAT = System.getProperty( + "org.eclipse.ecf.osgi.services.remoteserviceadmin.console.exportlineformat", "%1$-37s|%2$-45s|%3$s"); + private static final String IMPORT_LINE_FORMAT = System.getProperty( + "org.eclipse.ecf.osgi.services.remoteserviceadmin.console.importlineformat", "%1$-37s|%2$-45s|%3$s"); private IContainerManager containerManager; private IIDFactory idFactory; private RemoteServiceAdmin rsa; + private BundleContext context; + private ServiceRegistration<?> debugReg; @Reference void bindContainerManager(IContainerManager cm) { @@ -102,6 +113,114 @@ public class RSACommand extends AbstractCommand implements Converter { return this.rsa; } + private List<RemoteServiceAdmin.ExportReference> getExports() { + List<RemoteServiceAdmin.ExportReference> results = new ArrayList<RemoteServiceAdmin.ExportReference>(); + for (org.osgi.service.remoteserviceadmin.ExportReference er : getRSA().getExportedServices()) + results.add((RemoteServiceAdmin.ExportReference) er); + return results; + } + + private List<RemoteServiceAdmin.ImportReference> getImports() { + List<RemoteServiceAdmin.ImportReference> results = new ArrayList<RemoteServiceAdmin.ImportReference>(); + for (org.osgi.service.remoteserviceadmin.ImportReference er : getRSA().getImportedEndpoints()) + results.add((RemoteServiceAdmin.ImportReference) er); + return results; + } + + public Object convert(Class<?> desiredType, Object in) throws Exception { + if (desiredType == RemoteServiceAdmin.ExportReference.class && in instanceof String) + return getExportReferenceForIdOrContainerId((String) in); + else if (desiredType == RemoteServiceAdmin.ExportReference.class && in instanceof Long) + return getExportReferenceForServiceId((Long) in); + else if (desiredType == RemoteServiceAdmin.ImportReference.class && in instanceof String) + return getImportReferenceForIdOrContainerId((String) in); + else if (desiredType == RemoteServiceAdmin.ExportReference.class && in instanceof Long) + return getImportReferenceForServiceId((Long) in); + + return null; + } + + private String formatExportReference(RemoteServiceAdmin.ExportReference er, int level, Converter escape) { + EndpointDescription ed = (EndpointDescription) er.getExportedEndpoint(); + if (ed == null) + return null; + return formatReference(EXPORT_LINE_FORMAT, ed, ed.getContainerID().getName(), ed.getServiceId(), level, escape); + } + + private String formatReference(String lineFormat, EndpointDescription ed, String containerId, long serviceId, + int level, Converter escape) { + if (ed == null) + return null; + switch (level) { + case Converter.PART: + return null; + case Converter.LINE: + return formatLine(lineFormat, ed.getId(), containerId, serviceId); + case Converter.INSPECT: + return formatEndpoint(ed); + default: + return null; + } + } + + private String formatImportReference(RemoteServiceAdmin.ImportReference ir, int level, Converter escape) { + ID localContainerID = ir.getLocalContainerID(); + if (localContainerID == null) + return null; + @SuppressWarnings("rawtypes") + ServiceReference ref = ir.getImportedService(); + if (ref == null) + return null; + return formatReference(IMPORT_LINE_FORMAT, (EndpointDescription) ir.getImportedEndpoint(), + localContainerID.getName(), (Long) ref.getProperty(Constants.SERVICE_ID), level, escape); + } + + private String formatEndpoint(EndpointDescription ed) { + EndpointDescriptionWriter edw = new EndpointDescriptionWriter(); + StringWriter sw = new StringWriter(); + try { + edw.writeEndpointDescription(sw, ed); + } catch (IOException e) { + e.printStackTrace(); + return null; + } + return formatLine("%s", sw.toString()); + } + + public String format(Object target, int level, Converter escape) { + if (target instanceof RemoteServiceAdmin.ExportReference) + return formatExportReference((RemoteServiceAdmin.ExportReference) target, level, escape); + else if (target instanceof RemoteServiceAdmin.ImportReference) + return formatImportReference((RemoteServiceAdmin.ImportReference) target, level, escape); + return null; + } + + @Activate + void activate(BundleContext context) { + this.context = context; + if (DEBUGON) + debugOn(); + } + + @Deactivate + void deactivate() { + debugOff(); + } + + synchronized void debugOff() { + if (debugReg != null) { + debugReg.unregister(); + debugReg = null; + } + } + + synchronized void debugOn() { + if (debugReg == null) + debugReg = this.context.registerService( + org.osgi.service.remoteserviceadmin.RemoteServiceAdminListener.class, + new DebugRemoteServiceAdminListener(), null); + } + private RemoteServiceAdmin.ExportReference getExportReferenceForIdOrContainerId(String exportRefId) { for (RemoteServiceAdmin.ExportReference r : getExports()) { EndpointDescription ed = (EndpointDescription) r.getExportedEndpoint(); @@ -162,44 +281,54 @@ public class RSACommand extends AbstractCommand implements Converter { return null; } - public static final String EXPORT_LINE_FORMAT = "%1$-37s|%2$-45s|%3$s"; - public static final String IMPORT_LINE_FORMAT = "%1$-37s|%2$-45s|%3$s"; - + @Descriptor("List RSA exported services") public List<RemoteServiceAdmin.ExportReference> listexports(CommandSession cs) { consoleLine(cs, EXPORT_LINE_FORMAT, "Endpoint Id", "Exporting Container ID", "Exported Service Id\n"); return getExports(); } - public List<RemoteServiceAdmin.ExportReference> lex(CommandSession cs) { + @Descriptor("List RSA exported services") + public List<RemoteServiceAdmin.ExportReference> lexps(CommandSession cs) { return listexports(cs); } - public RemoteServiceAdmin.ExportReference listexports(RemoteServiceAdmin.ExportReference r) { + @Descriptor("Details about a single RSA exported service") + public RemoteServiceAdmin.ExportReference listexports( + @Descriptor("The Endpoint Id of the exported service") RemoteServiceAdmin.ExportReference r) { return r; } - public RemoteServiceAdmin.ExportReference lex(RemoteServiceAdmin.ExportReference r) { + @Descriptor("Details about a single RSA exported service") + public RemoteServiceAdmin.ExportReference lexps( + @Descriptor("The Endpoint Id of the exported service") RemoteServiceAdmin.ExportReference r) { return r; } + @Descriptor("List RSA imported services") public List<RemoteServiceAdmin.ImportReference> listimports(CommandSession cs) { consoleLine(cs, IMPORT_LINE_FORMAT, "Endpoint Id", "Importing Container ID", "Imported Service Id\n"); return getImports(); } - public List<RemoteServiceAdmin.ImportReference> lim(CommandSession cs) { + @Descriptor("List RSA imported services") + public List<RemoteServiceAdmin.ImportReference> limps(CommandSession cs) { return listimports(cs); } - public RemoteServiceAdmin.ImportReference listimports(RemoteServiceAdmin.ImportReference r) { + @Descriptor("Details about a single RSA imported service") + public RemoteServiceAdmin.ImportReference listimports( + @Descriptor("The Endpoint Id of the exported service") RemoteServiceAdmin.ImportReference r) { return r; } - public RemoteServiceAdmin.ImportReference lim(RemoteServiceAdmin.ImportReference r) { + @Descriptor("Details about a single RSA imported service") + public RemoteServiceAdmin.ImportReference limps( + @Descriptor("The Endpoint Id of the exported service") RemoteServiceAdmin.ImportReference r) { return r; } - public String unexport(String endpointId) { + @Descriptor("Unexport an RSA exported service") + public String unexportservice(@Descriptor("The Endpoint Id of the exported service") String endpointId) { RemoteServiceAdmin.ExportRegistration reg = getExportRegistrationForId(endpointId); if (reg != null) { reg.close(); @@ -208,11 +337,13 @@ public class RSACommand extends AbstractCommand implements Converter { return endpointId + " not found"; } - public String une(String endpointId) { - return unexport(endpointId); + @Descriptor("Unexport an RSA exported service") + public String unexpsvc(@Descriptor("The Endpoint Id of the exported service") String endpointId) { + return unexportservice(endpointId); } - public String unimport(String endpointId) { + @Descriptor("Unimport an RSA imported service") + public String unimportservice(@Descriptor("The Endpoint Id of the imported service") String endpointId) { RemoteServiceAdmin.ImportRegistration reg = getImportRegistrationForId(endpointId); if (reg != null) { reg.close(); @@ -221,173 +352,91 @@ public class RSACommand extends AbstractCommand implements Converter { return endpointId + " not found"; } - public String uni(String endpointId) { - return unimport(endpointId); + @Descriptor("Unimport an RSA imported service") + public String unimpsvc(@Descriptor("The Endpoint Id of the imported service") String endpointId) { + return unimportservice(endpointId); } - private List<RemoteServiceAdmin.ExportReference> getExports() { - List<RemoteServiceAdmin.ExportReference> results = new ArrayList<RemoteServiceAdmin.ExportReference>(); - for (org.osgi.service.remoteserviceadmin.ExportReference er : getRSA().getExportedServices()) - results.add((RemoteServiceAdmin.ExportReference) er); - return results; - } - - public List<RemoteServiceAdmin.ImportReference> getImports() { - List<RemoteServiceAdmin.ImportReference> results = new ArrayList<RemoteServiceAdmin.ImportReference>(); - for (org.osgi.service.remoteserviceadmin.ImportReference er : getRSA().getImportedEndpoints()) - results.add((RemoteServiceAdmin.ImportReference) er); - return results; - } - - public Object convert(Class<?> desiredType, Object in) throws Exception { - if (desiredType == RemoteServiceAdmin.ExportReference.class && in instanceof String) - return getExportReferenceForIdOrContainerId((String) in); - else if (desiredType == RemoteServiceAdmin.ExportReference.class && in instanceof Long) - return getExportReferenceForServiceId((Long) in); - else if (desiredType == RemoteServiceAdmin.ImportReference.class && in instanceof String) - return getImportReferenceForIdOrContainerId((String) in); - else if (desiredType == RemoteServiceAdmin.ExportReference.class && in instanceof Long) - return getImportReferenceForServiceId((Long) in); - - return null; - } - - private String formatExportReference(RemoteServiceAdmin.ExportReference er, int level, Converter escape) { - EndpointDescription ed = (EndpointDescription) er.getExportedEndpoint(); - if (ed == null) - return null; - return formatReference(EXPORT_LINE_FORMAT, ed, ed.getContainerID().getName(), ed.getServiceId(), level, escape); - } - - private String formatReference(String lineFormat, EndpointDescription ed, String containerId, long serviceId, - int level, Converter escape) { - if (ed == null) - return null; - switch (level) { - case Converter.PART: - return null; - case Converter.LINE: - return formatLine(lineFormat, ed.getId(), containerId, serviceId); - case Converter.INSPECT: - return formatEndpoint(ed); - default: - return null; - } - } - - private String formatImportReference(RemoteServiceAdmin.ImportReference ir, int level, Converter escape) { - ID localContainerID = ir.getLocalContainerID(); - if (localContainerID == null) - return null; - @SuppressWarnings("rawtypes") - ServiceReference ref = ir.getImportedService(); - if (ref == null) - return null; - return formatReference(IMPORT_LINE_FORMAT, (EndpointDescription) ir.getImportedEndpoint(), - localContainerID.getName(), (Long) ref.getProperty(Constants.SERVICE_ID), level, escape); - } - - private String formatEndpoint(EndpointDescription ed) { - EndpointDescriptionWriter edw = new EndpointDescriptionWriter(); - StringWriter sw = new StringWriter(); - try { - edw.writeEndpointDescription(sw, ed); - } catch (IOException e) { - e.printStackTrace(); - return null; - } - return formatLine("%s", sw.toString()); - } - - public String format(Object target, int level, Converter escape) { - if (target instanceof RemoteServiceAdmin.ExportReference) - return formatExportReference((RemoteServiceAdmin.ExportReference) target, level, escape); - else if (target instanceof RemoteServiceAdmin.ImportReference) - return formatImportReference((RemoteServiceAdmin.ImportReference) target, level, escape); - return null; - } - - private BundleContext context; - private ServiceRegistration<?> debugReg; - - @Activate - void activate(BundleContext context) { - this.context = context; - if (DEBUGON) - debugOn(); - } - - @Deactivate - void deactivate() { - debugOff(); - } - - synchronized void debugOff() { - if (debugReg != null) { - debugReg.unregister(); - debugReg = null; + @Descriptor("Toggle whether RSA debug output is output to console") + public String rsadebug() { + synchronized (this) { + return rsadebug(debugReg == null); } } - synchronized void debugOn() { - if (debugReg == null) - debugReg = this.context.registerService( - org.osgi.service.remoteserviceadmin.RemoteServiceAdminListener.class, - new DebugRemoteServiceAdminListener(), null); - } - - public String rsadebug(boolean on) { + @Descriptor("Set whether RSA debug output is output to console") + public String rsadebug(@Descriptor("Whether to turn debug on or off") boolean on) { String msg = null; synchronized (this) { if (debugReg == null) { if (on) { debugOn(); msg = "RSA debugging ON"; - } else - msg = "RSA debugging already on"; + } else + msg = "RSA debugging already off"; } else { if (debugReg != null) { debugOff(); msg = "RSA debugging OFF"; } else - msg = "RSA debugging already off"; + msg = "RSA debugging already on"; } } return msg; } - public RemoteServiceAdmin.ExportReference rsexport(CommandSession cs, @Parameter(names = { "-s", "--serviceid" }, absentValue = "") long serviceid, - @Parameter(names = { "--properties", "-p" }, absentValue = "") Map<String,?> map) { + @Descriptor("Export a service via Remote Service Admin") + public RemoteServiceAdmin.ExportReference exportservice(CommandSession cs, + @Descriptor("service.id of service to export") long serviceid, @Parameter(names = { + "-props" }, absentValue = "") @Descriptor("Map of service properties for exporting the service") Map<String, ?> map) { ServiceReference<?> ref = null; try { - ServiceReference<?>[] refs = context.getAllServiceReferences(null, "("+Constants.SERVICE_ID+"="+String.valueOf(serviceid)+")"); + ServiceReference<?>[] refs = context.getAllServiceReferences(null, + "(" + Constants.SERVICE_ID + "=" + String.valueOf(serviceid) + ")"); if (refs == null || refs.length < 1) - cs.getConsole().println("Cannot find service with id="+String.valueOf(serviceid)); + cs.getConsole().println("Cannot find registered service with service.id=" + String.valueOf(serviceid)); ref = refs[0]; } catch (InvalidSyntaxException e) { e.printStackTrace(cs.getConsole()); return null; } - Map<String,Object> op = new HashMap<String,Object>(map); + // Create map given map from console + Map<String, Object> op = (map == null) ? new HashMap<String, Object>() : new HashMap<String, Object>(map); if (!op.containsKey(RemoteConstants.SERVICE_EXPORTED_INTERFACES)) op.put(RemoteConstants.SERVICE_EXPORTED_INTERFACES, "*"); if (!op.containsKey(RemoteConstants.SERVICE_EXPORTED_CONFIGS)) - op.put(RemoteConstants.SERVICE_EXPORTED_CONFIGS, "ecf.generic.server"); + op.put(RemoteConstants.SERVICE_EXPORTED_CONFIGS, DEFAULT_EXPORT_CONFIG); + // Now export service with reference and overriding properties Collection<org.osgi.service.remoteserviceadmin.ExportRegistration> regs = getRSA().exportService(ref, op); - for(org.osgi.service.remoteserviceadmin.ExportRegistration reg: regs) { - Throwable t = reg.getException(); - if (t != null) - t.printStackTrace(cs.getConsole()); - else { - RemoteServiceAdmin.ExportReference er = (RemoteServiceAdmin.ExportReference) reg.getExportReference(); - if (er != null) - return er; + // Should always return >= 1 registration + if (regs != null) + for (org.osgi.service.remoteserviceadmin.ExportRegistration reg : regs) { + Throwable t = reg.getException(); + if (t != null) + t.printStackTrace(cs.getConsole()); + else { + RemoteServiceAdmin.ExportReference er = (RemoteServiceAdmin.ExportReference) reg + .getExportReference(); + if (er != null) { + cs.getConsole().println("Service.id=" + String.valueOf(serviceid) + + " successfully exported with endpoint description:"); + return er; + } + } } - } return null; } - - public RemoteServiceAdmin.ImportReference rsimport(CommandSession cs, @Parameter(names = { "-e", "--endpointdescriptionurl" }, absentValue="") String endpointurl) { + + @Descriptor("Export a service via Remote Service Admin") + public RemoteServiceAdmin.ExportReference expsvc(CommandSession cs, + @Descriptor("service.id of service to export") long serviceid) { + return exportservice(cs, serviceid, null); + } + + @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", "--endpointdescriptionurl" }, absentValue = "") String endpointurl) { InputStream ins = null; URL url = null; if ("".equals(endpointurl)) { @@ -417,18 +466,18 @@ public class RSACommand extends AbstractCommand implements Converter { } } // Close the input stream if this was from a url - if (url != null) + 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); + eds = r.readEndpointDescriptions(bins); } catch (IOException e) { e.printStackTrace(cs.getConsole()); return null; @@ -444,11 +493,51 @@ public class RSACommand extends AbstractCommand implements Converter { return null; } else { RemoteServiceAdmin.ImportReference ir = (RemoteServiceAdmin.ImportReference) reg.getImportReference(); - if (ir != null) + 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 + } else return null; } } } + + @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 impsvc(CommandSession cs, + @Descriptor("Optional URL indicating location of an Endpoint Description (EDEF format)") @Parameter(names = { + "-e", "--endpointdescriptionurl" }, absentValue = "") String endpointurl) { + return importservice(cs, endpointurl); + } + + @Descriptor("Update the properties of a remote service via Remote Service Admin") + public RemoteServiceAdmin.ExportReference updateservice(CommandSession cs, + @Descriptor("Endpoint Id of remote service to update") String endpointid, + @Descriptor("Map of properties for update") Map<String, ?> map) { + RemoteServiceAdmin.ExportRegistration ereg = getExportRegistrationForId(endpointid); + if (ereg == null) { + cs.getConsole().println("Cannot find export with endpointid=" + endpointid); + return null; + } + RemoteServiceAdmin.ExportReference eref = (RemoteServiceAdmin.ExportReference) ereg.getExportReference(); + if (eref == null) { + cs.getConsole().println("The remote service with endpointid=" + endpointid + " has been closed"); + return null; + } + ereg.update(map); + return eref; + } + + @Descriptor("Update the properties of a remote service via Remote Service Admin") + public RemoteServiceAdmin.ExportReference updsvc(CommandSession cs, + @Descriptor("Endpoint Id of remote service to update") String endpointid, + @Descriptor("Map of properties for update") Map<String, ?> map) { + return updateservice(cs, endpointid, map); + } + } |