WordPress: Custom Post Order

September 25th, 2009 No comments

I recently had a requirement for custom ordering on WordPress categories and thought that I would share how I did it.

All that is needed is this piece of code which essentially modifies the SQL statement being generated. Note that the argument assigned to orderby is the column name, hence it is case sensitive (I found out that ID breaks the naming conversion on the post table);

<?php 
   if (is_category(array($myCatId))){
      query_posts($query_string . "&orderby=ID&order=DESC");
   } 
?>

Code needs to be placed before this line

<?php if (have_posts()) : while (have_posts()) : the_post(); update_post_caches($posts); ?>
Categories: Code, General, PHP, Wordpress Tags: ,

Auto Object Population via Reflection

September 24th, 2009 1 comment

Using reflection in the right place can some what simplify certain tasks, in this case the population of Custom classes (data objects). I have supplied below an example of a Class being auto-populated using ResultSet data from a JDBC request (of course it could really be any type of 2D data object).

If a public class variable is declared in the implementation of IModelObject (in this case the implementation is the RemoteConfig class) if the variable name is matched against a column name in the JDBC ResultSet then the value is assigned to the class variable. The great thing is if the variable is not defined in the class then it will quite simply never be populated…obviously as it doesn’t exist :)

In the example below only; String, int and boolean types are supported.

Basic interface class to keep thing reasonably generic

package Model.Objects;

import java.sql.ResultSet;

public interface IModelObject{
	
	public void populate(ResultSet rs);
	
	public String objToString();
	
	public void setDebug(boolean debug);
	
	public boolean getDebug();

}

Implementation of IModelObject which is the Class (data access object) to be auto-populated, (variables; var1, var2, var3, var4 will attempt to be auto-populated)

package Model.Objects;

import java.lang.reflect.*;
import java.sql.ResultSet;
import java.sql.SQLException;

@SuppressWarnings("unused")
public class RemoteConfig implements IModelObject{
	
	// Public variables ONLY are auto-populated using reflection

	public String var1;
	
	public int var2;
	
	public String var3;
	
	public boolean var4;
	
	private ResultSet resultSet;
	
	private static final String REFLECTION_TYPE_INT = "int";
	
	private static final String REFLECTION_TYPE_INTEGER = "Integer";
	
	private static final String REFLECTION_TYPE_STRING = "String";
	
	private static final String REFLECTION_TYPE_BOOLEAN = "boolean";
	
	private boolean debug = false;
	
	public RemoteConfig(){
		// Default constructor
	}
	
	public RemoteConfig(ResultSet rs){
		this.resultSet = rs;
		populate(rs);
	}
	
	public RemoteConfig(ResultSet rs,boolean debug){
		this.resultSet = rs;
		this.setDebug(debug);
		populate(rs);
	}
	
	public void populate(ResultSet rs){
		// NOTE: getFields only returns public variables (which is what we want)
		// if you need all declared variables the use getDeclaredFields
		for(int i=0;i< this.getClass().getFields().length;i++){
			try {
				String varName = this.getClass().getDeclaredFields()[i].getName();
				Field field = this.getClass().getDeclaredField(varName);
				
				try{
					populateClassVarByName(field);
				} 
				catch (IllegalArgumentException e) {
					e.printStackTrace();
				} 
			} 
			catch (SecurityException e) {
				e.printStackTrace();
			}
			catch (NoSuchFieldException e) {
				e.printStackTrace();
			}
		}
		if (debug){
			System.out.print(objToString());
		}
	}
	
	private void populateClassVarByName(Field field){
		try{
			if (field.getType().getSimpleName().equals(REFLECTION_TYPE_INT) || 
				field.getType().getSimpleName().equals(REFLECTION_TYPE_INTEGER)){
			
				try {
					field.set(this,resultSet.getInt(field.getName()));
				} 
				catch (IllegalArgumentException e){
					e.printStackTrace();
				} 
				catch (IllegalAccessException e){
					e.printStackTrace();
				}
			}
			else if (field.getType().getSimpleName().equals(REFLECTION_TYPE_STRING)){
				try{
					field.set(this,resultSet.getString(field.getName()));
				}
				catch (IllegalArgumentException e){
					e.printStackTrace();
				}
				catch (IllegalAccessException e){
					e.printStackTrace();
				}
			}
			else if (field.getType().getSimpleName().equals(REFLECTION_TYPE_BOOLEAN)){
				try{
					if (resultSet.getString(field.getName()).equals("1")){
						field.set(this,true);
					}
					else if (resultSet.getString(field.getName()).equals("0")){
						field.set(this,false);
					}
					else{
						field.set(this,resultSet.getBoolean(field.getName()));
					}
				}
				catch (IllegalArgumentException e){
					e.printStackTrace();
				}
				catch (IllegalAccessException e){
					e.printStackTrace();
				}
			}
			
		} 
		catch (SQLException e){
			System.out.print("\n" + e.toString() + "\n");
		}
	}
	
	public String objToString(){
		String toStringResult = "\n---------" + this.getClass().getName() + "---------\n";
		
		for(int i=0;i< this.getClass().getDeclaredFields().length;i++){
			String varName = this.getClass().getDeclaredFields()[i].getName();
			try{
				Field field = this.getClass().getDeclaredField(varName);
				try {
					toStringResult += varName + ":" + field.get(this) + " (" + field.getType().getSimpleName() + ")\n";
				}
				catch (IllegalArgumentException e) {
					e.printStackTrace();
				} 
				catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			} 
			catch (SecurityException e) {
				e.printStackTrace();
			} 
			catch (NoSuchFieldException e) {
				e.printStackTrace();
			}
		}
		toStringResult += "---------\n";
		
		return toStringResult;
	}
	
	public void setDebug(boolean debug){
		this.debug = debug;
	}
	
	public boolean getDebug(){
		return debug;
	}
}

Basic usage


/**
 * Get you your ResultSet via JDBC and pass it in
 */

RemoteConfig remoteConfig = new RemoteConfig(myResultSet,debug);

New: Google Fast Flip

September 15th, 2009 No comments

Looks like GoogleLabs are working on something new FastFlip, which appears to be a “flippy” news reader. It essentially screen shots important news from various sites, catalogs them and displays them with a “flippy” browse view. In conjunction with the browse view you can drill down and view a screen shot of the websites main page and if need be click and follow through to the actual site.

Google FastFlipGoogle FastFlip

fastflip.googlelabs.com

Categories: General, Google Tags: ,

Java Reflection Examples

September 14th, 2009 No comments

Just to clear the air we are not talking about some funky new graphics render we are talking about programmatic reflection access of variables and methods :)

1. All of the variables public or private in a class can be accessed via reflection, it is assumed for this example that the code is in a class with class variables, hence the usage of “this” (but it doesn’t have to be it could be an instance object). Although we are only echoing the variable name and value to the screen we could be assigning them using the setter functions in the Field class. Really practical usage of this functionality is automatic population of Model classes from the Data Access layer, for example when using SOAP.

for(int i=0;i< this.getClass().getDeclaredFields().length;i++){
	try {
		String varName = this.getClass().getDeclaredFields()[i].getName();
		Field field = this.getClass().getDeclaredField(varName);
		field.setAccessible(true);
				
		try {
			System.out.print("Variablename: " + varName + "\n");
			System.out.print("Value: " + field.get(this).toString() + "\n");
		} 
		catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	} 
	catch (SecurityException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	catch (NoSuchFieldException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

2. This is an example of invoking a method via reflection, again this code assumes you are in a class. Also that class should contain a method called “test” with one String and an argument.

Class<?> params[] = {String.class};
		
try {
	Method method = this.getClass().getDeclaredMethod("test",params);
	try {
		method.invoke(this,"argument!!!!!");
	} 
	catch (IllegalArgumentException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} 
	catch (IllegalAccessException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} 
	catch (InvocationTargetException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
} 
catch (SecurityException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} 
catch (NoSuchMethodException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}

Test function to be called via reflection.

public void test(String str){
	System.out.print("Method test invoked via reflection with arg: " + str);
}
Categories: Code, General, Java Tags: ,

Java: Multiple Thread Controller

September 10th, 2009 No comments

ThreadThis example is for those of you writing server side Java daemons who want to take full control over how much CPU is used on multi-core machines. Essentially you have a controller objected which when constructed creates up to maxThreads worth of “thread workers” (this is your thread pool) which can be assigned to generic code of your choice, in the form of an IRunnableThread, which is basically a Java Runnable object.

ThreadTester is a basic implementation of an IRunnableThread which contains a simple unit test to show the thread controller working. Of course you can swap out ThreadTester for you own implementation.

The Daemon main statement shows how to start and use the thread controller at the upper most level. The argument to thread controller is the number of threads you require for simultaneous processing, in this example its 3

package ThreadControl;

import java.util.LinkedList;
import ThreadControl.ThreadWorker;

public class ThreadController implements Runnable{

private int maxThreads;

private ThreadWorker[] threads;

public static LinkedList<IRunnableThread> queue;

    /**
     *
     */

    public ThreadController(int maxThreads){
        this.maxThreads = maxThreads;
        queue = new LinkedList<IRunnableThread>();
        threads = new ThreadWorker[maxThreads];
    }

    public static int queueSize(){
    	return queue.size();
    }

    public int maxQueueSize(){
    	return maxThreads;
    }

    public synchronized void push(IRunnableThread threadCode){
    	 queue.addLast(threadCode);
         queue.notify();
    }

    public void push(IRunnableThread threadCode){
    	synchronized(queue) {
          queue.addLast(threadCode);
          queue.notify();
        }
    }

    public void run() {
        for (int i=0; i<maxThreads; i++) {
            threads[i] = new ThreadWorker();
            threads[i].start();
        }
    }

}

The implementation for one of the thread workers that is part of your thread pool of size: maxThreads

package ThreadControl;

import ThreadControl.ThreadController;

public class ThreadWorker extends Thread{

	public void run(){
		IRunnableThread r;
		
		while (true){
			synchronized(ThreadController.queue){
				while(ThreadController.queue.isEmpty()){
					try{
						ThreadController.queue.wait();
					}
					catch (InterruptedException ignored){
						
					}
				}
				r = ThreadController.queue.removeFirst();
			}
			// If we don't catch RuntimeException, 
			// the pool could leak threads
			try{
				r.setThreadId(Long.toString(this.getId()));
                r.run();
            }
			catch (RuntimeException e) {
                 // You might want to log something here
			}
		}
	}
	
}

Runnable thread interface

package ThreadControl;

public interface IRunnableThread extends Runnable{
	
	public String getThreadId();

	public void setThreadId(String threadId);
	
}

Test implementation for IRunnableThread,

package ThreadControl;

public class ThreadTester implements IRunnableThread{
	
	private final static int noAlertMessages = 5; 
	
	// In milliseconds
	private int messageDelay = 1000;
	
	private String testId = "";

	private String threadId = "";
	
	public ThreadTester(String testId){
		this.testId = testId;
	}
	
	public ThreadTester(String testId,int messageDelay){
		this.testId = testId;
		this.messageDelay = messageDelay;
	}
	
	public void run(){
		for(int i=0;i<noAlertMessages;i++){
			System.out.print("Thread Id: " + threadId + " - [" + testId + "]: test message #" + (i + 1) + "\n");
			System.out.print("Thread Queue Size: " + ThreadController.queueSize() + "\n");
			try{
				Thread.sleep(messageDelay);
			}
			catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.print("*** " + testId + " complete! ***\n");
	}

	public String getThreadId() {
		return threadId;
	}

	public void setThreadId(String threadId) {
		this.threadId = threadId;
	}

}

Main statement where the test case is demonstrated

import ThreadControl.ThreadController;
import ThreadControl.ThreadTester;

public class Daemon {

	public static void main(String[] args) {
		ThreadController threadController = new ThreadController(3);
		
		try{
			new Thread(threadController).start();
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
		
		threadController.push(new ThreadTester("Test 1",5000));
		threadController.push(new ThreadTester("Test 2",333));
		threadController.push(new ThreadTester("Test 3",777));
		threadController.push(new ThreadTester("Test 4",450));
		threadController.push(new ThreadTester("Test 5",1024));
	}

}
Categories: Code, General, Java Tags: , ,