Example usage for java.net URI getAuthority

List of usage examples for java.net URI getAuthority

Introduction

In this page you can find the example usage for java.net URI getAuthority.

Prototype

public String getAuthority() 

Source Link

Document

Returns the decoded authority component of this URI.

Usage

From source file:org.apache.oozie.tools.OozieSharelibCLI.java

public synchronized int run(String[] args) throws Exception {
    if (used) {//from   w  ww . j  a  v a2  s  .  c o m
        throw new IllegalStateException("CLI instance already used");
    }

    used = true;

    CLIParser parser = new CLIParser("oozie-setup.sh", HELP_INFO);
    String oozieHome = System.getProperty(OOZIE_HOME);
    parser.addCommand(HELP_CMD, "", "display usage for all commands or specified command", new Options(),
            false);
    parser.addCommand(CREATE_CMD, "", "create a new timestamped version of oozie sharelib",
            createUpgradeOptions(CREATE_CMD), false);
    parser.addCommand(UPGRADE_CMD, "",
            "[deprecated][use command \"create\" to create new version]   upgrade oozie sharelib \n",
            createUpgradeOptions(UPGRADE_CMD), false);

    try {
        final CLIParser.Command command = parser.parse(args);
        String sharelibAction = command.getName();

        if (sharelibAction.equals(HELP_CMD)) {
            parser.showHelp(command.getCommandLine());
            return 0;
        }

        if (!command.getCommandLine().hasOption(FS_OPT)) {
            throw new Exception("-fs option must be specified");
        }

        int threadPoolSize = Integer.valueOf(command.getCommandLine().getOptionValue(CONCURRENCY_OPT, "1"));
        File srcFile = null;

        //Check whether user provided locallib
        if (command.getCommandLine().hasOption(LIB_OPT)) {
            srcFile = new File(command.getCommandLine().getOptionValue(LIB_OPT));
        } else {
            //Since user did not provide locallib, find the default one under oozie home dir
            Collection<File> files = FileUtils.listFiles(new File(oozieHome),
                    new WildcardFileFilter("oozie-sharelib*.tar.gz"), null);

            if (files.size() > 1) {
                throw new IOException("more than one sharelib tar found at " + oozieHome);
            }

            if (files.isEmpty()) {
                throw new IOException("default sharelib tar not found in oozie home dir: " + oozieHome);
            }

            srcFile = files.iterator().next();
        }

        File temp = File.createTempFile("oozie", ".dir");
        temp.delete();
        temp.mkdir();
        temp.deleteOnExit();

        //Check whether the lib is a tar file or folder
        if (!srcFile.isDirectory()) {
            FileUtil.unTar(srcFile, temp);
            srcFile = new File(temp.toString() + "/share/lib");
        } else {
            //Get the lib directory since it's a folder
            srcFile = new File(srcFile, "lib");
        }

        String hdfsUri = command.getCommandLine().getOptionValue(FS_OPT);
        Path srcPath = new Path(srcFile.toString());

        Services services = new Services();
        services.getConf().set(Services.CONF_SERVICE_CLASSES,
                "org.apache.oozie.service.LiteWorkflowAppService, org.apache.oozie.service.HadoopAccessorService");
        services.getConf().set(Services.CONF_SERVICE_EXT_CLASSES, "");
        services.init();
        WorkflowAppService lwas = services.get(WorkflowAppService.class);
        HadoopAccessorService has = services.get(HadoopAccessorService.class);
        Path dstPath = lwas.getSystemLibPath();

        if (sharelibAction.equals(CREATE_CMD) || sharelibAction.equals(UPGRADE_CMD)) {
            dstPath = new Path(
                    dstPath.toString() + Path.SEPARATOR + SHARE_LIB_PREFIX + getTimestampDirectory());
        }

        System.out.println("the destination path for sharelib is: " + dstPath);

        URI uri = new Path(hdfsUri).toUri();
        Configuration fsConf = has.createJobConf(uri.getAuthority());
        FileSystem fs = FileSystem.get(uri, fsConf);

        if (!srcFile.exists()) {
            throw new IOException(srcPath + " cannot be found");
        }

        if (threadPoolSize > 1) {
            concurrentCopyFromLocal(fs, threadPoolSize, srcFile, dstPath);
        } else {
            fs.copyFromLocalFile(false, srcPath, dstPath);
        }

        services.destroy();
        FileUtils.deleteDirectory(temp);

        return 0;
    } catch (ParseException ex) {
        System.err.println("Invalid sub-command: " + ex.getMessage());
        System.err.println();
        System.err.println(parser.shortHelp());
        return 1;
    } catch (Exception ex) {
        logError(ex.getMessage(), ex);
        return 1;
    }
}

From source file:org.apache.pig.tools.DownloadResolver.java

/**
 * @param uri//from   w  w  w .  j av a2s. c  om
 * @return Returns a Map containing the organization, module, version and
 *         all the query string parameters
 * @throws ParserException
 */
private Map<String, Object> parseUri(URI uri) throws ParserException {
    // Parse uri for artifact organization, module and version
    Map<String, Object> uriMap = new HashMap<String, Object>();
    String authority = uri.getAuthority();
    if (authority != null) {
        String[] tokens = authority.split(":", -1);
        if (tokens.length == 3) {
            uriMap.put("org", tokens[0]);
            if (tokens[1].isEmpty()) {
                throw new ParserException("Please specify the artifact module.");
            }
            uriMap.put("module", tokens[1]);
            uriMap.put("version", tokens[2]);
        } else {
            throw new ParserException("Invalid Artifact. Please specify organization, module and version");
        }
    } else {
        throw new ParserException("Invalid Artifact. Please specify organization, module and version");
    }

    // Parse query string for exclude list and other parameters
    uriMap.putAll(parseQueryString(uri));
    if (uriMap.containsKey("transitive")) {
        uriMap.put("transitive", Boolean.parseBoolean(uriMap.get("transitive").toString()));
    }
    List<Map<String, Object>> excludeList = new LinkedList<Map<String, Object>>();
    if (uriMap.containsKey("exclude")) {
        for (String exclude : uriMap.get("exclude").toString().split(",")) {
            Map<String, Object> excludeMap = new HashMap<String, Object>();
            String parts[] = exclude.split(":", -1);
            if (parts.length == 2) {
                excludeMap.put("group", parts[0]);
                excludeMap.put("module", parts[1]);
            } else {
                throw new ParserException("Exclude must contain organization and module separated by a colon.");
            }
            excludeList.add(excludeMap);
        }
    }
    uriMap.put("excludes", excludeList);

    return uriMap;
}

From source file:com.subgraph.vega.internal.model.web.WebPath.java

private URI generateURI() {
    final URI hostUri = mountPoint.getWebHost().getUri();
    try {//w  ww . jav a2s.c o m
        return new URI(hostUri.getScheme(), hostUri.getAuthority(), getFullPath(), null, null);
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
}

From source file:gobblin.service.FlowConfigsResource.java

/**
 * Delete a configured flow. Running flows are not affected. The schedule will be removed for scheduled flows.
 * @param key composite key containing flow group and flow name that identifies the flow to remove from the
 * {@link FlowCatalog}//from   ww w  .ja va  2 s.  co m
 * @return {@link UpdateResponse}
 */
@Override
public UpdateResponse delete(ComplexResourceKey<FlowId, EmptyRecord> key) {
    String flowGroup = key.getKey().getFlowGroup();
    String flowName = key.getKey().getFlowName();
    URI flowUri = null;

    LOG.info("Delete called with flowGroup " + flowGroup + " flowName " + flowName);

    try {
        URI flowCatalogURI = new URI("gobblin-flow", null, "/", null, null);
        flowUri = new URI(flowCatalogURI.getScheme(), flowCatalogURI.getAuthority(),
                "/" + flowGroup + "/" + flowName, null, null);
        FlowSpec flowSpec = (FlowSpec) getFlowCatalog().getSpec(flowUri);

        getFlowCatalog().remove(flowUri);

        return new UpdateResponse(HttpStatus.S_200_OK);
    } catch (URISyntaxException e) {
        logAndThrowRestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "bad URI " + flowUri, e);
    } catch (SpecNotFoundException e) {
        logAndThrowRestLiServiceException(HttpStatus.S_404_NOT_FOUND,
                "Flow does not exist: flowGroup " + flowGroup + " flowName " + flowName, null);
    }

    return null;
}

From source file:gobblin.service.FlowConfigsResource.java

/**
 * Update the flow configuration with the specified key. Running flows are not affected.
 * An error is raised if the flow configuration does not exist.
 * @param key composite key containing group name and flow name that identifies the flow to update
 * @param flowConfig new flow configuration
 * @return {@link UpdateResponse}/*from w w w.  j ava  2  s.  c  o  m*/
 */
@Override
public UpdateResponse update(ComplexResourceKey<FlowId, EmptyRecord> key, FlowConfig flowConfig) {
    String flowGroup = key.getKey().getFlowGroup();
    String flowName = key.getKey().getFlowName();
    URI flowUri = null;

    LOG.info("Update called with flowGroup " + flowGroup + " flowName " + flowName);

    if (!flowGroup.equals(flowConfig.getId().getFlowGroup())
            || !flowName.equals(flowConfig.getId().getFlowName())) {
        logAndThrowRestLiServiceException(HttpStatus.S_400_BAD_REQUEST,
                "flowName and flowGroup cannot be changed in update", null);
    }

    try {
        URI flowCatalogURI = new URI("gobblin-flow", null, "/", null, null);
        flowUri = new URI(flowCatalogURI.getScheme(), flowCatalogURI.getAuthority(),
                "/" + flowGroup + "/" + flowName, null, null);
        FlowSpec oldFlowSpec = (FlowSpec) getFlowCatalog().getSpec(flowUri);
        FlowSpec newFlowSpec = createFlowSpecForConfig(flowConfig);

        getFlowCatalog().put(newFlowSpec);

        return new UpdateResponse(HttpStatus.S_200_OK);
    } catch (URISyntaxException e) {
        logAndThrowRestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "bad URI " + flowUri, e);
    } catch (SpecNotFoundException e) {
        logAndThrowRestLiServiceException(HttpStatus.S_404_NOT_FOUND,
                "Flow does not exist: flowGroup " + flowGroup + " flowName " + flowName, null);
    }

    return null;
}

From source file:gobblin.service.FlowConfigsResource.java

/**
 * Create a flow configuration that the service will forward to execution instances for execution
 * @param flowConfig flow configuration//from  w w w . jav a  2 s .  co  m
 * @return {@link CreateResponse}
 */
@Override
public CreateResponse create(FlowConfig flowConfig) {
    LOG.info("Create called with flowName " + flowConfig.getId().getFlowName());

    LOG.info("ReadyToUse is: " + readyToUse);
    LOG.info("FlowCatalog is: " + getFlowCatalog());

    if (!readyToUse && getFlowCatalog() == null) {
        throw new RuntimeException("Not ready for use.");
    }

    try {
        URI flowCatalogURI = new URI("gobblin-flow", null, "/", null, null);
        URI flowUri = new URI(flowCatalogURI.getScheme(), flowCatalogURI.getAuthority(),
                "/" + flowConfig.getId().getFlowGroup() + "/" + flowConfig.getId().getFlowName(), null, null);

        if (getFlowCatalog().getSpec(flowUri) != null) {
            logAndThrowRestLiServiceException(HttpStatus.S_409_CONFLICT,
                    "Flow with the same name already exists: " + flowUri, null);
        }
    } catch (URISyntaxException e) {
        logAndThrowRestLiServiceException(HttpStatus.S_400_BAD_REQUEST,
                "bad URI " + flowConfig.getId().getFlowName(), e);
    } catch (SpecNotFoundException e) {
        // okay if flow does not exist
    }

    getFlowCatalog().put(createFlowSpecForConfig(flowConfig));

    return new CreateResponse(flowConfig.getId().getFlowName(), HttpStatus.S_201_CREATED);
}

From source file:com.groupon.mesos.zookeeper.ZookeeperMasterDetector.java

public ZookeeperMasterDetector(final String master, final ManagedEventBus eventBus) throws IOException {
    checkNotNull(master, "master is null");
    this.eventBus = checkNotNull(eventBus, "eventBus is null");

    final URI zookeeperUri = URI.create(master);
    checkState(zookeeperUri.getScheme().equals("zk"), "Only zk:// URIs are supported (%s)", master);

    String authority = zookeeperUri.getAuthority();
    final int atIndex = authority.indexOf('@');
    if (atIndex != -1) {
        final List<String> userPass = ImmutableList
                .copyOf(Splitter.on(':').trimResults().split(authority.substring(0, atIndex)));
        checkState(userPass.size() == 2, "found %s for user name and password", userPass);
        user = userPass.get(0);/* w w  w .ja v  a 2 s.c  o  m*/
        password = userPass.get(1);
        authority = authority.substring(atIndex + 1);
    } else {
        user = null;
        password = null;
    }

    String zookeeperPath = zookeeperUri.getPath();
    while (zookeeperPath.endsWith("/")) {
        zookeeperPath = zookeeperPath.substring(0, zookeeperPath.length() - 1);
    }
    this.zookeeperPath = zookeeperPath;

    checkState(!zookeeperPath.equals(""), "A zookeeper path must be given! (%s)", zookeeperPath);

    checkState(user == null && password == null,
            "Current version of Zkclient does not support authentication!");

    this.client = new ZkClient(authority);
    this.client.setZkSerializer(new MasterInfoZkSerializer());
}

From source file:org.apache.hadoop.hive.metastore.MultiHdfsInfo.java

private String getExternalScratchDir(URI extURI) {
    try {/*  w  w w  .  j  a  v a  2  s  . c  o m*/
        for (Path p : allScratchDirs) {
            URI pURI = p.toUri();
            if (strEquals(pURI.getScheme(), extURI.getScheme())
                    && strEquals(pURI.getAuthority(), extURI.getAuthority())) {
                return p.toString();
            }
        }
        return makeExternalScratchDir(extURI).toString();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:gobblin.service.FlowConfigsResource.java

/**
 * Retrieve the flow configuration with the given key
 * @param key flow config id key containing group name and flow name
 * @return {@link FlowConfig} with flow configuration
 *///from   ww  w  . j  av  a2  s  .c  o m
@Override
public FlowConfig get(ComplexResourceKey<FlowId, EmptyRecord> key) {
    String flowGroup = key.getKey().getFlowGroup();
    String flowName = key.getKey().getFlowName();

    LOG.info("Get called with flowGroup " + flowGroup + " flowName " + flowName);

    try {
        URI flowCatalogURI = new URI("gobblin-flow", null, "/", null, null);
        URI flowUri = new URI(flowCatalogURI.getScheme(), flowCatalogURI.getAuthority(),
                "/" + flowGroup + "/" + flowName, null, null);
        FlowSpec spec = (FlowSpec) getFlowCatalog().getSpec(flowUri);
        FlowConfig flowConfig = new FlowConfig();
        Properties flowProps = spec.getConfigAsProperties();
        Schedule schedule = null;

        if (flowProps.containsKey(ConfigurationKeys.JOB_SCHEDULE_KEY)) {
            schedule = new Schedule();
            schedule.setCronSchedule(flowProps.getProperty(ConfigurationKeys.JOB_SCHEDULE_KEY));
        }
        if (flowProps.containsKey(ConfigurationKeys.JOB_TEMPLATE_PATH)) {
            flowConfig.setTemplateUris(flowProps.getProperty(ConfigurationKeys.JOB_TEMPLATE_PATH));
        } else if (spec.getTemplateURIs().isPresent()) {
            flowConfig.setTemplateUris(StringUtils.join(spec.getTemplateURIs().get(), ","));
        } else {
            flowConfig.setTemplateUris("NA");
        }
        if (schedule != null) {
            if (flowProps.containsKey(ConfigurationKeys.FLOW_RUN_IMMEDIATELY)) {
                schedule.setRunImmediately(
                        Boolean.valueOf(flowProps.getProperty(ConfigurationKeys.FLOW_RUN_IMMEDIATELY)));
            }

            flowConfig.setSchedule(schedule);
        }

        // remove keys that were injected as part of flowSpec creation
        flowProps.remove(ConfigurationKeys.JOB_SCHEDULE_KEY);
        flowProps.remove(ConfigurationKeys.JOB_TEMPLATE_PATH);

        StringMap flowPropsAsStringMap = new StringMap();
        flowPropsAsStringMap.putAll(Maps.fromProperties(flowProps));

        return flowConfig.setId(new FlowId().setFlowGroup(flowGroup).setFlowName(flowName))
                .setProperties(flowPropsAsStringMap);
    } catch (URISyntaxException e) {
        logAndThrowRestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "bad URI " + flowName, e);
    } catch (SpecNotFoundException e) {
        logAndThrowRestLiServiceException(HttpStatus.S_404_NOT_FOUND,
                "Flow requested does not exist: " + flowName, null);
    }

    return null;
}

From source file:org.apache.hadoop.mapreduce.JobSubmitter.java

@SuppressWarnings("deprecation")
private static void addMRFrameworkToDistributedCache(Configuration conf) throws IOException {
    String framework = conf.get(MRJobConfig.MAPREDUCE_APPLICATION_FRAMEWORK_PATH, "");
    if (!framework.isEmpty()) {
        URI uri;
        try {/*from   w  w w  .  j  a v  a 2  s . c  o  m*/
            uri = new URI(framework);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("Unable to parse '" + framework
                    + "' as a URI, check the setting for " + MRJobConfig.MAPREDUCE_APPLICATION_FRAMEWORK_PATH,
                    e);
        }

        String linkedName = uri.getFragment();

        // resolve any symlinks in the URI path so using a "current" symlink
        // to point to a specific version shows the specific version
        // in the distributed cache configuration
        FileSystem fs = FileSystem.get(conf);
        Path frameworkPath = fs.makeQualified(new Path(uri.getScheme(), uri.getAuthority(), uri.getPath()));
        FileContext fc = FileContext.getFileContext(frameworkPath.toUri(), conf);
        frameworkPath = fc.resolvePath(frameworkPath);
        uri = frameworkPath.toUri();
        try {
            uri = new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), null, linkedName);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException(e);
        }

        DistributedCache.addCacheArchive(uri, conf);
    }
}