Pages

Saturday, April 16, 2011

Code Snippet. Intercepting "On Save" Action in IntelliJIDEA

1. Add the following to META-INF/plugin.xml of your plug-in:
<application-components>
   <component>
      <implementation-class>my.plugin.OnFileSaveComponent</implementation-class>
    </component>
 </application-components>

2. Implement com.intellij.openapi.components.ApplicationComponent:
package my.plugin;

import com.intellij.AppTopics;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.ApplicationComponent;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManagerAdapter;
import com.intellij.util.messages.MessageBus;
import com.intellij.util.messages.MessageBusConnection;
import org.jetbrains.annotations.NotNull;

public class OnFileSaveComponent implements ApplicationComponent {
  @NotNull
  public String getComponentName() {
    return "My On-Save Component";
  }

  public void initComponent() {
    MessageBus bus = ApplicationManager.getApplication().getMessageBus();

    MessageBusConnection connection = bus.connect();

    connection.subscribe(AppTopics.FILE_DOCUMENT_SYNC, 
      new FileDocumentManagerAdapter() {
      @Override
      public void beforeDocumentSaving(Document document) {
         // create your custom logic here
      }
    });
  }

  public void disposeComponent() {
  }
}

That's it! :)

So how could it be used? If you'd like to perform some synchronization with a remote server on each file save in IntelliJ, probably, this code snippet would help you.

Wednesday, April 6, 2011

33rd Degree, 2011, Day 1


ZeroTurnaround is exhibiting at 33rd degree conference in Krakow. In fact, we are the only software product company at the tiny expo here. The others, Luxoft and Tieto are the outsourcing companies, so we feel a bit lonely in this situation. BTW, Luxof has the most meaningless booth I've ever seen - it is huge and it completely ineffective.


Anyways, we've met a lot of smart people asking a lot of smart questions about JRebel and it is tough to answer all kind of questions - I wish I could.

I didn't have much time to attend the sessions myself. However I've managed to escape from the expo area and to attend Ted Neward's talk Busy Developer's Guide to Scala: Patterns. I've got smarter in a way. I've learned about some Scala idioms that diminish some of the classical design patterns that you need to implement in Java from scratch. In fact, I realized that many of the design patterns become obsolete once the target language supports closures.

At the end of the day we've organized the beer party for all the conference attendees. It is a funny thing but people are more interested in JRebel if they're are served with the free beer :)

Friday, April 1, 2011

Just Some Java Reflection Madness

import java.lang.reflect.Field;

public class Test {

  public static void main(String[] args) throws Exception {
    doStuff();
    System.out.println("Hello".equals("World"));  // --> true
  }

  public static void doStuff() throws Exception {
    Field value = String.class.getDeclaredField("value");
    value.setAccessible(true);
    value.set("Hello", "World".toCharArray());
  }

}

http://www.javaspecialists.eu/talks/oslo09/ReflectionMadness.pdf

Tuesday, March 22, 2011

Presentation Slides: Java Bytecode Fundamentals I - Tech Talk for JUG.lv, March 2011


JRebel 4.0 M1 Released

JRebel 4.0-M1 was released. Grab it while it's hot!

The new features include integration with HotSwap, support for anonymous classes and adding new session beans to JBoss 4.2/5.1 and Glassfish 2/3.

For the new features, I would encourage everyone to give it a try! JRebel already provides the integration for JSF (Mojarra), JAX-RS (Jersey), CDI (Weld), and now the support for EJBs is extended! It is possible to create a JavaEE6 app with almost no redeploys already. Things are improving quite fast!

Sunday, March 6, 2011

Embedded Tomcat, The Minimal Version

Tomcat 7 has been improved a lot and along with all the features that it brings, a very nice feature is provided - the API for embedding tomcat into the application. The API was provided in earlier versions of Tomcat but it was quite cumbersome to use.

To to start the embedded version of Tomcat one may need to build the required JARs.


$> svn co https://svn.apache.org/repos/asf/tomcat/trunk tomcat
$> cd tomcat
$> ant embed-jars
$> ls -l output/embed
total 5092
-rw-r--r-- 1 anton None 56802 2011-03-06 17:09 LICENSE
-rw-r--r-- 1 anton None 1194 2011-03-06 17:09 NOTICE
-rw-r--r-- 1 anton None 1690519 2011-03-06 17:09 ecj-3.6.jar
-rw-r--r-- 1 anton None 234625 2011-03-06 17:09 tomcat-dbcp.jar
-rw-r--r-- 1 anton None 2402517 2011-03-06 17:09 tomcat-embed-core.jar
-rw-r--r-- 1 anton None 781989 2011-03-06 17:09 tomcat-embed-jasper.jar
-rw-r--r-- 1 anton None 34106 2011-03-06 17:09 tomcat-embed-logging-juli.jar


The following snippet demonstrates the embedded Tomcat usage with a deployed servlet instance.

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.Writer;

public class Main {

  public static void main(String[] args)
  throws LifecycleException, InterruptedException, ServletException {
    Tomcat tomcat = new Tomcat();
    tomcat.setPort(8080);

    Context ctx = tomcat.addContext("/", new File(".").getAbsolutePath());

    Tomcat.addServlet(ctx, "hello", new HttpServlet() {
      protected void service(HttpServletRequest req, HttpServletResponse resp) 
      throws ServletException, IOException {
        Writer w = resp.getWriter();
        w.write("Hello, World!");
        w.flush();
      }
    });
    ctx.addServletMapping("/*", "hello");

    tomcat.start();
    tomcat.getServer().await();
  }

}

The only two JARs required are tomcat-embed-core.jar and tomcat-embed-logging-juli.jar. It means that there will be no JSP support and pooling will also be disabled. But that's enough to start a servlet and in most cases that is what you probably need.

Disqus for Code Impossible