Flex: Show HTML text inside an Alert

As i just wanted to insert some bold text into an Alert box, I wondered why the Flex Alert doesn’t support HTML Text. So i wrote a Custom Alert class which just overwrites the regular Alert.show(). Through a trick, it allows to use HTML tags inside the text.

package tools
{
	import flash.display.Sprite;

	import mx.controls.Alert;
	import mx.core.IFlexModuleFactory;
	import mx.core.mx_internal;

	/**
	 * Alert-Box which makes using HTML text possible
	 */
	public class HTMLAlert extends Alert
	{
		/**
		 * Given text is interpreted as HTML text,
		 * Example: "bold text" is rendered as bold text.
		 */
		public static function show(text:String = "", title:String = "",
						flags:uint = 0x4,
						parent:Sprite = null,
						closeHandler:Function = null,
						iconClass:Class = null,
						defaultButtonFlag:uint = 0x4,
						moduleFactory:IFlexModuleFactory = null):Alert
		{
			var alert:Alert = Alert.show(text, title, flags, parent,
				closeHandler, iconClass, defaultButtonFlag, moduleFactory);
			alert.mx_internal::alertForm.mx_internal::textField.htmlText = text;
			return alert;
		}
	}
}

Just use it by calling it like the regular Alert-class:

HTMLAlert.show("A bold Alert text", "HTMLAlert", Alert.OK);

Which then looks like this:

Posted in General | Tagged | 4 Comments

Python script: Wait for a site to come up and notify me

For the last weeks, i’ve been working a lot with different servers, which take longer and longer to deploy new application bundles. Mostly i know that the deployment is complete when the website or the webservices of that application are up and return a successful status code (not 404).
Another use case is when trying to access a remote website which is temporarily down, but you want to be notified immediately when it comes up again.

Since i don’t want to sit in front of my browser hitting F5 again and again (especially if it takes some minutes), i found it nice to watch the HTTP Server with a little script, which can run in background and send a little OS notification when the Site or Webservice is up again.

I’ll share it and hope anyone also has a use for this. For the notifications, you need the OS-independent notification library pynotify. If you are running Debian/Ubuntu, get it with apt-get install python-notify

#!/usr/bin/python
# Partly "borrowed" from:
# http://code.activestate.com/recipes/267197-urllib2-for-actions-depending-on-http-response-cod/
import urllib2
import pynotify
import time
import sys
from urllib2 import Request, urlopen, URLError, HTTPError

url = sys.argv[1]

print "Waiting for Site " + url + " to come up"

while True:
  try:
    urllib2.urlopen(url)
  except urllib2.HTTPError, e:
    if e.code == 401:
      print 'not authorized'
    elif e.code == 404:
      print 'not found'
    elif e.code == 503:
      print 'service unavailable'
    else:
      print 'unknown error: '
    time.sleep(3)
  else:
    print 'success'
    if pynotify.init("HTTP Monitor"):
      title_string = "HTTP Monitor"
      author_string = "Site " + url + " is available"
      n = pynotify.Notification(title_string, author_string, "emblem-shared")
      n.set_timeout(0)
      n.show()
      print author_string
      break

How the script is called:

python ~/tools/watchsite.py http://192.168.178.152:8080/UserService/UserServiceWSService?wsdl

Which looks like this:

Waiting for Site http://192.168.178.152:8080/UserService/UserServiceWSService?wsdl to come up....

And gives this nice notification (on Gnome) when the Server/Site/Service is up:

Posted in General | Tagged | Leave a comment

Bash script: Recursively convert line breaks in all files from Dos/Windows to Unix AND Replace Tabs with Spaces.

It’s really annoying that on Windows, nearly every program that deals with text saves the text in Windows-format. That’s not good when you work in a multi-OS-environment.
Another annoying thing are TABS, which often are much wider in Linux editors than in Windows, and are generally superflous as i think. Especially when programming Scala, where the style guide suggests using spaces instead of tabs.

So today i ended my misery with converting text-files and tabs by hand and finally wrote a Bash script. It works both on Linux and on Cygwin.

It solves both problems at once: It converts Windows CR/LF line breaks (only when there is one!), AND replaces those superflous tabs with spaces. And all that with one single line.

Read More »

Posted in General | Tagged , | Leave a comment

Get Weld project running in Glassfish Tools Bundle for Eclipse 1.2

Today i took a look into the GlassFish Tools Bundle for Eclipse to see if it could speed up my Java EE6-development. I was especially curious about the hot-deployment capabilities.

After downloading the Glassfish Tools from the download site and importing a simple Weld-JEE-project (i was using the Maven archetype “weld-jsf-jee-archetype”), an error showed up, pointing to the beans.xml of my project and saying

Referenced file contains errors (jar:file:/D:/Program Files (x86)/GlassFish-Tools-Bundle-For-Eclipse-1.2/dropins/feature-1.0.50/eclipse/plugins/com.sun.enterprise.jst.server.sunappsrv_1.0.50.jar!/ee6_schemas/beans_1_0.xsd).

So i opened the file com.sun.enterprise.jst.server.sunappsrv_1.0.50.jar in the mentioned directory with an archiver, where i found the beans_1_0.xsd in the folder ee6_schemas, which i opened with a text editor.

The targetNamespace of the schema file was pointing to the Seamframework, which sounded strange to me. So i googled to find another beans_1_0.xsd schema file and found one in the Weld trunk, with the targetNamespace=”http://java.sun.com/xml/ns/javaee” – which sounded much better to me.

So i replaced the schema file in the archive with the one from the Weld repo (you have to shutdown the GlassFish Tools Bundle first) and restarted the bundle, made a clean of my project – and voilĂ , the error vanished!

[EDIT]
I switched back to Eclipse and downloaded the Glassfish adapter seperately. Seems to work faster and more stable to me.

Also, there are some really annoying issues when deploying a Maven JEE6-app on Glassfish within Eclipse (both in bundled and non-bundle version with GF3 adapter). When “enable dependency management” in M2Eclipse is enabled, i can’t deploy the project. When it’s disabled, deploying works – but programming is hard because of the missing dependencies (although it works somehow).
Will write an article about that issue soon…

Posted in General | Tagged , , | Leave a comment

Enable Weld Logging on Glassfish with Maven

Just figured out how to use a Weld-injected Logger on Glassfish.
Unfortunately, i couldn’t get Log4J12 as SL4J Implementation running, like the author of this post did (i hope i’ll find out later), but with the sl4j-jdk14 binding, the logging just works.

Steps:

  1. Add the following dependencies to the pom.xml:
<!-- SL4J API -->
 <dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-api</artifactId>
 <version>1.6.0</version>
 </dependency>

 <!-- SLF4J JDK14 Binding  -->
 <dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-jdk14</artifactId>
 <version>1.6.0</version>
 </dependency>

 <!-- Injectable Weld-Logger -->
 <dependency>
 <groupId>org.jboss.weld</groupId>
 <artifactId>weld-logger</artifactId>
 <version>1.0.0-CR2</version>
 </dependency>

2. Inject the Logger and use it as usual:

import javax.inject.Inject;
import org.slf4j.Logger;

public class Example {
  @Inject
  private Logger logger;

  public void exampleFunc() {
    logger.info("Hello Logger!");
  }
}
Posted in General | Tagged , | 2 Comments

Use Ant-Contrib Tasks in Maven

I just messed around a while to get a simple Ant-contrib task running inside a Maven build – a simple <if>.
The examples on the net weren’t very helpful or too bloated, so i’ll post a very simple one.

The problem with Ant-contrib is that this library doesn’t come with the regular Ant-distribution. So it has to be downloaded separately.
But inside the Maven-antrun-plugin, Ant can’t access the ant-contrib.jar without explicitely referencing it with a hard-coded path (for example classpath=”C:/Dev/Ant/ant-contrib-1.0b3.jar”) – although it’s in the system classpath.

As it’s not the best practice to refer to local filesystem dependencies and rather downloading and using them within the Maven lifecycle, i’m referencing the ant-contrib.jar that Maven downloads when resolving the dependencies.

In my example i’m checking if the environment variable GLASSFISH has been set, failing the complete build if it’s not.
So my pom.xml looks like this: Read More »

Posted in General | Tagged , | 3 Comments

Batch script: Convert long folder name to short 8.3 name

When running Windows, i often had problems with long folder-names, especially on the command line.
Since Scala also has a problem with a JDK lying in a folder like “Program Files(x86)” i decided to write a little batch-helper to convert long folder names to the short 8.3-form.
Then i put the short name of my JDK folder into my environment variables, voilĂ  – i don’t need to move the JDK to satisfy Scalas needs ;)
The script works like this:

C:\>83conv C:\Program Files (x86)\Java\jdk1.6.0_20
C:\PROGRA~2\Java\JDK16~1.0_2

C:\>

The source:
Read More »

Posted in General | Tagged , | 2 Comments

Programmer approaching

And now for something completely different:

No, i wasn’t the pilot. Not yet ;)

Posted in General | Leave a comment

Maven & Scala: Add source folder to Eclipse Project

EDIT:
You don’t need to do the things described here if you’re also using Scala. In this case, you can switch to the m2eclipse-scala integration which automatically adds the Scala source folder (beside some other useful things).

Just solved one another annoying problem:
Everytime i had to clean my maven-eclipse-project with “mvn eclipse:clean” and re-created the project, i had to manually re-add the “src/test/scala”-folder to the eclipse project.
Now i found a way to tell maven additional source folders to integrate into the eclipse project: the build-helper-plugin.

So to add my src/test/scala-folder permanently i added the following plugin-configuration to my pom.xml (i also had to add the source) :

<!-- ================== MAVEN BUILDER PLUGIN =================== -->
<plugins>
(...)
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>1.1</version>
  <executions>
    <execution>
      <id>add-source</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>add-source</goal>
      </goals>
      <configuration>
        <sources>
          <source>src/main/scala</source>
        </sources>
      </configuration>
    </execution>
    <execution>
      <id>add-test-source</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>add-test-source</goal>
      </goals>
      <configuration>
        <sources>
          <source>src/test/scala</source>
        </sources>
      </configuration>
    </execution>
  </executions>
</plugin>
(...)
</plugins>

Wonderful plugin. You can add infinite source folders to your project.
I will surely need it again, when i’m adding the next programming language to my project :-D

Posted in General | Tagged , , | 2 Comments

Maven Scala Build Error “error while loading Function1, class file needed by Function1 is missing”

Scala and Maven… a kinda’ fragile combination ;)
Today i got the following error when trying to compile my scala-project with “mvn compile”:

[ERROR] error: error while loading Function1, class file needed by Function1 is missing.
[INFO] reference value Unit of package scala refers to nonexisting symbol.
[ERROR] one error found
[INFO] ————————————————————————
[ERROR] BUILD ERROR
[INFO] ————————————————————————
[INFO] wrap: org.apache.commons.exec.ExecuteException: Process exited with an error: 1(Exit value: 1)

Nothing worked anymore – no compiling, cleaning, testing or anything else concerning scala and maven.
My solution: Delete all scala-folders/files in your Maven Repository (%USER%/.M2), so that maven re-downloads them.
After that, anything worked again for me.

Posted in General | Tagged , | Leave a comment