Thursday, August 30, 2012

Eclipse stuck on Workspace building

If your Eclipse is freezing during Workspace building, try to do following:

  • Close/Kill Eclipse
  • Go to your Workspace folder
  • Go to .metadata\.plugins\org.eclipse.core.resources\projects folder
  • Move the content to any temporary directory
  • Start Eclipse

If Eclipse is not coming up, move back the content of the folder.

Wednesday, August 29, 2012

How to initialize a java.util.Map inline

If you're declaring a Map which will contain some constant values, you can add necessary data to this Map inline:

Map<String, String> map = new HashMap<String, String>() {{
  put("1", "value 1");
  put("2", "value 2");
  put("3", "value 3");

Monday, August 27, 2012

Check that ResultSet is not empty

I think it's most common method to find if ResultSet is empty, and then to loop through results:

if ( {
  do {
    //your code
  } while (;
else {
  // throw an Exception

Tuesday, March 27, 2012

Eclipse update: how to copy plugins

I've installed Eclipse Indigo couple of days ago, but postponed migration to this version  from Helios, because I had several plugins installed and configured there (Subclipse, Perl, etc). 
I did not know how to copy them, but finally I've found a solution.

You can make this easily by doing following:

  1. Copy plugins and features folders from the old Eclipse folder to dropins directory in new installation.
  2. Run Eclipse
  3. Go to Help > Check for Updates
  4. Select and install plugins. This will copy the configuration too.

Friday, February 10, 2012

HTTPS: Apache, Tomcat and Session ID

If you're using HTTPS protocol in your web projects on Java, and storing some login data linked to the session ID, you've probably faced a problem with forwarding this session ID through Apache Web Server to Tomcat in cases when you need to use same session in different HTTPS calls.
Using HttpServletRequest's sessionId might not help, because Apache will overwrite it. All you need is to copy a Cookie named "JSESSIONID" from the original request, and pass it to your HTTPS calls. This will solve the problem if this Cookie presents in original HttpServletRequest:

public String getJSessionId(Cookie[] cookies)
   String ret = null;
      if (cookies != null)
         for (Cookie cookie : cookies)
            if ("JSESSIONID".equals(cookie.getName()))
               ret = cookie.getValue();
   catch (Exception ex)
      //show alarm or ignore it
   return ret;

If you don't have a JSESSIONID in request's Cookies, use HttpServletRequest's sessionId:

String jSessionId =  getJSessionId(request.getCookies());
if (jSessionId == null
   jSessionId = request.getSession.getId();
   //register login data using jSessionId

If you have a JSESSIONID cookie in your request, Apache Web Server will use it as a session Id.