List of usage examples for java.io File exists
public boolean exists()
From source file:ee.ria.xroad.asyncdb.AsyncDBIntegrationTest.java
/** * * * @param args/*from w ww .java 2 s .com*/ * - use 'preservedb' to retain directory structure for further * investigation * @throws Exception - when running integration test fails. */ public static void main(String[] args) throws Exception { File provider = new File(AsyncDBTestUtil.getProviderDirPath()); File logFile = new File(AsyncDBTestUtil.getAsyncLogFilePath()); if (provider.exists() || logFile.exists()) { throw new IntegrationTestFailedException( "Directory '" + AsyncDBTestUtil.DB_FILEPATH + "' and file '" + AsyncDBTestUtil.LOG_FILEPATH + "' must not be present in the beginning of integration test, delete it!"); } File logDir = logFile.getParentFile(); logDir.mkdirs(); boolean preserveDB = false; if (args.length > 0) { preserveDB = "preservedb".equalsIgnoreCase(args[0]); LOG.warn("Preserving DB file tree after test, be sure to remove them later by yourself!"); } long freeFileDescriptorsAtBeginning = SystemMetrics.getFreeFileDescriptorCount(); try { addRequestToNonExistentProvider(); addRequestToExistentProvider(); markSecondRequestAsRemoved(); restoreSecondRequest(); getAllMessageQueues(); sendFirstRequestSuccessfully(); sendSecondRequestUnsuccessfully(); resetSendCountOfSecondRequest(); skipNotSendingRequest(); revertWritingFailure(); // Test cases from real life removeCorruptRequestAndSendNext(); } finally { validateFileDescriptors(freeFileDescriptorsAtBeginning); if (!preserveDB) { FileUtils.deleteDirectory(new File(AsyncDBTestUtil.getProviderDirPath())); FileUtils.deleteDirectory(logDir); } } LOG.info("Integration test of ASYNC-DB accomplished successfully."); }
From source file:de.morbz.osmpoispbf.Scanner.java
public static void main(String[] args) { System.out.println("OsmPoisPbf " + VERSION + " started"); // Get input file if (args.length < 1) { System.out.println("Error: Please provide an input file"); System.exit(-1);//from w ww. j a v a 2s.c o m } String inputFile = args[args.length - 1]; // Get output file String outputFile; int index = inputFile.indexOf('.'); if (index != -1) { outputFile = inputFile.substring(0, index); } else { outputFile = inputFile; } outputFile += ".csv"; // Setup CLI parameters options = new Options(); options.addOption("ff", "filterFile", true, "The file that is used to filter categories"); options.addOption("of", "outputFile", true, "The output CSV file to be written"); options.addOption("rt", "requiredTags", true, "Comma separated list of tags that are required [name]"); options.addOption("ut", "undesiredTags", true, "Comma separated list of tags=value combinations that should be filtered [key=value]"); options.addOption("ot", "outputTags", true, "Comma separated list of tags that are exported [name]"); options.addOption("ph", "printHeader", false, "If flag is set, the `outputTags` are printed as first line in the output file."); options.addOption("r", "relations", false, "Parse relations"); options.addOption("nw", "noWays", false, "Don't parse ways"); options.addOption("nn", "noNodes", false, "Don't parse nodes"); options.addOption("u", "allowUnclosedWays", false, "Allow ways that aren't closed"); options.addOption("d", "decimals", true, "Number of decimal places of coordinates [7]"); options.addOption("s", "separator", true, "Separator character for CSV [|]"); options.addOption("v", "verbose", false, "Print all found POIs"); options.addOption("h", "help", false, "Print this help"); // Parse parameters CommandLine line = null; try { line = (new DefaultParser()).parse(options, args); } catch (ParseException exp) { System.err.println(exp.getMessage()); printHelp(); System.exit(-1); } // Help if (line.hasOption("help")) { printHelp(); System.exit(0); } // Get filter file String filterFile = null; if (line.hasOption("filterFile")) { filterFile = line.getOptionValue("filterFile"); } // Get output file if (line.hasOption("outputFile")) { outputFile = line.getOptionValue("outputFile"); } // Check files if (inputFile.equals(outputFile)) { System.out.println("Error: Input and output files are the same"); System.exit(-1); } File file = new File(inputFile); if (!file.exists()) { System.out.println("Error: Input file doesn't exist"); System.exit(-1); } // Check OSM entity types boolean parseNodes = true; boolean parseWays = true; boolean parseRelations = false; if (line.hasOption("noNodes")) { parseNodes = false; } if (line.hasOption("noWays")) { parseWays = false; } if (line.hasOption("relations")) { parseRelations = true; } // Unclosed ways allowed? if (line.hasOption("allowUnclosedWays")) { onlyClosedWays = false; } // Get CSV separator char separator = '|'; if (line.hasOption("separator")) { String arg = line.getOptionValue("separator"); if (arg.length() != 1) { System.out.println("Error: The CSV separator has to be exactly 1 character"); System.exit(-1); } separator = arg.charAt(0); } Poi.setSeparator(separator); // Set decimals int decimals = 7; // OSM default if (line.hasOption("decimals")) { String arg = line.getOptionValue("decimals"); try { int dec = Integer.valueOf(arg); if (dec < 0) { System.out.println("Error: Decimals must not be less than 0"); System.exit(-1); } else { decimals = dec; } } catch (NumberFormatException ex) { System.out.println("Error: Decimals have to be a number"); System.exit(-1); } } Poi.setDecimals(decimals); // Verbose mode? if (line.hasOption("verbose")) { printPois = true; } // Required tags if (line.hasOption("requiredTags")) { String arg = line.getOptionValue("requiredTags"); requiredTags = arg.split(","); } // Undesired tags if (line.hasOption("undesiredTags")) { String arg = line.getOptionValue("undesiredTags"); undesiredTags = new HashMap<>(); for (String undesired : arg.split(",")) { String[] keyVal = undesired.split("="); if (keyVal.length != 2) { System.out.println("Error: Undesired Tags have to formated like tag=value"); System.exit(-1); } if (!undesiredTags.containsKey(keyVal[0])) { undesiredTags.put(keyVal[0], new HashSet<>(1)); } undesiredTags.get(keyVal[0]).add(keyVal[1]); } } // Output tags if (line.hasOption("outputTags")) { String arg = line.getOptionValue("outputTags"); outputTags = arg.split(","); } // Get filter rules FilterFileParser parser = new FilterFileParser(filterFile); filters = parser.parse(); if (filters == null) { System.exit(-1); } // Setup CSV output try { writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF8")); } catch (IOException e) { System.out.println("Error: Output file error"); System.exit(-1); } // Print Header if (line.hasOption("printHeader")) { String header = "category" + separator + "osm_id" + separator + "lat" + separator + "lon"; for (int i = 0; i < outputTags.length; i++) { header += separator + outputTags[i]; } try { writer.write(header + "\n"); } catch (IOException e) { System.out.println("Error: Output file write error"); System.exit(-1); } } // Setup OSMonaut EntityFilter filter = new EntityFilter(parseNodes, parseWays, parseRelations); Osmonaut naut = new Osmonaut(inputFile, filter, false); // Start watch StopWatch stopWatch = new StopWatch(); stopWatch.start(); // Start OSMonaut String finalSeparator = String.valueOf(separator); naut.scan(new IOsmonautReceiver() { boolean entityNeeded; @Override public boolean needsEntity(EntityType type, Tags tags) { // Are there any tags? if (tags.size() == 0) { return false; } // Check required tags for (String tag : requiredTags) { if (!tags.hasKey(tag)) { return false; } } entityNeeded = getCategory(tags, filters) != null; if (undesiredTags != null && entityNeeded) { for (String key : undesiredTags.keySet()) { if (tags.hasKey(key)) { for (String val : undesiredTags.get(key)) { if (tags.hasKeyValue(key, val)) { return false; } } } } } return entityNeeded; } @Override public void foundEntity(Entity entity) { // Check if way is closed if (onlyClosedWays && entity.getEntityType() == EntityType.WAY) { if (!((Way) entity).isClosed()) { return; } } // Get category Tags tags = entity.getTags(); String cat = getCategory(tags, filters); if (cat == null) { return; } // Get center LatLon center = entity.getCenter(); if (center == null) { return; } // Make OSM-ID String type = ""; switch (entity.getEntityType()) { case NODE: type = "node"; break; case WAY: type = "way"; break; case RELATION: type = "relation"; break; } String id = String.valueOf(entity.getId()); // Make output tags String[] values = new String[outputTags.length]; for (int i = 0; i < outputTags.length; i++) { String key = outputTags[i]; if (tags.hasKey(key)) { values[i] = tags.get(key).replaceAll(finalSeparator, "").replaceAll("\"", ""); } } // Make POI poisFound++; Poi poi = new Poi(values, cat, center, type, id); // Output if (printPois && System.currentTimeMillis() > lastMillis + 40) { printPoisFound(); lastMillis = System.currentTimeMillis(); } // Write to file try { writer.write(poi.toCsv() + "\n"); } catch (IOException e) { System.out.println("Error: Output file write error"); System.exit(-1); } } }); // Close writer try { writer.close(); } catch (IOException e) { System.out.println("Error: Output file close error"); System.exit(-1); } // Output results stopWatch.stop(); printPoisFound(); System.out.println(); System.out.println("Elapsed time in milliseconds: " + stopWatch.getElapsedTime()); // Quit System.exit(0); }
From source file:io.s4.MainApp.java
public static void main(String args[]) throws Exception { Options options = new Options(); options.addOption(OptionBuilder.withArgName("corehome").hasArg().withDescription("core home").create("c")); options.addOption(/*from www .j a v a 2 s .c om*/ OptionBuilder.withArgName("appshome").hasArg().withDescription("applications home").create("a")); options.addOption(OptionBuilder.withArgName("s4clock").hasArg().withDescription("s4 clock").create("d")); options.addOption(OptionBuilder.withArgName("seedtime").hasArg() .withDescription("event clock initialization time").create("s")); options.addOption( OptionBuilder.withArgName("extshome").hasArg().withDescription("extensions home").create("e")); options.addOption( OptionBuilder.withArgName("instanceid").hasArg().withDescription("instance id").create("i")); options.addOption( OptionBuilder.withArgName("configtype").hasArg().withDescription("configuration type").create("t")); CommandLineParser parser = new GnuParser(); CommandLine commandLine = null; String clockType = "wall"; try { commandLine = parser.parse(options, args); } catch (ParseException pe) { System.err.println(pe.getLocalizedMessage()); System.exit(1); } int instanceId = -1; if (commandLine.hasOption("i")) { String instanceIdStr = commandLine.getOptionValue("i"); try { instanceId = Integer.parseInt(instanceIdStr); } catch (NumberFormatException nfe) { System.err.println("Bad instance id: %s" + instanceIdStr); System.exit(1); } } if (commandLine.hasOption("c")) { coreHome = commandLine.getOptionValue("c"); } if (commandLine.hasOption("a")) { appsHome = commandLine.getOptionValue("a"); } if (commandLine.hasOption("d")) { clockType = commandLine.getOptionValue("d"); } if (commandLine.hasOption("e")) { extsHome = commandLine.getOptionValue("e"); } String configType = "typical"; if (commandLine.hasOption("t")) { configType = commandLine.getOptionValue("t"); } long seedTime = 0; if (commandLine.hasOption("s")) { seedTime = Long.parseLong(commandLine.getOptionValue("s")); } File coreHomeFile = new File(coreHome); if (!coreHomeFile.isDirectory()) { System.err.println("Bad core home: " + coreHome); System.exit(1); } File appsHomeFile = new File(appsHome); if (!appsHomeFile.isDirectory()) { System.err.println("Bad applications home: " + appsHome); System.exit(1); } if (instanceId > -1) { System.setProperty("instanceId", "" + instanceId); } else { System.setProperty("instanceId", "" + S4Util.getPID()); } List loArgs = commandLine.getArgList(); if (loArgs.size() < 1) { // System.err.println("No bean configuration file specified"); // System.exit(1); } // String s4ConfigXml = (String) loArgs.get(0); // System.out.println("s4ConfigXml is " + s4ConfigXml); ClassPathResource propResource = new ClassPathResource("s4-core.properties"); Properties prop = new Properties(); if (propResource.exists()) { prop.load(propResource.getInputStream()); } else { System.err.println("Unable to find s4-core.properties. It must be available in classpath"); System.exit(1); } ApplicationContext coreContext = null; String configBase = coreHome + File.separatorChar + "conf" + File.separatorChar + configType; String configPath = ""; List<String> coreConfigUrls = new ArrayList<String>(); File configFile = null; // load clock configuration configPath = configBase + File.separatorChar + clockType + "-clock.xml"; coreConfigUrls.add(configPath); // load core config xml configPath = configBase + File.separatorChar + "s4-core-conf.xml"; configFile = new File(configPath); if (!configFile.exists()) { System.err.printf("S4 core config file %s does not exist\n", configPath); System.exit(1); } coreConfigUrls.add(configPath); String[] coreConfigFiles = new String[coreConfigUrls.size()]; coreConfigUrls.toArray(coreConfigFiles); String[] coreConfigFileUrls = new String[coreConfigFiles.length]; for (int i = 0; i < coreConfigFiles.length; i++) { coreConfigFileUrls[i] = "file:" + coreConfigFiles[i]; } coreContext = new FileSystemXmlApplicationContext(coreConfigFileUrls, coreContext); ApplicationContext context = coreContext; Clock s4Clock = (Clock) context.getBean("clock"); if (s4Clock instanceof EventClock && seedTime > 0) { EventClock s4EventClock = (EventClock) s4Clock; s4EventClock.updateTime(seedTime); System.out.println("Intializing event clock time with seed time " + s4EventClock.getCurrentTime()); } PEContainer peContainer = (PEContainer) context.getBean("peContainer"); Watcher w = (Watcher) context.getBean("watcher"); w.setConfigFilename(configPath); // load extension modules String[] configFileNames = getModuleConfigFiles(extsHome, prop); if (configFileNames.length > 0) { String[] configFileUrls = new String[configFileNames.length]; for (int i = 0; i < configFileNames.length; i++) { configFileUrls[i] = "file:" + configFileNames[i]; } context = new FileSystemXmlApplicationContext(configFileUrls, context); } // load application modules configFileNames = getModuleConfigFiles(appsHome, prop); if (configFileNames.length > 0) { String[] configFileUrls = new String[configFileNames.length]; for (int i = 0; i < configFileNames.length; i++) { configFileUrls[i] = "file:" + configFileNames[i]; } context = new FileSystemXmlApplicationContext(configFileUrls, context); // attach any beans that implement ProcessingElement to the PE // Container String[] processingElementBeanNames = context.getBeanNamesForType(ProcessingElement.class); for (String processingElementBeanName : processingElementBeanNames) { Object bean = context.getBean(processingElementBeanName); try { Method getS4ClockMethod = bean.getClass().getMethod("getS4Clock"); if (getS4ClockMethod.getReturnType().equals(Clock.class)) { if (getS4ClockMethod.invoke(bean) == null) { Method setS4ClockMethod = bean.getClass().getMethod("setS4Clock", Clock.class); setS4ClockMethod.invoke(bean, coreContext.getBean("clock")); } } } catch (NoSuchMethodException mnfe) { // acceptable } System.out.println("Adding processing element with bean name " + processingElementBeanName + ", id " + ((ProcessingElement) bean).getId()); peContainer.addProcessor((ProcessingElement) bean, processingElementBeanName); } } }
From source file:org.apache.s4.MainApp.java
public static void main(String args[]) throws Exception { Options options = new Options(); options.addOption(OptionBuilder.withArgName("corehome").hasArg().withDescription("core home").create("c")); options.addOption(//from w w w . j a v a2s .c om OptionBuilder.withArgName("appshome").hasArg().withDescription("applications home").create("a")); options.addOption(OptionBuilder.withArgName("s4clock").hasArg().withDescription("s4 clock").create("d")); options.addOption(OptionBuilder.withArgName("seedtime").hasArg() .withDescription("event clock initialization time").create("s")); options.addOption( OptionBuilder.withArgName("extshome").hasArg().withDescription("extensions home").create("e")); options.addOption( OptionBuilder.withArgName("instanceid").hasArg().withDescription("instance id").create("i")); options.addOption( OptionBuilder.withArgName("configtype").hasArg().withDescription("configuration type").create("t")); CommandLineParser parser = new GnuParser(); CommandLine commandLine = null; String clockType = "wall"; try { commandLine = parser.parse(options, args); } catch (ParseException pe) { System.err.println(pe.getLocalizedMessage()); System.exit(1); } int instanceId = -1; if (commandLine.hasOption("i")) { String instanceIdStr = commandLine.getOptionValue("i"); try { instanceId = Integer.parseInt(instanceIdStr); } catch (NumberFormatException nfe) { System.err.println("Bad instance id: %s" + instanceIdStr); System.exit(1); } } if (commandLine.hasOption("c")) { coreHome = commandLine.getOptionValue("c"); } if (commandLine.hasOption("a")) { appsHome = commandLine.getOptionValue("a"); } if (commandLine.hasOption("d")) { clockType = commandLine.getOptionValue("d"); } if (commandLine.hasOption("e")) { extsHome = commandLine.getOptionValue("e"); } String configType = "typical"; if (commandLine.hasOption("t")) { configType = commandLine.getOptionValue("t"); } long seedTime = 0; if (commandLine.hasOption("s")) { seedTime = Long.parseLong(commandLine.getOptionValue("s")); } File coreHomeFile = new File(coreHome); if (!coreHomeFile.isDirectory()) { System.err.println("Bad core home: " + coreHome); System.exit(1); } File appsHomeFile = new File(appsHome); if (!appsHomeFile.isDirectory()) { System.err.println("Bad applications home: " + appsHome); System.exit(1); } if (instanceId > -1) { System.setProperty("instanceId", "" + instanceId); } else { System.setProperty("instanceId", "" + S4Util.getPID()); } List loArgs = commandLine.getArgList(); if (loArgs.size() < 1) { // System.err.println("No bean configuration file specified"); // System.exit(1); } // String s4ConfigXml = (String) loArgs.get(0); // System.out.println("s4ConfigXml is " + s4ConfigXml); ClassPathResource propResource = new ClassPathResource("s4-core.properties"); Properties prop = new Properties(); if (propResource.exists()) { prop.load(propResource.getInputStream()); } else { System.err.println("Unable to find s4-core.properties. It must be available in classpath"); System.exit(1); } ApplicationContext coreContext = null; String configBase = coreHome + File.separatorChar + "conf" + File.separatorChar + configType; String configPath = ""; List<String> coreConfigUrls = new ArrayList<String>(); File configFile = null; // load clock configuration configPath = configBase + File.separatorChar + clockType + "-clock.xml"; coreConfigUrls.add(configPath); // load core config xml configPath = configBase + File.separatorChar + "s4-core-conf.xml"; configFile = new File(configPath); if (!configFile.exists()) { System.err.printf("S4 core config file %s does not exist\n", configPath); System.exit(1); } coreConfigUrls.add(configPath); String[] coreConfigFiles = new String[coreConfigUrls.size()]; coreConfigUrls.toArray(coreConfigFiles); String[] coreConfigFileUrls = new String[coreConfigFiles.length]; for (int i = 0; i < coreConfigFiles.length; i++) { coreConfigFileUrls[i] = "file:" + coreConfigFiles[i]; } coreContext = new FileSystemXmlApplicationContext(coreConfigFileUrls, coreContext); ApplicationContext context = coreContext; Clock clock = (Clock) context.getBean("clock"); if (clock instanceof EventClock && seedTime > 0) { EventClock s4EventClock = (EventClock) clock; s4EventClock.updateTime(seedTime); System.out.println("Intializing event clock time with seed time " + s4EventClock.getCurrentTime()); } PEContainer peContainer = (PEContainer) context.getBean("peContainer"); Watcher w = (Watcher) context.getBean("watcher"); w.setConfigFilename(configPath); // load extension modules String[] configFileNames = getModuleConfigFiles(extsHome, prop); if (configFileNames.length > 0) { String[] configFileUrls = new String[configFileNames.length]; for (int i = 0; i < configFileNames.length; i++) { configFileUrls[i] = "file:" + configFileNames[i]; } context = new FileSystemXmlApplicationContext(configFileUrls, context); } // load application modules configFileNames = getModuleConfigFiles(appsHome, prop); if (configFileNames.length > 0) { String[] configFileUrls = new String[configFileNames.length]; for (int i = 0; i < configFileNames.length; i++) { configFileUrls[i] = "file:" + configFileNames[i]; } context = new FileSystemXmlApplicationContext(configFileUrls, context); // attach any beans that implement ProcessingElement to the PE // Container String[] processingElementBeanNames = context.getBeanNamesForType(AbstractPE.class); for (String processingElementBeanName : processingElementBeanNames) { AbstractPE bean = (AbstractPE) context.getBean(processingElementBeanName); bean.setClock(clock); try { bean.setSafeKeeper((SafeKeeper) context.getBean("safeKeeper")); } catch (NoSuchBeanDefinitionException ignored) { // no safe keeper = no checkpointing / recovery } // if the application did not specify an id, use the Spring bean name if (bean.getId() == null) { bean.setId(processingElementBeanName); } System.out.println("Adding processing element with bean name " + processingElementBeanName + ", id " + ((AbstractPE) bean).getId()); peContainer.addProcessor((AbstractPE) bean); } } }
From source file:au.org.ala.names.search.DwcaNameIndexer.java
/** * Example run/*from w w w . j a v a 2 s . c om*/ * * java cp .:names.jar au.org.ala.checklist.lucene.DwcaNameIndexer * -all * -dwca /data/bie-staging/names-lists/dwca-col * -target /data/lucene/testdwc-namematching * -irmng /data/bie-staging/irmng/IRMNG_DWC_HOMONYMS * -common /data/bie-staging/ala-names/col_vernacular.txt * * @param args */ public static void main(String[] args) { final String DEFAULT_DWCA = "/data/lucene/sources/dwca-col"; final String DEFAULT_IRMNG = "/data/lucene/sources/IRMNG_DWC_HOMONYMS"; final String DEFAULT_COMMON_NAME = "/data/lucene/sources/col_vernacular.txt"; final String DEFAULT_TARGET_DIR = "/data/lucene/namematching"; final String DEFAULT_TMP_DIR = "/data/lucene/nmload-tmp"; Options options = new Options(); options.addOption("v", "version", false, "Retrieve version information"); options.addOption("h", "help", false, "Retrieve options"); options.addOption("all", false, "Generates the load index and search index"); options.addOption("load", false, "Generate the load index only. " + "The load index is a temporary index generated from the raw data files" + " used to load the main search index"); options.addOption("search", false, "Generates the search index. A load index must already be created for this to run."); options.addOption("irmng", true, "The absolute path to the unzipped irmng DwCA. IRMNG is used to detect homonyms. Defaults to " + DEFAULT_IRMNG); options.addOption("dwca", true, "The absolute path to the unzipped DwCA for the scientific names. Defaults to " + DEFAULT_DWCA); options.addOption("target", true, "The target directory to write the new name index to. Defaults to " + DEFAULT_TARGET_DIR); options.addOption("tmp", true, "The tmp directory for the load index. Defaults to " + DEFAULT_TMP_DIR); options.addOption("common", true, "The common (vernacular) name file. Defaults to " + DEFAULT_COMMON_NAME); options.addOption("testSearch", true, "Debug a name search. This uses the target directory to search against."); CommandLineParser parser = new BasicParser(); try { // parse the command line arguments CommandLine line = parser.parse(options, args); if (line.hasOption("v")) { //only load the properties file if it exists otherwise default to the biocache-test-config.properties on the classpath InputStream stream = DwcaNameIndexer.class.getResourceAsStream("/git.properties"); Properties properties = new Properties(); if (stream != null) { properties.load(stream); properties.list(System.out); } else { System.err.println("Unable to retrieve versioning information"); } System.exit(-1); } if (line.hasOption("help")) { //only load the properties file if it exists otherwise default to the biocache-test-config.properties on the classpath new HelpFormatter().printHelp("nameindexer", options); System.exit(-1); } if (line.hasOption("testSearch")) { boolean indexExists = (new File(DEFAULT_TARGET_DIR).exists()); if (indexExists) { //do a name search - with option flag pointing to index location System.out.println("Search for name"); ALANameSearcher searcher = new ALANameSearcher( line.getOptionValue("target", DEFAULT_TARGET_DIR)); NameSearchResult nsr = searcher.searchForRecord(line.getOptionValue("testSearch")); if (nsr != null) { Map<String, String> props = nsr.toMap(); for (Map.Entry<String, String> entry : props.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } } else { System.err.println("No match for " + line.getOptionValue("testSearch")); } } else { System.err.println("Index unreadable. Check " + DEFAULT_TARGET_DIR); } System.exit(-1); } boolean load = line.hasOption("load") || line.hasOption("all"); boolean search = line.hasOption("search") || line.hasOption("all"); if (!line.hasOption("load") && !line.hasOption("search") && !line.hasOption("all")) { load = true; search = true; } log.info("Generating loading index: " + load); log.info("Generating searching index: " + search); boolean defaultIrmngReadable = (new File(DEFAULT_IRMNG).exists()); boolean defaultCommonReadable = (new File(DEFAULT_COMMON_NAME).exists()); boolean defaultDwcaReadable = (new File(DEFAULT_DWCA).exists()); if (line.getOptionValue("dwca") != null) { log.info("Using the DwCA name file: " + line.getOptionValue("dwca")); } else if (defaultDwcaReadable) { log.info("Using the default DwCA name file: " + DEFAULT_DWCA); } else { log.error( "No DwC Archive specified and the default file path does not exist or is inaccessible. Default path: " + DEFAULT_DWCA); System.exit(-1); } if (line.getOptionValue("irmng") == null && !defaultIrmngReadable) { log.warn( "No IRMNG export specified and the default file path does not exist or is inaccessible. Default path: " + DEFAULT_IRMNG); } else { log.info("Using the default IRMNG name file: " + DEFAULT_IRMNG); } if (line.getOptionValue("common") == null && !defaultCommonReadable) { log.warn( "No common name export specified and the default file path does not exist or is inaccessible. Default path: " + DEFAULT_COMMON_NAME); } else { log.info("Using the default common name file: " + DEFAULT_COMMON_NAME); } File targetDirectory = new File(line.getOptionValue("target", DEFAULT_TARGET_DIR)); if (targetDirectory.exists()) { String newPath = targetDirectory.getAbsolutePath() + "_" + DateFormatUtils.format(new Date(), "yyyy-MM-dd_hh-mm-ss"); log.info("Target directory already exists. Backing up to : " + newPath); File newTargetDirectory = new File(newPath); FileUtils.moveDirectory(targetDirectory, newTargetDirectory); FileUtils.forceMkdir(targetDirectory); } DwcaNameIndexer indexer = new DwcaNameIndexer(); indexer.create(load, search, line.getOptionValue("target", DEFAULT_TARGET_DIR), line.getOptionValue("tmp", DEFAULT_TMP_DIR), line.getOptionValue("dwca", DEFAULT_DWCA), line.getOptionValue("irmng", DEFAULT_IRMNG), line.getOptionValue("common", DEFAULT_COMMON_NAME)); } catch (Exception e) { e.printStackTrace(); } }
From source file:fr.inria.edelweiss.kgimport.RdfSplitter.java
/** * The application entrypoint, configured through the command line input * arguments./*w w w . ja va 2 s. c om*/ * * @param args the input command line arguments. */ public static void main(String args[]) { RdfSplitter rdfSplitter = new RdfSplitter(); Options options = new Options(); Option helpOpt = new Option("h", "help", false, "Print usage information."); Option inDirOpt = new Option("i", "input-dir", true, "The directory containing RDF files to be loaded."); Option outDirOpt = new Option("o", "output-dir", true, "The directory containing the generated RDF fragments"); Option predFiltOpt = new Option("p", "predicate-filter", true, "Predicate filter used to segment the dataset. " + "You can use multiple filters, typically one per fragment."); Option fragNbOpt = new Option("n", "number-of-fragments", true, "Number of fragments generated for the whole input dataset."); Option fragRepOpt = new Option("f", "fractionning-percentage", true, "Percentage of the whole input dataset for this fragment."); Option tdbOpt = new Option("tdb", "tdb-storage", false, "RDF fragments are persisted into a Jena TDB backend."); Option versionOpt = new Option("v", "version", false, "Print the version information and exit."); options.addOption(inDirOpt); options.addOption(outDirOpt); options.addOption(predFiltOpt); options.addOption(helpOpt); options.addOption(versionOpt); options.addOption(fragNbOpt); options.addOption(fragRepOpt); options.addOption(tdbOpt); String header = "RDF data fragmentation tool command line interface"; String footer = "\nPlease report any issue to alban.gaignard@cnrs.fr"; CommandLineParser parser = new BasicParser(); CommandLine cmd = null; try { cmd = parser.parse(options, args); if (cmd.hasOption("h")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("java -jar [].jar", header, options, footer, true); System.exit(0); } if (!cmd.hasOption("i")) { logger.warn("You must specify a valid input directory !"); System.exit(-1); } else { rdfSplitter.setInputDirPath(cmd.getOptionValue("i")); } if (!cmd.hasOption("o")) { logger.warn("You must specify a valid output directory !"); System.exit(-1); } else { rdfSplitter.setOutputDirPath(cmd.getOptionValue("o")); } if (cmd.hasOption("p")) { rdfSplitter.setInputPredicates(new ArrayList<String>(Arrays.asList(cmd.getOptionValues("p")))); } if (cmd.hasOption("f")) { ArrayList<String> opts = new ArrayList<String>(Arrays.asList(cmd.getOptionValues("f"))); for (String opt : opts) { try { rdfSplitter.getFragList().add(Integer.parseInt(opt)); } catch (NumberFormatException e) { logger.error(opt + " cannot be pased as an percentage value."); System.exit(-1); } } } if (cmd.hasOption("n")) { try { rdfSplitter.setFragNb(Integer.parseInt(cmd.getOptionValue("n"))); } catch (NumberFormatException e) { logger.error(cmd.getOptionValue("n") + " cannot be pased as an integer value."); System.exit(-1); } } File oDir = new File(rdfSplitter.getOutputDirPath()); if (oDir.exists()) { logger.warn(rdfSplitter.getOutputDirPath() + " already exists !"); oDir = Files.createTempDir(); logger.warn(oDir.getAbsolutePath() + " created."); rdfSplitter.setOutputDirPath(oDir.getAbsolutePath()); } else { if (oDir.mkdir()) { logger.info(rdfSplitter.getOutputDirPath() + " created."); } } if (!cmd.hasOption("n") && !cmd.hasOption("f") && !cmd.hasOption("p")) { logger.error("You must specify just one fragmentation type through '-n', '-f', or 'p' options"); for (String arg : args) { logger.trace(arg); } System.exit(-1); } String fragName = rdfSplitter.getInputDirPath() .substring(rdfSplitter.getInputDirPath().lastIndexOf("/") + 1); //Input data loading Model model = ModelFactory.createDefaultModel(); File inputDir = new File(rdfSplitter.getInputDirPath()); if (inputDir.isDirectory()) { for (File f : inputDir.listFiles()) { logger.info("Loading " + f.getAbsolutePath()); if (f.isDirectory()) { String directory = f.getAbsolutePath(); Dataset dataset = TDBFactory.createDataset(directory); dataset.begin(ReadWrite.READ); // Get model inside the transaction model.add(dataset.getDefaultModel()); dataset.end(); } else { InputStream iS; try { iS = new FileInputStream(f); if (f.getAbsolutePath().endsWith(".n3")) { model.read(iS, null, "N3"); } else if (f.getAbsolutePath().endsWith(".nt")) { model.read(iS, null, "N-TRIPLES"); } else if (f.getAbsolutePath().endsWith(".rdf")) { model.read(iS, null); } } catch (FileNotFoundException ex) { LogManager.getLogger(RdfSplitter.class.getName()).log(Level.ERROR, "", ex); } } } logger.info("Loaded " + model.size() + " triples"); } else { System.exit(0); } StopWatch sw = new StopWatch(); if (cmd.hasOption("n")) { sw.start(); if (cmd.hasOption("tdb")) { rdfSplitter.saveFragmentsTDB(rdfSplitter.getFragHoriz(model, rdfSplitter.getFragNb()), "Homog-" + fragName); } else { rdfSplitter.saveFragmentsRDF(rdfSplitter.getFragHoriz(model, rdfSplitter.getFragNb()), "Homog-" + fragName); } logger.info("Homog horiz frag in " + sw.getTime() + "ms"); sw.reset(); } else if (cmd.hasOption("f")) { sw.start(); if (cmd.hasOption("tdb")) { rdfSplitter.saveFragmentsTDB(rdfSplitter.getFragHoriz(model, rdfSplitter.getFragList()), "Inhomog-" + fragName); } else { rdfSplitter.saveFragmentsRDF(rdfSplitter.getFragHoriz(model, rdfSplitter.getFragList()), "Inhomog-" + fragName); } logger.info("Inhomog horiz frag in " + sw.getTime() + "ms"); sw.reset(); } else if (cmd.hasOption("p")) { sw.start(); if (cmd.hasOption("tdb")) { rdfSplitter.saveFragmentsTDB(rdfSplitter.getFragVert(model, rdfSplitter.getInputPredicates())); } else { rdfSplitter.saveFragmentsRDF(rdfSplitter.getFragVert(model, rdfSplitter.getInputPredicates())); } logger.info("Vert frag in " + sw.getTime() + "ms"); sw.reset(); } } catch (ParseException ex) { logger.error("Impossible to parse the input command line " + cmd.toString()); } }
From source file:com.github.pitzcarraldo.dissimilar.Dissimilar.java
/** * Main method//from ww w . ja v a 2 s. co m * @param args command line arguments */ public static void main(String[] args) { Properties mavenProps = new Properties(); try { mavenProps.load(Dissimilar.class.getClassLoader() .getResourceAsStream("META-INF/maven/uk.bl.dpt.dissimilar/dissimilar/pom.properties")); version = mavenProps.getProperty("version"); } catch (Exception e) { } boolean calcPSNR = true; boolean calcSSIM = true; String heatMapImage = null; CommandLineParser parser = new PosixParser(); Options options = new Options(); options.addOption("m", "heatmap", true, "file to save the ssim heatmap to (png)"); options.addOption("p", "psnr", false, "calculate just psnr"); options.addOption("s", "ssim", false, "calculate just ssim"); options.addOption("h", "help", false, "help text"); CommandLine com = null; try { com = parser.parse(options, args); } catch (ParseException e) { HelpFormatter help = new HelpFormatter(); help.printHelp("Dissimilar v" + version, options); return; } if (com.hasOption("help")) { HelpFormatter help = new HelpFormatter(); help.printHelp("Dissimilar v" + version, options); return; } if (com.hasOption("psnr") & com.hasOption("ssim")) { //do nothing - both on by default } else { if (com.hasOption("psnr")) { calcPSNR = true; calcSSIM = false; } if (com.hasOption("ssim")) { calcPSNR = false; calcSSIM = true; } } if (com.hasOption("heatmap")) { heatMapImage = com.getOptionValue("heatmap"); } File one = new File(com.getArgs()[0]); File two = new File(com.getArgs()[1]); if (one.exists() && two.exists()) { compare(one, two, heatMapImage, calcSSIM, calcPSNR); } }
From source file:at.ac.tuwien.inso.subcat.postprocessor.PostProcessor.java
public static void main(String[] args) { Map<String, PostProcessorTask> steps = new HashMap<String, PostProcessorTask>(); PostProcessorTask _step = new ClassificationTask(); steps.put(_step.getName(), _step);/*from ww w .j av a2s . c o m*/ CommentAnalyserTask commentAnalysisStep = new CommentAnalyserTask(); steps.put(commentAnalysisStep.getName(), commentAnalysisStep); AccountInterlinkingTask interlinkingTask = new AccountInterlinkingTask(); steps.put(interlinkingTask.getName(), interlinkingTask); _step = new CommitBugInterlinkingTask(); steps.put(_step.getName(), _step); Options options = new Options(); options.addOption("h", "help", false, "Show this options"); options.addOption("d", "db", true, "The database to process (required)"); options.addOption("v", "verbose", false, "Show details"); options.addOption("p", "project", true, "The project ID to process"); options.addOption("P", "list-projects", false, "List all registered projects"); options.addOption("S", "list-processor-steps", false, "List all registered processor steps"); options.addOption("s", "processor-step", true, "A processor step name"); options.addOption("c", "commit-dictionary", true, "Path to a classification dictionary for commit message classification"); options.addOption("b", "bug-dictionary", true, "Path to a classification dictionary for bug classification"); options.addOption("m", "smart-matching", true, "Smart user matching configuration. Syntax: <method>:<distance>"); options.addOption("M", "list-matching-methods", false, "List smart matching methods"); final Reporter reporter = new Reporter(true); reporter.startTimer(); Settings settings = new Settings(); ModelPool pool = null; boolean printTraces = false; CommandLineParser parser = new PosixParser(); try { CommandLine cmd = parser.parse(options, args); printTraces = cmd.hasOption("verbose"); if (cmd.hasOption("help")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("postprocessor", options); return; } if (cmd.hasOption("list-processor-steps")) { for (String proj : steps.keySet()) { System.out.println(" " + proj); } return; } if (cmd.hasOption("list-matching-methods")) { for (String method : HashFunc.getHashFuncNames()) { System.out.println(" " + method); } return; } if (cmd.hasOption("db") == false) { reporter.error("post-processor", "Option --db is required"); reporter.printSummary(); return; } File dbf = new File(cmd.getOptionValue("db")); if (dbf.exists() == false || dbf.isFile() == false) { reporter.error("post-processor", "Invalid database file path"); reporter.printSummary(); return; } pool = new ModelPool(cmd.getOptionValue("db"), 2); if (cmd.hasOption("list-projects")) { Model model = pool.getModel(); for (Project proj : model.getProjects()) { System.out.println(" " + proj.getId() + ": " + proj.getDate()); } model.close(); return; } Integer projId = null; if (cmd.hasOption("project") == false) { reporter.error("post-processor", "Option --project is required"); reporter.printSummary(); return; } else { try { projId = Integer.parseInt(cmd.getOptionValue("project")); } catch (NumberFormatException e) { reporter.error("post-processor", "Invalid project ID"); reporter.printSummary(); return; } } Model model = pool.getModel(); Project project = model.getProject(projId); model.close(); if (project == null) { reporter.error("post-processor", "Invalid project ID"); reporter.printSummary(); return; } if (cmd.hasOption("bug-dictionary")) { DictionaryParser dp = new DictionaryParser(); for (String path : cmd.getOptionValues("bug-dictionary")) { try { Dictionary dict = dp.parseFile(path); settings.bugDictionaries.add(dict); } catch (FileNotFoundException e) { reporter.error("post-processor", "File not found: " + path + ": " + e.getMessage()); reporter.printSummary(); return; } catch (XmlReaderException e) { reporter.error("post-processor", "XML Error: " + path + ": " + e.getMessage()); reporter.printSummary(); return; } } } if (cmd.hasOption("commit-dictionary")) { DictionaryParser dp = new DictionaryParser(); for (String path : cmd.getOptionValues("commit-dictionary")) { try { Dictionary dict = dp.parseFile(path); settings.srcDictionaries.add(dict); } catch (FileNotFoundException e) { reporter.error("post-processor", "File not found: " + path + ": " + e.getMessage()); reporter.printSummary(); return; } catch (XmlReaderException e) { reporter.error("post-processor", "XML Error: " + path + ": " + e.getMessage()); reporter.printSummary(); return; } } } if (cmd.hasOption("smart-matching")) { String str = cmd.getOptionValue("smart-matching"); String[] parts = str.split(":"); if (parts.length != 2) { reporter.error("post-processor", "Unexpected smart-matching format"); reporter.printSummary(); return; } HashFunc func = HashFunc.getHashFunc(parts[0]); if (func == null) { reporter.error("post-processor", "Unknown smart matching hash function"); reporter.printSummary(); return; } int dist = -1; try { dist = Integer.parseInt(parts[1]); } catch (NumberFormatException e) { dist = -1; } if (dist < 0) { reporter.error("post-processor", "Invalid smart matching edist distance"); reporter.printSummary(); return; } interlinkingTask.setDistance(dist); interlinkingTask.setHashFunc(func); } PostProcessor processor = new PostProcessor(project, pool, settings); if (cmd.hasOption("processor-step")) { for (String stepName : cmd.getOptionValues("processor-step")) { PostProcessorTask step = steps.get(stepName); if (step == null) { reporter.error("post-processor", "Unknown processor step: '" + stepName + "'"); reporter.printSummary(); return; } processor.register(step); } } else { processor.register(steps.values()); } if (printTraces == true) { model = pool.getModel(); final Stats stats = model.getStats(project); model.close(); processor.addListener(new PostProcessorListener() { private int commitCount = 0; private int bugCount = 0; @Override public void commit(PostProcessor proc) { commitCount++; reporter.note("post-processor", "status: Commit " + commitCount + "/" + stats.commitCount); } @Override public void bug(PostProcessor proc) { bugCount++; reporter.note("post-processor", "status: Bug " + bugCount + "/" + stats.bugCount); } }); } processor.process(); } catch (ParseException e) { reporter.error("post-processor", "Parsing failed: " + e.getMessage()); if (printTraces == true) { e.printStackTrace(); } } catch (ClassNotFoundException e) { reporter.error("post-processor", "Failed to create a database connection: " + e.getMessage()); if (printTraces == true) { e.printStackTrace(); } } catch (SQLException e) { reporter.error("post-processor", "Failed to create a database connection: " + e.getMessage()); if (printTraces == true) { e.printStackTrace(); } } catch (PostProcessorException e) { reporter.error("post-processor", "Post-Processor Error: " + e.getMessage()); if (printTraces == true) { e.printStackTrace(); } } finally { if (pool != null) { pool.close(); } } reporter.printSummary(true); }
From source file:com.linkedin.sample.Main.java
public static void main(String[] args) { /*//from w w w. j ava2s .c om we need a OAuthService to handle authentication and the subsequent calls. Since we are going to use the REST APIs we need to generate a request token as the first step in the call. Once we get an access toke we can continue to use that until the API key changes or auth is revoked. Therefore, to make this sample easier to re-use we serialize the AuthHandler (which stores the access token) to disk and then reuse it. When you first run this code please insure that you fill in the API_KEY and API_SECRET above with your own credentials and if there is a service.dat file in the code please delete it. */ //The Access Token is used in all Data calls to the APIs - it basically says our application has been given access //to the approved information in LinkedIn Token accessToken = null; //Using the Scribe library we enter the information needed to begin the chain of Oauth2 calls. OAuthService service = new ServiceBuilder().provider(LinkedInApi.class).apiKey(API_KEY) .apiSecret(API_SECRET).build(); /************************************* * This first piece of code handles all the pieces needed to be granted access to make a data call */ try { File file = new File("service.dat"); if (file.exists()) { //if the file exists we assume it has the AuthHandler in it - which in turn contains the Access Token ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file)); AuthHandler authHandler = (AuthHandler) inputStream.readObject(); accessToken = authHandler.getAccessToken(); } else { System.out.println("There is no stored Access token we need to make one"); //In the constructor the AuthHandler goes through the chain of calls to create an Access Token AuthHandler authHandler = new AuthHandler(service); ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("service.dat")); outputStream.writeObject(authHandler); outputStream.close(); accessToken = authHandler.getAccessToken(); } } catch (Exception e) { System.out.println("Threw an exception when serializing: " + e.getClass() + " :: " + e.getMessage()); } /* * We are all done getting access - time to get busy getting data *************************************/ /************************** * * Querying the LinkedIn API * **************************/ System.out.println(); System.out.println("********A basic user profile call********"); //The ~ means yourself - so this should return the basic default information for your profile in XML format //https://developer.linkedin.com/documents/profile-api String url = "http://api.linkedin.com/v1/people/~"; OAuthRequest request = new OAuthRequest(Verb.GET, url); service.signRequest(accessToken, request); Response response = request.send(); System.out.println(response.getBody()); System.out.println(); System.out.println(); System.out.println("********Get the profile in JSON********"); //This basic call profile in JSON format //You can read more about JSON here http://json.org url = "http://api.linkedin.com/v1/people/~"; request = new OAuthRequest(Verb.GET, url); request.addHeader("x-li-format", "json"); service.signRequest(accessToken, request); response = request.send(); System.out.println(response.getBody()); System.out.println(); System.out.println(); System.out.println("********Get the profile in JSON using query parameter********"); //This basic call profile in JSON format. Please note the call above is the preferred method. //You can read more about JSON here http://json.org url = "http://api.linkedin.com/v1/people/~"; request = new OAuthRequest(Verb.GET, url); request.addQuerystringParameter("format", "json"); service.signRequest(accessToken, request); response = request.send(); System.out.println(response.getBody()); System.out.println(); System.out.println(); System.out.println("********Get my connections - going into a resource********"); //This basic call gets all your connections each one will be in a person tag with some profile information //https://developer.linkedin.com/documents/connections-api url = "http://api.linkedin.com/v1/people/~/connections"; request = new OAuthRequest(Verb.GET, url); service.signRequest(accessToken, request); response = request.send(); System.out.println(response.getBody()); System.out.println(); System.out.println(); System.out.println("********Get only 10 connections - using parameters********"); //This basic call gets only 10 connections - each one will be in a person tag with some profile information //https://developer.linkedin.com/documents/connections-api //more basic about query strings in a URL here http://en.wikipedia.org/wiki/Query_string url = "http://api.linkedin.com/v1/people/~/connections"; request = new OAuthRequest(Verb.GET, url); request.addQuerystringParameter("count", "10"); service.signRequest(accessToken, request); response = request.send(); System.out.println(response.getBody()); System.out.println(); System.out.println(); System.out.println("********GET network updates that are CONN and SHAR********"); //This basic call get connection updates from your connections //https://developer.linkedin.com/documents/get-network-updates-and-statistics-api //specifics on updates https://developer.linkedin.com/documents/network-update-types url = "http://api.linkedin.com/v1/people/~/network/updates"; request = new OAuthRequest(Verb.GET, url); request.addQuerystringParameter("type", "SHAR"); request.addQuerystringParameter("type", "CONN"); service.signRequest(accessToken, request); response = request.send(); System.out.println(response.getBody()); System.out.println(); System.out.println(); System.out.println("********People Search using facets and Encoding input parameters i.e. UTF8********"); //This basic call get connection updates from your connections //https://developer.linkedin.com/documents/people-search-api#Facets //Why doesn't this look like //people-search?title=developer&location=fr&industry=4 //url = "http://api.linkedin.com/v1/people-search?title=D%C3%A9veloppeur&facets=location,industry&facet=location,fr,0"; url = "http://api.linkedin.com/v1/people-search:(people:(first-name,last-name,headline),facets:(code,buckets))"; request = new OAuthRequest(Verb.GET, url); request.addQuerystringParameter("title", "Dveloppeur"); request.addQuerystringParameter("facet", "industry,4"); request.addQuerystringParameter("facets", "location,industry"); System.out.println(request.getUrl()); service.signRequest(accessToken, request); response = request.send(); System.out.println(response.getBody()); System.out.println(); System.out.println(); /////////////////field selectors System.out.println("********A basic user profile call with field selectors********"); //The ~ means yourself - so this should return the basic default information for your profile in XML format //https://developer.linkedin.com/documents/field-selectors url = "http://api.linkedin.com/v1/people/~:(first-name,last-name,positions)"; request = new OAuthRequest(Verb.GET, url); service.signRequest(accessToken, request); response = request.send(); System.out.println(response.getHeaders().toString()); System.out.println(response.getBody()); System.out.println(); System.out.println(); System.out .println("********A basic user profile call with field selectors going into a subresource********"); //The ~ means yourself - so this should return the basic default information for your profile in XML format //https://developer.linkedin.com/documents/field-selectors url = "http://api.linkedin.com/v1/people/~:(first-name,last-name,positions:(company:(name)))"; request = new OAuthRequest(Verb.GET, url); service.signRequest(accessToken, request); response = request.send(); System.out.println(response.getHeaders().toString()); System.out.println(response.getBody()); System.out.println(); System.out.println(); System.out.println("********A basic user profile call into a subresource return data in JSON********"); //The ~ means yourself - so this should return the basic default information for your profile //https://developer.linkedin.com/documents/field-selectors url = "https://api.linkedin.com/v1/people/~/connections:(first-name,last-name,headline)?format=json"; request = new OAuthRequest(Verb.GET, url); service.signRequest(accessToken, request); response = request.send(); System.out.println(response.getHeaders().toString()); System.out.println(response.getBody()); System.out.println(); System.out.println(); System.out.println("********A more complicated example using postings into groups********"); //https://developer.linkedin.com/documents/field-selectors //https://developer.linkedin.com/documents/groups-api url = "http://api.linkedin.com/v1/groups/3297124/posts:(id,category,creator:(id,first-name,last-name),title,summary,creation-timestamp,site-group-post-url,comments,likes)"; request = new OAuthRequest(Verb.GET, url); service.signRequest(accessToken, request); response = request.send(); System.out.println(response.getHeaders().toString()); System.out.println(response.getBody()); System.out.println(); System.out.println(); /************************** * * Wrting to the LinkedIn API * **************************/ /* * Commented out so we don't write into your LinkedIn/Twitter feed while you are just testing out * some code. Uncomment if you'd like to see writes in action. * * System.out.println("********Write to the share - using XML********"); //This basic shares some basic information on the users activity stream //https://developer.linkedin.com/documents/share-api url = "http://api.linkedin.com/v1/people/~/shares"; request = new OAuthRequest(Verb.POST, url); request.addHeader("Content-Type", "text/xml"); //Make an XML document Document doc = DocumentHelper.createDocument(); Element share = doc.addElement("share"); share.addElement("comment").addText("Guess who is testing the LinkedIn REST APIs"); Element content = share.addElement("content"); content.addElement("title").addText("A title for your share"); content.addElement("submitted-url").addText("http://developer.linkedin.com"); share.addElement("visibility").addElement("code").addText("anyone"); request.addPayload(doc.asXML()); service.signRequest(accessToken, request); response = request.send(); //there is no body just a header System.out.println(response.getBody()); System.out.println(response.getHeaders().toString()); System.out.println();System.out.println(); System.out.println("********Write to the share and to Twitter - using XML********"); //This basic shares some basic information on the users activity stream //https://developer.linkedin.com/documents/share-api url = "http://api.linkedin.com/v1/people/~/shares"; request = new OAuthRequest(Verb.POST, url); request.addQuerystringParameter("twitter-post","true"); request.addHeader("Content-Type", "text/xml"); //Make an XML document doc = DocumentHelper.createDocument(); share = doc.addElement("share"); share.addElement("comment").addText("Guess who is testing the LinkedIn REST APIs and sending to twitter"); content = share.addElement("content"); content.addElement("title").addText("A title for your share"); content.addElement("submitted-url").addText("http://developer.linkedin.com"); share.addElement("visibility").addElement("code").addText("anyone"); request.addPayload(doc.asXML()); service.signRequest(accessToken, request); response = request.send(); //there is no body just a header System.out.println(response.getBody()); System.out.println(response.getHeaders().toString()); System.out.println();System.out.println(); System.out.println("********Write to the share and to twitter - using JSON ********"); //This basic shares some basic information on the users activity stream //https://developer.linkedin.com/documents/share-api //NOTE - a good troubleshooting step is to validate your JSON on jsonlint.org url = "http://api.linkedin.com/v1/people/~/shares"; request = new OAuthRequest(Verb.POST, url); //set the headers to the server knows what we are sending request.addHeader("Content-Type", "application/json"); request.addHeader("x-li-format", "json"); //make the json payload using json-simple Map<String, Object> jsonMap = new HashMap<String, Object>(); jsonMap.put("comment", "Posting from the API using JSON"); JSONObject contentObject = new JSONObject(); contentObject.put("title", "This is a another test post"); contentObject.put("submitted-url","http://www.linkedin.com"); contentObject.put("submitted-image-url", "http://press.linkedin.com/sites/all/themes/presslinkedin/images/LinkedIn_WebLogo_LowResExample.jpg"); jsonMap.put("content", contentObject); JSONObject visibilityObject = new JSONObject(); visibilityObject.put("code", "anyone"); jsonMap.put("visibility", visibilityObject); request.addPayload(JSONValue.toJSONString(jsonMap)); service.signRequest(accessToken, request); response = request.send(); //again no body - just headers System.out.println(response.getBody()); System.out.println(response.getHeaders().toString()); System.out.println();System.out.println(); */ /************************** * * Understanding the response, creating logging, request and response headers * **************************/ System.out.println(); System.out.println("********A basic user profile call and response dissected********"); //This sample is mostly to help you debug and understand some of the scaffolding around the request-response cycle //https://developer.linkedin.com/documents/debugging-api-calls url = "https://api.linkedin.com/v1/people/~"; request = new OAuthRequest(Verb.GET, url); service.signRequest(accessToken, request); response = request.send(); //get all the headers System.out.println("Request headers: " + request.getHeaders().toString()); System.out.println("Response headers: " + response.getHeaders().toString()); //url requested System.out.println("Original location is: " + request.getHeaders().get("content-location")); //Date of response System.out.println("The datetime of the response is: " + response.getHeader("Date")); //the format of the response System.out.println("Format is: " + response.getHeader("x-li-format")); //Content-type of the response System.out.println("Content type is: " + response.getHeader("Content-Type") + "\n\n"); //get the HTTP response code - such as 200 or 404 int responseNumber = response.getCode(); if (responseNumber >= 199 && responseNumber < 300) { System.out.println("HOORAY IT WORKED!!"); System.out.println(response.getBody()); } else if (responseNumber >= 500 && responseNumber < 600) { //you could actually raise an exception here in your own code System.out.println("Ruh Roh application error of type 500: " + responseNumber); System.out.println(response.getBody()); } else if (responseNumber == 403) { System.out.println("A 403 was returned which usually means you have reached a throttle limit"); } else if (responseNumber == 401) { System.out.println("A 401 was returned which is a Oauth signature error"); System.out.println(response.getBody()); } else if (responseNumber == 405) { System.out.println( "A 405 response was received. Usually this means you used the wrong HTTP method (GET when you should POST, etc)."); } else { System.out.println("We got a different response that we should add to the list: " + responseNumber + " and report it in the forums"); System.out.println(response.getBody()); } System.out.println(); System.out.println(); System.out.println("********A basic error logging function********"); // Now demonstrate how to make a logging function which provides us the info we need to // properly help debug issues. Please use the logged block from here when requesting // help in the forums. url = "https://api.linkedin.com/v1/people/FOOBARBAZ"; request = new OAuthRequest(Verb.GET, url); service.signRequest(accessToken, request); response = request.send(); responseNumber = response.getCode(); if (responseNumber < 200 || responseNumber >= 300) { logDiagnostics(request, response); } else { System.out.println("You were supposed to submit a bad request"); } System.out.println("******Finished******"); }
From source file:at.ac.tuwien.inso.subcat.reporter.Reporter.java
public static void main(String[] args) { Options options = new Options(); options.addOption("h", "help", false, "Show this options"); options.addOption("d", "db", true, "The database to process (required)"); options.addOption("p", "project", true, "The project ID to process"); options.addOption("P", "list-projects", false, "List all registered projects"); options.addOption("C", "config", true, "A configuration file including reports"); options.addOption("F", "list-formats", false, "List all supported output formats"); options.addOption("f", "format", true, "Output format"); options.addOption("R", "list-reports", false, "List all report types"); options.addOption("r", "report", true, "Report type"); options.addOption("o", "output", true, "Output path"); options.addOption("c", "commit-dictionary", true, "The commit dictionary ID to use"); options.addOption("b", "bug-dictionary", true, "The bug dictionary ID to use"); options.addOption("D", "list-dictionaries", false, "List all dictionaries"); options.addOption("v", "verbose", false, "Show details"); at.ac.tuwien.inso.subcat.utility.Reporter errReporter = new at.ac.tuwien.inso.subcat.utility.Reporter( false);/*www .j a v a 2 s. c o m*/ Settings settings = new Settings(); boolean verbose = false; ModelPool pool = null; Model model = null; CommandLineParser parser = new PosixParser(); try { CommandLine cmd = parser.parse(options, args); verbose = cmd.hasOption("verbose"); if (cmd.hasOption("help")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("postprocessor", options); return; } if (cmd.hasOption("db") == false) { errReporter.error("reporter", "Option --db is required"); errReporter.printSummary(); return; } if (cmd.hasOption("config") == false) { errReporter.error("reporter", "Option --config is required"); errReporter.printSummary(); return; } Configuration config = new Configuration(); Parser configParser = new Parser(); try { configParser.parse(config, new File(cmd.getOptionValue("config"))); } catch (IOException e) { errReporter.error("reporter", "Could not read configuration file: " + e.getMessage()); errReporter.printSummary(); return; } catch (ParserException e) { errReporter.error("reporter", "Could not parse configuration file: " + e.getMessage()); errReporter.printSummary(); return; } if (cmd.hasOption("list-reports")) { int i = 1; for (ExporterConfig exconf : config.getExporterConfigs()) { System.out.println(" (" + i + ") " + exconf.getName()); i++; } return; } File dbf = new File(cmd.getOptionValue("db")); if (dbf.exists() == false || dbf.isFile() == false) { errReporter.error("reporter", "Invalid database file path"); errReporter.printSummary(); return; } pool = new ModelPool(cmd.getOptionValue("db"), 2); pool.setPrintTemplates(verbose); model = pool.getModel(); if (cmd.hasOption("list-formats")) { Reporter exporter = new Reporter(model); int i = 1; for (ReportWriter formatter : exporter.getWriters()) { System.out.println(" (" + i + ") " + formatter.getLabel()); i++; } return; } if (cmd.hasOption("list-projects")) { for (Project proj : model.getProjects()) { System.out.println(" " + proj.getId() + ": " + proj.getDate()); } return; } Integer projId = null; if (cmd.hasOption("project") == false) { errReporter.error("reporter", "Option --project is required"); errReporter.printSummary(); return; } else { try { projId = Integer.parseInt(cmd.getOptionValue("project")); } catch (NumberFormatException e) { errReporter.error("reporter", "Invalid project ID"); errReporter.printSummary(); return; } } if (cmd.hasOption("output") == false) { errReporter.error("reporter", "Option --output is required"); errReporter.printSummary(); return; } String outputPath = cmd.getOptionValue("output"); model = pool.getModel(); Project project = model.getProject(projId); if (project == null) { errReporter.error("reporter", "Invalid project ID"); errReporter.printSummary(); return; } if (cmd.hasOption("list-dictionaries")) { List<at.ac.tuwien.inso.subcat.model.Dictionary> dictionaries = model.getDictionaries(project); for (at.ac.tuwien.inso.subcat.model.Dictionary dict : dictionaries) { System.out .println(" (" + dict.getId() + ") " + " " + dict.getContext() + " " + dict.getName()); } return; } int bugDictId = -1; if (cmd.hasOption("bug-dictionary")) { try { bugDictId = Integer.parseInt(cmd.getOptionValue("bug-dictionary")); List<at.ac.tuwien.inso.subcat.model.Dictionary> dictionaries = model.getDictionaries(project); boolean valid = false; for (at.ac.tuwien.inso.subcat.model.Dictionary dict : dictionaries) { if (dict.getId() == bugDictId) { valid = true; break; } } if (valid == false) { errReporter.error("reporter", "Invalid bug dictionary ID"); errReporter.printSummary(); return; } } catch (NumberFormatException e) { errReporter.error("reporter", "Invalid bug dictionary ID"); errReporter.printSummary(); return; } } int commitDictId = -1; if (cmd.hasOption("commit-dictionary")) { try { commitDictId = Integer.parseInt(cmd.getOptionValue("commit-dictionary")); List<at.ac.tuwien.inso.subcat.model.Dictionary> dictionaries = model.getDictionaries(project); boolean valid = false; for (at.ac.tuwien.inso.subcat.model.Dictionary dict : dictionaries) { if (dict.getId() == commitDictId) { valid = true; break; } } if (valid == false) { errReporter.error("reporter", "Invalid commit dictionary ID"); errReporter.printSummary(); return; } } catch (NumberFormatException e) { errReporter.error("reporter", "Invalid commit dictionary ID"); errReporter.printSummary(); return; } } if (cmd.hasOption("format") == false) { errReporter.error("reporter", "Option --format is required"); errReporter.printSummary(); return; } Reporter exporter = new Reporter(model); ReportWriter writer = null; try { int id = Integer.parseInt(cmd.getOptionValue("format")); if (id < 1 || id > exporter.getWriters().size()) { errReporter.error("reporter", "Invalid output format"); errReporter.printSummary(); return; } writer = exporter.getWriters().get(id - 1); } catch (NumberFormatException e) { errReporter.error("reporter", "Invalid output format"); errReporter.printSummary(); return; } ExporterConfig exporterConfig = null; if (cmd.hasOption("report") == false) { errReporter.error("reporter", "Option --report is required"); errReporter.printSummary(); return; } else { try { int id = Integer.parseInt(cmd.getOptionValue("report")); if (id < 1 || id > config.getExporterConfigs().size()) { errReporter.error("reporter", "Invalid reporter ID"); errReporter.printSummary(); return; } exporterConfig = config.getExporterConfigs().get(id - 1); } catch (NumberFormatException e) { errReporter.error("reporter", "Invalid reporter ID"); errReporter.printSummary(); return; } } exporter.export(exporterConfig, project, commitDictId, bugDictId, settings, writer, outputPath); } catch (ParseException e) { errReporter.error("reporter", "Parsing failed: " + e.getMessage()); if (verbose == true) { e.printStackTrace(); } } catch (ClassNotFoundException e) { errReporter.error("reporter", "Failed to create a database connection: " + e.getMessage()); if (verbose == true) { e.printStackTrace(); } } catch (SQLException e) { errReporter.error("reporter", "Failed to create a database connection: " + e.getMessage()); if (verbose == true) { e.printStackTrace(); } } catch (ReporterException e) { errReporter.error("reporter", "Reporter Error: " + e.getMessage()); if (verbose == true) { e.printStackTrace(); } } finally { if (model != null) { model.close(); } if (pool != null) { pool.close(); } } errReporter.printSummary(); }