Satya Aravind Kopparthi Weblog

02/25/2009

Java classes to Actionscript3 classes using Maven 2

Filed under: ActionScript, Flex, Maven — akopparthi @ 9:23 pm

At some point I found myself in a situation where I had data objects in Java and in order to use the same objects in Flex application I had to duplicate these classes in Actionscript 3. It was pretty boring and in addition to that, the possibility to forget to add or change some attribute was very high and it happened of course more than once. As I started to look around in web I had very low expectations to find a tool for converting Java classes to Actionscript classes. After some digging I found that actually there is a great tool, which does exactly what I needed.

Franck Wolff had created an Actionscript generator which can be run as an Ant task and you only have to define the source directory where your compiled Java classes are located and the destination directory where you want the generated Actionscript classes to have and that’s it. As I was more used to Maven and couldn’t spend too much time on the topic I decided to use the Maven’s Ant plugin to call the generator from pom.xml (an example how I use it can be seen below).

- compile the Java classes in my web application
- call the Actionscript generator to take the compiled Java classes and generate Actionscript out of them
- call the Flex compiler to compile the Flex application with the generated classes (you can get a more detailed view of this and following points – also here)
- call the maven-flex2-plugin to copy the compiled Flex app to my web application module
- package all together to a war file
- deploy it to Tomcat server
Ok, there was also some other stuff too like generating hibernate mappings and adding functionality to generated Actionscript classes but I’ll try to write about it soon. Great thing was that all of it actually worked and I had the possibility to configure this tool the way I wanted.

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <phase>install</phase>
            <configuration>
                <tasks>
                    <taskdef className="org.granite.generator.ant.As3BeanAntTask" name="gas3" />
                    <gas3 outputdir="../flex/src">
                        <classpath>
                            <pathelement location="target/classes" />
                        </classpath>
                        <fileset dir="target/classes">
                            <include name="ee/quest/model/question/*.class" />
                        </fileset>
                    </gas3>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>antlr</groupId>
            <artifactId>antlr</artifactId>
            <version>2.7.6</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>asm</groupId>
            <artifactId>asm</artifactId>
            <version>1.5.3</version>
            <scope>compile</scope>
        </dependency>
          <dependency>
            <groupId>org.granite</groupId>
            <artifactId>granite-generator</artifactId>
            <version>1.0.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.granite</groupId>
            <artifactId>gas3</artifactId>
            <version>1.0.0</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</plugin>

02/15/2009

JSF Flex Framework

Filed under: Uncategorized — akopparthi @ 9:39 pm

JSF Flex goal is to provide users capability in creating standard Flex components as JSF components {note dynamic charts and some other components are not part of standard Flex components}. So users would create the components as normal JSF components and the project will create the necessary MXML, SWC, SWF files and etcetera and link the values of the components back to the managed beans using JSON+Javascript and Actionscript. {standard Flex components have been open sourced through MPL license}
Components

JSF Flex project contains concrete JSF component for each Flex component.

Each of the JSF component that needs to preserve the value will have the following syntax of values checked during the decode process

_ {i.e. for List _selectedIndex}. For certain components there will be a set of ids sent to the decode process {i.e. RadioButton component has two values checked during the JSF decode process { _selectedValue and _selected }.

JSF Flex project utilizes the myfaces build plugIn which automatically creates :

the concrete class for each abstract class of JSF component
the tag class for each JSF component
the tld file
the facesconfig.xml for the configuration
In order to view how to create such components, please refer to the current components within jsf-flex project.

better support for creating XMLList and its associated data for Facelet using dataBinding + static XML data
possible creation of Array + list of Objects using dataBinding to beans {similar implementation as for XMLList + XML}
Main points for JSF Flex Renderers

Main thing to note in terms of design for the renderers are the following :

Classes which are not concrete, meaning MXML.+TemplateRenderer will mainly have a method called mapFields which is simply to inspect either the replaceMapping XML or annotation {depending on the JRE version} to retrieve the values from the sub-class of UIComponent during the JSF Flex’s encodeBegin lifecycle.
Concrete classes have a method createPreMxml which is to create the preMxml file with the responsibility of creating these files lie with implementations of _FileManipulatorTaskRunner interface {default being VelocityFileManipulatorTaskRunnerImpl }. The implementation choice for this interface and two other interfaces _CommonTaskRunner and _FlexTaskRunner are determined during the maven build process.
MXMLComponentBaseRenderer class during the encodeEnd will write the body content of the component {i.e. MXMLScript} as long as the correct content is pushed to the attribute map. For an example, view the MXMLUIScriptTag within the jsf-flex project where the body content is pushed to the component’s attribute map as MXMLConstants.TAG_BODY_CONTENT_ATTR.
MXMLApplicationRenderer is a special class that has an added responsibility during its encodeEnd to
merge the preMxml files into a MXML file
create SWC file for the system’s library
extract the SWF file from SWC for the shared resource of all application SWF files
flush necessary resources {i.e. componentValueMapper.xml} for the system’s library to the correct directory
extract the flexSDK to the correct directory
create the application’s SWF file.

References
Further details and the source code: http://code.google.com/p/jsf-flex/

02/14/2009

8 essential MySQL queries

Filed under: Uncategorized — akopparthi @ 9:43 pm

Here”s a list of queries that I found myself using very often and that save me a lot of development time. I hope you can benefit from them as well.

1. Create a quick backup

Before testing a new piece of code you suspect might mess up data in one or more tables it”s always a good practice to create a backup. To quickly create a backup copy of a table, use this query:

CREATE TABLE backup SELECT * FROM TABLE original;

The query creates a table backup which is a copy of the original table and includes both structure and content.

2. Create/change/restore a password

Many applications store MD5-crypted passwords in the database. If you want to quickly create a new MD5-ed password, or you have forgotten your password, use the following query to get a new one:

SELECT MD5(“somepasshere”);

This statement will give you “b5bab206cc8002bf7c10d47b24a2d0e6″ which is the encrypted version of the string “somepasshere”. There are other function that crypt stings in MySQL using different algorithms, most notably PASSWORD() which is using MySQL“s own crypting algorithm.

3. Working with Unix timestamps

To convert from human-readable MySQL date/time format into Unix timestamp, use: SELECT UNIX_TIMESTAMP(); Without parameters, this will give you the timestamp of the current date and time. With parameters, you can get timestamp for any date. For example: SELECT UNIX_TIMESTAMP(“2006-12-31″); This statement gives you the timestamp 1167541200. To get a date back from a timestamp, use: SELECT FROM_UNIXTIME(1167541200); This results in “2006-12-31 00:00:00″

4. Quick increment

To increment an integer stored in a table (useful for stats for example), use: UPDATE sometable SET counter=counter+1 WHERE …; Here “counter” is the name of the field that stores the integer value.

5. Toggle a value

If you have a field that stores a Boolean type of value, like 0/1 or yes/no, you can easily toggle the value with one if-statement:

UPDATE sometable SET flag=(IF(flag=”no”,”yes”,”no”));

6. Find/replace

Say you want to update a piece of text if all records in a table field. REPLACE() comes to the rescue:

UPDATE sometable SET field = REPLACE(field, “black”,”white”);

This statement will replace all occurrences of the string “black” with the string “white” in all records of the “field” column. Apart from the string “white” the rest of the text contained in the field will be left as is.

7. Get a random record

If you want to select a random row in your table, you can use the statement:

SELECT * FROM table ORDER BY RAND();

8. Upper/lower case

If you want to modify a value and make it upper or lowercase, use the UPPER or LOWER functions, like this:

SELECT LOWER(“Value”); // gives you “value”

SELECT UPPER(“Value”); // gives you “VALUE”

I hope you learned something new today which will make your everyday life as a developer just a bit easier. Thank you for reading!

02/13/2009

eclipse -ganymede save action

Filed under: Uncategorized — akopparthi @ 7:08 pm

I am absolutely convinced that source code must be formatted according to rules a team agrees on. Therefore I have my Save Actions setup for auto-formatting my code (by the way I just rely on the eclipse built-in rules, I would only adjust the line length to 120) every time it is saved. For those who often need to fix/change unformatted code (code that is not auto formatted at all) are going nuts. Once you start comparing with older revisions you can’t actually tell anymore what exactly you have modified, the auto-formatter has done a damn good job! So now you can set an option that only formats actually modified lines code.

Windows-preferences->java->Editor->Save Actions
Check Perform the selected actions on save
and select format edited lines.

02/11/2009

tomcat – configure SSL/HTTPS

Filed under: tomcat — Tags: , , — akopparthi @ 10:18 pm
  1. >cd $CATALINA_HOME
  2. $JAVA_HOME/keytool -genkey -alias tomcat -keyalg RSA -keystore mycert.jks
  3. Enter keystore password: changeit
  4. What is your first and last name? : Aravind Kopparthi
  5. What is the name of your organizational unit? [Unknown]: CAT
  6. What is the name of your organization? [Unknown]: GOOGLE
  7. What is the name of your City or Locality? [Unknown]: FL
  8. What is the name of your State or Province? [Unknown]: FL
  9. What is the two-letter country code for this unit? [Unknown]: US
  10. US Is CN=Danesh Manoharan, OU=IT, O=”My Comp.”, L=KL, ST=KL, C=MY correct? [no]: yes
  11. Enter key password for (RETURN if same as keystore password): Hit Enter.

Tomcat will assume the password is “changeit” by default so it’s advised to leave it that way. Now let’s tell Tomcat to use the keystore file.

  1. cd $CATALINA_HOME/conf/
  2. vi server.xml
  3. Look for “<!– Define a SSL HTTP/1.1 Connector on port 8443 –>”. Remove the <!– –> comments indicator and add the keystore info.

<!– Define a SSL HTTP/1.1 Connector on port 8443 –>
<Connector port=”443” maxHttpHeaderSize=”8192″
maxThreads=”150″ minSpareThreads=”25″ maxSpareThreads=”75″
enableLookups=”false” disableUploadTimeout=”true”
acceptCount=”100″ scheme=”https” secure=”true”
keystoreFile=”/opt/Tomcat5/mycert.jks
clientAuth=”false” sslProtocol=”TLS” />

Time to restart Tomcat and test.

  1. cd $CATALINA_HOME/bin/
  2. ./shutdown.sh to make sure Tomcat is down.
  3. ./startup.sh to start Tomcat.
  4. Fire up your browser and test your new https site. https://localhost:9080/

Tomcat – Use catalina servlet to disable file listing…

Filed under: tomcat — Tags: — akopparthi @ 10:12 pm

1. Edit the default servlet in the {$CATALINA_HOME}/conf/web.xml file.

2. Look for the <init-param> section within the <servlet section>

<servlet>
<servlet-name>default</servlet-name>
<servlet-class>
org.apache.catalina.servlets.DefaultServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

3. Change the <param-value> to false for the <param-name>listing</param-name> section.

<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>

Blog at WordPress.com.