Commits

Roman Dawydkin committed 0b2c9a6

Initial commit.

  • Participants

Comments (0)

Files changed (5)

+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+	<classpathentry combineaccessrules="false" kind="src" path="/org.eclipse.core.commands"/>
+	<classpathentry combineaccessrules="false" kind="src" path="/org.eclipse.core.runtime"/>
+	<classpathentry combineaccessrules="false" kind="src" path="/org.eclipse.equinox.common"/>
+	<classpathentry combineaccessrules="false" kind="src" path="/org.eclipse.jface"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/SWT"/>
+	<classpathentry kind="output" path="build/classes"/>
+</classpath>
+^build$
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>ldap_shell</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+	<filteredResources>
+		<filter>
+			<id>1337249820504</id>
+			<name>build</name>
+			<type>10</type>
+			<matcher>
+				<id>org.eclipse.ui.ide.multiFilter</id>
+				<arguments>1.0-name-matches-false-false-dist</arguments>
+			</matcher>
+		</filter>
+	</filteredResources>
+</projectDescription>

File src/ru/ezsk/ldapshell/LdapDirectory.java

+package ru.ezsk.ldapshell;
+
+import java.util.Hashtable;
+import java.util.Properties;
+
+import javax.naming.Context;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.directory.InitialDirContext;
+import javax.naming.directory.SearchControls;
+import javax.naming.directory.SearchResult;
+
+public class LdapDirectory
+{
+	private InitialDirContext initialContext;
+
+	private String baseName;
+
+	private SearchControls searchControls;
+
+	public LdapDirectory(Properties properties) throws NamingException {
+		final Hashtable<String, String> env = new Hashtable<>();
+		env.put(Context.INITIAL_CONTEXT_FACTORY,
+				"com.sun.jndi.ldap.LdapCtxFactory");
+		env.put(Context.PROVIDER_URL, properties.getProperty("url"));
+		final String user = properties.getProperty("user");
+		if (user != null) {
+			env.put(Context.SECURITY_PRINCIPAL, user);
+		}
+		final String password = properties.getProperty("password");
+		if (password != null) {
+			env.put(Context.SECURITY_CREDENTIALS, password);
+		}
+		baseName = properties.getProperty("base");
+
+		initialContext = new InitialDirContext(env);
+
+		searchControls = new SearchControls();
+		searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
+	}
+
+	public void close() throws NamingException {
+		if (initialContext != null) {
+			initialContext.close();
+			initialContext = null;
+		}
+	}
+
+	public NamingEnumeration<SearchResult> search(String query)
+			throws NamingException
+	{
+		return initialContext.search(baseName, query, searchControls);
+	}
+}

File src/ru/ezsk/ldapshell/LdapWindow.java

+package ru.ezsk.ldapshell;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Properties;
+
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.SearchResult;
+
+import org.eclipse.jface.layout.GridDataFactory;
+import org.eclipse.jface.layout.GridLayoutFactory;
+import org.eclipse.jface.window.ApplicationWindow;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.StyledText;
+import org.eclipse.swt.events.DisposeEvent;
+import org.eclipse.swt.events.DisposeListener;
+import org.eclipse.swt.events.KeyAdapter;
+import org.eclipse.swt.events.KeyEvent;
+import org.eclipse.swt.events.TraverseEvent;
+import org.eclipse.swt.events.TraverseListener;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Text;
+
+public class LdapWindow extends ApplicationWindow
+{
+	private static class AttributeComparator implements Comparator<Attribute>
+	{
+		static final Comparator<Attribute> INSTANCE = new AttributeComparator();
+
+		public int compare(Attribute o1, Attribute o2) {
+			return o1.getID().compareTo(o2.getID());
+		}
+	}
+
+	public static Properties loadProperties() throws IOException {
+		final Properties properties = new Properties();
+		final String configFileName = System.getProperty("user.home")
+				+ File.separator + "ldap.properties";
+		try (final InputStream input = new FileInputStream(configFileName);
+		     final Reader reader = new InputStreamReader(input, "UTF-8"))
+		{
+			properties.load(reader);
+		}
+		return properties;
+	}
+
+	public static void main(String[] args) throws Exception {
+		final Properties properties = LdapWindow.loadProperties();
+		final LdapDirectory directory = new LdapDirectory(properties);
+		final LdapWindow window = new LdapWindow(directory);
+		window.setBlockOnOpen(true);
+		window.open();
+	}
+
+	private LdapDirectory ldapDirectory;
+	StyledText console;
+	Text input;
+
+	public LdapWindow(LdapDirectory ldapDirectory) {
+		super(null);
+		this.ldapDirectory = ldapDirectory;
+	}
+
+	protected boolean showTopSeperator() {
+		return false;
+	}
+
+	protected void configureShell(Shell shell) {
+		super.configureShell(shell);
+		shell.addDisposeListener(new DisposeListener() {
+			public void widgetDisposed(DisposeEvent event) {
+				disposed();
+			}
+		});
+		shell.setSize(800, 600);
+		shell.setText("LDAP Shell");
+	}
+
+	void disposed() {
+		if (ldapDirectory != null) {
+			try {
+				ldapDirectory.close();
+			} catch (NamingException ex) {
+				ex.printStackTrace();
+			}
+			ldapDirectory = null;
+		}
+	}
+
+	protected Control createContents(Composite parent) {
+		final Composite composite = (Composite) super.createContents(parent);
+		GridLayoutFactory.swtDefaults().applyTo(composite);
+
+		console = new StyledText(
+				composite,
+				SWT.BORDER | SWT.READ_ONLY | SWT.WRAP | SWT.V_SCROLL);
+		GridDataFactory.swtDefaults().align(SWT.FILL, SWT.FILL)
+				.grab(true, true).applyTo(console);
+
+		input = new Text(composite, SWT.BORDER | SWT.SINGLE);
+		GridDataFactory.swtDefaults().align(SWT.FILL, SWT.CENTER)
+				.grab(true, false).applyTo(input);
+		input.addTraverseListener(new TraverseListener() {
+			public void keyTraversed(TraverseEvent event) {
+				if (event.detail == SWT.TRAVERSE_RETURN) {
+					event.doit = false;
+					try {
+						search(input.getText());
+					} catch (NamingException ex) {
+						printLn(ex.getMessage()); // TODO
+					}
+					input.setText("");
+				}
+			}
+		});
+		input.setFocus();
+
+		final KeyAdapter keyListener = new KeyAdapter() {
+			public void keyPressed(KeyEvent ev) {
+				if ((ev.keyCode == 'l') &&
+				    ((ev.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL))
+				{
+					ev.doit = false;
+					console.setText("");
+				}
+			}
+		};
+		console.addKeyListener(keyListener);
+		input.addKeyListener(keyListener);
+
+		return composite;
+	}
+
+	void print(String line) {
+		console.append(line);
+		console.setSelection(console.getCharCount());
+	}
+
+	void printLn(String line) {
+		console.append(line);
+		console.append("\n");
+		console.setSelection(console.getCharCount());
+	}
+
+	void search(String query) throws NamingException {
+		printLn("> " + query);
+		final NamingEnumeration<SearchResult> enumeration =
+				ldapDirectory.search(query);
+		while (enumeration.hasMore()) {
+			final SearchResult result = enumeration.next();
+			printLn(result.getNameInNamespace());
+			final Attributes attributes = result.getAttributes();
+			final List<Attribute> list = toList(attributes.getAll());
+			for (Attribute attribute : list) {
+				final String id = attribute.getID();
+				print("\t" + id + ":");
+				final int size = attribute.size();
+				if (size <= 1) {
+					printLn(" " + attribute.get().toString());
+				} else {
+					printLn("");
+					for (int i = 0; i < size; i++) {
+						printLn("\t\t" + attribute.get(i).toString());
+					}
+				}
+			}
+		}
+	}
+
+	private static List<Attribute> toList(
+			NamingEnumeration<? extends Attribute> enumeration)
+			throws NamingException
+	{
+		final ArrayList<Attribute> list = new ArrayList<>();
+		while (enumeration.hasMore()) {
+			list.add(enumeration.next());
+		}
+		Collections.sort(list, AttributeComparator.INSTANCE);
+		return list;
+	}
+}