Sat, 30 May 2009

10:02 PM - Apache Cayenne

 I've been working on converting just journal over to Apache Cayenne today.  I figured it would give me an opportunity to get better with it.  Cayenne has a nice modeling program, but their documentation is often lacking.

For example, suppose you're getting an error from your servlet container (Tomcat in my case) about a filter not loading.  With Cayenne, the "correct" way to use it is to load a filter mapped to /* that catches all the sessions and maps a DataContext to them.  You might find after pulling your hair out that there is a syntax error in your DomainNode.driver.xml file.  Even with logging cranked up in log4j, it will not tell you what's wrong with the filter.  However, if you initialize like a traditional app in a jsp page, you can see the error.  Invalid passwords are an example of what can cause this.

 

tags: java apache cayenne

()

10:14 PM - JSON in Java

I've been using JSON with Dojo Data Grids lately in PHP.  Now, I want to use something similar on this site.  I decided on the json-simple library.  It's actually quite easy to use.

Encoding examples:

http://code.google.com/p/json-simple/wiki/EncodingExamples 

<%@ page contentType="text/json;charset=UTF-8" language="java"%>

<%@ page import="com.justjournal.model.User" %>

<%@ page import="org.apache.cayenne.DataObjectUtils" %>

<%@ page import="org.apache.cayenne.access.DataContext" %>

<%@ page import="org.apache.cayenne.query.SelectQuery" %>

<%@ page import="org.json.simple.JSONObject" %>

<%@ page import="java.util.*" %>

<%@ page import="org.json.simple.JSONArray" %>

<%

    out.clearBuffer();

    SelectQuery query = new SelectQuery(User.class);

    query.addOrdering(User.USERNAME_PROPERTY, true);


    DataContext context = DataContext.getThreadDataContext();

    List users = context.performQuery(query);

    JSONObject obj = new JSONObject();

    JSONArray list1 = new JSONArray();


    Iterator it = users.iterator();

    while (it.hasNext()) {

        Map map = new HashMap();

        User u = (User) it.next();

        if (u.getUsername().length() > 2 && !new com.justjournal.User(u.getUsername()).isPrivateJournal())

        {

            map.put("id", DataObjectUtils.intPKForObject(u));

            map.put("username", u.getUsername());

            map.put("name", u.getName());

            map.put("since", u.getSince());

            list1.add(map);

        }

    }

    obj.put("items", list1);

    obj.put("label","id");

    obj.put("identifier", "id");

    obj.writeJSONString(out);

 

tags: dojo json java cayenne

()

Wed, 8 Jul 2009

10:33 PM - (no subject)

 Today was an interesting day at work.  I managed to get Cayenne working properly for a Project -> User mapping through some intermediate tables.  There were few tricks involved.  First, I had to modify the User class within Cayenne to explicitly have a mapping for the Primary key.  This allows you to do an Expression.fromString and refer to the primary key.  Second, I had to do a double mapping using the relationship name between the project and the mapping table then the mapping table to user table relationship.  The context had to be the name from the project end and from the mapping table end to the user table.  (left to right... )  

One problem I had early on was that we also have a user_id stored as the "owner" with a relationship in the project table.  That means there's a direct relationship and an indirect relationship between project and user.  

The idea here was to map all the projects that the User can access.  The mapping table contains a PK, user_id and project_id.  A user can potentially access many projects.  We knew the user id of the user logged in from the session (username would work too and require less work, but could not benefit from the database indexes).  

This sounds trivial, but there is almost no documentation on something like this with Cayenne.  Those apache developers need to get up some real examples of Cayenne apps.  

tags: java db apache mysql cayenne

()

Fri, 5 Feb 2010

7:50 PM - Cayenne 3.0RC2

I just read that Cayenne 3.0 RC2 is out.  Cayenne is an ORM.   I've been using it for almost a year now.  It's a very easy way to do data access in Java.  There are a few quirks.  Most people love Hibernate and don't consider alternatives.  Cayenne is very easy to work with.  It has a client gui to configure and setup your mappings or you can write an XML file by hand.  

 

location: Home

tags: cayenne apache database orm java

()

Wed, 17 Feb 2010

3:23 PM - HTML cleaning in Java

HTML Cleaner 

JTidy

TagSoup

JTidy cannot create valid XHTML strict pages.  A combination of HTML cleaner and JTidy cannot make valid XHTML strict pages for some input.  double br tags, some attributes like height, and duplicate id attributes cause problems.

location: Work

tags: validate clean java html

()

Tue, 20 Jun 2017

3:52 PM - Installing SSL certs from Amazon AWS RDS instances in java

Having problems connecting Java apps to amazon RDS instances? Try installing the CAcerts from amazon as follows. These instructions cover freebsd and Mac OS X.

# (use curl or wget on mac)
fetch http://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem


# create individual files from the bundle for loading into java
split -p "-----BEGIN CERTIFICATE-----" rds-combined-ca-bundle.pem individual-


# load them into your cacerts file in freebsd
find . -iname 'individual*' -exec keytool -import -file {} -alias {} -storepass changeit -keystore /usr/local/openjdk8/jre/lib/security/cacerts \;

# mac version, tweak for your JDK path
find . -iname 'individual*' -exec keytool -import -file {} -alias {} -storepass changeit -keystore /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/security/cacerts \;

tags: java aws keytool rds ssl

()