Archive for July, 2009

m2eclipse and separate output folders

Wednesday, July 29th, 2009

The recent versions of m2eclipse (> 0.9.3) are a nightmare to get working. The biggest issues is that if you run a command line build whilst eclipse is working, one or the other will get totally borked. there is a section in the FAQ describing how to use separate output folders. Essentially you have to tell eclipse to use a certain maven profile, which changes your project configuration.

The instructions almost work. However, some plugins need the project.build.directory variable (for example for code-generation) to be set, and the FAQ does not achieve that.

Ordinarily you would have to specify the directory element in the build section:

<directory>${basedir}/${target.dir}</directory>

If you do this however, anything you launch in eclipse will not have the correct classpath. You must fully specify the output directories:

<directory>${basedir}/${target.dir}</directory>
<outputDirectory>${basedir}/${target.dir}/classes</outputDirectory>
<testOutputDirectory>${basedir}/${target.dir}/test-classes</testOutputDirectory>

I assume this is because m2eclipse is doing some kind of parsing of the pom.xml

Maven Default Execution Ids

Monday, July 27th, 2009

Maven execution IDs have changed in maven versions greater than 2.2.0.

It seems that you can disable a default a default execution by binding it to the phase “none”:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-dependency-plugin</artifactId>
   <executions>
      <execution>
         <id>default</id>
         <goals>
            <goal>unpack</goal>
         </goals>
         <phase>none</phase>
      </execution>
      <execution>
         <id>unpack</id>
         <phase>process-resources</phase>
         <goals>
            <goal>unpack</goal>
         </goals>
         <configuration>
            <artifactItems>
               <artifactItem>
                  [...]
               </artifactItem>
            </artifactItems>
         </configuration>
      </execution>
   </executions>
</plugin>

Linked Folders in Eclipse

Tuesday, July 21st, 2009

In your .project file:

<projectDescription>
   [...]
   <linkedResources>
      <link>
         <name>target</name>
         <type>2</type>
         <locationURI>some/location</locationURI>
      </link>
   </linkedResources>
</projectDescription>

Multiline Regular Expression Search in Eclipse

Tuesday, July 21st, 2009

Turn on the DOTALL flag by putting (?s) at the start of the expression.

Following Redirects in Jersey REST Client

Friday, July 17th, 2009

There is a method in com.sun.jersey.api.client.Client that causes the Client to follow any redirections:

 /**
  * Set if redirection should be performed or not.
  *
  * This method is the functional equivalent to setting the property
  * {@link ClientConfig#PROPERTY_FOLLOW_REDIRECTS} on the property bag
  * returned from {@link #getProperties}
  *
  * @param redirect if true then the client will automatically redirect
  *        to the URI declared in 3xx responses.
  */
 void setFollowRedirects(Boolean redirect)

Eclipse Galileo and Glassfish

Thursday, July 16th, 2009

There is an update site available for getting the glassfish server adapter directly into eclipse 3.5 (Galileo) http://ajax.dev.java.net/eclipse

Useful Documentation on Struts2 Interceptors

Monday, July 13th, 2009

Xml file describing the various default interceptor stacks.

Document containing links describing each of the interceptors.

Backup MySql

Saturday, July 11th, 2009

Give permissions for every database to the backup user (you might not need lock tables).

GRANT SELECT, LOCK TABLES ON *.* 
     TO backupuser@host IDENTIFIED BY 'password';
dbUser=
dbHost=
dbPass=
 
for db in $(mysql --user=$dbUser --host=$dbHost \
                  --password=$dbPass -Bse 'show databases')
do
   backupFile=sql/$db.$(date +%F)-$(date +"%T").sql.gz
   mysqldump --add-drop-database --user=$dbUser \
             --host=$dbHost --password=$dbPass  \
             $db | gzip -9 > $backupFile
done

WordPress and MySQL

Friday, July 10th, 2009
<?php
define('DB_NAME', 'username');
define('DB_USER', 'username');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'sqlhost');
 
$table_prefix  = 'wp_';
 
$server = DB_HOST;
$loginsql = DB_USER;
$passsql = DB_PASSWORD;
$base = DB_NAME;
?>
CREATE DATABASE username;
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER
   ON username.* TO username@webhost IDENTIFIED BY 'password';
FLUSH PRIVILEGES;

Bug in HTTPBasicAuthFilter

Friday, July 10th, 2009

com.sun.jersey.api.client.filter.HTTPBasicAuthFilter incorrectly pads the Base64 encoded strings with null characters, instead of ‘=’. jersey-client-1.0.3.jar

It might be better to use a pre-exisiting Base64 library. Like the one in CommonsCodec.

public class HttpBasicFilter extends ClientFilter
{
   private static final String c_base64code = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
                                              "abcdefghijklmnopqrstuvwxyz0123456789+/";
 
   private final String m_authentication;
 
   /**
    * Adds an authentication header using a username and password
    *
    * @param username the user name to send
    * @param password the passowrd to send
    */
   public HttpBasicFilter(final String username, final String password)
   {
      m_authentication = "Basic " + encode(username + ":" + password);
   }
 
   private String encode(final String string)
   {
      byte[] bytes = getBytes(string);
      final int padding = (3 - (bytes.length % 3)) % 3;
 
      bytes = zeroPad(bytes.length + padding, bytes);
 
      final StringBuilder encoded = new StringBuilder();
      for (int i = 0; i &lt; bytes.length; i += 3)
      {
         final int threeBytes = (bytes[i] &lt;&lt; 16) + (bytes[i + 1] &lt;&lt; 8) + bytes[i + 2];
         encoded.append(c_base64code.charAt((threeBytes &gt;&gt; 18) &amp; 0x3f)).
                 append(c_base64code.charAt((threeBytes &gt;&gt; 12) &amp; 0x3f)).
                 append(c_base64code.charAt((threeBytes &gt;&gt; 6) &amp; 0x3f)).
                 append(c_base64code.charAt(threeBytes &amp; 0x3f));
      }
      return encoded.substring(0, encoded.length() - padding) + "==".substring(0, padding);
   }
 
   private byte[] getBytes(final String string)
   {
      byte[] bytes;
      try
      {
         bytes = string.getBytes("UTF-8");
      }
      catch (final UnsupportedEncodingException e)
      {
         g_logger.log(Level.WARNING, "unable to decode string as UTF-8", e);
         bytes = string.getBytes();
      }
      return bytes;
   }
 
   private byte[] zeroPad(final int length, final byte[] bytes)
   {
      final byte[] padded = new byte[length];
      System.arraycopy(bytes, 0, padded, 0, bytes.length);
      return padded;
   }
 
   /**
    * {@inheritDoc}
    */
   @Override
   public ClientResponse handle(final ClientRequest request) throws ClientHandlerException
   {
      if (!request.getMetadata().containsKey(HttpHeaders.AUTHORIZATION))
      {
         request.getMetadata().add(HttpHeaders.AUTHORIZATION, m_authentication);
      }
      return getNext().handle(request);
   }
}