Monday, March 22, 2010
AspectJ Programming in Scala and Groovy
AspectJ has been very useful for me while programming in Java (programming language), and I still think it'd be useful in Groovy and Scala programming. However, some of the things that I did with AspectJ is "obsolete" with introduction of Groovy and Scala. Take a look at Java bean getter/setters: it's fun to use AspectJ to solve that problem, but the problem doesn't exist at all in Groovy and Scala. You just use native support or add i.e. @BeanProperty to Scala beans.
AspectJ can be used in Java to implement mixins... but Scala and Groovy has its own integrated way for doing mixins/traits. I saw AspectJ as an alternative programming language! And I guess I was abusing AspectJ for things not at its core goals.
I was using AspectJ for two things: micro-structural aspects (traits, etc.) and for cross-cutting concerns aka Aspect-Oriented Programming (AOP).
Now, the micro-structural aspects are what bugs me here. Traits/mixins, getter/setters, PropertyChangeListener support can be done in AspectJ, but I think it's an abuse of AOP to implement something that Java didn't have. It's not AOP, it's fixing a programming language!
You can do the above pretty easily in Scala, natively. For example, Eclipse ScalaModules enhances OSGi BundleContext with RichBundleContext.
Cross cutting concerns aka AOP is a more valid reason to use AspectJ. Things like security, logging, Data-Context-Interaction pattern, and other macro-infrastructural aspects.
I'm not sure yet how AspectJ fares with Groovy and Scala classes. I know AspectJ deals with bytecode, not Java source, so I hope AspectJ weaves Groovy/Scala classes just fine.
AspectJ aspects are also written in Java programming language, which in some way feels backwards. (come on, enhance Scala classes using Java code? Shouldn't it be the other way around?) With @AspectJ annotation style aspects, it's possible to write Groovy/Scala aspects too, though I'm not sure about its practicalness or convenience.
If you're not familiar yet with Scala I strongly recommend Programming Scala: Scalability = Functional Programming + Objects.
Do you think AOP or Scala matters for you?
Is Java (Programming Language) Dead?
Scala is superior to Java in all if not more ways. Scala's static typing system is way more advanced than Java. Taking type safety to the extreme, even with Java's strength in strong typing, Scala beats even Java itself in this department.
To learn more about Scala, get the book Programming Scala: Scalability = Functional Programming + Objects.
Groovy is a very good dynamically typed programming language. Groovy has strengths and weaknesses compared to Scala. I tend to think Scala and Groovy as complementary, not competition. Scala is more suited to replace Java programming language itself (the lower-level stuff) and Groovy is good for higher-level ones or for scripting purposes. Although that's a very rough distribution, the goal is to use the best tool for the job.
I recommend reading Groovy in Action book for more learning resources on Groovy.
The only things blocking mainstream Groovy and Scala usage are: overhead, Java integration, tooling, and integration with libraries.
I'm not worried about overhead since it solved itself as time pass. Nowadays, nobody complains Windows Vista or Windows7's Aero UI is "too slow". Or that there are not enough WAP websites for their iPhone. Or that Eclipse RCP is too bloated and should reduce footprint under 10 MB. So I still think overhead is a problem but can be solved by applying the solution at the right problems.
Java integration is being worked on. Groovy can already create Java stubs so you can develop Java sources (or Scala source) with Groovy sources smoothly, with full code completion, javadocs, etc. even before the Groovy sources are compiled to classes. I don't think Scala has this stub creation functionality yet, but you can use Scala .class files just as you would in Java. So you can compile a mixed project of Java, Scala, and Groovy sources as one.
Tooling is the most important here, and it being the most visible weakness of both Scala and Groovy. However, things are doing very well today compared to a few years ago. Groovy support for Eclipse IDE has official support from SpringSource/VMware. Scala Eclipse IDE Plugin is pretty good too, and development of Scala language itself is rapid (as of this writing Scala is at Scala 2.8 beta version).
Integration with libraries is very important, aka with Spring, OSGi, JPA, Java EE, and the like. Fortunately this is also not something to be worried about, as very few libraries/frameworks explicitly require you to use the Java programming language. All they need are .class-es, and Scala produces them just fine. You can use any and all sorts of Java libraries from Scala, so there's no barrier of entry at all. Aside from JSP, some so-called "Java libraries" are not even Java language at all!
Don't believe me? Take a look at JSF 2.0. It mainly consists of annotations (i.e. @ManagedBean), Unified EL (which is yet another programming language, NOT Java!) and XML (that is, Facelets and namespaced JSF component libraries).
What do you think?
Saturday, March 20, 2010
Parse Java Source Code with JaxMe JS
Here is a code examples that demonstrates how to use JaxME JS. The code won't work as it is, but it's useful to demonstrate JaxMe JS usage in practice.
A weakness of JaxMe JS is that it can only parse and generate Java 1.4 source code files. As of version 2, JaxMe JS cannot parse/generate Java 5 source code that uses Java generics and Java annotations features.
JavaSourceFactory jsf = new JavaSourceFactory(); JavaParser jp = new JavaParser(jsf); jp.parse(file); Iterator<JavaSource> javaSources = jsf.getJavaSources(); JavaSource javaSource = javaSources.next(); ObjectModel model = new ObjectModel(javaSource.getPackageName(), javaSource.getClassName()); try { for (JavaField field : javaSource.getFields()) { log.log(Level.INFO, "Processing " + field.asString()); model.addProperty(field.getName(), Class.forName(field .getType().toString())); } } catch (ClassNotFoundException e) { e.printStackTrace(); throw new ParserException(e); } return model;Put the following on your Maven project's pom.xml to specify the JaxME JS depency. ANTLR must be specified too.
<dependencies> <dependency> <groupId>org.apache.ws.jaxme</groupId> <artifactId>jaxmejs</artifactId> <version>0.5.2</version> </dependency> <dependency> <groupId>antlr</groupId> <artifactId>antlr</artifactId> <version>2.7.7</version> </dependency> </dependencies>For more information on grammar/language parsing and code generation with Java (and ANTLR), get the book The Definitive Antlr Reference: Building Domain-Specific Languages (Pragmatic Programmers).
Using AspectJ for AOP on Maven project
To configure AspectJ inside a Maven project you use aspectj-maven-plugin.
Modify your Maven project's pom.xml as follows:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
...
<properties>
<aspectj.version>1.6.8</aspectj.version>
</properties>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.3</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal> <!-- use this goal to weave all your main classes -->
<goal>test-compile</goal> <!-- use this goal to weave all your test classes -->
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
When you want to upgrade AspectJ version, simply change aspectj.version from properties.
Specify Java Source Level using Maven
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
Replace "1.5" if necessary. Valid values are: 1.3, 1.4, 1.5, 1.6.
Sunday, March 14, 2010
Installing GlassFish JST Server Adapter on Eclipse IDE / SpringSource Tool Suite
Oracle GlassFish aka Sun Application Server is an open source Java EE 6 container, bundled in a complete NetBeans download. By installing the Eclipse Plugin, you can use the full features of GlassFish Java EE 6 server administration and management within your Eclipse IDE.
Problem Details
Here's a log of the Eclipse error messages generated during GlassFish JST Server Adapter installation:– Error Details – Date: Sat Mar 13 07:34:18 WIT 2010 Message: An error occurred during the org.eclipse.equinox.internal.provisional.p2.engine.phases.CheckTrust phase. Severity: Error Product: SpringSource Tool Suite 2.3.1.201003091003-RELEASE (com.springsource.sts.ide) Plugin: org.eclipse.equinox.p2.engine Session Data: eclipse.buildId=2.3.1.201003091003-RELEASE java.version=1.6.0_15 java.vendor=Sun Microsystems Inc. BootLoader constants: OS=linux, ARCH=x86, WS=gtk, NL=en_US Framework arguments: -product com.springsource.sts.ide Command-line arguments: -os linux -ws gtk -arch x86 -product com.springsource.sts.ide – Error Details – Date: Sat Mar 13 07:34:18 WIT 2010 Message: Failed to prepare partial IU: [R]com.sun.enterprise.jst.server.sunappsrv 1.0.52. Severity: Error Product: SpringSource Tool Suite 2.3.1.201003091003-RELEASE (com.springsource.sts.ide) Plugin: org.eclipse.equinox.p2.touchpoint.eclipse – Error Details – Date: Sat Mar 13 07:34:18 WIT 2010 Message: session context was:(profile=com.springsource.sts.ide, phase=org.eclipse.equinox.internal.provisional.p2.engine.phases.CheckTrust, operand=null --> [R]com.sun.enterprise.jst.server.sunappsrv 1.0.52, action=). Severity: Error Product: SpringSource Tool Suite 2.3.1.201003091003-RELEASE (com.springsource.sts.ide) Plugin: org.eclipse.equinox.p2.engine
I tried a supposed workaround of adding the GlassFish Eclipse Update Site (https://ajax.dev.java.net/eclipse) but this didn't work for me. Exact same error.
Working Workaround
The workaround is to download the GlassFish Plugin JARs manually from GlassFish Eclipse Plugin Download page. You need to download two JARs, a feature JAR and a plugin JAR.Installation is easy, but you must know how to properly install plugins manually. Do not blindly copy them to Eclipse plugins and features folder!
- Navigate to your Eclipse IDE / SpringSource Tool Suite dropins folder using your OS' File Explorer.
- Create a folder named glassfish-jst-adapter . (this is not mandatory but it makes it easier if you need to remove this plugin later). Navigate into this folder.
- Create a folder named features and copy the feature JAR into it e.g. com.sun.enterprise.jst.server.sunappsrv.feature_1.0.51.jar
Hint: it's the JAR with smaller file size. - Create a folder named plugins and copy the plugin JAR into it e.g. com.sun.enterprise.jst.server.sunappsrv_1.0.51.jar
Hint: it's the JAR around 17 MB in size.
Restart Eclipse IDE / SpringSource Tool Suite and you should be able to use GlassFish Server Adapter / Plugin just fine. :-)
A great resource on Java EE 6 and GlassFish is the book
Beginning Java EE 6 Platform with GlassFish 3: From Novice to Professional. Check it out now.
See also: Eclipse bug #280365 and my bug report on SpringSource Tool Suite STS-865.
Friday, March 12, 2010
Common Maven Repositories
java.net
<repository> <id>java.net.m2</id> <url>http://download.java.net/maven/2</url> <snapshots> <enabled>false</enabled> </snapshots> </repository>
GlassFish
<repository> <id>java.net.glassfish.m2</id> <url>http://download.java.net/maven/glassfish</url> <snapshots> <enabled>false</enabled> </snapshots> </repository>
JBoss
This repository contains Java EE API and RI artifacts not available in Maven central.<repository> <id>repository.jboss.org</id> <name>JBoss Repository</name> <url>http://repository.jboss.org/maven2</url> <snapshots> <enabled>false</enabled> </snapshots> </repository>
PrimeFaces
<repository> <id>prime-repo</id> <name>Prime Technology Maven Repository</name> <url>http://repository.prime.com.tr</url> <snapshots> <enabled>false</enabled> </snapshots> </repository>
SpringSource Tool Suite 2.3.1 Released (on Eclipse 3.5.2)
Download the new SpringSource Tool Suite here.
Announcement: Now Available: SpringSource Tool Suite 2.3.1 | SpringSource.org
The latest version of SpringSource Tool Suite (STS) is now available. STS is the best Eclipse-powered development environment for building Spring, Groovy and Grails powered enterprise applications. The new version (2.3.1) is now available for download and uses the latest Eclipse 3.5.2 base. Other features include
* The latest version of tc Server Developer Edition with improvements to the integration with the Spring Insight console.
* Namespace Handlers can now be loaded from the project’s classpath which allows the use of namespaces that are not already known to STS
* Significant performance and memory consumption improvements.
Now is a great time to start working with STS and please use the community forum to give your feedback and ask questions.
Thursday, March 11, 2010
Solving Maven Deploy/Release Problem: Unsupported Protocol: 'dav': Cannot find wagon which supports the requested protocol: dav
[INFO] [INFO] [deploy:deploy {execution: default-deploy}] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] [ERROR] BUILD ERROR [INFO] [INFO] ------------------------------------------------------------------------ [INFO] [INFO] Error deploying artifact: Unsupported Protocol: 'dav': Cannot find wagon which supports the requested protocol: dav [INFO] [INFO] Component descriptor cannot be found in the component repository: org.apache.maven.wagon.Wagondav. [INFO] [INFO] ------------------------------------------------------------------------ [INFO] [INFO] For more information, run Maven with the -e switch [INFO] [INFO] ------------------------------------------------------------------------ [INFO] [INFO] Total time: 7 seconds [INFO] [INFO] Finished at: Fri Mar 12 01:01:37 WIT 2010 [INFO] [INFO] Final Memory: 20M/74M [INFO] [INFO] ------------------------------------------------------------------------ [INFO] ------------------------------------------------------------------------ [ERROR] BUILD ERROR [INFO] ------------------------------------------------------------------------ [INFO] Maven execution failed, exit code: '1' [INFO] ------------------------------------------------------------------------ [INFO] Trace org.apache.maven.lifecycle.LifecycleExecutionException: Maven execution failed, exit code: '1' at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:719) at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:569) at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:539) at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387) at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:284) at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328) at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138) at org.apache.maven.cli.MavenCli.main(MavenCli.java:362) at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315) at org.codehaus.classworlds.Launcher.launch(Launcher.java:255) at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430) at org.codehaus.classworlds.Launcher.main(Launcher.java:375) Caused by: org.apache.maven.plugin.MojoExecutionException: Maven execution failed, exit code: '1' at org.apache.maven.plugins.release.PerformReleaseMojo.execute(PerformReleaseMojo.java:133) at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490) at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694) ... 17 more Caused by: org.apache.maven.shared.release.ReleaseExecutionException: Maven execution failed, exit code: '1' at org.apache.maven.shared.release.phase.AbstractRunGoalsPhase.execute(AbstractRunGoalsPhase.java:89) at org.apache.maven.shared.release.phase.RunPerformGoalsPhase.execute(RunPerformGoalsPhase.java:67) at org.apache.maven.shared.release.DefaultReleaseManager.perform(DefaultReleaseManager.java:334) at org.apache.maven.shared.release.DefaultReleaseManager.perform(DefaultReleaseManager.java:282) at org.apache.maven.shared.release.DefaultReleaseManager.perform(DefaultReleaseManager.java:262) at org.apache.maven.plugins.release.PerformReleaseMojo.execute(PerformReleaseMojo.java:129) ... 19 more Caused by: org.apache.maven.shared.release.exec.MavenExecutorException: Maven execution failed, exit code: '1' at org.apache.maven.shared.release.exec.InvokerMavenExecutor.executeGoals(InvokerMavenExecutor.java:375) at org.apache.maven.shared.release.exec.InvokerMavenExecutor.executeGoals(InvokerMavenExecutor.java:393) at org.apache.maven.shared.release.phase.AbstractRunGoalsPhase.execute(AbstractRunGoalsPhase.java:81) ... 24 more
Cause
If you have an old version of Maven, this is a known problem as the DAV wagon was not built in. Upgrade to the latest Maven version first (Maven 2.2.1 and later).
If you have a newer version of Maven, you might be using Debian or Ubuntu, which contains an incomplete version of Maven, that doesn't include the DAV wagon (wagon-webdav-jackrabbit).
Solution
Add this to your POM :
<build> <extensions> <extension> <groupId>org.apache.maven.wagon</groupId> <artifactId>wagon-webdav-jackrabbit</artifactId> <version>1.0-beta-6</version> </extension> </extensions> </build>Tip: For Maven Release Plugin, you might want to use version 2.0.
<build> <plugins> <plugin> <artifactId>maven-release-plugin</artifactId> <version>2.0</version> </plugin> </plugins> </build>In multi-module project, theoretically you just need to put this in the super POM. But I haven't checked it, it may not work on child modules. If so, add the "extension" part on all POMs that require deploying to a WebDAV repository server.
For in-depth information about Maven, get Apache Maven 2 Effective Implementation book.
JBoss Tools 3.1 Plugin for Eclipse IDE Released
Download JBoss Tools here.
The update site for the final release is http://download.jboss.org/jbosstools/updates/stable/galileo.
New Features
Additional Server and deployment support
Support for JBoss Enterprise Application Platform (EAP) and Enterprise SOA Platform (SOA-P) 5 as well as community JBoss AS 5.x and even AS 6 M1 is supported.
With the Module Assembly Page it is now possible to more finegrained control over the assembly of Web Tools Projects.
We introduced the notion of deploying to SSH enabled hosts allowing for
basic remote deployments on the local network or on remote clouds
such as Amazon EC2.
Portal
Support for JBoss Portal and it's successor GateIn and Enterprise Portal Platform (EPP) have been added.
JSF 2
JSF 2 is now supported as well as JSF 1 w/facelets with many optimization in the performance of the visual page editor.
New features in JSF 2 such as composite components and resource look ups are now supported in the visual page editor and in the (x)html code completion.
A lot of improvements in the code completion and visual presentation have made it now even easier and faster to write JSF components no matter if you are into editing source code or visual previewing.
Seam
Seam 2.2 support including improved navigation and refactoring of Seam components together with the improvements in JSF support makes JBoss Tools perfect for Seam development.
Hibernate
Hibernate tooling works with connections configured in DTP and Eclipse Dali making it simpler to share connection settings.
If you have an existing Java model you can now easily get either hbm.xml mappings or JPA annotations generated for this model.
Project Examples
It is now easier to get started using JBoss technology by using the Help > Project Examples menu.
More examples have been added and the import have been enhanced to make it more informative about what runtime platforms and versions it will work on.
Maven
Maven users can now easily import their Maven projects with m2eclipse and JBoss Tools Maven integration will configure support for JSF, Maven and Portal development in the IDE.
CDI
The Context and Dependency Injection specification is supported by providing code completion for @Named component and all the code completion, open-on navigation and refactoring that was done for Seam also applies to CDI components.
SOA
SOA tooling have been extended massively by adding support for BPEL on Riftsaw, ESB projects, jbpm4, Drools 5 and Smooks.
Smooks got its own revamped editor and the other editors have been extended and interlinking between the various SOA editors allowing for easy navigation is in place.
ESB Projects allow for easy creation of ESB esb-service and deployment with instant debugging.
...and more!
In Relation To... JBoss Tools 3.1 FinalThis is the official version of JBoss Tools that will run on and require Eclipse 3.5 (Galileo). When installing you can either use the remote Update Site or download the update site zip from the main Download for offline installation. In both cases you can pick and choose which plugins/features you want to install.
For some features other dependencies are needed. For example Maven integration requires m2eclipse 0.10. We have done what we can to enable the related update sites, but in case you disabled them explicitly you would need to add or enable them manually. If you have problems with the installation see this
Tuesday, March 2, 2010
How to Setup Java EE Web Development Environment on Eclipse IDE
You need to get Eclipse Java EE Edition or SpringSource Tool Suite. I recommend SpringSource Tool Suite since it contains essential Java EE environment plus enterprise-grade Tomcat (SpringSource tc Server).
- Get the Cyclos distribution with sources here
- In Eclipse/STS, File > New > Create a Dynamic Web Project
- Under Project contents. Uncheck "use defaults" and choose your Cyclos root directory.
- Target runtime: Tomcat (default)
- Dynamic web module version: 2.5
- Configuration: Default Configuration for Apache Tomcat v6.0 3. - Click Next until you get to "Web Module" page.
- For web module content directory, change to "web".
- Uncheck "Generate web.xml deployment descriptor".
- Finish
Although I did the above process for Cyclos project, it can be used for any web project, as long as you recognize the structure (the most important is where the Java sources directory and the webapp directory).
Good luck!
For further resource on Java EE Web Development with Eclipse, get The Java Developer's Guide to Eclipse, 2nd Edition.