private void configFromJndiConf(Object rootContextName) { if (rootContextName instanceof String) { String name = (String) rootContextName; name = name.substring(0, name.lastIndexOf('/')) + "/conf" + name.substring(name.lastIndexOf('/')); try { InitialContext ctx = new InitialContext(); NamingEnumeration<Binding> bindings = ctx.listBindings(name); while (bindings.hasMore()) { Binding bd = bindings.next(); IntrospectionSupport.setProperty(this, bd.getName(), bd.getObject()); } } catch (Exception ignored) { if (LOG.isTraceEnabled()) { LOG.trace("exception on config from jndi: " + name, ignored); } } } }
/** * List resource paths (recursively), and store all of them in the given * Set. */ private static void listCollectionPaths(Set<String> set, DirContext resources, String path) throws NamingException { Enumeration<Binding> childPaths = resources.listBindings(path); while (childPaths.hasMoreElements()) { Binding binding = childPaths.nextElement(); String name = binding.getName(); StringBuilder childPath = new StringBuilder(path); if (!"/".equals(path) && !path.endsWith("/")) childPath.append("/"); childPath.append(name); Object object = binding.getObject(); if (object instanceof DirContext) { childPath.append("/"); } set.add(childPath.toString()); } }
/** * Enumerates the names bound in the named context, along with the * objects bound to them. The contents of any subcontexts are not * included. * <p> * If a binding is added to or removed from this context, its effect on * an enumeration previously returned is undefined. * * @param name the name of the context to list * @return an enumeration of the bindings in this context. * Each element of the enumeration is of type Binding. * @exception NamingException if a naming exception is encountered */ @Override public NamingEnumeration<Binding> listBindings(Name name) throws NamingException { // Removing empty parts while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1); if (name.isEmpty()) { return new NamingContextBindingsEnumeration(bindings.values().iterator(), this); } NamingEntry entry = bindings.get(name.get(0)); if (entry == null) { throw new NameNotFoundException (sm.getString("namingContext.nameNotBound", name, name.get(0))); } if (entry.type != NamingEntry.CONTEXT) { throw new NamingException (sm.getString("namingContext.contextExpected")); } return ((Context) entry.value).listBindings(name.getSuffix(1)); }
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/plain;UTF-8"); PrintWriter out = resp.getWriter(); try { Context ctx = new InitialContext(); NamingEnumeration<Binding> enm = ctx.listBindings("java:comp/env/list"); while (enm.hasMore()) { Binding b = enm.next(); out.print(b.getObject().getClass().getName()); } } catch (NamingException ne) { ne.printStackTrace(out); } }
/** * List resource paths (recursively), and store all of them in the given * Set. */ private static void listCollectionPaths (Set set, DirContext resources, String path) throws NamingException { Enumeration childPaths = resources.listBindings(path); while (childPaths.hasMoreElements()) { Binding binding = (Binding) childPaths.nextElement(); String name = binding.getName(); StringBuffer childPath = new StringBuffer(path); if (!"/".equals(path) && !path.endsWith("/")) childPath.append("/"); childPath.append(name); Object object = binding.getObject(); if (object instanceof DirContext) { childPath.append("/"); } set.add(childPath.toString()); } }
/** * List resource paths (recursively), and store all of them in the given * Set. */ private static void listPaths(Set set, DirContext resources, String path) throws NamingException { Enumeration childPaths = resources.listBindings(path); while (childPaths.hasMoreElements()) { Binding binding = (Binding) childPaths.nextElement(); String name = binding.getName(); String childPath = path + "/" + name; set.add(childPath); Object object = binding.getObject(); if (object instanceof DirContext) { listPaths(set, resources, childPath); } } }
/** * Enumerates the names bound in the named context, along with the objects * bound to them. The contents of any subcontexts are not included. * <p> * If a binding is added to or removed from this context, its effect on an * enumeration previously returned is undefined. * * @param name * the name of the context to list * @return an enumeration of the bindings in this context. Each element of * the enumeration is of type Binding. * @exception NamingException * if a naming exception is encountered */ @Override public NamingEnumeration<Binding> listBindings(Name name) throws NamingException { // Removing empty parts while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1); if (name.isEmpty()) { return new NamingContextBindingsEnumeration(bindings.values().iterator(), this); } NamingEntry entry = bindings.get(name.get(0)); if (entry == null) { throw new NameNotFoundException(sm.getString("namingContext.nameNotBound", name, name.get(0))); } if (entry.type != NamingEntry.CONTEXT) { throw new NamingException(sm.getString("namingContext.contextExpected")); } return ((Context) entry.value).listBindings(name.getSuffix(1)); }
public void init() throws ServletException { super.init(); // Nombre del layout por defecto defaultLayoutName = getServletConfig().getInitParameter("defaultLayoutName"); try { Context context = new InitialContext(); // Bajo JNDI habr� todos los pares: nombre, path que metemos en el Map NamingEnumeration namingEnum = context.listBindings("java:comp/env/layout"); while (namingEnum.hasMore()) { Binding binding = (Binding) namingEnum.next(); layoutPathMap.put(binding.getName(), binding.getObject()); } } catch (NamingException e) { log.error("Error accediendo a JNDI", e); } // El layout por defecto debe estar en el Map! if (!layoutPathMap.containsKey(defaultLayoutName)) { log.error("El layout por defecto \"" + defaultLayoutName + "\" no est� definido bajo java:comp/env/layout"); throw new UnavailableException("Error de configuraci�n"); } }
private void listServices(BundleContext bundleContext, Context context) throws NamingException { Dictionary<String, Object> propertyMap = new Hashtable<>(); propertyMap.put("osgi.jndi.service.name", "foo/myService"); FooService fooService = new FooServiceImpl(); ServiceRegistration<FooService> fooServiceRegistration = bundleContext.registerService( FooService.class, fooService, propertyMap); NamingEnumeration<Binding> listBindings = context.listBindings("osgi:service/foo/myService"); Binding binding = listBindings.nextElement(); logger.info("First service retrieved from the listBinding has the class : " + binding.getClassName()); listBindings.close(); //call the close() method so that it will unget all the gotten services. fooServiceRegistration.unregister(); }
/** * Enumerates the names bound in the named context, along with the * objects bound to them. The contents of any subcontexts are not * included. * <p> * If a binding is added to or removed from this context, its effect on * an enumeration previously returned is undefined. * * @param name the name of the context to list * @return an enumeration of the bindings in this context. * Each element of the enumeration is of type Binding. * @throws NamingException if a jndi exception is encountered */ @Override public NamingEnumeration<Binding> listBindings(Name name) throws NamingException { // Removing empty parts while ((!name.isEmpty()) && (name.get(0).length() == 0)) { name = name.getSuffix(1); } if (name.isEmpty()) { return new NamingContextBindingsEnumeration(new ArrayList<>(bindings.values()), this); } NamingEntry entry = bindings.get(name.get(0)); if (entry == null) { throw new NameNotFoundException(SM.getString("namingContext.nameNotBound", name, name.get(0))); } if (entry.type != NamingEntry.CONTEXT) { throw new NamingException(SM.getString("namingContext.contextExpected")); } return ((Context) entry.value).listBindings(name.getSuffix(1)); }
/** * Recursively exhaust the JNDI tree */ private static final void listContext(javax.naming.Context ctx, String indent) { try { NamingEnumeration<Binding> list = ctx.listBindings(""); while (list.hasMore()) { Binding item = (Binding) list.next(); String className = item.getClassName(); String name = item.getName(); log.info(indent + className + " | " + name); Object o = item.getObject(); if (o instanceof javax.naming.Context) { listContext((javax.naming.Context) o, indent + " "); } } } catch (NamingException ex) { log.warn("JNDI failure: ", ex); } }
public void changePassord(ServiceContext serviceContext, User user, String password) throws Exception{ long companyId = serviceContext.getCompanyId(); Properties userMappings = getUserMappings(serviceContext.getCompanyId()); Binding binding = getUser(companyId, user.getScreenName()); System.out.println("bingging " + binding); System.out.println("Pass " + user.getPassword()); String name = StringPool.BLANK; StringBuilder sb = new StringBuilder(); LdapContext ctx = getContext(serviceContext.getCompanyId()); sb = new StringBuilder(); sb.append(userMappings.getProperty("screenName")); sb.append(StringPool.EQUAL); sb.append(user.getScreenName()); sb.append(StringPool.COMMA); sb.append(getUsersDN(companyId)); name = sb.toString(); Modifications mods = Modifications.getInstance(); mods.addItem(userMappings.getProperty(UserConverterKeys.PASSWORD),password); ModificationItem[] modItems = mods.getItems(); if (binding != null) { ctx.modifyAttributes(name, modItems); } }
public String getNameInNamespace(long companyId, Binding binding) throws Exception { String baseDN = PrefsPropsUtil.getString(companyId, PropsKeys.LDAP_BASE_DN); if (Validator.isNull(baseDN)) { return binding.getName(); } else { StringBuilder sb = new StringBuilder(); sb.append(binding.getName()); sb.append(StringPool.COMMA); sb.append(baseDN); return sb.toString(); } }
@Override public Binding next() throws NamingException { if (hasMore()) { final Entry<String, Object> entry = iterator.next(); final String name = entry.getKey(); Object value = entry.getValue(); try { value = NamingManager.getObjectInstance(value, new CompositeName().add(name), context, environnement); } catch (final Throwable err) { final NamingException errNaming = new NamingException("Failed To Get Instance "); errNaming.setRootCause(err); throw errNaming; } return new Binding(name, value); } throw new NoSuchElementException(); }
@Override public NamingEnumeration<Binding> listBindings(final Name name) throws NamingException { final Name searchName = validateName(name); // If null, it means we don't know how to handle this -> throw to the // parent if (searchName == null) { return parent.listBindings(name); } if (searchName.isEmpty()) { // listing this context return new Bindings(this, environnement, bindings); } // Perhaps 'name' names a context final Object target = lookup(name); if (target instanceof Context) { return ((Context) target).listBindings(""); } throw new NotContextException(name + " cannot be listed"); }
private static JndiBinding createJndiBinding(String path, Binding binding) { final String name = getBindingName(path, binding); final String className = binding.getClassName(); final Object object = binding.getObject(); final String contextPath; final String value; if (object instanceof Context // "javax.naming.Context".equals(className) nécessaire pour le path "comp" dans JBoss 6.0 || "javax.naming.Context".equals(className) // pour jetty : || object instanceof Reference && "javax.naming.Context".equals(((Reference) object).getClassName())) { if (!path.isEmpty()) { contextPath = path + '/' + name; } else { // nécessaire pour jonas 5.1.0 contextPath = name; } value = null; } else { contextPath = null; value = formatValue(object); } return new JndiBinding(name, className, contextPath, value); }
@Override public NamingEnumeration<Binding> listBindings(String contextName) throws NamingException { contextName = trimSlashes(contextName); if (!"".equals(contextName) && !".".equals(contextName)) { try { return ((InVMNamingContext) lookup(contextName)).listBindings(""); } catch (Throwable t) { throw new NamingException(t.getMessage()); } } List<Binding> l = new ArrayList<>(); for (Object element : map.keySet()) { String name = (String) element; Object object = map.get(name); l.add(new Binding(name, object)); } return new NamingEnumerationImpl<>(l.iterator()); }
@Override public NamingEnumeration<Binding> listBindings(String contextName) throws NamingException { contextName = trimSlashes(contextName); if (!"".equals(contextName) && !".".equals(contextName)) { try { return ((InVMContext) lookup(contextName)).listBindings(""); } catch (Throwable t) { throw new NamingException(t.getMessage()); } } List<Binding> l = new ArrayList<>(); for (String name : map.keySet()) { Object object = map.get(name); l.add(new Binding(name, object)); } return new NamingEnumerationImpl(l.iterator()); }
@Override public NamingEnumeration<Binding> listBindings(String contextName) throws NamingException { contextName = trimSlashes(contextName); if (!"".equals(contextName) && !".".equals(contextName)) { try { return ((InVMContext) lookup(contextName)).listBindings(""); } catch (Throwable t) { throw new NamingException(t.getMessage()); } } List<Binding> l = new ArrayList<>(); for (Object element : map.keySet()) { String name = (String) element; Object object = map.get(name); l.add(new Binding(name, object)); } return new NamingEnumerationImpl<>(l.iterator()); }
/** * Returns a map of service implementation class names and instances of all OSGi services for the given service * class name. * * @param serviceClassName service class name * @return a map of implementation class and instances */ public static Map<String, Object> getOSGiServices(String serviceClassName) { try { Context context = new InitialContext(); NamingEnumeration<Binding> enumeration = context.listBindings("osgi:service/" + serviceClassName); Map<String, Object> services = new HashMap<>(); while (enumeration.hasMore()) { Binding binding = enumeration.next(); services.put(binding.getClassName(), binding.getObject()); } return services; } catch (NamingException e) { throw new UUFRuntimeException("Cannot create the initial context when calling OSGi service '" + serviceClassName + "'.", e); } }
private void debugLookup() { try { Context ctx = new InitialContext(); //System.out.println("Context Environment: " + ctx.getEnvironment()); NamingEnumeration e = ctx.listBindings("java:comp/env/"); while (e.hasMore()) { Binding binding = (Binding)e.next(); System.out.println("---"); System.out.println("Name: " + binding.getName()); System.out.println("Type: " + binding.getClassName()); System.out.println("Value: " + binding.getObject()); } e.close(); } catch (NamingException ex) { ex.printStackTrace(); } }
private static void dumpBindings(Context ctx, int indent) { try { NamingEnumeration<Binding> bindings = ctx.listBindings(""); while (bindings.hasMore()) { Binding binding = bindings.next(); StringBuffer blanks = new StringBuffer(); for (int i = 0; i < indent; i++) { blanks.append(" "); } System.out.println(String.format("%s%s (%s)", blanks, binding.getName(), binding.getClassName())); Object boundObj = binding.getObject(); if (boundObj instanceof Context) { dumpBindings((Context) boundObj, indent + 3); } } } catch (NamingException e) { System.out.println(e); } }
/** * {@inheritDoc} */ public NamingEnumeration<Binding> listBindings(Name name) throws NamingException { if (name.isEmpty()) { try { return new BindingEnumeration(registry.list(), this); } catch (RemoteException e) { throw (NamingException) newNamingException(e) .fillInStackTrace(); } } Object obj = lookup(name); if (obj instanceof Context) { try { return ((Context) obj).listBindings(""); //$NON-NLS-1$ } finally { ((Context) obj).close(); } } // jndi.80=Name specifies an object that is not a context: {0} throw new NotContextException(Messages.getString("jndi.80", name)); //$NON-NLS-1$ }
/** * {@inheritDoc} */ public NamingEnumeration<Binding> listBindings(Name name) throws NamingException { if (!(name instanceof CompositeName)) { // jndi.26=URL context can't accept non-composite name: {0} throw new InvalidNameException(Messages.getString("jndi.26", name)); //$NON-NLS-1$ } if (name.size() == 1) { return listBindings(name.get(0)); } Context context = getContinuationContext(name); try { return context.listBindings(name.getSuffix(1)); } finally { context.close(); } }
@Override protected void loadProperties(Properties props) throws IOException { Hashtable<String,String> env = new Hashtable<String,String>(); env.put(Context.PROVIDER_URL,"jnp://localhost:1099"); env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory"); env.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces"); InitialContext context = null; Properties jndiProperties = new Properties(); NamingEnumeration<Binding> bindings; try { context = new InitialContext(env); bindings = context.listBindings(""); while (bindings.hasMore()) { Binding bd = (Binding)bindings.next(); logger.debug("Property read from JNDI: " + bd.getName() + ": " + bd.getObject()); jndiProperties.put(bd.getName(), bd.getObject()); } } catch (NamingException ne) { throw new RuntimeException(ne); } props.putAll(jndiProperties); super.loadProperties(props); }
@Override public NamingEnumeration<Binding> listBindings(Name name) throws NamingException { int size = name.size(); Hashtable<String, Object> tempSchema = doLookup(name, size); if (size == level - 1) { return new LdapNamingEnumeration<Binding>(null, null); } Iterator<String> keys = tempSchema.keySet().iterator(); List<Binding> list = new ArrayList<Binding>(); while (keys.hasNext()) { list.add(new Binding(ldap2jndi(keys.next()), this.getClass() .getName())); } return new LdapNamingEnumeration<Binding>(list, null); }