1 package com.ca.directory.jxplorer; 2 3 import com.ca.commons.cbutil.*; 4 import com.ca.commons.jndi.ConnectionData; 5 import com.ca.commons.naming.CBOpenConWin; 6 import com.ca.directory.jxplorer.broker.JNDIBroker; 7 8 import javax.swing.*; 9 import java.awt.*; 10 import java.awt.event.ActionEvent ; 11 import java.awt.event.ActionListener ; 12 import java.net.URISyntaxException ; 13 import java.util.*; 14 import java.util.logging.Level ; 15 import java.util.logging.Logger ; 16 17 27 public class JXOpenConWin extends CBOpenConWin implements DataListener 28 { 29 private final static Logger log = Logger.getLogger(CBOpenConWin.class.getName()); 30 31 private final static HashMap cachedps = new HashMap(20); 33 34 private JXplorer jxplorer; 35 private JNDIBroker jndiBroker; 36 private JTextField dsmlService; 37 40 protected static final String DSMLV2 = CBIntText.get("DSML v2"); 41 42 59 public JXOpenConWin(JXplorer jx, JLabel statusDisplay, String clientcerts, String cacerts, 60 String referral, String aliasType) 61 { 62 super(jx, statusDisplay, clientcerts, cacerts, referral, aliasType, HelpIDs.CONNECT); 63 64 jxplorer = jx; 65 newCon.tracing = jxplorer.jndiBroker.getTracing(); 66 67 addPasswordHandlingListener(); 68 69 } 70 71 80 private void addPasswordHandlingListener() 81 { 82 CBJComboBox templateSelector = myTemplater.getLoadComboBox(); 84 85 ActionListener [] listeners = templateSelector.getActionListeners(); 87 for (int i = 0; i < listeners.length; i++) 88 templateSelector.removeActionListener((ActionListener ) listeners[i]); 89 90 templateSelector.addActionListener(new ActionListener () 93 { 94 public void actionPerformed(ActionEvent e) 95 { 96 myTemplater.load(); 97 checkSecurityLevel(); 98 retrieveCachedPassword(); 99 } 100 }); 101 102 106 CBButton save = myTemplater.getSaveButton(); 107 108 save.addActionListener(new ActionListener () 109 { 110 public void actionPerformed(ActionEvent e) 111 { 112 cachePassword(); 113 } 114 }); 115 } 116 117 120 protected void initGUI(JLabel statusDisplay) 121 { 122 123 super.initGUI(statusDisplay); 124 125 if (JXplorer.getProperty("dsml") == null || 127 JXplorer.getProperty("dsml").equalsIgnoreCase("false") == false) 128 version.addItem(DSMLV2); 129 130 myTemplater.loadDefault(); 132 retrieveCachedPassword(); 133 134 display.validate(); 135 136 } 137 138 142 private void retrieveCachedPassword() 143 { 144 if (!JXplorer.getProperty("jxplorer.cache.passwords").equals("true")) 145 return; 146 147 if (hostName.getText().trim() == "") 149 return; 150 151 String key = makePwdKey(); 152 153 if (cachedps.containsKey(key)) 154 { 155 String p = (String ) cachedps.get(key); 156 password.setText(p); 157 } 158 } 159 160 163 private void cachePassword() 164 { 165 166 if (!JXplorer.getProperty("jxplorer.cache.passwords").equals("true")) 167 return; 168 169 String key = makePwdKey(); 170 cachedps.put(key, new String (password.getPassword())); 171 } 172 173 179 private String makePwdKey() 180 { 181 String key = new StringBuffer (50).append(port.getText()).append(managerDN.getText()).append(version.getSelectedItem().toString()).append(level.getSelectedIndex()).toString(); 182 return key; 183 } 184 185 189 public void addExtraComponent() 190 { 191 JLabel urlLabel; 192 display.makeLight(); 193 display.add(urlLabel = new JLabel(" " + CBIntText.get("DSML Service") + ": "), 0, 2, 1, 1); 194 display.addWide(dsmlService = new JTextField("", 30), 4); 195 urlLabel.setToolTipText(CBIntText.get("The DSML service; e.g. ") + "'dsml/services/DSML?ldapHost=localhost&ldapPort=19289'"); 196 197 VersionActionListener versionListener = new VersionActionListener(); 198 version.addActionListener(versionListener); 199 } 200 201 207 class VersionActionListener implements ActionListener 208 { 209 210 214 public void actionPerformed(ActionEvent event) 215 { 216 if (!version.getSelectedItem().equals(DSMLV2)) 217 { 218 dsmlService.setEnabled(false); 219 dsmlService.setText(""); 220 dsmlService.setBackground(Color.lightGray); 221 222 level.setEnabled(true); 223 checkSecurityLevel(); 224 } 225 else 226 { 227 dsmlService.setEnabled(true); 228 dsmlService.setBackground(Color.white); 229 230 level.setSelectedIndex(0); 231 managerDN.setText(""); 232 password.setText(""); 233 checkSecurityLevel(); 234 level.setEnabled(false); 235 236 } 237 } 238 } 239 240 243 public void doOK() 244 { 245 if (version.getSelectedItem().equals(DSMLV2)) 246 newCon.protocol = ConnectionData.DSML; 247 else 248 newCon.protocol = ConnectionData.LDAP; 249 250 addExtraEnvironmentProperties(); 251 252 cachePassword(); 253 254 super.doOK(); 255 256 } 257 258 private void addExtraEnvironmentProperties() 259 { 260 Properties props = JXplorer.getMyProperties(); 261 Enumeration keys = props.keys(); 262 while (keys.hasMoreElements()) 263 { 264 String key = (String ) keys.nextElement(); 265 if (key.startsWith("Context") 267 || key.startsWith("context") 268 || key.startsWith("com.sun.jndi.ldap") || key.startsWith("java.security") || key.startsWith("javax.security")) { 272 newCon.putExtraProperty(key, props.getProperty(key)); 273 } 274 } 275 } 276 277 287 public void reinit(String newclientcerts, String newcacerts, 288 String newreferral, String newaliasType) 289 { 290 newCon.clientcerts = newclientcerts; 291 newCon.cacerts = newcacerts; 292 newCon.referralType = newreferral; 293 newCon.aliasType = newaliasType; 294 295 if (jndiBroker == null) newCon.tracing = jxplorer.jndiBroker.getTracing(); 297 else 298 newCon.tracing = jndiBroker.getTracing(); 299 } 300 301 305 public void resetTitleAndPassword() 306 { 307 this.setTitle(CBIntText.get("Open LDAP/DSML Connection")); 308 309 if (!JXplorer.getProperty("jxplorer.cache.passwords").equals("true")) 310 password.setText(""); 311 } 312 313 320 public void connect(ConnectionData connectData) 321 { 322 325 try 326 { 327 jndiBroker = jxplorer.jndiBroker; 328 329 jxplorer.preConnectionSetup(); 331 332 DataQuery query = jndiBroker.connect(connectData); 334 335 query.addDataListener(this); 336 337 } 338 343 catch (Exception e) 344 { 345 log.log(Level.WARNING, "Unexpected exception in JXOpenConWin.connect", e); 346 e.printStackTrace(); 347 } 348 } 349 350 358 359 public void dataReady(DataQuery request) 362 { 363 if (!(request instanceof JNDIBroker.DataConnectionQuery)) 364 { 365 log.warning("Incorrect data for connection - cannot connect"); 366 return; 367 } 368 369 if (request.hasException() == false) { 371 if (jxplorer.postConnectionSetup((JNDIBroker.DataConnectionQuery) request)) 372 { 373 setVisible(false); 374 375 ((JNDIBroker.DataConnectionQuery) request).conData.clearPasswords(); 376 377 dispose(); 378 } 379 else 380 { 381 jxplorer.disconnect(); 382 } 383 } 384 else { 386 new CBErrorWin(this, "Error opening connection:\n" + request.getException().getMessage(), request.getException()); 389 log.log(Level.WARNING, "Error opening connection ", request.getException()); 390 request.clearException(); 391 setTitle(CBIntText.get("Couldn't Connect" + ": " + "Try Again")); 392 dispose(); setVisible(true); 394 userMessage.setText(CBIntText.get("Couldn't Open ") + request.getExtendedData("url")); 395 jxplorer.disconnect(); 396 request.squelch(); } 398 } 399 400 405 protected String getURL() throws NumberFormatException , URISyntaxException 406 { 407 String url = super.getURL(); 408 if (version.getSelectedItem().equals(DSMLV2)) { 410 String dsml = dsmlService.getText(); 411 if (dsml.startsWith("/")) dsml = dsml.substring(1); 413 414 if (url.startsWith("ldap://")) url = url.substring(7); 416 417 url = "http://" + url + "/" + dsml; } 419 log.fine("connecting with url: " + url); 420 return url; 421 } 422 } | Popular Tags |