Archive for the 'IT' Category

Jun 30 2008

Acegi RoleVoter

Published by under IT,Java

Adding new role to Acegi secured application may be painful sometime.

Tip of the day: by default RoleVoter is only accepting roles with names starting with prefix ROLE_ (take a look on javadoc here). You easily can change this prefix while defining RoleVoter bean (this is usually configured in file WEB-INF/security.xml)

<bean class="org.acegisecurity.vote.RoleVoter">
    <property name="rolePrefix">
        <value>YOUR_CUSTOM_PREFIX</value>
    </property>
</bean>

That’s all for today about Acegi ;-)

No responses yet

Jun 28 2008

Eclipse Ganymede

Published by under Eclipse,IT,Java

Finally Eclipse 3.4 Ganymede has been officially released! While I’m currently working on Eclipse based product I was monitoring the progress of the platform beginning with Milestone 3. It looks like release 3.4 is moving Eclipse into the right direction. Provisioning mechanism was included into standard distribution, it is called p2. We probably have to wait some time to get more mature version of it – but it’s great that it is in a “main stream” now.

I really enjoy some enhancements that make working with Eclipse IDE more efficient:

  • Automatic updates done in background;
  • Option for determining network proxy settings from system instead of configuring them in preferences;
  • Mylyn extension for search plugin;

Small but annoying issue: Delta Pack is not included into standard distribution for RCP/Plugin development (bugzilla)! Fortunately there is now at least a message in editor for *.product configuration explaining that Delta Pack has to be installed separately (in Eclipse 3.3 you could spent several hours investigating the issue of missing Delta Pack).

No responses yet

Jun 28 2008

Eclipse (anti)patterns (1).

Published by under Eclipse,IT,Java

Activator is a class in a Eclipse plugin which controls the plug-in life cycle. It contains methods like start(BundleContext context), stop(BundleContext context) and a static method Activator getDefault() – which returns static field plugin. What is interesting the the static field plugin is set in the non-static public method start(BundleContext context) or in a public constructor.
Such code is generated by Eclipse wizard for creating new plug-in. And this is Eclipse pattern for accessing the shared instance

Actually there are more places where this pattern is used in Eclipse framework and very often it is the only way for obtaining a reference to particular object. Let me discuss those later.

No responses yet

Jun 14 2008

Capturing log-in event in Acegi

Published by under IT,Java

Adding some custom code that is supposed to be launched while user logs in it isn’t available out of the box but it can be done easily. In order to do this you have to implement interface org.springframework.context.ApplicationListener.

public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof AuthenticationSuccessEvent) {
        AuthenticationSuccessEvent authEvent = (AuthenticationSuccessEvent) event;
        String username = authEvent.getAuthentication().getName();
        // Any custom logic
}
}

Such listener has to be registered. In order to do this – just create a bean in xml configuration file where other Acegi related beans are declared (usually security.xml or applicationContext-security.xml).

<bean class="my.package.SomeExampleListener">
    <property name="someExampleManager" ref="someExampleManager"></property>
</bean>

You can inject any required dependencies here.

That’s all!

One response so far

May 22 2008

Eclipse DemoCamp is coming to town

Published by under Eclipse,IT,Java

Eclipe 3.4 Ganymede It looks like Eclipse DemoCamp will come to Krakow sometime in June. The agenda is not available now, but I hope it will be interesting. There is a lot of exciting stuff coming with Eclipse 3.4 (I’m currently working with Eclipse 3.4 M7 platform and looking forward for final release that is coming in end of June).

Agenda, specific date and place should be announced here

No responses yet

Apr 18 2008

NetBeans World Tour 2008 in Krakow

Published by under IT,Java

NetBeans World Tour 2008
Last Friday NetBeans Roadshow which is part of NetBeans WorldTour took place in Krakow at AGH University. Guys from Sun department were presenting new capabilities of NetBeans 6.0 trying to convience participants that it is better or at least as good as Eclipse;-)

Interesting stuff I have found out is that actually one of the presenters (Karol Harezlak) fixed the bug in NetBeans mobile component FileBrowser that I have described couple of weeks before and spent couple of extra hours on code because of it. Besides that nice demo of using SVG (JSR 226) in J2ME was presented – it allows to make really cool looking stuff on mobile devices easily, especially while using Mobility Pack.

My general impression of NetBeans 6.0 – it goes in right direction. There are a lot on nice and unique features (e.g. Mobility Pack, JavaFX plugin, quite good Maven integration) but until Java editor in NetBeans isn’t as efficient as Eclipse one I will not switch with development completly to NetBeans.

Keep the fingers crossed for NetBeans ;-)

No responses yet

Mar 15 2008

Eclipse – a workspace and its resources. Problem with case-insensitive file systems.

Published by under Eclipse,IT,Java

All the resources in Eclipse workspace are case-sensitive. Name of the file inside a project in Eclipse workspace is case-sensitive. On Eclipse API level two resources called somefile.TXT and somefile.txt are different resources. The problem starts while such resources are stored in the file system. Method org.eclipse.core.resources.IResource.exists() will return false for somefile.TXT if there is already somefile.txt in the workbench. This cause problem if you are for example implementing wizard for creating new file resource – basing on the Eclipse API you cannot check if such file already exists. My workaround for this was using java.io classes directly for this. Another problem is if in you wizard you want to overwrite existing file with new one e.g. somefile.TXT with somefile.txt. If you try to use org.eclipse.ui.ide.IDE.openEditor(IWorkbenchPage page, IFile input, String editorId, boolean activate) for this overwritten file – it will fail opening it. In Eclipse 3.3 there was introduced method openEditor(IWorkbenchPage page, URI uri, String editorId, boolean activate) – which doesn’t have problem with case-sensitivity. Unfortunately it opens the file in read-only mode while the new resource is out of the workbench! I haven’t found any nice programmatic way to “refresh” workbench in order to reflect the changes that were made on file system behind the scene (as user can do manually by pressing F5). May workaround for this is opening the editor twice – first time using openEditor(IWorkbenchPage page, IFile input, String editorId, boolean activate) and closing it immediately in order to “refresh” workbench and after that using openEditor(IWorkbenchPage page, URI uri, String editorId, boolean activate) – now the new file is already in the workbench and the it’s not opened in read-only mode.

This is one of the problems I encountered while developing Eclipse plugins on Windows platform. I’m missing possibility for configuring all the resources related code for dealing with OS-dependent stuff like mentioned above problem with case-sensitivity.

Eclipse 3.3 API reference can be found here.

No responses yet

Feb 28 2008

Jakarta BCEL

Published by under IT,Java

BCEL (Byte Code Engineering Library) gives a possibility to analyze, create and modify Java class files. It gives very nice API for manipulating inside bytecode. Here is an example of using BCEL. It adds instructions relevant to System.out.println(“About to call: METHOD_NAME METHOD_SIGNATURE”) before invocation of any method in the class.

 
import java.io.IOException;
import java.util.Iterator;
 
import org.apache.bcel.Repository;
import org.apache.bcel.classfile.ClassFormatException;
import org.apache.bcel.classfile.Method;
import org.apache.bcel.generic.*;
 
/**
 * Instruments byte-code by adding: <code>
 * System.out.println("About to call: METHOD_NAME METHOD_SIGNATURE");
 * </code>
 * before method invocation.
 *
 * @author Radoslaw Urbas
 * @since Oct 27, 2007
 *
 */
public class Transform
{
 
    private ClassGen classGen = null;
 
    /**
     *
     * @param className name of class to instrument
     * @throws ClassNotFoundException
     * @throws ClassFormatException
     * @throws IOException
     */
    public Transform(String className) throws ClassNotFoundException,
            ClassFormatException, IOException
    {
        this.classGen = new ClassGen(Repository.lookupClass(className));
    }
 
    /**
     * Instruments the class by displaying method information before invocation.
     *
     * @return instrumented ClassGen object
     */
    private ClassGen instrument()
    {
        ConstantPoolGen pgen = classGen.getConstantPool();
        String cname = classGen.getClassName();
        for (Method method : classGen.getMethods())
        {
            MethodGen methgen = instrumentMethod(pgen, cname, method);
            classGen.replaceMethod(method, methgen.getMethod());
        }
        return classGen;
    }
 
    /**
     * Instruments method by displaying information before method invocation.
     *
     * @param constantPoolGen constant pool for class containing method to
     *            instrument
     * @param className name of the class containing method to instrument
     * @param method method to instrument
     * @return instrumented MethodGen object
     */
    private MethodGen instrumentMethod(ConstantPoolGen constantPoolGen,
            String className, Method method)
    {
        InstructionFactory instructionFactory = new InstructionFactory(classGen);
        MethodGen methgen = new MethodGen(method, className, constantPoolGen);
        InstructionList originalInstructionList = methgen.getInstructionList();
        Iterator instructionIterator = originalInstructionList.iterator();
        while (instructionIterator.hasNext())
        {
            InstructionHandle ih = (InstructionHandle) instructionIterator
                    .next();
            if (ih.getInstruction() instanceof InvokeInstruction)
            {
                originalInstructionList.insert(ih, instructionFactory
                        .createPrintln(getMessage((InvokeInstruction) ih
                                .getInstruction(), constantPoolGen)));
            }
        }
        methgen.setMaxStack();
        return methgen;
    }
 
    /**
     *
     * @param invokeInstruction object representing invoke instruction
     * @param constantPoolGen
     * @return message based on method name and signature
     */
    private String getMessage(InvokeInstruction invokeInstruction,
            ConstantPoolGen constantPoolGen)
    {
        String text = "About to call: "
                + invokeInstruction.getMethodName(constantPoolGen)
                + invokeInstruction.getSignature(constantPoolGen);
        return text;
    }
 
    /**
     *
     * @param args requires one parameter: class name. Class should be available
     *            in directory ./classes
     * @throws ClassNotFoundException
     * @throws IOException
     */
    public static void main(String[] args) throws ClassNotFoundException,
            IOException
    {
        String className = args[0];
        Transform transform = new Transform(className);
        ClassGen classGen = transform.instrument();
        classGen.getJavaClass().dump(className + ".class");
    }
}

Homepage: http://jakarta.apache.org/bcel/

One response so far

Feb 21 2008

PicLens Add-on

Published by under IT

PicLens is a Firefox Add-on for displaying images in more “interactive” way. It has sort 3D Wall of images and allows to view them with smooth mouse movements.

It works with multiple websites, e.g. Picasa, Flickr, Facebook, MySpace and image search on Google, Yahoo, Live, AOL.

Check it out https://addons.mozilla.org/en-US/firefox/addon/5579

No responses yet

Feb 12 2008

AppFuse – first impression

Published by under Java

Couple days ago I was looking for some starter kit for Java web applications based on Spring framework. After short investigation I have decide to try some of Maven2 archetypes. If you just type: mvn archetype:create you get list of standard archetypes. Take a look on positions 1-9 (AppFuse starter-kits).

For further investigation I chose appfuse-modular-struts which is a solution based on Hibernate, Spring and Struts2.

To kickoff application kickoffs there are just few simple steps:

  • Launch database. I was trying it with Derby and PostgreSQL.
  • Configure database settings in pom.xml in main directory by adding line true in ‘profiles’ section after ‘id’ of DB of your choice and setting proper user/pass and database name if applicable.
  • Kickoff Maven build and run application:
    mvn
    cd web
    mvn jetty:run-war
  • Navigate to http://localhost:8080 in you browser.

You will see simple application that allows to:

  • Log in (admin/admin).
  • List the users.
  • Edit user data.

Everything looks great. That is the moment when you are starting to look for sources of this application. To get the source you need to invoke another Maven target
mvn appfuse:full-source.

Now you can rebuild the application and do the changes you want.
There is also nice feature for adding standard code. You can generate all required class and configurations for domain objects. To do this add new domain object in your.application.model package in core module and annotate it properly. After that:
cd core
mvn appfuse:gen -Dentity=YourNewObject
cd ..
cd web
mvn appfuse:gen -Dentity=YourNewObject

All the classes, configurations and webpages should be ready for performing CRUD operations for this POJO!

Everything looks great, but there is one concern. Don’t use Java 1.6. Use Java 1.5. It took me several hours to figure out that this is the problem while tests fails (just HTTP 500 respond, no exceptions, no logs). Haven’t investigated yet what’s it the core of the problem with using it on Java 6.

AppFuse homepage: http://appfuse.org/display/APF/Home

No responses yet

« Prev - Next »