private void includeDisclaimer(File file, String disclaimer) throws ConfigException, IOException { String fileContent = FileUtils.readFileToString(file, brjs.bladerunnerConf().getDefaultFileCharacterEncoding()); FileUtils.writeStringToFile( file, disclaimer + fileContent, brjs.bladerunnerConf().getDefaultFileCharacterEncoding()); }
public static void run( BRJS brjs, CommandList commandList, LogLevelAccessor logLevelAccessor, String args[]) throws CommandOperationException { ConsoleWriter out = brjs.getConsoleWriter(); if (!CommandRunner.extractCommandFromArgs(args).equals(new VersionCommand().getCommandName())) { out.println(brjs.versionInfo().toString()); out.println(""); } doRunCommand(brjs, args, out); }
@SuppressWarnings("unchecked") public static <P extends Plugin, VPP extends P> List<P> createPluginsOfType( BRJS brjs, Class<P> pluginInterface, Class<VPP> virtualProxyClass) { ClassLoader classLoader = Plugin.class.getClassLoader(); try { pluginInterface = (Class<P>) classLoader.loadClass(pluginInterface.getCanonicalName()); } catch (Exception ex) { throw new RuntimeException(ex); } Logger logger = brjs.logger(BRJSPluginLocator.class); List<P> objectList = new ArrayList<P>(); try { ServiceLoader<P> loader = ServiceLoader.load(pluginInterface, classLoader); Iterator<P> objectIterator = loader.iterator(); while (objectIterator.hasNext()) { P object = objectIterator.next(); if (virtualProxyClass != null) { object = virtualProxyClass.getConstructor(pluginInterface).newInstance(object); } objectList.add(object); } } catch (ServiceConfigurationError serviceError) { Throwable cause = serviceError.getCause(); if (cause != null && cause.getClass() == InstantiationException.class) { if (logger != null) { logger.error(Messages.CANNOT_CREATE_INSTANCE_LOG_MSG, cause.getMessage()); } else { System.err.println( String.format(Messages.CANNOT_CREATE_INSTANCE_LOG_MSG, cause.getMessage())); } } else { if (logger != null) { logger.error(Messages.ERROR_CREATING_OBJECT_LOG_MSG, serviceError); } else { System.err.println(String.format(Messages.ERROR_CREATING_OBJECT_LOG_MSG, serviceError)); } } } catch (NullPointerException | NoSuchMethodException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) { throw new RuntimeException(e); } // use this utility to set BRJS so we catch any runtime errors thrown by model observers PluginLocatorUtils.setBRJSForPlugins(brjs, objectList); return objectList; }
private IOFileFilter createExcludeUserLibsTestsFilter(String appName) { IOFileFilter excludeDirFilter = new ExcludeDirFileFilter(""); for (JsLib jsLib : brjs.app(appName).jsLibs()) { if (jsLib.parentNode() instanceof App) { excludeDirFilter = new AndFileFilter(excludeDirFilter, new ExcludeDirFileFilter(jsLib.getName(), "test")); } } return excludeDirFilter; }
@Override protected void doCommand(JSAPResult parsedArgs) throws CommandArgumentsException, CommandOperationException { String appName = parsedArgs.getString("app-name"); String disclaimer = "/*\n* " + parsedArgs.getString("disclaimer") + "\n*/\n\n"; App app = brjs.app(appName); if (!app.dirExists()) throw new CommandArgumentsException("Could not find application '" + appName + "'", this); File destinationZipLocation = new File(brjs.storageDir("exported-app").getAbsolutePath() + "/" + appName + ".zip"); try { File temporaryExportDir = FileUtility.createTemporaryDirectory(appName); IOFileFilter excludeUserLibraryTestsFilter = createExcludeUserLibsTestsFilter(appName); NotFileFilter brjsJarFilter = new NotFileFilter( new AndFileFilter(new PrefixFileFilter("brjs-"), new SuffixFileFilter(".jar"))); IOFileFilter combinedFilter = new AndFileFilter(new ExcludeDirFileFilter("js-test-driver", "bundles"), brjsJarFilter); combinedFilter = new AndFileFilter(combinedFilter, excludeUserLibraryTestsFilter); createResourcesFromSdkTemplate(app.dir(), temporaryExportDir, combinedFilter); includeDisclaimerInDirectoryClasses(new File(temporaryExportDir, "libs"), disclaimer); FileUtility.zipFolder(temporaryExportDir, destinationZipLocation, false); } catch (Exception e) { throw new CommandOperationException( "Could not create application zip for application '" + appName + "'", e); } logger.info("Successfully exported application '" + appName + "'"); logger.info(" " + destinationZipLocation.getAbsolutePath()); }
@Override public void writeContent(ParsedContentPath contentPath, BundleSet bundleSet, OutputStream os) throws ContentProcessingException { // TODO not parse the config on every execution XmlBundlerConfig config = new XmlBundlerConfig(brjs); XmlBundleWriter bundleWriter = new XmlBundleWriter(config); List<Asset> xmlAssets = bundleSet.getResourceFiles(xmlAssetPlugin); try { String outputEncoding = brjs.bladerunnerConf().getBrowserCharacterEncoding(); Writer output = new OutputStreamWriter(os, outputEncoding); if (config.isbundleConigAvailable()) { bundleWriter.writeBundle(xmlAssets, output); } else { bundleWriter.concatenateBundle(xmlAssets, output); } output.flush(); } catch (IOException | ConfigException | XMLStreamException e) { throw new ContentProcessingException(e, "Error while processing XML assets '"); } }
private static void doRunCommand(BRJS brjs, String[] args, ConsoleWriter out) throws CommandOperationException { try { brjs.runCommand(args); } catch (NoSuchCommandException e) { if (e.getCommandName().length() > 0) { out.println(e.getMessage()); out.println("--------"); out.println(""); } doRunCommand(brjs, new String[] {new HelpCommand().getCommandName()}, out); } catch (CommandArgumentsException e) { out.println("Problem:"); out.println(" " + e.getMessage()); out.println(""); out.println("Usage:"); out.println( " brjs " + e.getCommand().getCommandName() + " " + e.getCommand().getCommandUsage()); } catch (CommandOperationException e) { out.println("Error:"); out.println(" " + e.getMessage()); if (e.getCause() != null) { out.println(""); out.println("Caused By:"); out.println(" " + e.getCause().getMessage()); } out.println(""); out.println("Stack Trace:"); StringWriter stackTrace = new StringWriter(); e.printStackTrace(new PrintWriter(stackTrace)); out.println(stackTrace.toString()); throw e; } }
@Override public void setBRJS(BRJS brjs) { this.brjs = brjs; xmlAssetPlugin = brjs.plugins().assetProducer(XMLAssetPlugin.class); }
@Override public void doCommand(String[] args) throws CommandArgumentsException, CommandOperationException { brjs.getConsoleWriter() .println( String.format("command '%s' did command with args: (%s)", name, Arrays.toString(args))); }
@Override public void setBRJS(BRJS brjs) { this.brjs = brjs; logger = brjs.logger(LoggerType.APP_SERVER, this.getClass()); }