Example usage for java.util PropertyResourceBundle getString

List of usage examples for java.util PropertyResourceBundle getString

Introduction

In this page you can find the example usage for java.util PropertyResourceBundle getString.

Prototype

public final String getString(String key) 

Source Link

Document

Gets a string for the given key from this resource bundle or one of its parents.

Usage

From source file:org.nuxeo.apidoc.introspection.ServerInfo.java

protected static BundleInfoImpl computeBundleInfo(Bundle bundle) {
    RuntimeService runtime = Framework.getRuntime();
    BundleInfoImpl binfo = new BundleInfoImpl(bundle.getSymbolicName());
    binfo.setFileName(runtime.getBundleFile(bundle).getName());
    binfo.setLocation(bundle.getLocation());
    if (!(bundle instanceof BundleImpl)) {
        return binfo;
    }/*from ww  w  .j a  v  a2 s  .  c o m*/
    BundleImpl nxBundle = (BundleImpl) bundle;
    File jarFile = nxBundle.getBundleFile().getFile();
    if (jarFile == null) {
        return binfo;
    }
    try {
        if (jarFile.isDirectory()) {
            // directory: run from Eclipse in unit tests
            // .../nuxeo-runtime/nuxeo-runtime/bin
            // or sometimes
            // .../nuxeo-runtime/nuxeo-runtime/bin/main
            File manifest = new File(jarFile, META_INF_MANIFEST_MF);
            if (manifest.exists()) {
                InputStream is = new FileInputStream(manifest);
                String mf = IOUtils.toString(is, Charsets.UTF_8);
                binfo.setManifest(mf);
            }
            // find and parse pom.xml
            File up = new File(jarFile, "..");
            File pom = new File(up, POM_XML);
            if (!pom.exists()) {
                pom = new File(new File(up, ".."), POM_XML);
                if (!pom.exists()) {
                    pom = null;
                }
            }
            if (pom != null) {
                DocumentBuilder b = documentBuilderFactory.newDocumentBuilder();
                Document doc = b.parse(new FileInputStream(pom));
                XPath xpath = xpathFactory.newXPath();
                String groupId = (String) xpath.evaluate("//project/groupId", doc, XPathConstants.STRING);
                if ("".equals(groupId)) {
                    groupId = (String) xpath.evaluate("//project/parent/groupId", doc, XPathConstants.STRING);
                }
                String artifactId = (String) xpath.evaluate("//project/artifactId", doc, XPathConstants.STRING);
                if ("".equals(artifactId)) {
                    artifactId = (String) xpath.evaluate("//project/parent/artifactId", doc,
                            XPathConstants.STRING);
                }
                String version = (String) xpath.evaluate("//project/version", doc, XPathConstants.STRING);
                if ("".equals(version)) {
                    version = (String) xpath.evaluate("//project/parent/version", doc, XPathConstants.STRING);
                }
                binfo.setArtifactId(artifactId);
                binfo.setGroupId(groupId);
                binfo.setArtifactVersion(version);
            }
        } else {
            try (ZipFile zFile = new ZipFile(jarFile)) {
                ZipEntry mfEntry = zFile.getEntry(META_INF_MANIFEST_MF);
                if (mfEntry != null) {
                    try (InputStream mfStream = zFile.getInputStream(mfEntry)) {
                        String mf = IOUtils.toString(mfStream, Charsets.UTF_8);
                        binfo.setManifest(mf);
                    }
                }
                Enumeration<? extends ZipEntry> entries = zFile.entries();
                while (entries.hasMoreElements()) {
                    ZipEntry entry = entries.nextElement();
                    if (entry.getName().endsWith(POM_PROPERTIES)) {
                        try (InputStream is = zFile.getInputStream(entry)) {
                            PropertyResourceBundle prb = new PropertyResourceBundle(is);
                            String groupId = prb.getString("groupId");
                            String artifactId = prb.getString("artifactId");
                            String version = prb.getString("version");
                            binfo.setArtifactId(artifactId);
                            binfo.setGroupId(groupId);
                            binfo.setArtifactVersion(version);
                        }
                        break;
                    }
                }
            }
            try (ZipFile zFile = new ZipFile(jarFile)) {
                EmbeddedDocExtractor.extractEmbeddedDoc(zFile, binfo);
            }
        }
    } catch (IOException | ParserConfigurationException | SAXException | XPathException | NuxeoException e) {
        log.error(e, e);
    }
    return binfo;
}

From source file:com.chilmers.configbootstrapper.ConfigServletContextListener.java

/**
 * Loads the log4j configuration from a file whose location is given in the application configuration. 
 * //w ww .  jav  a2  s  .  c  o  m
 * The location of the log4j configuration file shall by default be specified in 
 * an entry in the application configuration file with key "application.log4j.config.location"
 * This key can be overridden by configuration if you need to. 
 * 
 * If no such property is found in the application configuration, log4j's default 
 * configuration mechanism will be used, e.g. it will look for log4j.xml 
 * or log4j.properties on the classpath.
 */
private void loadLoggingConfiguration(PropertyResourceBundle configBundle) {
    logToSystemOut("Finding log4j configuration location in application configuration...");

    String log4jConfigLocation = null;

    try {
        log4jConfigLocation = configBundle.getString(this.log4jConfigLocationPropertyKey);
    } catch (MissingResourceException e) {
        logToSystemOut("No log4j configuration location was found for property "
                + this.log4jConfigLocationPropertyKey + " in the application configuration. ");
    }

    if (StringUtils.isNotBlank(log4jConfigLocation)) {
        LogManager.resetConfiguration();
        logToSystemOut("Found log4j configuration location in the application configuration. "
                + "Configuring logger using file: " + log4jConfigLocation);
        if (log4jConfigLocation.startsWith("file:")) {
            log4jConfigLocation = log4jConfigLocation.replaceFirst("file:", "");
        }
        if (log4jConfigLocation.endsWith(".xml")) {
            if (log4jConfigLocation.startsWith("classpath:")) {
                log4jConfigLocation = log4jConfigLocation.replaceFirst("classpath:", "");
                InputStream is = Thread.currentThread().getContextClassLoader()
                        .getResourceAsStream(log4jConfigLocation);
                new DOMConfigurator().doConfigure(is, LogManager.getLoggerRepository());
            } else {
                DOMConfigurator.configureAndWatch(log4jConfigLocation);
            }
        } else if (log4jConfigLocation.endsWith(".properties")) {
            if (log4jConfigLocation.startsWith("classpath:")) {
                log4jConfigLocation = log4jConfigLocation.replaceFirst("classpath:", "");
                InputStream is = Thread.currentThread().getContextClassLoader()
                        .getResourceAsStream(log4jConfigLocation);
                PropertyConfigurator.configure(is);
            } else {
                PropertyConfigurator.configureAndWatch(log4jConfigLocation);
            }
        } else {
            logToSystemOut("The log4j configuration file location must end with .xml or .properties. "
                    + "\nFalling back to the default log4j configuration mechanism.");
        }
    } else {
        logToSystemOut("Didn't find log4j configuration location in application configuration. "
                + "Falling back to the default log4j configuration mechanism.");
    }
    log.info("Log4j was configured, see System.out log for initialization information.");
}

From source file:com.chilmers.configbootstrapper.ConfigServletContextListener.java

/**
 * Loads system properties from the application configuration
 *  /*from ww w.  java  2  s  . c o  m*/
 * @param configBundle The application configuration
 */
protected void loadApplicationConfigurationSystemProperties(PropertyResourceBundle configBundle) {
    logToSystemOut("Checking for system properties in application configuration");
    Enumeration<String> keys = configBundle.getKeys();

    while (keys.hasMoreElements()) {

        String key = keys.nextElement();

        if (key.startsWith(CONFIG_SYSTEM_PROPERTY_PREFIX)) {
            String systemPropertyKey = key.substring(CONFIG_SYSTEM_PROPERTY_PREFIX.length());

            if (systemPropertyKey.length() > 0) {
                setSystemProperty(systemPropertyKey, configBundle.getString(key));
            }
        }
    }
}

From source file:com.flexive.shared.FxSharedUtils.java

private static String _lookupResource(String resourceBundle, String key, String localeIso) {
    try {//  www. j  a  v  a  2  s  .  c  o  m
        String isoCode = localeIso != null ? localeIso : Locale.getDefault().getLanguage();
        PropertyResourceBundle bundle = (PropertyResourceBundle) PropertyResourceBundle
                .getBundle(resourceBundle, new Locale(isoCode));
        return bundle.getString(key);
    } catch (MissingResourceException e) {
        //try default (english) locale
        try {
            PropertyResourceBundle bundle = (PropertyResourceBundle) PropertyResourceBundle
                    .getBundle(resourceBundle, Locale.ENGLISH);
            return bundle.getString(key);
        } catch (MissingResourceException e1) {
            return null;
        }
    }
}

From source file:com.amalto.workbench.utils.Util.java

public static String checkOnVersionCompatibility(String url, String username, String password) {
    IProduct product = Platform.getProduct();
    String versionComp = "";//$NON-NLS-1$
    try {/*from   w  w  w .  java2 s. c  o m*/
        URL resourceURL = product.getDefiningBundle().getResource("/about.mappings");//$NON-NLS-1$
        PropertyResourceBundle bundle = new PropertyResourceBundle(resourceURL.openStream());
        String studioVersion = bundle.getString("1").trim();//$NON-NLS-1$
        Pattern vsnPtn = Pattern.compile("^(\\d+)\\.(\\d+)(\\.)*(\\d*)$");//$NON-NLS-1$
        Matcher match = vsnPtn.matcher(studioVersion);
        if (!match.find()) {
            return null;
        }
        versionComp = Messages.Util_45 + studioVersion + Messages.Util_46;

        int major = Integer.parseInt(match.group(1));
        int minor = Integer.parseInt(match.group(2));
        int rev = match.group(4) != null && !match.group(4).equals("") ? Integer.parseInt(match.group(4)) : 0;//$NON-NLS-1$
        TMDMService service = Util.getMDMService(new URL(url), username, password);
        WSVersion wsVersion = service
                .getComponentVersion(new WSGetComponentVersion(WSComponent.DATA_MANAGER, null));
        versionComp += Messages.Util_47 + wsVersion.getMajor() + Messages.Util_48 + wsVersion.getMinor()
                + Messages.Util_49 + wsVersion.getRevision();
        if (major != wsVersion.getMajor() || minor != wsVersion.getMinor()) {
            return versionComp;
        }
        if (rev == 0) {
            // major version compare
            if (wsVersion.getRevision() != 0) {
                return versionComp;
            }
        }
    } catch (Exception e) {

    }
    return null;
}

From source file:de.innovationgate.wgpublisher.WGACore.java

public String getSystemLabel(String systemBundleName, String labelKey, HttpServletRequest servletRequest) {

    String label = null;//from   www.  j a  v  a  2s. c  o  m
    PropertyResourceBundle bundle = null;

    List<Locale> locales = new ArrayList<Locale>();
    if (servletRequest != null) {
        locales.addAll(WGUtils.extractEntryList(servletRequest.getLocales()));
    }
    locales.add(Locale.getDefault());
    locales.add(Locale.ENGLISH);

    for (Locale locale : locales) {
        try {
            bundle = (PropertyResourceBundle) ResourceBundle.getBundle(
                    WGACore.SYSTEMLABEL_BASE_PATH + systemBundleName, locale, this.getClass().getClassLoader());
            label = bundle.getString(labelKey);
            if (label != null) {
                return label;
            }
        } catch (java.util.MissingResourceException e) {
        }
    }

    return null;
}

From source file:no.kantega.commons.util.LocaleLabels.java

private static String getLabel(String key, String bundleName, String locale, Map<String, Object> parameters) {
    String msg = key;/*w  w  w. j av a 2  s .c  o m*/

    PropertyResourceBundle bundle = getBundle(bundleName, locale);
    if (bundle == null) {
        return msg;
    }

    try {
        msg = bundle.getString(key);
    } catch (MissingResourceException e) {
        // Do nothing
    }

    if (parameters != null) {
        for (Map.Entry<String, ?> o : parameters.entrySet()) {
            Object value = o.getValue();
            if (value != null) {
                msg = StringUtils.replace(msg, "${" + o.getKey() + "}", value.toString());
            }
        }
    }
    return msg;
}

From source file:org.gcaldaemon.gui.Messages.java

public static final String[][] getTranslatorTable(Locale locale) {
    Enumeration keyEnumerator = DEFAULT_RESOURCE_BUNDLE.getKeys();
    LinkedList list = new LinkedList();
    while (keyEnumerator.hasMoreElements()) {
        list.addLast(keyEnumerator.nextElement());
    }//from   w w  w. j  a va  2 s .  c  o m
    String[] keys = new String[list.size()];
    list.toArray(keys);
    Arrays.sort(keys, String.CASE_INSENSITIVE_ORDER);
    String[][] data = new String[keys.length][3];
    PropertyResourceBundle localeBundle = null;
    try {
        String programDir = System.getProperty("gcaldaemon.program.dir", "/Progra~1/GCALDaemon");
        File langDir = new File(programDir, "lang");
        if (!langDir.isDirectory()) {
            langDir.mkdir();
        } else {
            File msgFile = new File(langDir, "messages-" + locale.getLanguage().toLowerCase() + ".txt");
            if (msgFile.isFile()) {
                InputStream in = new BufferedInputStream(new FileInputStream(msgFile));
                localeBundle = new PropertyResourceBundle(in);
                in.close();
            }
        }
    } catch (Exception ignored) {
        log.warn("Unable to load messages!", ignored);
    }
    for (int i = 0; i < keys.length; i++) {
        data[i][0] = keys[i];
        data[i][1] = DEFAULT_RESOURCE_BUNDLE.getString(keys[i]);
        if (localeBundle != null) {
            try {
                data[i][2] = localeBundle.getString(keys[i]);
            } catch (Exception ignored) {
            }
        }
        if (data[i][2] == null) {
            data[i][2] = data[i][1];
        }
    }
    return data;
}

From source file:org.jpos.q2.Q2.java

private void initConfigDecorator() {
    InputStream in = Q2.class.getClassLoader()
            .getResourceAsStream("META-INF/org/jpos/config/Q2-decorator.properties");
    try {//from   ww  w  .  j a  va  2 s .c  om
        if (in != null) {
            PropertyResourceBundle bundle = new PropertyResourceBundle(in);
            String ccdClass = bundle.getString("config-decorator-class");
            if (log != null)
                log.info("Initializing config decoration provider: " + ccdClass);
            decorator = (ConfigDecorationProvider) Q2.class.getClassLoader().loadClass(ccdClass).newInstance();
            decorator.initialize(getDeployDir());
        }
    } catch (IOException ignored) {
    } catch (Exception e) {
        if (log != null)
            log.error(e);
        else {
            e.printStackTrace();
        }
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ignored) {
            }
        }
    }
}

From source file:org.nuxeo.connect.NuxeoConnectClient.java

public static String getBuildVersion() {
    if (buildVersion == null) {
        try {//from w  ww  .j a v a2s  .  c o  m
            InputStream pomStream = NuxeoConnectClient.class.getClassLoader().getResourceAsStream(
                    "META-INF/maven/org.nuxeo.connect/nuxeo-connect-client/pom.properties");
            if (pomStream == null) {
                buildVersion = "Unknown (no pom)";
            } else {
                PropertyResourceBundle prb = new PropertyResourceBundle(pomStream);
                buildVersion = prb.getString("version");
                if (buildVersion == null) {
                    buildVersion = "Unknown (not found)";
                }
            }
        } catch (Throwable t) {
            log.error("Unable to find Nuxeo Client Build Version", t);
            buildVersion = "Unknown (error)";
        }
    }
    return buildVersion;
}