There are many posts about the difference on git’s rebase and merge commands, here I’d like to link two of them that I think did a great job on the explanation.

1. Rebase vs Merge in Git
2. Avoid Merge commit in Git, this one shows very detail about the difference on these two options, and it is the exact problem I was facing.

I’d also like to quote the work pattern that is described in the above 1st link. (The result is basically put all your branch work/commits ahead of your base branch, for example master, and of course without adding those meaningless merge commits that you would have if you use the git merge.)

  1. Create new branch B from existing branch A
  2. Add/commit changes on branch B
  3. Rebase updates from branch A
  4. Merge changes from branch B onto branch A

I typically use the ‘master’ branch as the base branch, which means that I only sync this branch from the remote repository, and only merge commits into this branch when I think the bug/feature is completed. Once I’ve done the merge in master, I’ll do the commit and push, so the code in master branch’s staging repository is very short. However, I’ve done a lot of pull request from remote repository into the master branch, as I need to sync other people’s work, and then I’ll do the rebase on the topic/bugfix branch that I am working on to sync the work without adding the merge commit, and put my commit in front of others commits that are already in the repo.

One caution with regard to the rebase operation is: (Again quote from the 2nd link that I pointed)

If you’ve shared the branch with anyone else, or are pushing it to a clone of the repository, do not rebase, but use merge instead. From the man page:

When you rebase a branch, you are changing its history in a way that will cause problems for anyone who already has a copy of the branch in their repository and tries to pull updates from you. You should understand the implications of using git rebase on a repository that you share.

I knew it has some good Git Cheat Sheets on the internet. So I won’t try to create yet another Git Cheat Sheet. Instead, I’ll focus on some commands for the history and file difference, as I often use them to check what has been updated on the commits, and what files been changed in there etc. Usually I went to GitHub to check out the previous commits, as it did a very good job on showing the updated files, difference on each file etc. However, at some time, my home internet is very slow, so it could be painful to go to the Github to check on those commits. (It is nothing wrong in Github, it is just in my particular case at some time due to bad network).

Git is a DVCS, so it means that we have all histories, logs in my local machine, the remote one should be served as a backup/collaborate purpose. So it means for checking the commits/difference, it should be accomplished without connecting to internet.  Here I’ll try to enumerate some options/commands that I ran for checking those updates/commits. If you have any other good usages/commands that I left out. please feel free to make a comment.

1. Show all of previous commits (result only shows commit summary):

git  log

2. Show the latest n commits:

git  log  -n

3. Show the statistics (like updated file list) in previous commits:

git log –stat

4. Show previous commits with patches:

git log -p

5. Show a specific commit with commit hash id, you just need to have enough first couple letters to make it unique:
Add –stats (if you want to show the statistics) and -p (if you want to show the detailed diff) options.

git show 36b314c (first couple letters of id)

6. Show commits/histories to a specific file (-p, -n apply here as well, but should be in front of filepath):

git log — filepath

7. Show a diff on a specific file with a specific commit:

git show commitId (like 36b314c for example) — filepath

9. Show difference between two commits:

git diff firstCommitId secondCommitId

10. Show difference between two commits for a specific file.

git diff firstCommitId secondCommitId — filepath

Just finished the <Functional Programming for Java Developers> book that written by Dean Wampler.

As Functional Programming got more buzz and more attentions lately, for Java developers (seems always busy, btw), I think this is a good and concise book for you to read on the subject, it is only 60 pages. Looks more like a long essay instead of a book, given most of IT books are having more than 200 pages. However, the 60 pages serves this book’s purpose very well, it will motivate you to enter into the Functional Programming world, at least, I am motivated!

The author begins with “Why Functional Programming”,  and the main reasons are: 1) Have to be good at writing concurrent program 2) Most programs are just Data Management problem 3) Functional Programming is more Modular 4) Have to work faster and faster and Functional Programming is a return to simplicity.

I particularly like the last point of returning to simplicity, remember that how many times we need to use the IDE’s ‘generate getter and setter methods’ function to generate those boilerplate code,  and how many times we wrote similar code to iterate a collection just because we are using another criterion. I believe, less codes means less bugs, with Java, we sometimes always bury our business logic codes into the codes that needed by language itself. Frameworks/Libraries surely offer a great help here, but wouldn’t it been even better that is supported in the language level.

The author goes on with “What Is Functional Programming”, Some of basic principles of Functional Programming, like 1) Avoid mutable state 2) Functions as first-class values 3)Lambda and closures 4)High-order Functions 5)Side-Efffect-Free Functions 6) Recursion 7) Lazy vs Eager Evaluation 8)Declarative vs. Imperative Programming. In this chapter, I particularly impressed by Recursion and Declarative vs. Imperative Programming. In Java programming, we don’t usually see many recursion usage in our daily code, I am not sure why, vaguely remembered that it may affect the performance. With Functional Programming, it doesn’t use the loop, it uses the Recursion instead. And author list two different ways to program the factorial function. The declarative one that used the recursion seems much easier to understand for me.

The author continues with “Data Structure and Algorithms”, “Functional Concurrency” and “Better Object-Oriented Programming” chapter to expand a bit explanation/examples on the items that he described in the first chapter “Why Functional Programming”.

Last chapter is called “Where to go from here”, basically lists some good resources that for you continue to explore the wonderful world of Functional Programming.

Well, writing a review for a concise book should be concise as well, otherwise, why don’t you spend the time of reading my review in reading the book itself. :) Overall, this is a very good Functional Programming introduction book for you to read, especially for those busy Java developers.

,

As you may know, JBoss AS 7 is incredible fast and low-memory, and it comes with Hibernate 4.0.0 by default. Since AS 7.0.1, it is easy for you to support both Hibernate 3.x  and Hibernate 4.0.0 under JBoss AS, thanks to the modular classpath here. If you want to run Hibernate 3.x under AS7 along with Hibernate 4, you should check out this blog entry and this documentation.

In RiftSaw 3 development, we are taking this opportunity to upgrading its JPA layer from JPA 1.0 to JPA 2.0, as JPA 2.0 is back compatible, it shouldn’t be an big issue. However, we’ve implemented the ConnectionProvider and TransactionManagerLookup interfaces, since we’ve created the Datasource and TransactionManager on our own, we need to implement these two interfaces to ask Hibernate to use our own one.

In Hibernate 4.x, the whole package has been split into three categories, API/Impl/SPI.  So the ConnectionProvider class has been re-packaged at: org.hibernate.service.jdbc.connections.spi.ConnectionProvider, if you see the internal implementation of this, you would also notice that it also implements the Configurable and Stoppable. The Configurable interface allows you to access to the Hibernate’s properties, which is kept as Map. It is much like Spring’s BeanNameAware interface, where you get to access to the BeanName. So below is the code snippet that I used for my ConnectionProvider.

public class DataSourceConnectionProvider implements ConnectionProvider, Configurable, Stoppable  {

  private Properties _props;

  private boolean available = true;

  public DataSourceConnectionProvider() {
  }

  @SuppressWarnings( {"unchecked"})
  public void configure(Map properties) {
	 _props = new Properties();
	 _props.putAll(properties);
  }

  public Connection getConnection() throws SQLException {
	if (!available) {
		throw new HibernateException( "Provider is closed!" );
	}
    Connection c = HibernateUtil.getConnection(_props);
    DbIsolation.setIsolationLevel(c);
    return c;
  }

  public void closeConnection(Connection con) throws SQLException {
    con.close();
  }

  public boolean supportsAggressiveRelease() {
    return true;
  }

  public boolean isUnwrappableAs(Class unwrapType) {
		return ConnectionProvider.class.equals(unwrapType) ||
				DataSourceConnectionProvider.class.isAssignableFrom(unwrapType);
  }

  @SuppressWarnings( {"unchecked"})
  public  T unwrap(Class unwrapType) {
		if (ConnectionProvider.class.equals(unwrapType) ||
				DataSourceConnectionProvider.class.isAssignableFrom(unwrapType)) {
			return (T) this;
		} else {
			throw new UnknownUnwrapTypeException( unwrapType );
		}
  }

  public void stop() {
	available = false;
  }
}

For the TransactionManagerLookup interface, you would get WARN  saying this API has been deprecated ( I believe it has been deprecated before 4.x, but I didn’t get a chance to upgrade it until now), we should implement the org.hibernate.service.jta.platform.spi.JtaPlatform SPI instead, and set it to the ‘hibernate.transaction.jta.platform’ property. Instead of having a class that implement the JtaPlatform SPI directly, I’ve subclassed it from the  AbstractJtaPlatform class, so that I’ve only needed to override two interfaces. Below is the code snippet for my custom JtaPlatform impl.

/**
 *
 * uses {@link HibernateUtil} to obtain the JTA {@link TransactionManager} object.
 *
 * @author Jeff Yu
 *
 */
public class OdeJtaPlatform extends AbstractJtaPlatform {

	private Properties properties = new Properties();

	public void configure(Map configValues) {
		super.configure(configValues);
		properties.putAll(configValues);
	}

	@Override
	protected TransactionManager locateTransactionManager() {
		return HibernateUtil.getTransactionManager(properties);
	}

	@Override
	protected UserTransaction locateUserTransaction() {
		throw new UnsupportedOperationException("locateUserTransaction is not supported. Use the locateTransactionManager instead.");
	}

} 

And then when you create your entityManagerFactory, remember to pass the following properties in.

props.put(Environment.CONNECTION_PROVIDER, DataSourceConnectionProvider.class.getName());
props.put(Environment.JTA_PLATFORM, OdeJtaPlatform.class.getName());

Done, the custom ConnectionProvider and JtaPlatform implementation should be all set now.

Hope this will help you do the Hibernate upgrade at some point. Here, I’d like to thanks Strong Liu to send the helpful links to me on this upgrading. :-)

,

JBoss AS Version 5 and above used the JBoss MicroContainer project to accomplish the service management/dependency injection. If we want to write a service that uses a JBoss Server service, like the ‘HAPartitionService’ for example, the below shows several ways that can be done.

1. Using JBoss MC’s xml syntax
This is the most common and recommended way to do. Below is the JBoss RiftSaw Clustering service that uses the ‘HAPartitonService’ from JBoss AS Cluster.

   <bean name="RiftSawClusteringService" class="org.jboss.soa.bpel.clustering.JBossClusteringService">
   	 <property name="haPartition"><inject bean="HAPartition" /></property>
   	 <property name="bpelEngineName">bpel/Engine</property>
   	 <depends>BPELEngine</depends>
   </bean>

See Ales’ Advanced Dependency Injections article for more about MC’s xml syntax.

 

2. Using JBoss MC’s service programmatically
This way actually is the main topic for this blog entry. If you are a Spring framework user, you would know you can use BeanFactory or ApplicationContext to get the service bean. Then in JBoss, you would wonder, whats the equivalent way to do this?

1) org.jboss.kernel.Kernel, this is the service that has the KernelController and ControllerContext that we need for getting the service. so firstly, you can define a service in -jboss-bean.xml that injects the Kernel service, like the following:

<!--    Locate the single instance of the kernel    -->
<bean name="org.jboss.soa.bpel.runtime.util:service=KernelLocator"
     class="org.jboss.soa.bpel.runtime.integration.KernelLocator">
    <property name="kernel">
      <inject bean="jboss.kernel:service=Kernel" />
    </property>
</bean>

We’ve defined above class as following:

package org.jboss.soa.bpel.runtime.integration;
public class KernelLocator{
  private static Kernel kernel;
  public static Kernel getKernel()  {    return KernelLocator.kernel;  }
  public void setKernel(Kernel kernel)  {    KernelLocator.kernel = kernel;  }
}

2) Use the KernelController and ControllerContext to get the service that we defined in *-jboss-bean.xml.
Below is the code to actually obtain the started service out of JBoss AS.

public class KernelAwareSPIFactory{
   @SuppressWarnings("unchecked")
   public <T> T getKernelProvidedSPI(String beanName, Class<T> spiArtifact)   {
      KernelController controller = KernelLocator.getKernel().getController();
      ControllerContext ctx = controller.getInstalledContext(beanName);
      return (T)ctx.getTarget();   }
}

In the end, let’s take an example from RiftSaw code base to show how it was used.

 

In RiftSaw, we’ve defined a ServerConfig interface.

public interface ServerConfig{
  /** The default bean name */
  String BEAN_NAME = "org.jboss.soa.bpel.runtime.util:service=ServerConfig";

  String getImplementationTitle();

  String getImplementationVersion();

  File getServerTempDir();

  File getServerDataDir();

  String getWebServiceHost();

  int getWebServicePort();

  .....
}

And then we’ve had the ServerConfigImpl class.

public class ServerConfigImpl implements ServerConfig{  ....}

With this implementation, we’ve defined the ServerConfig service in the *-jboss-bean.xml as following.

<!--       ServerConfig    -->
 <bean name="org.jboss.soa.bpel.runtime.util:service=ServerConfig"
        class="org.jboss.soa.bpel.runtime.integration.ServerConfigImpl">
    <property name="mbeanServer">
     <inject bean="JMXKernel" property="mbeanServer"/>
    </property>
    <property name="webServiceHost">${jboss.bind.address}</property>
  </bean>

Now, finally let’s see how we get this ServerConfig Service in our code.

ServerConfig = new KernelAwareSPIFactory()
               .getKernelProvidedSPI("org.jboss.soa.bpel.runtime.util:service=ServerConfig", ServerConfig.class

And then, here you go, you’ve got the ServerConfig service that is in the JBoss AS.

 

Hope this can help people who are doing integration with JBoss AS a little bit.

PS: Thanks Glen for pointing out some grammar errors that I did earlier. As what we used to say “your patch has been applied, thanks a lot. ;)