Search in sources :

Example 16 with NamingEnumeration

use of javax.naming.NamingEnumeration in project ACS by ACS-Community.

the class TestJDAL method testListContext.

/**
	 * Listing different contexts - simple and XML contexts
	 * We shouldn't see any difference in listing just 'directory' context
	 * or listing inside a XML data
	 */
public void testListContext() throws Exception {
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.cosylab.cdb.jdal.JNDIContextFactory");
    env.put(Context.PROVIDER_URL, strIOR);
    Context context = null;
    context = new InitialContext(env);
    assertNotNull(context);
    // try simple listing
    String list = "";
    NamingEnumeration ne = context.list("MACI");
    while (ne.hasMore()) {
        NameClassPair pair = (NameClassPair) ne.nextElement();
        list = list + pair.getName() + " ";
    }
    // same as we did listing
    assertTrue(list.indexOf("Containers") != -1);
    assertTrue(list.indexOf("Managers") != -1);
    // try with a XML
    ne = ((Context) (((Context) context.lookup("MACI")).lookup("Managers"))).list("Manager");
    list = "";
    while (ne.hasMore()) {
        NameClassPair pair = (NameClassPair) ne.nextElement();
        list = list + pair.getName() + " ";
    //System.out.println(pair.getName() + " " + pair.getClassName());
    }
    // this should be in Manager data
    assertTrue(list.indexOf("ServiceComponents") != -1);
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) Hashtable(java.util.Hashtable) NameClassPair(javax.naming.NameClassPair) NamingEnumeration(javax.naming.NamingEnumeration) InitialContext(javax.naming.InitialContext)

Example 17 with NamingEnumeration

use of javax.naming.NamingEnumeration in project adempiere by adempiere.

the class StatusInfo method doGet.

/**
	 * 	Get
	 *	@param request
	 *	@param response
	 *	@throws ServletException
	 *	@throws IOException
	 */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType(CONTENT_TYPE);
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head><title>Status Info</title></head>");
    out.println("<body>");
    InitialContext context = null;
    try {
        context = new InitialContext();
    } catch (Exception ex) {
        out.println("<p><b>" + ex + "</b></p>");
    }
    try {
        Status status = (Status) context.lookup(Status.JNDI_NAME);
        out.println("<p>" + status.getStatus() + "</p>");
    } catch (Exception ex) {
        out.println("<p><b>" + ex + "</b></p>");
    }
    try {
        Server server = (Server) context.lookup(Server.JNDI_NAME);
        out.println("<p>" + server.getStatus() + "</p>");
    } catch (Exception ex) {
        out.println("<p><b>" + ex + "</b></p>");
    }
    try {
        out.println("<h2>-- /</h2>");
        NamingEnumeration ne = context.list("/");
        while (ne.hasMore()) out.println("<br>   " + ne.nextElement());
        out.println("<h2>-- java</h2>");
        ne = context.list("java:");
        while (ne.hasMore()) out.println("<br>   " + ne.nextElement());
        out.println("<h2>-- ejb</h2>");
        ne = context.list("ejb");
        while (ne.hasMore()) out.println("<br>   " + ne.nextElement());
        //
        out.println("<h2>-- DS</h2>");
        DataSource ds = (DataSource) context.lookup("java:/OracleDS");
        out.println("<br>  DataSource " + ds.getClass().getName() + " LoginTimeout=" + ds.getLoginTimeout());
        Connection con = ds.getConnection("adempiere", "adempiere");
        out.println("<br>  Connection ");
        getServletContext().log("Connection closed=" + con.isClosed());
        DatabaseMetaData dbmd = con.getMetaData();
        getServletContext().log("DB " + dbmd.getDatabaseProductName());
        getServletContext().log("DB V " + dbmd.getDatabaseProductVersion());
        getServletContext().log("Driver " + dbmd.getDriverName());
        getServletContext().log("Driver V " + dbmd.getDriverVersion());
        getServletContext().log("JDBC " + dbmd.getJDBCMajorVersion());
        getServletContext().log("JDBC mV " + dbmd.getJDBCMinorVersion());
        getServletContext().log("User " + dbmd.getUserName());
        getServletContext().log("ANSI 92 " + dbmd.supportsANSI92FullSQL());
        getServletContext().log("Connection Alter Table ADD" + dbmd.supportsAlterTableWithAddColumn());
        getServletContext().log("Connection Alter Table DROP " + dbmd.supportsAlterTableWithDropColumn());
        getServletContext().log("Connection DDL&DML " + dbmd.supportsDataDefinitionAndDataManipulationTransactions());
        getServletContext().log("Connection CatalogsIn DML " + dbmd.supportsCatalogsInDataManipulation());
        getServletContext().log("Connection Schema In DML " + dbmd.supportsSchemasInDataManipulation());
        con.close();
    } catch (Exception e) {
        out.println("<p><b>" + e + "</b></p>");
    }
    out.println("</body></html>");
}
Also used : Status(org.compiere.interfaces.Status) Server(org.compiere.interfaces.Server) Connection(java.sql.Connection) NamingEnumeration(javax.naming.NamingEnumeration) DatabaseMetaData(java.sql.DatabaseMetaData) InitialContext(javax.naming.InitialContext) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) DataSource(javax.sql.DataSource)

Example 18 with NamingEnumeration

use of javax.naming.NamingEnumeration in project adempiere by adempiere.

the class ConnectTest method testJNP.

//	ConnectTest
/**
	 * 	Test JNP
	 * 	@param serverName server name
	 *  @param context context
	 */
private void testJNP(String serverName, InitialContext context) {
    //	Connect to MBean
    System.out.println();
    System.out.println("Connecting to MBean ...");
    /**
		try
		{
			String connectorName = "jmx:" + serverName + ":rmi";
			RMIAdaptor server = (RMIAdaptor) context.lookup (connectorName);
			System.out.println("- have Server");
			System.out.println("- Default Domain=" + server.getDefaultDomain());
			System.out.println("- MBeanCount = " + server.getMBeanCount());

	//		ObjectName serviceName = new ObjectName ("Adempiere:service=AdempiereCtrl");
	//		System.out.println("- " + serviceName + " is registered=" + server.isRegistered(serviceName));

	//		System.out.println("  - AdempiereSummary= "
	//				+ server.getAttribute(serviceName, "AdempiereSummary"));

			Object[] params = {};
			String[] signature = {};
		}
		catch (Exception e)
		{
			System.err.println("ERROR: Could not contact MBean: " + e);
			return;
		}
		**/
    //	List Context
    System.out.println();
    System.out.println(" Examining context ....");
    try {
        System.out.println("  Namespace=" + context.getNameInNamespace());
        System.out.println("  Environment=" + context.getEnvironment());
        System.out.println("  Context '/':");
        NamingEnumeration ne = context.list("/");
        while (ne.hasMore()) System.out.println("  - " + ne.nextElement());
        //
        System.out.println("  Context 'ejb':");
        ne = context.list("ejb");
        while (ne.hasMore()) System.out.println("  - " + ne.nextElement());
        //
        System.out.println("  Context 'ejb/adempiere':");
        ne = context.list("ejb/adempiere");
        while (ne.hasMore()) System.out.println("  - " + ne.nextElement());
    } catch (Exception e) {
        System.err.println("ERROR: Could not examine context: " + e);
        return;
    }
}
Also used : NamingEnumeration(javax.naming.NamingEnumeration) UnknownHostException(java.net.UnknownHostException)

Example 19 with NamingEnumeration

use of javax.naming.NamingEnumeration in project karaf by apache.

the class LDAPBackingEngine method listUsers.

@Override
public List<UserPrincipal> listUsers() {
    DirContext context = null;
    ArrayList<UserPrincipal> users = new ArrayList<>();
    try {
        context = cache.open();
        SearchControls controls = new SearchControls();
        if (options.getUserSearchSubtree()) {
            controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        } else {
            controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
        }
        String filter = options.getUserFilter();
        filter = filter.replaceAll(Pattern.quote("%u"), "*");
        filter = filter.replace("\\", "\\\\");
        LOGGER.debug("Looking for the users in LDAP with ");
        LOGGER.debug("  base DN: " + options.getUserBaseDn());
        LOGGER.debug("  filter: " + filter);
        NamingEnumeration namingEnumeration = context.search(options.getUserBaseDn(), filter, controls);
        try {
            while (namingEnumeration.hasMore()) {
                SearchResult result = (SearchResult) namingEnumeration.next();
                // We need to do the following because slashes are handled badly. For example, when searching
                // for a user with lots of special characters like cn=admin,=+<>#;\
                // SearchResult contains 2 different results:
                //
                // SearchResult.getName = cn=admin\,\=\+\<\>\#\;\\\\
                // SearchResult.getNameInNamespace = cn=admin\,\=\+\<\>#\;\\,ou=people,dc=example,dc=com
                //
                // the second escapes the slashes correctly.
                String userDNNamespace = result.getNameInNamespace();
                // handle case where cn, ou, dc case doesn't match
                int indexOfUserBaseDN = userDNNamespace.toLowerCase().indexOf("," + options.getUserBaseDn().toLowerCase());
                String userDN = (indexOfUserBaseDN > 0) ? userDNNamespace.substring(0, indexOfUserBaseDN) : result.getName();
                // we need to pull out the cn=, uid=, ect.. from the user name to get the actual user name
                String userName = userDN;
                if (userDN.contains("="))
                    userName = userDN.split("=")[1];
                users.add(new UserPrincipal(userName));
            }
        } finally {
            if (namingEnumeration != null) {
                try {
                    namingEnumeration.close();
                } catch (NamingException e) {
                // Ignore
                }
            }
        }
        return users;
    } catch (NamingException e) {
        throw new RuntimeException(e);
    }
}
Also used : ArrayList(java.util.ArrayList) SearchControls(javax.naming.directory.SearchControls) NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException) DirContext(javax.naming.directory.DirContext) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal)

Example 20 with NamingEnumeration

use of javax.naming.NamingEnumeration in project karaf by apache.

the class LDAPCache method doGetUserDnAndNamespace.

protected String[] doGetUserDnAndNamespace(String user) throws NamingException {
    DirContext context = open();
    SearchControls controls = new SearchControls();
    if (options.getUserSearchSubtree()) {
        controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    } else {
        controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
    }
    String filter = options.getUserFilter();
    filter = filter.replaceAll(Pattern.quote("%u"), Matcher.quoteReplacement(user));
    filter = filter.replace("\\", "\\\\");
    LOGGER.debug("Looking for the user in LDAP with ");
    LOGGER.debug("  base DN: " + options.getUserBaseDn());
    LOGGER.debug("  filter: " + filter);
    NamingEnumeration namingEnumeration = context.search(options.getUserBaseDn(), filter, controls);
    try {
        if (!namingEnumeration.hasMore()) {
            LOGGER.warn("User " + user + " not found in LDAP.");
            return null;
        }
        LOGGER.debug("Found the user DN.");
        SearchResult result = (SearchResult) namingEnumeration.next();
        // We need to do the following because slashes are handled badly. For example, when searching
        // for a user with lots of special characters like cn=admin,=+<>#;\
        // SearchResult contains 2 different results:
        //
        // SearchResult.getName = cn=admin\,\=\+\<\>\#\;\\\\
        // SearchResult.getNameInNamespace = cn=admin\,\=\+\<\>#\;\\,ou=people,dc=example,dc=com
        //
        // the second escapes the slashes correctly.
        String userDNNamespace = result.getNameInNamespace();
        // handle case where cn, ou, dc case doesn't match
        int indexOfUserBaseDN = userDNNamespace.toLowerCase().indexOf("," + options.getUserBaseDn().toLowerCase());
        String userDN = (indexOfUserBaseDN > 0) ? userDNNamespace.substring(0, indexOfUserBaseDN) : result.getName();
        return new String[] { userDN, userDNNamespace };
    } finally {
        if (namingEnumeration != null) {
            try {
                namingEnumeration.close();
            } catch (NamingException e) {
            // Ignore
            }
        }
    }
}
Also used : SearchControls(javax.naming.directory.SearchControls) NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) EventDirContext(javax.naming.event.EventDirContext) DirContext(javax.naming.directory.DirContext)

Aggregations

NamingEnumeration (javax.naming.NamingEnumeration)48 Attribute (javax.naming.directory.Attribute)22 SearchResult (javax.naming.directory.SearchResult)20 NamingException (javax.naming.NamingException)19 Attributes (javax.naming.directory.Attributes)19 SearchControls (javax.naming.directory.SearchControls)18 DirContext (javax.naming.directory.DirContext)14 ArrayList (java.util.ArrayList)11 LdapContext (javax.naming.ldap.LdapContext)10 Test (org.junit.Test)9 InitialContext (javax.naming.InitialContext)7 Hashtable (java.util.Hashtable)6 Context (javax.naming.Context)6 InitialDirContext (javax.naming.directory.InitialDirContext)6 HashMap (java.util.HashMap)5 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 Binding (javax.naming.Binding)3 NameClassPair (javax.naming.NameClassPair)3 NameNotFoundException (javax.naming.NameNotFoundException)3