Thursday, September 19, 2013

How to use java in a standalone bash script

Bash is good for some scripting purposes and not so good for some others.

 If your skills are in java then you can use java within a script like below.

The catch is there is a small delay of a second or two for the javac (compilation) step but beyond that it works a treat.

 I found that this is great for mucking around renaming and moving files on a recent scripting task.

Wednesday, January 9, 2013

How to generate a zip file of schemas from JAXB annotated class

This utility class will zip up all the schemas generated from a JAXB annotated class. I've found it handy sometimes.
package org.moten.david.util.xml;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;

/**
 * Utility class for xml schemas.
 * 
 * @author dxm
 * 
 */
public class SchemaUtil {

 /**
  * Returns a zip file containing all schemas generated from the given class.
  * The class may or may not have xml annotations as per normal JAXB
  * marshaler creation. All non-fundamental classes referenced by the class
  * will be described in the schema as well.
  * 
  * @param os
  *            the output stream to write the zip file to
  * @param cls
  *            the class to generate the schemas from
  */
 public static void writeSchemasAsZip(OutputStream os, Class cls) {
  try {
   JAXBContext context = JAXBContext.newInstance(cls);
   Map map = new HashMap();
   SchemaOutputResolver sor = new MySchemaOutputResolver(map);
   context.generateSchema(sor);

   ZipOutputStream zip = new ZipOutputStream(os);
   for (Entry entry : map.entrySet()) {
    ZipEntry ze = new ZipEntry(entry.getKey());
    zip.putNextEntry(ze);
    zip.write(entry.getValue().toByteArray());
    zip.closeEntry();
   }
   zip.close();
  } catch (IOException e) {
   throw new RuntimeException(e);
  } catch (JAXBException e) {
   throw new RuntimeException(e);
  }
 }

 private static class MySchemaOutputResolver extends SchemaOutputResolver {

  private final Map map;

  MySchemaOutputResolver(Map map) {
   this.map = map;
  }

  @Override
  public synchronized Result createOutput(String namespaceURI,
    String suggestedFileName) throws IOException {
   System.out.println(suggestedFileName);
   File file = new File(suggestedFileName);
   ByteArrayOutputStream bytes = new ByteArrayOutputStream();
   map.put(suggestedFileName, bytes);
   StreamResult result = new StreamResult(bytes);
   result.setSystemId(file.toURI().toURL().toString());
   return result;
  }
 }
}

Monday, September 10, 2012

How to fix Nexus One sd card corruption using linux

Every now and then a forced shutdown of my Nexus One phone leaves me with a corrupted sd card. To fix this using Linux (I use Ubuntu 12.04): Connect the card using a card reader to your linux machine. Type
df
to see the device name of your sdcard. For me it is /dev/sdc1. Then
sudo umount /dev/sdc1
dosfsck -a /dev/sdc1
Job done, remove the card and try it in your phone. Note that
fsck -a /dev/sdc1
has never fixed my problem. I had to use dosfsck.

Friday, January 27, 2012

Sonar and Maven with an overriden derby port

The Sonar server uses a derby database by default that listens on port 1527. This conflicted with another process on our server so we edited the conf/sonar.properties file and set the properties sonar.jdbc.url and sonar.derby.drda.portNumber to use say 1528 instead. Uncomment the sonar.derby.drda.host parameter to allow remote connections. Restart sonar.

To run the maven sonar plugin on a project is simple, you don't even need a reference to it in the pom.xml.

mvn sonar:sonar

However, the sonar maven plugin needs to know where the server is and also the jdbc url for the server (I guess it interacts directly with the sonar database). So add two properties to your pom.xml (we added it to our parent pom.xml so all child projects would inherit it):
<properties>
  <sonar.host.url>http://YOUR_SERVER:9000</sonar.host.url>
  <sonar.jdbc.url>jdbc:derby://localhost:1528/sonar</sonar.jdbc.url> 
</properties>

Tuesday, July 19, 2011

Using scala on android with maven

If you want to deploy an application written in Scala to the android platform then use this sample project to do the whole thing using maven plugins (although you still need a separate download of the Android SDK):

svn checkout http://moten-util.googlecode.com/svn/android-scala-sample android-scala-sample

You need the ANDROID_HOME environment variable to use the maven-android-plugin. On my machine this is

ANDROID_HOME='/opt/android/android-sdk-linux_x86'

cd to the android-scala-sample directory and type

mvn clean package

This will compile and test the scala source and build an android binary using ProGuard to strip out unused classes (needed to remove the bulk of the scala dependency).

If you have the emulator running then deploy to the emulator with

mvn android:deploy

This sample app is just a hello world app for you to modify as you like.

Wednesday, December 8, 2010

Pipe to clipboard in bash on ubuntu

Say you wanted the output of a command like ls to be copied directly into the clipboard for pasting into your gui windows. Use xclip:
sudo apt-get install xclip
Then
ls | xclip -selection clipboard
will copy the output of ls into the clipboard.

I use an alias in my .bashrc for it:
alias c='xclip -selection clipboard'
So the command becomes:
ls | c

Quickly changing your .bashrc file

I use aliases all the time and am always adding more shortcuts for my terminal experience to the .bashrc file. So it's only natural that I made a convenient alias for doing exactly that.

Add this line to your .bashrc file:
alias vib='vi ~/.bashrc && source ~/.bashrc'

Then type source ~/.bashrc and the change will have been made.

So whenever you want to change your .bashrc file and have it come into effect for the current terminal:
vib
which will edit .bashrc and will run source on it automatically when you finish editing.