After 2nd time running a method my app window freezes 7 seocnds,error msg->
Hello folks,
first you will surely say is that my server is offline or something with the network is wrong but it is not ;-)
thats the code:
Please read my words after the pasted code at the bottom of the page!
import java.awt.Color;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.naming.directory.*;
import javax.naming.ldap.LdapName;
import javax.naming.ldap.Rdn;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.*;
import java.io.UnsupportedEncodingException;
import java.util.*;
import javax.swing.BorderFactory;
publicclass MainWindowextends JFrameimplements ActionListener
{
staticfinallong serialVersionUID = 1;
private JLabel newPasswordLB =new JLabel("Neues Passwort");
private JLabel newPasswordRepeatLB =new JLabel("Neues Passwort wiederholen");
private JLabel errorLB =new JLabel("Fehlerstatus:");
private JLabel benutzerNameLB =new JLabel("Benutzername auswhlen:");
private JPasswordField neuesKennwortPF =new JPasswordField();
private JPasswordField neuesKennwortWdhPF =new JPasswordField(10);
//private JLabel fehlerNachrichtLB = new JLabel();
private JTextField fehlerNachrichtLB =new JTextField();
private JButton pwBT =new JButton("Setze neues Passwort");
private DefaultListModel listModel =new DefaultListModel();
private JList liste =new JList(listModel);
private JScrollPane benutzerListeSP =new JScrollPane(liste);
private JCheckBox kennwortVergebenCB =new JCheckBox("Benutzer vergibt Kennwort bei Neuanmeldung selbst");
JFrame frame;
String bla;
String neuesKennwortTemp;
String benutzername;
String klassengruppe;// OU=Klassen
String lehrergruppe;// OU=Lehrer
String edvlehrername;// z.B. "verenabit"
String schulname;// z.B. OU=ASR
String BaseDN=",DC=bodensee,DC=de";
String adminPassword ="test";
String adminUser ="cn=administrator,cn=users,dc=bodensee,dc=de";
public MainWindow()
{
super("LDAP Modification Tool");
setLayout(null);
add(newPasswordLB);
add(newPasswordRepeatLB);
add(neuesKennwortWdhPF);
add(neuesKennwortPF);
add(pwBT);
add(errorLB);
add(fehlerNachrichtLB);
add(benutzerListeSP);
add(benutzerNameLB);
add(kennwortVergebenCB);
kennwortVergebenCB.setBounds(16,120,350,25);
benutzerNameLB.setBounds(430,25,160,25);
fehlerNachrichtLB.setOpaque(true);
fehlerNachrichtLB.setBackground(new Color(255,255,255));
fehlerNachrichtLB.setBorder(BorderFactory.createLineBorder(new Color(155,155,155)));
neuesKennwortPF.setBounds(230,50,150,25);
neuesKennwortWdhPF.setBounds(230,80,150,25);
newPasswordLB.setBounds(20,50,130,25);
newPasswordRepeatLB.setBounds(20,80,190,25);
pwBT.setBounds(230,170,150,40);
errorLB.setBounds(20,385,100,25);
fehlerNachrichtLB.setBounds(110,385,270,25);
benutzerListeSP.setBounds(430,50,160,360);
kennwortVergebenCB.setHorizontalTextPosition(SwingConstants.LEADING);
neuesKennwortPF.setMargin(new Insets(1, 3, 1, 1));
neuesKennwortWdhPF.setMargin(new Insets(1, 3, 1, 1));
pwBT.setMargin(new Insets(0,0,0,0));
fehlerNachrichtLB.setForeground(new Color(100,120,255));
pwBT.addActionListener(this);
liste.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
liste.setVisibleRowCount(1);
listModel.addElement("verena bit");
listModel.addElement("verena bit");
listModel.addElement("verena bit");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(620,460);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setVisible(true);
}/* Konstruktor -*/
public String schulenameSuchen()
{
return"";
}
publicvoid edvlehrernameEinlesen()
{
edvlehrername = System.getProperty("user.name");
}
publicvoid benutzernameEinlesen()
{
int index = liste.getSelectedIndex();
benutzername = (String) liste.getModel().getElementAt(index);
benutzername ="CN=" + benutzername +",OU=Lehrer,OU=ASR";
}
publicvoid neuesKennwortEinlesen()
{
neuesKennwortTemp = String.valueOf(neuesKennwortPF.getPassword());
}
publicvoid actionPerformed(ActionEvent e)
{
char[] neuesKennwortArray = neuesKennwortPF.getPassword();
char[] neuesKennwortWdhArray = neuesKennwortWdhPF.getPassword();
if (e.getSource().equals(pwBT))
{
if(liste.isSelectionEmpty())
{
JOptionPane.showMessageDialog(frame,"Sie mssen einen Benutzer auswhlen!");
return;
}
elseif(!Arrays.equals(neuesKennwortArray,neuesKennwortWdhArray))
{
JOptionPane.showMessageDialog(frame,"Die eingegebenen Kennwrter sind nicht gleich!");
return;
}
elseif(neuesKennwortArray.length == 0 || neuesKennwortWdhArray.length == 0)
{
JOptionPane.showMessageDialog(frame,"Es wurde kein Kennwort eingegeben!");
return;
}
benutzernameEinlesen();// Fr diesen Benutzer wird das Kennwort gendert
neuesKennwortEinlesen();// Dieses Kennwort bekommt der obige Benutzer neu
edvlehrernameEinlesen();// Anhand dieses EDV-Lehrers wird die Schule ermittelt wo sich der EDV-Lehrer
// befindet. Dieser schulname bzw. z.B. OU="ASR" wird als Einstieg genommen, wenn sich der EDV-Lehrer
// mit dem W2K Server befindet. Somit gibt es nur noch das Unterverzeichnis
// Eine Hashtable speichert Schlssel/Wert Paare
Hashtable env =new Hashtable();
String keystore ="C:/Programme/Java/jre1.6.0_01/lib/security/ZertifikatBerlin";
System.setProperty("javax.net.ssl.trustStore",keystore);
// Die Methode put der Klasse Hashtable weist die rechten Werte (Stings hier) einen Schlssel zu (verschiedene Kontexte hier)
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.REFERRAL,"follow");
env.put(Context.PROVIDER_URL,"ldaps://rhein:636");
env.put(Context.SECURITY_PROTOCOL,"SSL");
env.put(Context.SECURITY_AUTHENTICATION,"simple");
env.put(Context.SECURITY_PRINCIPAL, adminUser);
env.put(Context.SECURITY_CREDENTIALS,adminPassword);
try
{
DirContext ctx =new InitialDirContext(env);
String base ="dc=bodensee,dc=de";
String filter ="(sAMAccountName=" + edvlehrername +")";
String[] attribut ={"distinguishedName"};
SearchControls kontroller =new SearchControls();
kontroller.setSearchScope(SearchControls.SUBTREE_SCOPE);
kontroller.setReturningAttributes(attribut);
kontroller.setReturningObjFlag(true);
NamingEnumeration enm = ctx.search(base,filter,kontroller);
String DNkette="";
while(enm.hasMore())
{
SearchResult resultat = (SearchResult) enm.next();
System.out.println(resultat.getNameInNamespace());
DNkette = resultat.getNameInNamespace();
}
// CN=verena bit,OU=Lehrer,OU=ASR ,DC=bodensee,DC=de
// index=4,index=3 ,index=2,index=1,incex=0
LdapName FullDN =new LdapName(DNkette);
for(int i = 0 ; i < FullDN.size() ; i++ )
{
System.out.println(FullDN.getRdn(i));
}
ctx.close();
/*
// Wert fr das Kenntwort in der Activev Directory erzeugen
String neuesKennwort= "mother"; //neuKennwortPF.getText();
String neuesKennwortAD = "\"" + neuesKennwort + "\"";
byte[] newpassword = neuesKennwortAD.getBytes("UTF-16LE");
// The username to be used for the password change
ModificationItem mods[] = new ModificationItem[1];
mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,new BasicAttribute("unicodePwd", newpassword));
ctx.modifyAttributes(fehlerNachrichtLB.getText(),mods); */
}
catch (Exception er)
{
fehlerNachrichtLB.setText(er.toString());
}
}// if Ende
}// actionPerformed Ende
publicstaticvoid main(String args[])throws NamingException, UnsupportedEncodingException
{
try
{
JFrame.setDefaultLookAndFeelDecorated(true);
}
catch (Exception e)
{
e.printStackTrace();
}
MainWindow Fenster =new MainWindow ();
}
}
When I run the application the first time enter twice a password and select a user etc.. to fullfill the if clauses and press the button all is working fine: I get this result:
CN=verena bit,OU=Lehrer,OU=ASR,DC=bodensee,DC=de
DC=de
DC=bodensee
OU=ASR
OU=Lehrer
CN=verena bit
the first line with CN=verena bit etc... I get because of this line:
System.out.println(resultat.getNameInNamespace());
the next 5 Relative DN`s i get because of this code:
// CN=verena bit,OU=Lehrer,OU=ASR ,DC=bodensee,DC=de
// index=4,index=3 ,index=2,index=1,incex=0
LdapName FullDN =new LdapName(DNkette);
for(int i = 0 ; i < FullDN.size() ; i++ )
{
System.out.println(FullDN.getRdn(i));
}
thats all fine,BUT when ihit the button in my application the 2nd time and the above code gets executed my whole app freezes for 7 seconds and i get the error:
javax.naming.ServiceUnavailableException: rhein:636; socket closed
and when iclick my button the 3rd time the app runs fine again and the fourth time i click my button the app throws me again the error and freezes ?
Can someone help?
another mysterious thing is when i dont use this line of code:
env.put(Context.REFERRAL,"follow");
I get a PartialResultException, but why ?

