Example usage for java.util ResourceBundle getString

List of usage examples for java.util ResourceBundle getString

Introduction

In this page you can find the example usage for java.util ResourceBundle 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:net.rim.ejde.internal.core.ContextManager.java

/**
 * Returns the string from the plugin's resource bundle, or 'key' if not found.
 */// w w  w  .  ja v a  2 s  .c  om
public static String getResourceString(String key) {
    ResourceBundle bundle = PLUGIN.getCoreResourcesBundle();

    try {
        return (bundle != null) ? bundle.getString(key) : key;
    } catch (MissingResourceException e) {
        return key;
    }
}

From source file:it.infn.ct.security.utilities.LDAPUtils.java

public static boolean addMail(LDAPUser user, String newMail) {
    DirContext ctx = null;/*from w w w . ja v  a 2s  . c  om*/
    try {
        ctx = getAuthContext(user.getUsername(), user.getPassword());

        ModificationItem[] modItems = new ModificationItem[1];
        modItems[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("mail", newMail));

        ResourceBundle rb = ResourceBundle.getBundle("ldap");

        ctx.modifyAttributes("cn=" + user.getUsername() + "," + rb.getString("peopleRoot"), modItems);
    } catch (NamingException ex) {
        _log.error(ex);
        return false;
    }

    return true;
}

From source file:it.infn.ct.security.utilities.LDAPUtils.java

public static boolean updatePassword(LDAPUser user, String newPassword) {
    DirContext ctx = null;//from   w ww .  j  av  a  2s.c  o  m
    try {
        ctx = getAuthContext(user.getUsername(), user.getPassword());

        ModificationItem[] modItems = new ModificationItem[1];
        modItems[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                new BasicAttribute("userPassword", newPassword));

        ResourceBundle rb = ResourceBundle.getBundle("ldap");

        ctx.modifyAttributes("cn=" + user.getUsername() + "," + rb.getString("peopleRoot"), modItems);
    } catch (NamingException ex) {
        _log.error(ex);
        return false;
    }

    return true;
}

From source file:it.infn.ct.security.utilities.LDAPUtils.java

public static String getOrgDN(String organisation, String countryCode) {
    NamingEnumeration results = null;
    DirContext ctx = null;//www  .j a v  a  2 s  .  c  o  m
    String dn = null;
    try {
        ctx = getContext();
        SearchControls controls = new SearchControls();
        controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        String retAttrs[] = { "dn" };
        controls.setReturningAttributes(retAttrs);
        ResourceBundle rb = ResourceBundle.getBundle("ldap");

        results = ctx.search("c=" + countryCode + "," + rb.getString("organisationsRoot"),
                "(&(objectclass=organization)(o=" + organisation + "))", controls);

        if (results.hasMore()) {
            SearchResult searchResult = (SearchResult) results.next();
            dn = searchResult.getNameInNamespace();
        }
    } catch (NameNotFoundException ex) {
        _log.error(ex);
    } catch (NamingException e) {
        throw new RuntimeException(e);
    } finally {
        if (results != null) {
            try {
                results.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
    }

    return dn;
}

From source file:it.infn.ct.security.utilities.LDAPUtils.java

public static LDAPUser getIfValidUser(String cn, String password) {
    LDAPUser user = null;//from w w  w.j  ava 2  s .c  o  m
    NamingEnumeration results = null;
    DirContext ctx = null;
    try {
        ctx = getAuthContext(cn, password);
        SearchControls controls = new SearchControls();
        String retAttrs[] = { "cn", "sn", "givenName", "title", "registeredAddress", "mail", "memberOf",
                "createTimestamp" };
        controls.setReturningAttributes(retAttrs);
        controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
        ResourceBundle rb = ResourceBundle.getBundle("ldap");

        results = ctx.search(rb.getString("peopleRoot"), "(cn=" + cn + ")", controls);
        if (results.hasMore()) {
            SearchResult searchResult = (SearchResult) results.next();
            Attributes attributes = searchResult.getAttributes();
            user = new LDAPUser();

            if (attributes.get("cn") != null)
                user.setUsername((String) attributes.get("cn").get());
            if (attributes.get("sn") != null)
                user.setSurname((String) attributes.get("sn").get());
            if (attributes.get("givenName") != null)
                user.setGivenname((String) attributes.get("givenName").get());
            if (attributes.get("title") != null)
                user.setTitle((String) attributes.get("title").get());
            if (attributes.get("registeredAddress") != null)
                user.setPreferredMail((String) attributes.get("registeredAddress").get(0));
            if (attributes.get("mail") != null) {
                String mails = "";
                for (int i = 0; i < attributes.get("mail").size(); i++) {
                    if (i != 0)
                        mails = mails + ", ";
                    mails = mails + (String) attributes.get("mail").get(i);
                }
                user.setAdditionalMails(mails);
            }
            if (attributes.get("memberOf") != null) {
                for (int i = 0; i < attributes.get("memberOf").size(); i++) {
                    user.addGroup((String) attributes.get("memberOf").get(i));
                }
            }
            if (attributes.get("createTimestamp") != null) {
                String time = (String) attributes.get("createTimestamp").get();
                DateFormat ldapData = new SimpleDateFormat("yyyyMMddHHmmss");
                user.setCreationTime(ldapData.parse(time));
            }

        }
    } catch (NameNotFoundException ex) {
        _log.error(ex);
    } catch (NamingException e) {
        _log.error(e);
    } catch (ParseException ex) {
        _log.error(ex);
    } finally {
        if (results != null) {
            try {
                results.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
    }

    return user;
}

From source file:it.infn.ct.security.utilities.LDAPUtils.java

public static LDAPUser getUser(String cn) {
    LDAPUser user = null;//from   ww  w.  j a  v a 2s. c  o m
    NamingEnumeration results = null;
    DirContext ctx = null;
    try {
        ctx = getContext();
        SearchControls controls = new SearchControls();
        String retAttrs[] = { "cn", "sn", "givenName", "title", "registeredAddress", "mail", "memberOf",
                "createTimestamp" };
        controls.setReturningAttributes(retAttrs);
        controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
        ResourceBundle rb = ResourceBundle.getBundle("ldap");

        results = ctx.search(rb.getString("peopleRoot"), "(cn=" + cn + ")", controls);
        if (results.hasMore()) {
            SearchResult searchResult = (SearchResult) results.next();
            Attributes attributes = searchResult.getAttributes();
            user = new LDAPUser();

            if (attributes.get("cn") != null)
                user.setUsername((String) attributes.get("cn").get());
            if (attributes.get("sn") != null)
                user.setSurname((String) attributes.get("sn").get());
            if (attributes.get("givenName") != null)
                user.setGivenname((String) attributes.get("givenName").get());
            if (attributes.get("title") != null)
                user.setTitle((String) attributes.get("title").get());
            if (attributes.get("registeredAddress") != null)
                user.setPreferredMail((String) attributes.get("registeredAddress").get(0));
            if (attributes.get("mail") != null) {
                String mails = "";
                for (int i = 0; i < attributes.get("mail").size(); i++) {
                    if (i != 0)
                        mails = mails + ", ";
                    mails = mails + (String) attributes.get("mail").get(i);
                }
                user.setAdditionalMails(mails);
            }
            if (attributes.get("memberOf") != null) {
                for (int i = 0; i < attributes.get("memberOf").size(); i++) {
                    user.addGroup((String) attributes.get("memberOf").get(i));
                }
            }

            if (attributes.get("createTimestamp") != null) {
                String time = (String) attributes.get("createTimestamp").get();
                DateFormat ldapData = new SimpleDateFormat("yyyyMMddHHmmss");
                user.setCreationTime(ldapData.parse(time));
            }

        }
    } catch (NameNotFoundException ex) {
        _log.error(ex);
    } catch (NamingException e) {
        _log.error(e);
    } catch (ParseException ex) {
        _log.error(ex);
    } finally {
        if (results != null) {
            try {
                results.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
    }

    return user;
}

From source file:org.fcrepo.client.FedoraClient.java

public static String getVersion() {

    ResourceBundle bundle = ResourceBundle.getBundle("org.fcrepo.client.resources.Client");
    return bundle.getString("version");
}

From source file:eu.planets_project.tb.impl.model.eval.PropertyEvaluation.java

private static String lookupName(EquivalenceStatement state) {
    try {/* w  w w. j ava  2  s.  c om*/
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        // Load the resource bundle:
        ResourceBundle bundle = null;
        /*
        try {
        Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
        bundle = ResourceBundle.getBundle("eu.planets_project.tb.gui.UIResources", locale );
        } catch ( MissingResourceException e ) {
        log.error("Could not load resource bundle: "+e);
        }
        */
        Map map = (Map) elContext.getELResolver().getValue(elContext, null, "res");
        // Look up
        String label = state.toString();
        String key = "exp_stage5.evaluation." + label;
        String name = (String) map.get(key);
        if (bundle != null)
            label = bundle.getString(key);
        //log.info("For "+state+" got "+label+" and "+name);
        if (name != null)
            label = name;
        return label;
    } catch (Exception e) {
        log.error("Failure when looking up " + state + " :: " + e);
        return state.toString();
    }
}

From source file:com.sun.socialsite.util.TextUtil.java

public static String getResourceString(String key, Locale locale) {
    String value = null;/*from w ww . j av  a  2s.c o m*/
    ResourceBundle defaultBundle = null;
    ResourceBundle customBundle = null;

    try {
        defaultBundle = getDefaultBundle(locale);
    } catch (Exception e) {
        // better be a default bundle for default locale...
        defaultBundle = getDefaultBundle();
    }

    try {
        customBundle = getCustomBundle(locale);
    } catch (Exception e) {
        log.debug("ERROR getting custom bundle for locale: " + locale, e);
    }

    if (customBundle == null)
        try {
            customBundle = getCustomBundle();
        } catch (Exception e) {
            log.debug("ERROR getting custom bundle", e);
        }

    if (customBundle != null)
        try {
            value = customBundle.getString(key);
        } catch (MissingResourceException e) {
            log.debug("String " + key + " not found in custom bundle, locale=" + locale);
            value = defaultBundle.getString(key);
        }
    else
        try {
            value = defaultBundle.getString(key);
        } catch (Exception e) {
            log.error("String " + key + " not found in default bundle, locale=" + locale);
        }

    return value != null ? value : key;
}

From source file:com.discovery.darchrow.util.ResourceBundleUtil.java

/**
 * ??,k/v ?map(HashMap)./*w ww .  j  a  v  a  2  s.c om*/
 * 
 * @param baseName
 *            ?+??<span style="color:red">(??)</span>,the base name of the resource bundle, a fully qualified class name
 * @param locale
 *            the locale ?
 * @return  baseName key value,null,?,?keyvalue?HashMap
 * @see #getResourceBundle(String, Locale)
 * @see java.util.ResourceBundle#getKeys()
 * @see org.apache.commons.collections.MapUtils#toMap(ResourceBundle)
 */
public static Map<String, String> readAllPropertiesToMap(String baseName, Locale locale) {
    ResourceBundle resourceBundle = getResourceBundle(baseName, locale);
    Enumeration<String> enumeration = resourceBundle.getKeys();
    if (Validator.isNotNullOrEmpty(enumeration)) {
        Map<String, String> propertyMap = new HashMap<String, String>();
        while (enumeration.hasMoreElements()) {
            String key = enumeration.nextElement();
            String value = resourceBundle.getString(key);
            propertyMap.put(key, value);
        }
        return propertyMap;
    }
    return null;
}