use of org.apache.karaf.shell.impl.action.command.ManagerImpl in project karaf by apache.
the class CommandExtension method updateState.
@SuppressWarnings("unchecked")
private synchronized void updateState(AggregateServiceTracker.State state) {
boolean wasSatisfied = manager != null;
boolean isSatisfied = state != null && state.isSatisfied();
String action;
if (wasSatisfied && isSatisfied) {
action = "Updating";
} else if (wasSatisfied) {
action = "Unregistering";
} else if (isSatisfied) {
action = "Registering";
} else {
return;
}
LOGGER.info("{} commands for bundle {}/{}", action, bundle.getSymbolicName(), bundle.getVersion());
if (wasSatisfied) {
for (Class clazz : classes) {
manager.unregister(clazz);
}
manager = null;
}
if (isSatisfied) {
Registry reg = new RegistryImpl(registry);
manager = new ManagerImpl(reg, registry);
reg.register(bundle.getBundleContext());
reg.register(manager);
for (Map.Entry<Class, Object> entry : state.getSingleServices().entrySet()) {
reg.register(entry.getValue());
}
for (final Map.Entry<Class, List> entry : state.getMultiServices().entrySet()) {
reg.register((Callable) entry::getValue, entry.getKey());
}
for (Class clazz : classes) {
manager.register(clazz);
}
}
}
use of org.apache.karaf.shell.impl.action.command.ManagerImpl in project karaf by apache.
the class Activator method start.
@Override
public void start(final BundleContext context) throws Exception {
threadIO = new ThreadIOImpl();
threadIO.start();
sessionFactory = new SecuredSessionFactoryImpl(context, threadIO);
sessionFactory.getCommandProcessor().addConverter(new Converters(context));
sessionFactory.getCommandProcessor().addConstant(".context", context.getBundle(0).getBundleContext());
final CopyOnWriteArraySet<CommandLoggingFilter> listeners = new CopyOnWriteArraySet<>();
filterTracker = new ServiceTracker<>(context, CommandLoggingFilter.class, new ServiceTrackerCustomizer<CommandLoggingFilter, CommandLoggingFilter>() {
@Override
public CommandLoggingFilter addingService(ServiceReference<CommandLoggingFilter> reference) {
CommandLoggingFilter service = context.getService(reference);
listeners.add(service);
return service;
}
@Override
public void modifiedService(ServiceReference<CommandLoggingFilter> reference, CommandLoggingFilter service) {
}
@Override
public void removedService(ServiceReference<CommandLoggingFilter> reference, CommandLoggingFilter service) {
listeners.remove(service);
context.ungetService(reference);
}
});
filterTracker.open();
LoggingCommandSessionListener loggingCommandSessionListener = new LoggingCommandSessionListener();
loggingCommandSessionListener.setFilters(listeners);
sessionFactory.getCommandProcessor().addListener(loggingCommandSessionListener);
try {
EventAdminListener listener = new EventAdminListener(context);
sessionFactory.getCommandProcessor().addListener(listener);
eventAdminListener = listener;
} catch (NoClassDefFoundError error) {
// Ignore the listener if EventAdmin package isn't present
}
sessionFactory.register(new ManagerImpl(sessionFactory, sessionFactory));
sessionFactoryRegistration = context.registerService(SessionFactory.class, sessionFactory, null);
commandProcessorRegistration = context.registerService(CommandProcessor.class, sessionFactory.getCommandProcessor(), null);
actionExtender = new CommandExtender(sessionFactory);
actionExtender.start(context);
commandTracker = new CommandTracker(sessionFactory, context);
commandTracker.open();
converterTracker = new ConverterTracker(sessionFactory, context);
converterTracker.open();
listenerTracker = new ListenerTracker(sessionFactory, context);
listenerTracker.open();
if (Boolean.parseBoolean(context.getProperty(START_CONSOLE))) {
localConsoleManager = new LocalConsoleManager(context, sessionFactory);
localConsoleManager.start();
} else {
LOGGER.info("Not starting local console. To activate set " + START_CONSOLE + "=true");
}
}
use of org.apache.karaf.shell.impl.action.command.ManagerImpl in project karaf by apache.
the class Main method discoverCommands.
protected void discoverCommands(Session session, ClassLoader cl, String resource) throws IOException, ClassNotFoundException {
Manager manager = new ManagerImpl(session.getRegistry(), session.getFactory().getRegistry(), true);
Enumeration<URL> urls = cl.getResources(resource);
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
BufferedReader r = new BufferedReader(new InputStreamReader(url.openStream()));
String line = r.readLine();
while (line != null) {
line = line.trim();
if (line.length() > 0 && line.charAt(0) != '#') {
final Class<?> actionClass = cl.loadClass(line);
manager.register(actionClass);
}
line = r.readLine();
}
r.close();
}
}
use of org.apache.karaf.shell.impl.action.command.ManagerImpl in project karaf by apache.
the class ParsingTest method testCommandLineParser.
@Test
public void testCommandLineParser() {
SessionFactoryImpl sessionFactory = new SessionFactoryImpl(new ThreadIOImpl());
ManagerImpl manager = new ManagerImpl(sessionFactory, sessionFactory);
sessionFactory.getRegistry().register(new ActionCommand(manager, FooCommand.class));
sessionFactory.getRegistry().register(new ActionCommand(manager, AnotherCommand.class));
sessionFactory.getRegistry().register(new CustomParser());
Session session = new HeadlessSessionImpl(sessionFactory, sessionFactory.getCommandProcessor(), new ByteArrayInputStream(new byte[0]), new PrintStream(new ByteArrayOutputStream()), new PrintStream(new ByteArrayOutputStream()));
String parsed = CommandLineParser.parse(session, " foo bar (a + b); another command with spaces ");
assertEquals("foo bar (a + b) ; another \"command with spaces\"", parsed);
}
use of org.apache.karaf.shell.impl.action.command.ManagerImpl in project karaf by apache.
the class ParsingTest method testCommandLineParserMultiLine.
@Test
public void testCommandLineParserMultiLine() {
SessionFactoryImpl sessionFactory = new SessionFactoryImpl(new ThreadIOImpl());
ManagerImpl manager = new ManagerImpl(sessionFactory, sessionFactory);
sessionFactory.getRegistry().register(new ActionCommand(manager, FooCommand.class));
sessionFactory.getRegistry().register(new ActionCommand(manager, AnotherCommand.class));
sessionFactory.getRegistry().register(new CustomParser());
Session session = new HeadlessSessionImpl(sessionFactory, sessionFactory.getCommandProcessor(), new ByteArrayInputStream(new byte[0]), new PrintStream(new ByteArrayOutputStream()), new PrintStream(new ByteArrayOutputStream()));
String parsed = CommandLineParser.parse(session, "echo a\necho b");
assertEquals("echo a\necho b", parsed);
}
Aggregations