Thursday, December 11, 2008

Camera Focal Range Data application

I'm currently using a point & shoot camera (Canon S2 IS), but have a DSLR on the way (Canon Rebel XS). The DSLR comes with a kit lens that ranges from 18-55mm (28.8mm - 88mm in 35mm equivalents), which is quite a bit shorter than my p&s (up to 432mm equiv!). So I got worried that I wouldn't be too happy with just the kit lens. At the same time, I wanted to try out LINQ in .NET, so a quick project was born...

I wrote a bare-bones console application that reads in EXIF data from images, extracts the focal length of each, and uses some simple LINQ queries that lets me see exactly how many photos were taken in wide, normal, tele, and super tele modes. It recurses through directories from a starting directory you specify, and tries to get info on all JPEG images.



Right now, there's no nice GUI, it's not multi-threaded, you can only search one folder at a time, there is no progress bar, and you can't actually see what images fall into which category, but if there's any interest in this app at all, I'll add all that sometime soon.

Also, you have to enter a 35mm focal length equivalent for the categories to be accurate. This is annoying, because for point and shoots, that info is sometimes hard to find. I had to actually take a picture all the way zoomed in, and compare the specs on dpreview.com with the EXIF data (72mm max zoom in the EXIF = 432mm max zoom in the specs, so it has a multiplier of "6"). I may end up looking up the multipliers on a bunch of popular cameras and just ask the user to select his/her camera, or optionally manually enter the focal length multiplier if the user's camera is not on the list.

Anyway, the application is fully working in it's current state - all you need is the .NET 3.5 framework. If you want to use the app and can't figure out the proper multiplier to use, just post a comment here, and I or another reader can certainly help you out.

No crazy code in this app, but here's the LINQ part:

//generic list
private List<double> zoomData;
...
zoomData = new List<double>();
...
//code in the directory/file loop
zoomData.Add(focalLength * focalLengthX);
...
int shortTele = (from t in zoomData where t > 100 && t <= 150 select t).Count();

In my case, I was happy to see that I had a significant amount more wide and normal shots than telephoto shots, so maybe I won't need an extra lens after all.

You can download the app here.

UPDATE: Picasa somehow figures out the 35mm equiv. from the EXIF data. I'm going to figure out how they're doing it and update my application soon.

Wednesday, December 3, 2008

System.Net.Mail throws "no such user" exceptions w/ Lotus Domino!

Code that I had running for years without issues suddenly broke with the error "Mailbox unavailable. The server response was: user@domain.com... No such user". Very useful, as with this information, I can now automate (or at least partially automate) internal mailing list management.

For now, I've just added code to catch this specific exception (in addition to the catch-all SmtpException) to have it generate a work order ticket if there's a problem:

try {
smtp.Send(newsletter);
} catch(System.Net.Mail.SmtpFailedRecipientException ex) {
//generate work order...
}

There's also "SmtpFailedRecipientsException" which triggers only if it can't send an email to all the recipients, though I don't see why you would use this over SmtpFailedRecipientException, as the latter informs you in the exception message if all recipients haven't received the email in addition to handling the case where one or some recipients failed.

However, I've noticed that if more than one recipient email fails, it only displays the first email address that failed in the error message. Makes sense, since it probably is processing one email completely at a time, and if it fails on one, it doesn't go any further. The solution to this, I guess, would be to have each email sent out within a loop of separate SMTP calls instead of using the handy "mail.To.Add" method. There would most likely be a performance hit doing it this way, though.

I'm going to keep looking into it, but does anyone know of a better way?

Also, I assume this works with MS Exchange as well, but don't know anyone using email with Exchange to test with. I have verified it doesn't work with Gmail and Hotmail, so this is best used only with an environment where you know that everyone is on the same mail server.

Friday, September 19, 2008

Java SwingX ActiveDirectory/LDAP LoginService

Sorry for the long break from posting, but I haven't worked on anything interesting enough to post for a while. I've just completed some basic ActiveDirectory auth code with a Java Swing application. I had a heck of a time connecting to AD at all with Java - all the examples I found online seemed like they should have worked, but my test code just hung with no errors, and with no useful debugging info. I'm still not sure what was going on exactly, but the following helper class worked for me (the most notable exception between this code and my previous attempts is that it's now specifying the domain controller):

LDAPUtils.java:
package app;

import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;

/*
@author Randy Coates
Modified slightly by Dave Gruska
*/
public class LDAPUtils {
static String ATTRIBUTE_FOR_USER = "sAMAccountName";
public Attributes authenticateUser(String username, String password, String _domain, String host, String dn) {
String returnedAtts[] ={ "sAMAccountName", "memberOf" };
String searchFilter = "(&(objectClass=user)(" + ATTRIBUTE_FOR_USER + "=" + username + "))";

//Create the search controls
SearchControls searchCtls = new SearchControls();
searchCtls.setReturningAttributes(returnedAtts);

//Specify the search scope
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
Hashtable environment = new Hashtable();
environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

//Using starndard Port, check your instalation
environment.put(Context.PROVIDER_URL, "ldap://" + host + ":389");
environment.put(Context.SECURITY_AUTHENTICATION, "simple");

environment.put(Context.SECURITY_PRINCIPAL, username + "@" + _domain);
environment.put(Context.SECURITY_CREDENTIALS, password);
LdapContext ctxGC = null;

try {
ctxGC = new InitialLdapContext(environment, null);

//search for objects in the GC using the filter
String searchBase = dn;
NamingEnumeration answer = ctxGC.search(searchBase, searchFilter, searchCtls);
while (answer.hasMoreElements()) {
SearchResult sr = (SearchResult)answer.next();
Attributes attrs = sr.getAttributes();
if (attrs != null)
return attrs;
}
}
catch (NamingException e) {
e.printStackTrace();
}
return null;
}
}
I'm then extending SwingX's LoginService to work with this class and return some extra details, like the user's group (this can be easily expanded to capture an Active Directory attribute):

LDAPLoginService:
package app;

import javax.naming.directory.Attributes;
import org.jdesktop.swingx.auth.LoginService;

/*
@author Dave Gruska
*/
public class LDAPLoginService extends LoginService {
private String domain;
private String host;
private String dn;
private String userName;
private String groupName;

public String getDomain() {
return domain;
}

public void setDomain(String domain) {
this.domain = domain;
}

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public String getDn() {
return dn;
}

public void setDn(String dn) {
this.dn = dn;
}

public String getUserName() {
return userName;
}

public String getGroupName() {
return groupName;
}

public LDAPLoginService(String domain, String host, String dn) {
this.domain = domain;
this.host = host;
this.dn = dn;
}

@Override
public boolean authenticate(String name, char[] password, String server) throws Exception {
LDAPUtils LDAPlogin = new LDAPUtils();

//TODO: investigate if there's a more efficient way to convert this
StringBuilder passwd = new StringBuilder();
for(char c : password) {
passwd.append(c);
}

Attributes attrs = LDAPlogin.authenticateUser(name, passwd.toString(), this.domain, this.host, this.dn);

if(attrs == null) {
//login failed
return false;
} else {
//login successful
String[] splitUserName = attrs.get("sAMAccountName").toString().split(":");
userName = splitUserName[1].trim();
groupName = attrs.get("memberOf").contains("CN=AppAdmins,CN=Users,DC=domain,DC=com") ? "admin" : "user";

return true;
}
}
}
and finally, the presentation layer code displays a login pane (JXLoginPane dialog) that gets called in the constructor right after the components are initialized:
loginService = new LDAPLoginService("domain.com", "[domain controller IP]", "cn=Users,dc=domain,dc=com");
JXLoginPane.Status status = JXLoginPane.showLoginDialog(null, loginService);

if(!status.equals(status.SUCCEEDED)) {
System.exit(0);
}

loggedInAsLabel.setText(String.format("%S (%s)", loginService.getUserName(), loginService.getGroupName()));
BTW, the System.exit code only gets called when the user gives up on trying to log in.

Monday, August 11, 2008

My personal Photoshop use is decreasing drastically

When I got Photoshop 4 back in the day, I used it daily to do work at home and to do my own personal creations. I also scanned in lots of photos and fixed them up.

Now that I'm a full-time developer (as opposed to part designer/part developer), I find myself using PS less and less. I've since upgraded to CS2, which in retrospect was actually quite a waste of money - PS 6 had everything I needed. My first digital camera was great (Sony DSC F-707), but almost every image really required post-processing to really look good, so I used PS quite a bit back then. I now have a Canon S2 IS, which has excellent color reproduction - good enough that I don't bother with most images anymore. And I've pretty much stopped doing my own image composites for fun (though I occasionally knock a few off for friends/co-workers).

But I still do a fair amount of image manipulation overall, considering I take tons of photos. I've found Google's Picasa software handles most of my modifications quite well (red-eye, cropping, slight color fixes) - its "Fill Light" tool is awesome for fixing underlit subjects btw, and way quicker than any method I've ever used in PS. Picasa also manages the photos better than Adobe's Bridge or Lightroom, IMHO. And I can upload straight to Snapfish. So my workflow all pretty much happens in one place.

For situations like noise reduction, I use Neat Image (free version). You can't beat its simplicity, and the results are fantastic in my experience so far (I've mostly used it to clean up high ISO noise in otherwise unacceptable-quality pictures). For stitched panoramas I use Hugin (also free). And for quick fixes, I use the free (and open source) Paint.NET application, which loads much quicker than PS, and has many of its basic tools.

There's still situations where I still use Photoshop - mostly for true HDR and HDR-like processing (levels adjustment layers, dodge/burn) to bring out detail, and I recently created a family photo for my parents from a number of different pictures that I wouldn't even consider using another application for, but it's probably dropped to about 5% or so of my total image processing time these days.

Monday, July 28, 2008

Retreiving and sending Hotmail email from Gmail

I had a Hotmail account years before Gmail was around, and I still keep it around just in case I get an important email there. I tried to use Gmail's "Get mail from other accounts" feature, but it always said "connection timed out". I had just assumed Hotmail offered POP3 - it doesn't. I found a helpful article on LifeHacker to get around this issue, but the software they mention is not a free service.

But, buried in the comments is a link to the "GetMail for Hotmail" Windows service. That link, however, sends you to the vendor's Web site, which wants you to sign up for a free trial of some other service in order to download the software. But, this Download.com link will let you download it with no strings attached.

You do have to enter your Hotmail password so that it can retreive the mail, which may be a deal-breaker for some. To be safer, I changed my Hotmail password to make sure it doesn't match any other service I use. I'm also confident that I don't use this account for any service I use, though that still doesn't technically make me 100% protected.
I'm also keeping an eye on it from a network perpective to make sure it's not trying to do anything it's not supposed to. I may actually attempt to write my own simple version of this some day just for the extra piece of mind. The program also is a resource hog, so you don't want have it set to fetch mail too often.

It does work well, otherwise, though. I've got mail set up to go directly to my Gmail archive (Settings --> Filters), and labeled with "Hotmail", so it's separate from my regular Gmail mail. I also set up my Hotmail.com account under "Settings --> Send mail as" so that I can have email sent as if it came from Hotmail.

If everything keeps working well, I'll be very glad to not have to log into Hotmail anymore.

Thursday, July 24, 2008

SQL Server 2000 not deleting backups

I'm sure there could be a lot more reasons for backup deletions failing than this, but detaching a database that was set to "offline" fixed the problem for me. No other error messages I could find - luckily I realized the problem started around the time we took that db offline, so it didn't take too long to troubleshoot.

Monday, July 14, 2008

JTabbedPane with close button that sets the tab title correctly

Java 6 includes the abilility to add a component (like a "x" to close the tab) to a tab pane, and Alexander Potochkin, has elegantly put together a "ButtonTabComponent" class to easily handle this.

I've made just a few changes to get the "x" to look more like the "x"'s on the rest of my application, and most importantly for me, to set the title of the JTabbedPane so that calling getTitleAt(tabPosition) doesn't return an empty string, since this is now handled completely by the ButtonTabComponent class. BTW, I'm mixing regular tabs with tabs that can be closed, so I want to be able to just have to call one method to check what each's title is, i.e.:

int existingTabPosition = 0;
int tabCount = tabbedPane.getTabCount();
for(int i = 0; i < tabCount; i++) {
String thisTabTitle = tabbedPane.getTitleAt(i);
if(thisTabTitle.equals(menuSelectedTabTitle)) {
existingTabPosition = i;
}
}

if(existingTabPosition != 0) {
//report already exists - just force focus to it
tabbedPane.setSelectedIndex(existingTabPosition);
} else {
//no report exists by this name - add new tab
tabbedPane.add(jrView);
int newTabPosition = tabbedPane.getTabCount() - 1;

tabbedPane.setTabComponentAt(newTabPosition, new ButtonTabComponent(menuSelectedTabTitle, tabbedPane));
tabbedPane.setSelectedIndex(newTabPosition);
}

Here's my updated "paintComponent" method of the TabButton inner class:
      //paint the "x" and update the title
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Stroke stroke = g2.getStroke();

//shift the image for pressed buttons
//changed by DG
//if(!getModel().isPressed()) {
// g2.translate(-1, -1);
//}

g2.setStroke(new BasicStroke(2));
g.setColor(Color.BLACK);
if(getModel().isRollover()) {
Color highlight = new Color(0, 51, 153); //dark blue - changed by DG
g.setColor(highlight);
}
int delta = 5; //changed by DG
g.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
g.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);

//NOTE: only needed if button pressed in effect is used above
//changed by DG
//leave the graphics unchanged
//if(!getModel().isPressed()) {
// g.translate(1, 1);
//}
g2.setStroke(stroke);

//add the title here - added by DG
int i = pane.indexOfTabComponent(ButtonTabComponent.this);
pane.setTitleAt(i, label.getText());
}
I assume the title is set here for a specific reason, though my next experiment will be to remove the title from here and add it back to the JTabbedPane - though the way it is might actually be a bit nicer to work with. Alternatively, you can set both at the same time - it works without problems, but it seems like bad form to have to manually set the same thing twice.

Monday, July 7, 2008

Apache Commons

I had forgotten that the first Java book I read, Bruce Eckel's "Thinking In Java", highly recommended using Apache Commons if you found that the core language didn't contain something you expected to be there. The "Commons" are vast set of libraries that improve and add to the core Java language, and can be a huge time saver.

My goal this week is to read all the Commons' user guides to get familiar with its capabilities.

Tuesday, July 1, 2008

JTextArea setEnabled strangeness (Java Swing)

NetBeans 6.1 automatically wraps a JScrollPane around a JTextArea, and handles all the details of setting it up nicely. But, if you need to work with all your components at once (i.e. to tick the "setEnabled" property on each), you'll find the following code works on everything but the JTextAreas (and probably all other nested components):
for(Component component : ProjectPanel.getComponents()) {
component.setEnabled(false);
}
I haven't found a good way around this yet, as the following addition to the loop fails as well:
//use reflection to get the class type
if(component.getClass().equals(JScrollPane.class)) {
JScrollPane scrollPane = (JScrollPane)component;

for(Component subComponent : scrollPane.getComponents()) {
subComponent.setEnabled(false);
}
}
Debugging reveals that the the JTextArea isn't seen as a component here (things like the scroll bar and viewport are, though). getComponents() doesn't loop over children, so maybe this does work two or three levels deeper, but that's getting a bit ridiculous.

Until I figure out a better way, I'm setting the values explicitly using the following method:
textArea.setEnabled(false);
textArea.setBackground(UIManager.getColor("TextArea.disabledBackground"));
The "setBackground" method is needed here (at least on Windows XP using the "Classic" theme), as the background isn't automatically set to the disabled color when setEnabled is set to "true". Most other components automatically handle this for you. This also means when you re-enable it, you need to set it back to the default enabled color:
textArea.setBackground(UIManager.getColor("TextArea.background"));
BTW, I found a good article on using UIManager here that explains how to use and manipulate UI settings.

In any case, I found an official Java bug report from 1998 concerning what appears to be the exact enabled/disabled problem I ran into - the bug's status is "6-Fix Understood, request for enhancement". Though the suggested fix actually didn't work for me...

Monday, June 23, 2008

Updated enum display values code

I previously posted about how to automatically format an enum value by overriding the enum's toString() method. The code works, but it doesn't scale very well and it's not as robust as it could be, so I've pulled it out into it's own method, and added a "case exception list" feature, so it can be called from any enum, and allows for more flexibility:

public class EnumUtils {
public static String displayFormat(String enumName, String... caseExceptions) {
String[] splitNames = enumName.split("_");
String caseExceptionName = "";
Boolean caseException = false;

for(String name : caseExceptions) {
String parsedName = name.replace(" ", "_");

if(enumName.equalsIgnoreCase(parsedName)) {
caseExceptionName = parsedName.replace("_", " ");
caseException = true;
}
}

StringBuffer fixedName = new StringBuffer();

if(caseException) {
fixedName.append(caseExceptionName);
} else {
for(int i = 0; i < splitNames.length; i++) {
String firstLetter = splitNames[i].substring(0, 1).toUpperCase(),
restOfWord = splitNames[i].substring(1).toLowerCase(),
spacer = i == splitNames.length ? "" : " ";

fixedName.append(firstLetter).append(restOfWord).append(spacer);
}
}

return fixedName.toString();
}
}

which is called from the enum like this, for example:
@Override
public String toString(){
return EnumUtils.displayFormat(name(), "NT Servers", "SQL Server");
}
The method takes in the name of the enum value, as usual, but also allows the developer to specify a variable-length exception list, thanks to Java's "varargs" feature, which lets you accept an array of any basic type as the last argument. All you need to do in the method is specify the type along with ellipses (...), and it takes care of the rest for you. The code allows the developer to specify the enum value with a space between words, or with "_"'s. BTW, it also works fine with no exceptions:
return EnumUtils.displayFormat(name());
If you've seen my original code in the post below, you'll also noticed that I've changed StringBuilder to StringBuffer, and changed the String.concat()s to StringBuffer.append()s. StringBuilder is actually just meant for single-threaded operations, so while it's probably safe to use it here, it could potentially wreak havoc. The append()s are for miniscule performance gains (though I haven't run tickcount tests yet to actually confirm that this method is faster). I've also defined the new strings in the second for loop with commas between them, instead of re-typing "String" three times. I heavily doubt there's a performance gain here, as I assume the compiler sees both the same, but it's slightly faster to type :-)

BTW, I'm now using the Blogger Syntax Highlighter to display formatted code, as it looks a bit nicer, and has native support for Java.

Thursday, June 19, 2008

Binding an enum to a jComboBox with user-friendly name values (Java Swing)

Binding an enum to a jComboBox is easy enough (though, as far as I know, not possible using the GUI tools that are available with NetBeans 6.1). First, of course, define the enum:

enum Timeframe {
THIS_WEEK,
THIS_MONTH,
THIS_YEAR
}

Then link it to the combo box:

ComboBoxModel cbModel = new DefaultComboBoxModel(Timeframe.values());
timeframeComboBox.setModel(cbModel);

This works fine, except that you get the not so user-friendly "THIS_WEEK", "THIS_MONTH", etc. values displayed in the dropdown. To fix this, the easiest way is to override the "toString()" method of the enum, so the modified enum becomes:

enum Timeframe {
THIS_WEEK,
THIS_MONTH,
THIS_YEAR;

@Override
public String toString(){
String[] splitNames = name().toLowerCase().split("_");
StringBuffer fixedName = new StringBuffer();

for(int i = 0; i < splitNames.length; i++) {
String firstLetter = splitNames[i].substring(0, 1).toUpperCase(),
restOfWord = splitNames[i].substring(1),
spacer = i == splitNames.length ? "" : " ";

fixedName.append(firstLetter).append(restOfWord).append(spacer);
}

return fixedName.toString();
}
}

This code first lowercases everything, then splits it up into separate words based on the "_" character. It then loops over all the words, isolates and capitalizes the first letter, and isolates the rest of the word in a separate variable.

Next, a space is added if there are more words left to be looped over. Finally, everything is re-assembled and added to the StringBuilder variable that's returned as the "fixed" name. BTW, if anyone know of a more efficient way to do this, I'd love to hear about it. Java's regex doesn't appear to support the "L" flag or anything similar, otherwise I could have done this much more efficiently with one regex replace.

In any case, no other changes are necessary, as toString() only affects the display of the enum and not its underlying values, so you can then use code that uses the enum as usual:

private void timeframeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {                                                  
JComboBox cb = (JComboBox)evt.getSource();
Timeframe timeFrame = (Timeframe)cb.getSelectedItem();
Date now = new Date();

switch(timeFrame) {
case THIS_WEEK:
//processing code...
BTW, it's interesting that Java's "switch/case" only supports ints, though I guess it does kind of force you into using enums for this type of thing, which, a lot of the time, would be the preferred method over using plain strings. (As you're probably aware, enums underlying real values are ints).

Tuesday, June 17, 2008

Using row highlighters in SwingX 0.9.3

I was excited to read about row highlighting in the JXTable SwingX component, and found a number of tutorials showing how to implement this. But, all of them show how to do it the same way, and none of them work any longer (at least as of v 0.9.2+). Here's the example code from the SwingLabs tutorial:

jxTable.setHighlighters(new HighlighterPipeline(
new Highlighter[]{ AlternateRowHighlighter.classicLinePrinter }));
There is no more "HighlighterPipeline", nor is there "AlternateRowHighlighter". I couldn't find any other up-to-date tutorials on the subject, so I did a little digging in the SwingX JavaDocs, and was able to put together the following code that accomplishes the same thing.
import java.awt.Color;
import org.jdesktop.swingx.decorator.*;
...
ColorHighlighter highlighter = new ColorHighlighter();
highlighter.setHighlightPredicate(HighlightPredicate.ODD);
highlighter.setBackground(Color.getHSBColor(0.62f, 0.01f, 0.92f)); //very light blue

DetailsTable.setHighlighters(highlighter);
Not much to explain here, just a different way of doing it. In addition to a number of other features, HighlightPredicate also supports "ROLLOVER_ROW", which works as you'd guess. "ROLLOVER_ROW" has also made the following unnecessary:
jxTable.setRolloverEnabled(true);
Code changes are obviously one of the problems with using pre-release code, though I've had great luck with SwingX components so far concerning stability. It's been worth the risk for me so far, though I'm not relying on the extra functions that SwingX provides for anything "mission critical" - just for row sorting, filtering, coloring, packing, etc.. JXTable inherits from the standard JTable, so if I decide to remove SwingX, there should be minimal pain involved (I hope).

See screenshots of your web site with different browsers

I've been looking for a site like this for quite a while. BrowserShots.org lets you select from 67 different browsers/versions, and allows you to select parameters like screen size, color depth, and what features are enabled in the browser.

It takes a little while for the results to come back (at least five minutes or so, though you can begin browsing the first few screenshots almost immediately), but this is a really helpful service that I could have used years ago.

Modifying a database schema in NetBeans 6.1

If the only changes to your schema are within existing tables, you're in luck, as you can just open the "META-INF" folder, right click on the schema, and choose "Recapture Schema from Database". After doing this, make sure to rebuild the project ("Build" --> "Build Main Project" from the top menu). Now the new schema info will be available in beans binding menus, etc.

However, the only way I could figure out how to change a schema that involves a new table is to create a new schema, delete the old one, and modify any code that references the old schema. "Recapture..." does not retrieve new tables, and there's no way I could find to actually edit the schema, without diving into the XML code (I've seen it - it's not worth it, and I'm not usually afraid to edit complex XML).

To create a new schema, right click on the "META-INF" folder, and select "New" --> "Other..." --> select the "Persistence" folder, and "Database Schema". Name it, select your database, select the tables you want to add. Next, either manually edit or delete and re-create any Entity Classes. Rebuild the project, and you're set.

I assume/hope an "Edit Schema" function is coming to NetBeans someday soon. "Update Entity Class" would be nice, too.

Wednesday, June 11, 2008

Retrieving a user's email address from Active Directory

An application I'm working on requires complete user management through Active Directory. So I'm slowly building up an "ADUtils" support class to wrap up all the necessary AD querying. I've found a decent amount of ADSI examples online, though finding specific information, and extracting best practices from these has been difficult.

Being rather new to AD, I don't make any claims that the code below follows all best practices, though I'll try to explain what I've done and why I've done it that way. If you know of a better way to accomplish the following, please let me know.

    /// <summary>
/// Retrieves the user's email address from AD
/// </summary>
/// <param name="user">User's NT account name without domain prefix</param>
/// <returns>User's email address</returns>
public static string GetEmailAddress(string user) {
DirectoryEntry entry = new DirectoryEntry("LDAP://DC=yourdomain,DC=com");
DirectorySearcher searcher = new DirectorySearcher(entry);

searcher.Filter = String.Format("(SAMAccountName={0})", user);
searcher.PropertiesToLoad.Add("mail");

SearchResult result = searcher.FindOne();

string emailAddress;

try {
emailAddress = result.Properties["mail"][0].ToString();
} catch (ArgumentOutOfRangeException e) {
emailAddress = String.Empty;

//TODO: log/display error
}

return emailAddress;
}

  • The LDAP address does not specify the domain controller. This allows AD to intelligently choose which DC to use.

  • By explicitly stating what properties to load, you save the server from loading them all.

  • "searcher.FindOne()" does exactly what it says and just loads one result. I'm not sure of the performance benefit of using this vs. "searcher.FindAll()" if there's only one result anyway, but this is certainly better form if you're expecting only one result.

  • Lastly, if the field is left blank in AD, you will get an ArgumentOutOfRangeException instead of a blank field back, so it's a good idea to explicitly handle it.

Using my "GetUsersInGroup" method, you can use the "GetEmailAddress" method to get all group members' email addresses:

using System.Configuration;
...
string LDAPGroupAddress = "LDAP://CN=yourgroup,CN=Users,DC=yourdomain,DC=com";
CommaDelimitedStringCollection emailList = new CommaDelimitedStringCollection();

ADUtils.GetUsersInGroup(LDAPGroupAddress).ForEach(delegate(string member) {
emailList.Add(AuthUtils.GetEmailAddress(member));
});
...
mail.To.Add(emailList.ToString());

And since we're on the topic of performance, the generic List method "ForEach" is faster than the equivalent regular "foreach", though "for" is actually the fastest, but there's really not much of a difference at all unless you're dealing with a huge number of iterations.

As a side note, this is my first post using Windows Live Writer, and with the incredibly helpful "Insert Formatted Code" add-in. I'm very impressed with both.

Tuesday, June 10, 2008

Dead power supply

My home desktop computer (Dell Dimension 4500) stopped working last night, but before it died, it blue screened on startup with the following:

0x0000008E
(0xC0000005, 0x805677CB, 0xF300CB34, 0x00000000)

According to a number of Google searches, this error can actually mean a few different things, including RAM and driver issues, but the one that made the most sense was that the power supply was weak, since on the next restart attempt, it didn't actually restart (just a green light when the power button is pressed - no fan, no display on the monitor). Also, the power supply fan had been very loud for a few weeks previous. So it all fits.

I don't have a spare power supply to test with, so I ordered another one last night. Hopefully that's the only problem. I really don't want to get a new computer before Nehalem comes out, and preferably not before Windows 7 comes out as well, but my computer is going on seven years, and I've resuscitated it a few times already, so I'm not holding my breath.

UPDATE (6/12): New power supply arrived (free delivery, only took two days - thanks, 911forPCs.com!). I installed it, and now everything works again. But, the CPU fan is still a bit loud, so I'm going to try greasing it, or replacing it if necessary.

Monday, June 9, 2008

Using the Java Persistence API with Swing in NetBeans 6.1 (part 1)

I have yet to find a good tutorial on how to get started using JPA with the new version of NetBeans that takes advantage of Beans Binding and other helpful new features. NetBeans has a lot built in to help you through it, but configuration is not obvious, and there are a few tricky parts along the way. So hopefully the steps below will at least get you started.

This tutorial will focus on using JPA without EJB (JPA is part of the EJB spec for now, but was built to be used with or without it - future versions will be separate from the EJB spec to better reflect this). I'm also assuming you have a database already set up, and are just looking to connect it to your application.

  1. Create a new project in NetBeans 6.1 (Ctrl+Shift+N)
  2. Choose "Java" --> "Java Desktop Application"
  3. Name the application, and select "Basic Application"
  4. Right click on your project, and select "New" --> "Entity Classes from Database..." and select your database connection (see my previous post about connecting to SQL Server)
  5. Change the class names if necessary, and click on "Create Persistence Unit..."
  6. The defaults here are fine - btw, the latest versions of TopLink and Hibernate both fully support JPA. I'm using TopLink for this project.
  7. Back on the main design screen, locate "Java Persistence" in the "Palette" section (normally on the right hand side, at the bottom of the Palette)
  8. Drag an "Entity Manager", "Query", and "Query Result" instance onto the design area, and rename them if you choose.
  9. Click on the instance of the "Query" in the "Inspector" (bottom left-hand side by default), and view its properties (bottom right-hand side by default).
  10. Click on the elipses "..." next to "query" on the properties pane, and type in a valid JPQL query. JPQL is similar in syntax to SQL, but it deal directly with objects. The JPQL query equivalent of


    SELECT *
    FROM WeeklyDetail
    INNER JOIN...
    would actually be


    SELECT wd
    FROM WeeklyDetail wd
    "wd" is just like a SQL alias (i.e. SELECT wd.* FROM WeeklyDetail wd), but it is required in JPQL.

    The nice part about querying objects is that for simple related objects/tables, you don't need to deal with JOINs. The above JPQL query actually returns all related objects (from different underlying SQL tables). To adjust for performance, you can explicitly state how the data is "fetched" - basically "all at one time" or "on demand". However, this is control in the entity classes.

    There's volumes more to JPA - to really get a handle on how to work with it, I suggest reading "Pro EJB 3: Java Persistence API" by Mike Keith and Merrick Schincariol.
  11. Make sure in "other properties" below the query field, that your Entity Manager is selected as the "entityManager".
  12. Select your "Query Results" instance (I've called mine "weeklyDetailsQueryResult"), and view it's properties.
  13. Select your query that you just worked with, and check "modifiableWrapper" and "observable".
  14. Under the "Code" tab (still in the properties), in the "Type Parameters" field, type the name of your package followed by a "." and the entity class name. Technically, the package name should be different casing from your application name (Pascal case for the application, camel case for the package.

    NOTE: If you type the casing incorrectly, it will appear to work until you run the project.
  15. Drag a jTable onto the design area (or jxTable if you're using SwingX (see my previous post) and want to have built-in column sorting).
  16. Right click on the table, and select "Bind" --> "elements".
  17. For the binding source, choose the query result you just created, and choose the columns you would like displayed in the table.
  18. Now when you run the project, the table should populate with the data from your database.
Part 2 of this tutorial will address how to update and delete data from within the table.

NOTE: screen captures showing each step coming soon

Friday, June 6, 2008

sp_MSForEachTable

I had to improve performance on a just-restored database, and remembered of one of my favorite undocumented SQL Server commands, sp_MSForEachTable. In the below example, the "?" is the variable for where the table name should go in the statement you want to execute:


EXEC sp_MSForEachTable "UPDATE STATISTICS ?"
Another quick example - to print out all the table names in a db, just run:


EXEC sp_MSForEachTable "PRINT '?'"
FYI, there's also sp_MSForEachDB that lets you do the same operation on all databases on the server. Learning this alone was worth the price of Brian Knight's "Admin 911: SQL Server 2000" book.

Thursday, June 5, 2008

Baluster End Spacing Calculator in C#

I'm working on putting up balusters (spindles) on my deck at home, and since all the posts are spaced differently, I needed to calculate the proper end spacing for each section. Granted, it's easy enough to do this by guessing at the first length, then physically marking out each baluster width and spacing on the actual deck, and then adding the start spacing with the end spacing and dividing by two. And that's probably what I'd normally do, but I wanted to take a little break, so I made a quick app to figure this out for me.

It's a very simple app, and took about 10 minutes to write, but here's the interesting parts in case anyone is interested:


/// <summary>
/// Determines the number of balusters needed based on the top railing width
/// </summary>
/// <returns>Baluster count</returns>
public int CalculateBalustersNeeded() {
int numberOfBalusters = Convert.ToInt32(Math.Ceiling(
(this.topRailingWidth - this.spacing) / (this.balusterWidth + this.spacing)));

return numberOfBalusters;
}
and


/// <summary>
/// Determines the spacing between the post and the baluster.
/// </summary>
/// <param name="balusterCount"># of balusters used.</param>
/// <returns>Spacing at each end in inches</returns>
public float CalculateEndSpacing(int balusterCount) {
//all balusters + spacing without end spacing
float totalUnitWidth = (balusterCount * this.balusterWidth)
+ (this.spacing * (balusterCount - 1));
float endSpacing = (this.topRailingWidth - totalUnitWidth) / 2;

return endSpacing;
}
Nothing too exciting, really. And Excel probably would have been a better choice than a console app. But since it's all done with more or less "proper" objects (these methods are part of a "RailingSection" class generic collection that's part of a "Deck" object), I can now easily expand this into a full-blown deck design software package. Right. I guess I just can't get enough of coding at work...

UPDATE (6/13): Wow, this post is getting a lot of hits by people searching for a baluster calculator - here's the compiled EXE file: BalusterCalc.exe. You'll need the .NET framework 2.0 or higher to run it.

Thursday, May 22, 2008

Querying Active Directory groups via LDAP in ASP.NET

The RoleManager in .NET is great for SQL-based auth, but is really lacking when it comes to Windows auth. I needed to get a list of all users in a role, and was excited to find that the following method exists:

Roles.GetUsersInRole("RoleName");
By the way, to use RoleManager, add the following code in your Web.Config file within the System.Web tags:


<authentication mode="Windows" />
<roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider" />
Perfect, except that you get the following error when using it with Windows auth:
"The configured Role Provider (WindowsTokenRoleProvider) relies upon Windows authentication to determine the groups that the user is allowed to be a member of. ASP.NET Role Manager cannot be used to manage Windows users and groups. Please use the SQLRoleProvider if you would like to support custom user/role assignment."
Rather lame. I don't want to add/edit/delete users and groups - just look up what users are in a group. In any case, I ended up querying this info from Active Directory with LDAP. I made a static class called "AuthUtils" and first added the following method:


using System.Collections.Generic;
using System.DirectoryServices;
...

public static List<string> GetUsersInGroup(string LDAPAddress) {
List<string> groupMembers = new List();
DirectoryEntry group = new DirectoryEntry(LDAPAddress);
foreach(object dn in group.Properties["member"]) {
string[] parsedName = dn.ToString().Replace("CN=", String.Empty).Split(',');
groupMembers.Add(parsedName[0]);
}

return groupMembers;
}
which is callable with the following:


string LDAPGroupAddress = "LDAP://domain.com/CN=GroupName,CN=Users,DC=domain,DC=com";
List<string> groupMembers = AuthUtils.GetUsersInGroup(LDAPGroupAddress);
The above method strips out the CN (Canonical Name) tags from the results, and returns a list of user name strings.

I also built another quick method in AuthUtils to determine if a user is in a role, which also depends on the "GetUsersInGroup" method that was just built:


public static bool IsInRole(string LDAPAddress, string user) {
List<string> groupMembers = GetUsersInGroup(LDAPAddress);
return groupMembers.Contains(user);
}
This simply gets the group members as above, and uses the handy "Contains" method that's available in generic lists to return a true or false.

There is a User.IsInRole that works with regular NTLM auth, but for some reason it seems a bit flaky with an Active Directory back end. The method I've created above has always worked for me so far. I'm in the process of figuring out what the issue is with the regular User.IsInRole("RoleName") method, as it's bothersome that this doesn't work consistently.

UPDATE: Roles.IsUserInRole("RoleName") works fine with AD (vs. using User.IsInRole("RoleName"), as long as you're using IIS. It doesn't seem to work well with Visual Studio's built-in Web server (it always caches the role info).

Tuesday, May 20, 2008

Free J2EE online course

The next run of the J2EE Programming course in the excellent "Java with Passion" series starts on July 1st. This is all put together by Sang Shin, a technology architect at Sun Microsystems.

More details here:
http://www.javapassion.com/j2ee/

I've just signed up.

Wednesday, May 14, 2008

Java --> .NET rough equivalents

To help me understand Java concepts better, I've made a comparison to the rough equivalents in .NET.

Here's the list so far:
  • Java ~= C# or VB.NET
  • J2EE/JEE (Java Enterprise Edition) ~= .NET framework
  • Swing ~= Windows Forms
  • AWT ~= VB6 Forms?
  • Beans Binding ~= data binding
  • JPA (Java Persistence API) ~= ORM via ADO.NET Entity Framework / LINQ
  • EJBQL/JPQL (Enterprise JavaBeans Query Language/Java Persistence Query Language) ~= LINQ
  • JDO ~= ADO.NET
  • JSF (Java ServerFaces) ~= ASP.NET
  • JSP (Java Server Pages) ~= plain ASP
  • Hibernate, JUnit, Spring all have direct .NET equivalents (NHibernate, NUnit, Spring.NET)
I'm still getting a grasp on a lot of this stuff, so please let me know if I've misrepresented anything. I'll also add to the list as I think of more things.

UPDATE: Paul Sasik posted a question about .NET/Java comparisons at StackOverflow.com, and got a lot of great responses, which update and expand this list considerably (see http://stackoverflow.com/questions/2526024/analogues-of-java-and-net-technologies-frameworks). Thanks for linking to my initial blog post, Paul.

Installing SwingX in NetBeans 6.1

SwingX adds some really nice UI and other improvements to Swing. It's sponsored by Sun, and may be released in a future version of Java, but appears stable enough to use in projects now.

There's a tutorial on how to set it up, but it's missing some important content, so here's how I did it:

1. Download and unzip from http://swinglabs.org/downloads.jsp
2. Place swingx-x.x.x.jar file (from swingx/dist/ folder) in a place that you won't delete it later.
3. Open your project in NetBeans, right-click on "libraries", and choose "Add Library..."
4. Click on "Create...", give it a name (i.e. "SwingX"), and select "Class Libraries".
5. On the next dialog box, under the "Classpath" tab, click "Add JAR/Folder..." and locate the swingx-x.x.x.jar file from step 2.
6. Add the new library to your project.
7. To get the visual items in the palette, go to a jFrame or other class that has a design view, right-click anywhere on the Palette view, and select "Palette Manager...".
8. Click on "New Category..." and type "SwingX" (or whatever you want to call it).
9. Click on "Add from JAR...".
10. Find your swingx.x.x.x.jar file again, and on the next screen, select all the components you want to be displayed.
11. Finally, select the category folder you just created in step 8.

Tuesday, May 13, 2008

Working with SQL Server in NetBeans 6.1

The JDBC-ODBC bridge wasn't recognizing any of the primary keys I had set up in SQL Server. After lots of head banging, here's the solution I came up with:

1. Download the appropriate driver. SQL Server 2000 requires the SQL Server 2000 JDBC driver (http://www.microsoft.com/downloads/details.aspx?FamilyId=07287B11-0502-461A-B138-2AA54BFDC03A&displaylang=en).
SQL Server 2005 download: (http://www.microsoft.com/downloads/details.aspx?familyid=C47053EB-3B64-4794-950D-81E1EC91C1BA&displaylang=en)
3. After installing, right-click on "Libraries" in your project, and choose "Add Library...". Next, give it a name (i.e. SQLServer2000), and select "Class Libraries".
4. On the next screen, find the JAR files (should be in C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib\), and add them under the "Classpath" tab. It will now be available under "Libraries" for future projects.
5. You can now create a connection to a specific database under the "Services" tab (next to "Projects" and "Files" in the top left of the screen). Select "Microsoft Sql Server 2000 (Microsoft Driver)" and format the "Database URL" like this:

jdbc:microsoft:sqlserver//YOURSERVER:1433;DatabaseName=YOURDATABASE

1433 is the default port, though your DBA may have changed it.

I posted a simpler version of this on the NetBeans.org FAQ page - they had the following title with no content on the answer page:

"Cannot Select Tables From MsSql 2000 Because It Says No Primary Key Next To Each Table But The Tables DO Have A Primary Key. What Do I Do?"

Looks like somebody was really frustrated, and posted a question in the hopes that somebody would answer it :-) Of course I was really frustrated seeing a question with no answer, so I added one after I figured it out myself.