public OSGiRuntimeService(OSGiRuntimeContext runtimeContext, BundleContext context) { super(runtimeContext); bundleContext = context; bundles = new ConcurrentHashMap<String, Bundle>(); contexts = new ConcurrentHashMap<String, RuntimeContext>(); String bindAddress = context.getProperty(PROP_NUXEO_BIND_ADDRESS); if (bindAddress != null) { properties.put(PROP_NUXEO_BIND_ADDRESS, bindAddress); } String homeDir = getProperty(PROP_HOME_DIR); log.debug("Home directory: " + homeDir); if (homeDir != null) { workingDir = new File(homeDir); } else { workingDir = bundleContext.getDataFile("/"); } // environment may not be set by some bootstrappers (like tests) - we // create it now if not yet created Environment env = Environment.getDefault(); if (env == null) { Environment.setDefault(new Environment(workingDir)); } workingDir.mkdirs(); persistence = new ComponentPersistence(this); log.debug("Working directory: " + workingDir); }
private void writeHistory(History history, BundleContext context) throws IOException { List<String> list = history.get(); File log = context.getDataFile("log.txt"); if (log.exists() && !log.delete()) { throw new IOException("Unable to delete previous log file!"); } write(list, log); }
private List<String> readHistory(BundleContext context) throws IOException { File log = context.getDataFile("log.txt"); List<String> result = new ArrayList<String>(); if (log.isFile()) { read(log, result); } return result; }
@Test public void testGetDataFile() throws Exception { Bundle bundle = installBundle(getBundleArchiveA()); try { bundle.start(); BundleContext bundleContext = bundle.getBundleContext(); assertNotNull(bundleContext); File dataFile = bundleContext.getDataFile("blah"); assertNotNull(dataFile); assertTrue(dataFile.toString().endsWith(File.separator + "blah")); } finally { bundle.uninstall(); } }
@Activate void activate(BundleContext context, Config config) throws Exception { this.context = context; Dictionary<String, Object> dict = new Hashtable<String, Object>(); dict.put(CommandProcessor.COMMAND_SCOPE, "gogo"); // register converters registrations.add( context.registerService( Converter.class.getName(), new Converters(context.getBundle(0).getBundleContext()), null)); // register commands dict.put( CommandProcessor.COMMAND_FUNCTION, new String[] {"format", "getopt", "new", "set", "tac", "type"}); registrations.add(context.registerService(Builtin.class.getName(), new Builtin(), dict)); dict.put( CommandProcessor.COMMAND_FUNCTION, new String[] {"each", "if", "not", "throw", "try", "until", "while"}); registrations.add(context.registerService(Procedural.class.getName(), new Procedural(), dict)); dict.put(CommandProcessor.COMMAND_FUNCTION, new String[] {"cat", "echo", "grep"}); registrations.add(context.registerService(Posix.class.getName(), new Posix(), dict)); dict.put(CommandProcessor.COMMAND_FUNCTION, new String[] {"aslist"}); registrations.add( context.registerService(EnRouteCommands.class.getName(), new EnRouteCommands(), dict)); // Setup command history File historyFile = context.getDataFile("history"); history = new FileHistory(historyFile); // Start shell on a separate thread Runnable runnable = new Runnable() { @Override public void run() { CommandSession cmdSession = null; ConsoleReader console = null; try { Terminal terminal = TerminalFactory.get(); try { terminal.init(); } catch (Exception e) { throw new RuntimeException("Failed to initialise terminal: ", e); } console = new ConsoleReader(System.in, System.out, terminal); console.setHistory(history); cmdSession = cmdProc.createSession( console.getInput(), new PrintStream(new WriterOutputStream(console.getOutput())), System.err); cmdSession.put(PROMPT, config.prompt()); setupSession(cmdSession); console.addCompleter( new Completer() { @Override public int complete(String buffer, int cursor, List<CharSequence> candidates) { String prefix = buffer.substring(0, cursor); boolean prefixHasColon = prefix.indexOf(':') >= 0; Collection<CommandName> commands = listCommands(); for (CommandName command : commands) { String scopedName = command.toString(); if (prefixHasColon) { if (scopedName.startsWith(prefix)) candidates.add(scopedName); } else { if (scopedName.startsWith(prefix) || command.getFunc().startsWith(prefix)) candidates.add(scopedName); } } return 0; } }); printMotd(console); while (!Thread.interrupted()) { // Read a line of input String inputLine; try { Object prompt = cmdSession.get(PROMPT); if (prompt == null) prompt = config.prompt(); if (prompt instanceof String && ((String) prompt).startsWith("(")) prompt = cmdSession.execute((String) prompt); else prompt = prompt.toString(); inputLine = console.readLine((String) prompt, null); if (inputLine == null) { shutdown(); return; } if (inputLine.isEmpty()) continue; } catch (UserInterruptException e) { console.println("Use Ctrl-D to exit from enRoute OSGi Shell."); continue; } // Try to execute the command try { Object reply = cmdSession.execute(inputLine); if (reply != null) { CharSequence replyStr = cmdSession.format(reply, Converter.INSPECT); String[] replyLines = replyStr.toString().split("[\n\r\f]+"); for (String replyLine : replyLines) { console.println(replyLine); } } } catch (Exception e) { cmdSession.put("exception-cmd", inputLine); cmdSession.put("exception", e); String message = e.getMessage(); console.println(message != null ? message : "<null>"); } } } catch (Exception e) { e.printStackTrace(); } finally { if (cmdSession != null) cmdSession.close(); if (console != null) console.shutdown(); } } private void printMotd(ConsoleReader console) throws IOException { String motd = config.motd(); if (motd != null && !motd.isEmpty()) { if (!motd.equals("!")) console.println(motd); } else { URL motdEntry = context.getBundle().getEntry("motd.txt"); if (motdEntry != null) { try (InputStream in = motdEntry.openStream()) { BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); while (true) { String line = reader.readLine(); if (line == null) break; console.println(line); } } catch (IOException e) { // No so important ... } } } } }; thread = new Thread(runnable, "OSGi enRoute Gogo Shell"); thread.start(); }
File propFile() { return bundleContext.getDataFile(CFG); }
public File getDataFile(String filename) { return delegate.getDataFile(filename); }